From 645bf5c839ee954f368fe3176096c7f86618dabf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 6 Nov 2025 13:39:30 +0800 Subject: [PATCH] Disk: moves disk hiding by folder and FS type to detection code Don't probe if hided Fixes #2043 --- src/detection/disk/disk.c | 26 ++++++++++++++++++++- src/detection/disk/disk.h | 4 ++++ src/detection/disk/disk_bsd.c | 6 +++++ src/detection/disk/disk_haiku.cpp | 6 +++++ src/detection/disk/disk_linux.c | 6 +++++ src/detection/disk/disk_sunos.c | 6 +++++ src/detection/disk/disk_windows.c | 38 ++++++++++++++++++++----------- src/modules/disk/disk.c | 38 ++++++------------------------- 8 files changed, 85 insertions(+), 45 deletions(-) diff --git a/src/detection/disk/disk.c b/src/detection/disk/disk.c index 5d52b86e7..d63a105fe 100644 --- a/src/detection/disk/disk.c +++ b/src/detection/disk/disk.c @@ -10,7 +10,7 @@ const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks) const char* error = ffDetectDisksImpl(options, disks); if (error) return error; - if (disks->length == 0) return "No disks found"; + if (disks->length == 0) return NULL; //We need to sort the disks, so that we can detect, which disk a path resides on // For example for /boot/efi/bootmgr we need to check /boot/efi before /boot @@ -31,3 +31,27 @@ const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks) return NULL; } + +#ifndef _WIN32 +#include + +bool ffDiskMatchesFolderPatterns(FFstrbuf* folders, const char* path, char separator) +{ + uint32_t startIndex = 0; + while(startIndex < folders->length) + { + uint32_t sepIndex = ffStrbufNextIndexC(folders, startIndex, separator); + + char savedSep = folders->chars[sepIndex]; // Can be '\0' if at end + folders->chars[sepIndex] = '\0'; + + bool matched = fnmatch(&folders->chars[startIndex], path, 0) == 0; + folders->chars[sepIndex] = savedSep; + + if (matched) return true; + + startIndex = sepIndex + 1; + } + return false; +} +#endif diff --git a/src/detection/disk/disk.h b/src/detection/disk/disk.h index f2fab8782..abf688448 100644 --- a/src/detection/disk/disk.h +++ b/src/detection/disk/disk.h @@ -35,3 +35,7 @@ typedef struct FFDisk const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks /* list of FFDisk */); const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks); + +#ifndef _WIN32 +bool ffDiskMatchesFolderPatterns(FFstrbuf* folders, const char* path, char separator); +#endif diff --git a/src/detection/disk/disk_bsd.c b/src/detection/disk/disk_bsd.c index 13597fb96..35386b8bb 100644 --- a/src/detection/disk/disk_bsd.c +++ b/src/detection/disk/disk_bsd.c @@ -151,6 +151,12 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) else if(!ffStrEquals(fs->f_mntonname, "/") && !ffStrStartsWith(fs->f_mntfromname, "/dev/") && !ffStrEquals(fs->f_fstypename, "zfs") && !ffStrEquals(fs->f_fstypename, "fusefs.sshfs")) continue; + if (options->hideFolders.length && ffDiskMatchesFolderPatterns(&options->hideFolders, fs->f_mntonname, FF_DISK_FOLDER_SEPARATOR)) + continue; + + if (options->hideFS.length && ffStrbufSeparatedContainS(&options->hideFS, fs->f_fstypename, ':')) + continue; + #ifdef __FreeBSD__ // f_bavail and f_ffree are signed on FreeBSD... if(fs->f_bavail < 0) fs->f_bavail = 0; diff --git a/src/detection/disk/disk_haiku.cpp b/src/detection/disk/disk_haiku.cpp index 25fe018b9..70479667e 100644 --- a/src/detection/disk/disk_haiku.cpp +++ b/src/detection/disk/disk_haiku.cpp @@ -27,6 +27,12 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) continue; } + if (options->hideFolders.length && ffDiskMatchesFolderPatterns(&options->hideFolders, path.Path(), FF_DISK_FOLDER_SEPARATOR)) + continue; + + if (options->hideFS.length && ffStrbufSeparatedContainS(&options->hideFS, fs.fsh_name, ':')) + continue; + FFDisk* disk = (FFDisk*) ffListAdd(disks); disk->bytesTotal = (uint64_t)fs.total_blocks * (uint64_t) fs.block_size; diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index 68f783e4c..518d356ac 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -297,6 +297,12 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) else if(!isPhysicalDevice(device)) continue; + if (options->hideFolders.length && ffDiskMatchesFolderPatterns(&options->hideFolders, device->mnt_dir, FF_DISK_FOLDER_SEPARATOR)) + continue; + + if (options->hideFS.length && ffStrbufSeparatedContainS(&options->hideFS, device->mnt_type, ':')) + continue; + //We have a valid device, add it to the list FFDisk* disk = ffListAdd(disks); disk->type = FF_DISK_VOLUME_TYPE_NONE; diff --git a/src/detection/disk/disk_sunos.c b/src/detection/disk/disk_sunos.c index 8272cc1ed..e7b0c2f53 100644 --- a/src/detection/disk/disk_sunos.c +++ b/src/detection/disk/disk_sunos.c @@ -119,6 +119,12 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) else if(!isPhysicalDevice(&device)) continue; + if (options->hideFolders.length && ffDiskMatchesFolderPatterns(&options->hideFolders, device.mnt_mountp, FF_DISK_FOLDER_SEPARATOR)) + continue; + + if (options->hideFS.length && ffStrbufSeparatedContainS(&options->hideFS, device.mnt_fstype, ':')) + continue; + //We have a valid device, add it to the list FFDisk* disk = ffListAdd(disks); disk->type = FF_DISK_VOLUME_TYPE_NONE; diff --git a/src/detection/disk/disk_windows.c b/src/detection/disk/disk_windows.c index ea6766b45..524f0cd91 100644 --- a/src/detection/disk/disk_windows.c +++ b/src/detection/disk/disk_windows.c @@ -53,6 +53,29 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) else if(driveType == DRIVE_NO_ROOT_DIR) continue; + if (options->hideFolders.length && ffStrbufSeparatedContain(&options->hideFolders, &buffer, FF_DISK_FOLDER_SEPARATOR)) + continue; + + wchar_t diskName[MAX_PATH + 1], diskFileSystem[MAX_PATH + 1]; + + DWORD diskFlags; + BOOL volumeInfoAvailable = GetVolumeInformationW(mountpoint, + diskName, ARRAY_SIZE(diskName), //Volume name + NULL, //Serial number + NULL, //Max component length + &diskFlags, //File system flags + diskFileSystem, ARRAY_SIZE(diskFileSystem) + ); + + FF_STRBUF_AUTO_DESTROY diskFileSystemBuf = ffStrbufCreate(); + + if (volumeInfoAvailable) + { + ffStrbufSetWS(&diskFileSystemBuf, diskFileSystem); + if (options->hideFS.length && ffStrbufSeparatedContain(&options->hideFS, &diskFileSystemBuf, ':')) + continue; + } + FFDisk* disk = ffListAdd(disks); disk->filesUsed = 0; @@ -97,20 +120,9 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) (PULARGE_INTEGER)&disk->bytesFree ); - wchar_t diskName[MAX_PATH + 1], diskFileSystem[MAX_PATH + 1]; - - DWORD diskFlags; - BOOL result = GetVolumeInformationW(mountpoint, - diskName, ARRAY_SIZE(diskName), //Volume name - NULL, //Serial number - NULL, //Max component length - &diskFlags, //File system flags - diskFileSystem, ARRAY_SIZE(diskFileSystem) - ); - - if(result) + if(volumeInfoAvailable) { - ffStrbufSetWS(&disk->filesystem, diskFileSystem); + ffStrbufInitMove(&disk->filesystem, &diskFileSystemBuf); ffStrbufSetWS(&disk->name, diskName); if(diskFlags & FILE_READ_ONLY_VOLUME) disk->type |= FF_DISK_VOLUME_TYPE_READONLY_BIT; diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index 3369287c2..947f7482f 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -189,30 +189,6 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index } } -static inline bool isMatchFolders(FFstrbuf* folders, const FFstrbuf* path, char separator) -{ - #ifndef _WIN32 - uint32_t startIndex = 0; - while(startIndex < folders->length) - { - uint32_t colonIndex = ffStrbufNextIndexC(folders, startIndex, separator); - - char savedColon = folders->chars[colonIndex]; // Can be '\0' if at end - folders->chars[colonIndex] = '\0'; - - bool matched = fnmatch(&folders->chars[startIndex], path->chars, 0) == 0; - folders->chars[colonIndex] = savedColon; - - if (matched) return true; - - startIndex = colonIndex + 1; - } - return false; - #else - return ffStrbufSeparatedContain(folders, path, separator); - #endif -} - bool ffPrintDisk(FFDiskOptions* options) { FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk)); @@ -224,18 +200,18 @@ bool ffPrintDisk(FFDiskOptions* options) return false; } + if(disks.length == 0) + { + ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No disks found"); + return false; + } + uint32_t index = 0; FF_LIST_FOR_EACH(FFDisk, disk, disks) { if(__builtin_expect(options->folders.length == 0, 1) && (disk->type & ~options->showTypes)) continue; - if (options->hideFolders.length && isMatchFolders(&options->hideFolders, &disk->mountpoint, FF_DISK_FOLDER_SEPARATOR)) - continue; - - if (options->hideFS.length && ffStrbufSeparatedContain(&options->hideFS, &disk->filesystem, ':')) - continue; - printDisk(options, disk, ++index); } @@ -407,7 +383,7 @@ bool ffGenerateDiskJsonResult(FFDiskOptions* options, yyjson_mut_doc* doc, yyjso if(error) { - yyjson_mut_obj_add_str(doc, module, "result", error); + yyjson_mut_obj_add_str(doc, module, "error", error); return false; }