diff --git a/src/detection/disk/disk.c b/src/detection/disk/disk.c index 55cfec7de..e033f6c93 100644 --- a/src/detection/disk/disk.c +++ b/src/detection/disk/disk.c @@ -1,35 +1,29 @@ #include "disk.h" -#include "detection/internal.h" -void ffDetectDisksImpl(FFDiskResult* disks); +const char* ffDetectDisksImpl(FFlist* disks); static int compareDisks(const void* disk1, const void* disk2) { return ffStrbufCompAlphabetically(&((const FFDisk*) disk1)->mountpoint, &((const FFDisk*) disk2)->mountpoint); } -const FFDiskResult* ffDetectDisks() +const char* ffDetectDisks(FFlist* disks) { - FF_DETECTION_INTERNAL_GUARD(FFDiskResult, - ffStrbufInit(&result.error); - ffListInitA(&result.disks, sizeof(FFDisk), 4); + const char* error = ffDetectDisksImpl(disks); - ffDetectDisksImpl(&result); + if (error) return error; + if (disks->length == 0) return "No disks found"; - if(result.disks.length == 0 && result.error.length == 0) - ffStrbufAppendS(&result.error, "No disks found"); - else - { - //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 - //Note that we sort alphabetically here for a better ordering when printing the list, - // so the check must be done in reverse order - ffListSort(&result.disks, compareDisks); - FF_LIST_FOR_EACH(FFDisk, disk, result.disks) - { - if(disk->bytesTotal == 0) - disk->type |= FF_DISK_TYPE_UNKNOWN_BIT; - } - } - ); + //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 + //Note that we sort alphabetically here for a better ordering when printing the list, + // so the check must be done in reverse order + ffListSort(disks, compareDisks); + FF_LIST_FOR_EACH(FFDisk, disk, *disks) + { + if(disk->bytesTotal == 0) + disk->type |= FF_DISK_TYPE_UNKNOWN_BIT; + } + + return NULL; } diff --git a/src/detection/disk/disk.h b/src/detection/disk/disk.h index 9572f267f..b4d9e53e7 100644 --- a/src/detection/disk/disk.h +++ b/src/detection/disk/disk.h @@ -19,18 +19,10 @@ typedef struct FFDisk uint32_t filesTotal; } FFDisk; -typedef struct FFDiskResult -{ - FFstrbuf error; - FFlist disks; //List of FFDisk -} FFDiskResult; - /** * Returns a List of FFDisk, sorted alphabetically by mountpoint. * If error is not set, disks contains at least one disk. - * - * @return const FFDiskResult* */ -const FFDiskResult* ffDetectDisks(); +const char* ffDetectDisks(FFlist* result /* list of FFDisk */); #endif diff --git a/src/detection/disk/disk_bsd.c b/src/detection/disk/disk_bsd.c index 3ff71925a..e6be77659 100644 --- a/src/detection/disk/disk_bsd.c +++ b/src/detection/disk/disk_bsd.c @@ -25,17 +25,17 @@ static void detectFsInfo(struct statfs* fs, FFDisk* disk) void detectFsInfo(struct statfs* fs, FFDisk* disk); #endif -void ffDetectDisksImpl(FFDiskResult* disks) +const char* ffDetectDisksImpl(FFlist* disks) { struct statfs* buf; int size = getmntinfo(&buf, MNT_WAIT); if(size <= 0) - ffStrbufAppendS(&disks->error, "getmntinfo() failed"); + return "getmntinfo(&buf, MNT_WAIT) failed"; for(struct statfs* fs = buf; fs < buf + size; ++fs) { - FFDisk* disk = ffListAdd(&disks->disks); + FFDisk* disk = ffListAdd(disks); #ifdef __FreeBSD__ // f_bavail and f_ffree are signed on FreeBSD... @@ -53,4 +53,6 @@ void ffDetectDisksImpl(FFDiskResult* disks) ffStrbufInitS(&disk->filesystem, fs->f_fstypename); detectFsInfo(fs, disk); } + + return NULL; } diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index 210417ef4..f2e9d71ed 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -192,14 +192,11 @@ static void detectStats(FFDisk* disk) disk->filesUsed = (uint32_t) (disk->filesTotal - fs.f_ffree); } -void ffDetectDisksImpl(FFDiskResult* disks) +const char* ffDetectDisksImpl(FFlist* disks) { FILE* mountsFile = fopen("/proc/mounts", "r"); if(mountsFile == NULL) - { - ffStrbufAppendS(&disks->error, "fopen(\"/proc/mounts\", \"r\") == NULL"); - return; - } + return "fopen(\"/proc/mounts\", \"r\") == NULL"; FF_LIST_AUTO_DESTROY devices = ffListCreate(sizeof(FFstrbuf)); @@ -224,7 +221,7 @@ void ffDetectDisksImpl(FFDiskResult* disks) } //We have a valid device, add it to the list - FFDisk* disk = ffListAdd(&disks->disks); + FFDisk* disk = ffListAdd(disks); //detect mountpoint ffStrbufInit(&disk->mountpoint); @@ -252,4 +249,6 @@ void ffDetectDisksImpl(FFDiskResult* disks) ffStrbufDestroy(device); fclose(mountsFile); + + return NULL; } diff --git a/src/detection/disk/disk_windows.c b/src/detection/disk/disk_windows.c index d98362694..472b9730f 100644 --- a/src/detection/disk/disk_windows.c +++ b/src/detection/disk/disk_windows.c @@ -4,11 +4,12 @@ #include #include -void ffDetectDisksImpl(FFDiskResult* disks) +const char* ffDetectDisksImpl(FFlist* disks) { wchar_t buf[MAX_PATH + 1]; uint32_t length = GetLogicalDriveStringsW(sizeof(buf) / sizeof(*buf), buf); - assert(length < sizeof(buf) / sizeof(*buf)); + if (length == 0 || length >= sizeof(buf) / sizeof(*buf)) + return "GetLogicalDriveStringsW(sizeof(buf) / sizeof(*buf), buf) failed"; for(uint32_t i = 0; i < length; i++) { @@ -21,7 +22,7 @@ void ffDetectDisksImpl(FFDiskResult* disks) continue; } - FFDisk* disk = ffListAdd(&disks->disks); + FFDisk* disk = ffListAdd(disks); ffStrbufInitWS(&disk->mountpoint, mountpoint); uint64_t bytesFree; @@ -61,10 +62,12 @@ void ffDetectDisksImpl(FFDiskResult* disks) ffStrbufSetWS(&disk->name, diskName); } - //TODO: implement + //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 6dd112216..1bacc0bda 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -133,17 +133,27 @@ static void printAutodetected(FFinstance* instance, FFDiskOptions* options, cons void ffPrintDisk(FFinstance* instance, FFDiskOptions* options) { - const FFDiskResult* disks = ffDetectDisks(); - if(disks->error.length > 0) + FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk)); + const char* error = ffDetectDisks(&disks); + + if(error) { - ffPrintError(instance, FF_DISK_MODULE_NAME, 0, &options->moduleArgs, "%s", disks->error.chars); - return; + ffPrintError(instance, FF_DISK_MODULE_NAME, 0, &options->moduleArgs, "%s", error); + } + else + { + if(options->folders.length == 0) + printAutodetected(instance, options, &disks); + else + printMountpoints(instance, options, &disks); } - if(options->folders.length == 0) - printAutodetected(instance, options, &disks->disks); - else - printMountpoints(instance, options, &disks->disks); + FF_LIST_FOR_EACH(FFDisk, disk, disks) + { + ffStrbufDestroy(&disk->mountpoint); + ffStrbufDestroy(&disk->filesystem); + ffStrbufDestroy(&disk->name); + } }