From 5472621ac0f4cb5296ef7acfb8443827c659215d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 27 Oct 2022 14:58:29 +0800 Subject: [PATCH] Disk: support freebsd Also rewrite macOS detection to share code with bsd --- CMakeLists.txt | 3 +- src/detection/disk/disk_apple.m | 64 ++++++--------------------------- src/detection/disk/disk_bsd.c | 56 +++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 src/detection/disk/disk_bsd.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 850a72a63..79e059926 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/detection/disk/disk_apple.m b/src/detection/disk/disk_apple.m index 42c2e6a26..b2f7cec08 100644 --- a/src/detection/disk/disk_apple.m +++ b/src/detection/disk/disk_apple.m @@ -1,61 +1,17 @@ #include "disk.h" -#include +#include #import -#import -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); } diff --git a/src/detection/disk/disk_bsd.c b/src/detection/disk/disk_bsd.c new file mode 100644 index 000000000..0e14ef3f2 --- /dev/null +++ b/src/detection/disk/disk_bsd.c @@ -0,0 +1,56 @@ +#include "disk.h" + +#include + +#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); + } +}