Disk: support freebsd

Also rewrite macOS detection to share code with bsd
This commit is contained in:
李通洲 2022-10-27 14:58:29 +08:00
parent 9ad7ba7144
commit 5472621ac0
No known key found for this signature in database
GPG Key ID: 3570F9F0F4410388
3 changed files with 68 additions and 55 deletions

View File

@ -302,6 +302,7 @@ endif()
if(BSD OR APPLE)
list(APPEND LIBFASTFETCH_SRC
src/common/sysctl.c
src/detection/disk/disk_bsd.c
src/detection/uptime/uptime_bsd.c
src/detection/processes/processes_bsd.c
)
@ -311,6 +312,7 @@ if(LINUX OR ANDROID)
list(APPEND LIBFASTFETCH_SRC
src/detection/cpu/cpu_linux.c
src/detection/cpuUsage/cpuUsage_linux.c
src/detection/disk/disk_linux.c
src/detection/memory/memory_linux.c
src/detection/processes/processes_linux.c
src/detection/swap/swap_linux.c
@ -320,7 +322,6 @@ endif()
if(LINUX OR ANDROID OR BSD)
list(APPEND LIBFASTFETCH_SRC
src/detection/disk/disk_linux.c
src/detection/temps/temps_linux.c
src/detection/opengl/opengl_linux.c
src/detection/packages/packages_linux.c

View File

@ -1,61 +1,17 @@
#include "disk.h"
#include <sys/statvfs.h>
#include <sys/mount.h>
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
void ffDetectDisksImpl(FFDiskResult* disks)
void detectFsInfo(struct statfs* fs, FFDisk* disk)
{
NSArray *keys = [NSArray arrayWithObjects:NSURLVolumeNameKey, nil];
NSArray *urls = [NSFileManager.defaultManager mountedVolumeURLsIncludingResourceValuesForKeys:keys options:0];
// FreeBSD doesn't support these flags
if(fs->f_flags & MNT_DONTBROWSE)
disk->type = FF_DISK_TYPE_HIDDEN;
else if(fs->f_flags & MNT_REMOVABLE)
disk->type = FF_DISK_TYPE_EXTERNAL;
else
disk->type = FF_DISK_TYPE_REGULAR;
if(urls == nil)
{
ffStrbufAppendS(&disks->error, "[NSFileManager.defaultManager mountedVolumeURLsIncludingResourceValuesForKeys] failed");
return;
}
for (NSURL *url in urls)
{
FFDisk* disk = ffListAdd(&disks->disks);
ffStrbufInitS(&disk->mountpoint, [url.relativePath cStringUsingEncoding:NSUTF8StringEncoding]);
NSString* filesystem;
BOOL removable;
[NSWorkspace.sharedWorkspace getFileSystemInfoForPath:url.relativePath
isRemovable:&removable
isWritable:nil
isUnmountable:nil
description:nil
type:&filesystem
];
ffStrbufInitS(&disk->filesystem, [filesystem cStringUsingEncoding:NSUTF8StringEncoding]);
NSError* error;
NSNumber* isBrowsable;
if([url getResourceValue:&isBrowsable forKey:NSURLVolumeIsBrowsableKey error:&error] == YES && !isBrowsable.boolValue)
disk->type = FF_DISK_TYPE_HIDDEN;
else if(removable)
disk->type = FF_DISK_TYPE_EXTERNAL;
else
disk->type = FF_DISK_TYPE_REGULAR;
NSString* volumeName;
if([url getResourceValue:&volumeName forKey:NSURLVolumeNameKey error:&error] == YES)
ffStrbufInitS(&disk->name, [volumeName cStringUsingEncoding:NSUTF8StringEncoding]);
else
ffStrbufInit(&disk->name);
struct statvfs fs;
if(statvfs(disk->mountpoint.chars, &fs) != 0)
memset(&fs, 0, sizeof(struct statvfs)); //Set all values to 0, so our values get initialized to 0 too
disk->bytesTotal = fs.f_blocks * fs.f_frsize;
disk->bytesUsed = disk->bytesTotal - (fs.f_bavail * fs.f_frsize);
disk->filesTotal = (uint32_t) fs.f_files;
disk->filesUsed = (uint32_t) (disk->filesTotal - fs.f_ffree);
}
ffStrbufInitS(&disk->name, [NSFileManager.defaultManager displayNameAtPath:@(fs->f_mntonname)].UTF8String);
}

View File

@ -0,0 +1,56 @@
#include "disk.h"
#include <sys/mount.h>
#ifdef __FreeBSD__
static void detectFsInfo(struct statfs* fs, FFDisk* disk)
{
if(
ffStrbufStartsWithS(&disk->mountpoint, "/boot") ||
ffStrbufStartsWithS(&disk->mountpoint, "/dev") ||
ffStrbufStartsWithS(&disk->mountpoint, "/var") ||
ffStrbufStartsWithS(&disk->mountpoint, "/tmp") ||
ffStrbufStartsWithS(&disk->mountpoint, "/proc") ||
ffStrbufStartsWithS(&disk->mountpoint, "/zroot")
)
disk->type = FF_DISK_TYPE_HIDDEN;
else if((fs->f_flags & MNT_NOSUID) || !(fs->f_flags & MNT_LOCAL))
disk->type = FF_DISK_TYPE_EXTERNAL;
else
disk->type = FF_DISK_TYPE_REGULAR;
ffStrbufInit(&disk->name);
}
#else
void detectFsInfo(struct statfs* fs, FFDisk* disk);
#endif
void ffDetectDisksImpl(FFDiskResult* disks)
{
struct statfs* buf;
int size = getmntinfo(&buf, MNT_WAIT);
if(size <= 0)
ffStrbufAppendS(&disks->error, "getmntinfo() failed");
for(struct statfs* fs = buf; fs < buf + size; ++fs)
{
FFDisk* disk = ffListAdd(&disks->disks);
#ifdef __FreeBSD__
// f_bavail and f_ffree are signed on FreeBSD...
if(fs->f_bavail < 0) fs->f_bavail = 0;
if(fs->f_ffree < 0) fs->f_ffree = 0;
#endif
disk->bytesTotal = fs->f_blocks * fs->f_bsize;
disk->bytesUsed = disk->bytesTotal - ((uint64_t)fs->f_bavail * fs->f_bsize);
disk->filesTotal = (uint32_t) fs->f_files;
disk->filesUsed = (uint32_t) (disk->filesTotal - (uint64_t)fs->f_ffree);
ffStrbufInitS(&disk->mountpoint, fs->f_mntonname);
ffStrbufInitS(&disk->filesystem, fs->f_fstypename);
detectFsInfo(fs, disk);
}
}