diff --git a/src/detection/disk/disk.c b/src/detection/disk/disk.c index e712a9c12..21520b532 100644 --- a/src/detection/disk/disk.c +++ b/src/detection/disk/disk.c @@ -1,6 +1,29 @@ #include "disk.h" -const char* ffDetectDisksImpl(FFlist* disks); +bool ffDiskMatchMountpoint(FFDiskOptions* options, const char* mountpoint) +{ + #ifdef _WIN32 + const char separator = ';'; + #else + const char separator = ':'; + #endif + + uint32_t mountpointLength = (uint32_t) strlen(mountpoint); + + uint32_t startIndex = 0; + while(startIndex < options->folders.length) + { + uint32_t colonIndex = ffStrbufNextIndexC(&options->folders, startIndex, separator); + + uint32_t folderLength = colonIndex - startIndex; + if (folderLength == mountpointLength && memcmp(options->folders.chars + startIndex, mountpoint, mountpointLength) == 0) + return true; + + startIndex = colonIndex + 1; + } + + return false; +} static int compareDisks(const FFDisk* disk1, const FFDisk* disk2) { @@ -9,7 +32,7 @@ static int compareDisks(const FFDisk* disk1, const FFDisk* disk2) const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks) { - const char* error = ffDetectDisksImpl(disks); + const char* error = ffDetectDisksImpl(options, disks); if (error) return error; if (disks->length == 0) return "No disks found"; diff --git a/src/detection/disk/disk.h b/src/detection/disk/disk.h index dc70bb88a..1700c6078 100644 --- a/src/detection/disk/disk.h +++ b/src/detection/disk/disk.h @@ -26,3 +26,6 @@ typedef struct FFDisk * If error is not set, disks contains at least one disk. */ const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks /* list of FFDisk */); + +const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks); +bool ffDiskMatchMountpoint(FFDiskOptions* options, const char* mountpoint); diff --git a/src/detection/disk/disk_bsd.c b/src/detection/disk/disk_bsd.c index c6f9a1c82..dcd6cc507 100644 --- a/src/detection/disk/disk_bsd.c +++ b/src/detection/disk/disk_bsd.c @@ -90,7 +90,7 @@ void detectFsInfo(struct statfs* fs, FFDisk* disk) } #endif -const char* ffDetectDisksImpl(FFlist* disks) +const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) { int size = getfsstat(NULL, 0, MNT_WAIT); @@ -103,7 +103,12 @@ const char* ffDetectDisksImpl(FFlist* disks) for(struct statfs* fs = buf; fs < buf + size; ++fs) { - if(!ffStrStartsWith(fs->f_mntfromname, "/dev/") && !ffStrEquals(fs->f_fstypename, "zfs")) + if(__builtin_expect(options->folders.length, 0)) + { + if(!ffDiskMatchMountpoint(options, fs->f_mntonname)) + continue; + } + else if(!ffStrStartsWith(fs->f_mntfromname, "/dev/") && !ffStrEquals(fs->f_fstypename, "zfs")) continue; #ifdef __FreeBSD__ diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index 31715e6d5..812cb5884 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -250,7 +250,7 @@ static void detectStats(FFDisk* disk) #endif } -const char* ffDetectDisksImpl(FFlist* disks) +const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) { FILE* mountsFile = setmntent("/proc/mounts", "r"); if(mountsFile == NULL) @@ -260,7 +260,12 @@ const char* ffDetectDisksImpl(FFlist* disks) while((device = getmntent(mountsFile))) { - if(!isPhysicalDevice(device)) + if (__builtin_expect(options->folders.length, 0)) + { + if (!ffDiskMatchMountpoint(options, device->mnt_dir)) + continue; + } + else if(!isPhysicalDevice(device)) continue; //We have a valid device, add it to the list diff --git a/src/detection/disk/disk_windows.c b/src/detection/disk/disk_windows.c index ef5329e50..8c0890eb6 100644 --- a/src/detection/disk/disk_windows.c +++ b/src/detection/disk/disk_windows.c @@ -6,23 +6,31 @@ #include #include -const char* ffDetectDisksImpl(FFlist* disks) +const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) { wchar_t buf[MAX_PATH + 1]; uint32_t length = GetLogicalDriveStringsW(sizeof(buf) / sizeof(*buf), buf); if (length == 0 || length >= sizeof(buf) / sizeof(*buf)) return "GetLogicalDriveStringsW(sizeof(buf) / sizeof(*buf), buf) failed"; + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); + for(uint32_t i = 0; i < length; i++) { wchar_t* mountpoint = buf + i; + ffStrbufSetWS(&buffer, mountpoint); + i += buffer.length; + UINT driveType = GetDriveTypeW(mountpoint); - if(driveType == DRIVE_NO_ROOT_DIR) + + if (__builtin_expect((long) options->folders.length, 0)) { - i += (uint32_t)wcslen(mountpoint); - continue; + if (!ffDiskMatchMountpoint(options, buffer.chars)) + continue; } + else if(driveType == DRIVE_NO_ROOT_DIR) + continue; FFDisk* disk = ffListAdd(disks); @@ -77,7 +85,7 @@ const char* ffDetectDisksImpl(FFlist* disks) else disk->createTime = 0; - ffStrbufInitWS(&disk->mountpoint, mountpoint); + ffStrbufInitMove(&disk->mountpoint, &buffer); if (mountpoint[2] == L'\\' && mountpoint[3] == L'\0') { wchar_t volumeName[MAX_PATH + 1]; @@ -91,8 +99,6 @@ const char* ffDetectDisksImpl(FFlist* disks) //Unsupported disk->filesUsed = 0; disk->filesTotal = 0; - - i += disk->mountpoint.length; } return NULL; diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index 364d5bf2d..d4946cf05 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -131,54 +131,6 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk) } } -static void printMountpoint(FFDiskOptions* options, const FFlist* disks, const char* mountpoint) -{ - FF_LIST_FOR_EACH(FFDisk, disk, *disks) - { - if(ffStrbufEqualS(&disk->mountpoint, mountpoint)) - { - printDisk(options, disk); - return; - } - } - - ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No disk found for mountpoint: %s", mountpoint); -} - -static void printMountpoints(FFDiskOptions* options, const FFlist* disks) -{ - #ifdef _WIN32 - const char separator = ';'; - #else - const char separator = ':'; - #endif - - FF_STRBUF_AUTO_DESTROY mountpoints = ffStrbufCreateCopy(&options->folders); - ffStrbufTrim(&mountpoints, separator); - - uint32_t startIndex = 0; - while(startIndex < mountpoints.length) - { - uint32_t colonIndex = ffStrbufNextIndexC(&mountpoints, startIndex, separator); - mountpoints.chars[colonIndex] = '\0'; - - printMountpoint(options, disks, mountpoints.chars + startIndex); - - startIndex = colonIndex + 1; - } -} - -static void printAutodetected(FFDiskOptions* options, const FFlist* disks) -{ - FF_LIST_FOR_EACH(FFDisk, disk, *disks) - { - if(disk->type & ~options->showTypes) - continue; - - printDisk(options, disk); - } -} - void ffPrintDisk(FFDiskOptions* options) { FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk)); @@ -190,10 +142,13 @@ void ffPrintDisk(FFDiskOptions* options) } else { - if(options->folders.length == 0) - printAutodetected(options, &disks); - else - printMountpoints(options, &disks); + FF_LIST_FOR_EACH(FFDisk, disk, disks) + { + if(__builtin_expect(options->folders.length == 0, 1) && (disk->type & ~options->showTypes)) + continue; + + printDisk(options, disk); + } } FF_LIST_FOR_EACH(FFDisk, disk, disks)