Disk: don't use singleton

This commit is contained in:
李通洲
2023-06-12 22:09:52 +08:00
committed by 李通洲
parent 88f354a872
commit d5a3802492
6 changed files with 53 additions and 53 deletions
+17 -23
View File
@@ -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;
}
+1 -9
View File
@@ -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
+5 -3
View File
@@ -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;
}
+5 -6
View File
@@ -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;
}
+7 -4
View File
@@ -4,11 +4,12 @@
#include <windows.h>
#include <assert.h>
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;
}
+18 -8
View File
@@ -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);
}
}