2022-10-09 19:47:13 +08:00
|
|
|
#include "disk.h"
|
2023-05-27 22:29:14 +08:00
|
|
|
#include "util/windows/unicode.h"
|
2022-10-09 19:47:13 +08:00
|
|
|
|
2022-10-17 13:19:48 +02:00
|
|
|
#include <windows.h>
|
2023-05-27 22:29:14 +08:00
|
|
|
#include <assert.h>
|
2022-10-09 19:47:13 +08:00
|
|
|
|
2023-06-12 22:09:52 +08:00
|
|
|
const char* ffDetectDisksImpl(FFlist* disks)
|
2022-10-09 19:47:13 +08:00
|
|
|
{
|
2023-05-27 22:29:14 +08:00
|
|
|
wchar_t buf[MAX_PATH + 1];
|
|
|
|
|
uint32_t length = GetLogicalDriveStringsW(sizeof(buf) / sizeof(*buf), buf);
|
2023-06-12 22:09:52 +08:00
|
|
|
if (length == 0 || length >= sizeof(buf) / sizeof(*buf))
|
|
|
|
|
return "GetLogicalDriveStringsW(sizeof(buf) / sizeof(*buf), buf) failed";
|
2022-10-09 19:47:13 +08:00
|
|
|
|
2022-10-17 13:19:48 +02:00
|
|
|
for(uint32_t i = 0; i < length; i++)
|
2022-10-09 19:47:13 +08:00
|
|
|
{
|
2023-05-27 22:29:14 +08:00
|
|
|
const wchar_t* mountpoint = buf + i;
|
2022-10-09 19:47:13 +08:00
|
|
|
|
2023-05-27 22:29:14 +08:00
|
|
|
UINT driveType = GetDriveTypeW(mountpoint);
|
2022-10-17 13:19:48 +02:00
|
|
|
if(driveType == DRIVE_NO_ROOT_DIR)
|
|
|
|
|
{
|
2023-05-27 22:29:14 +08:00
|
|
|
i += (uint32_t)wcslen(mountpoint);
|
2022-10-09 19:47:13 +08:00
|
|
|
continue;
|
2022-10-17 13:19:48 +02:00
|
|
|
}
|
2022-10-09 19:47:13 +08:00
|
|
|
|
2023-06-12 22:09:52 +08:00
|
|
|
FFDisk* disk = ffListAdd(disks);
|
2023-05-27 22:29:14 +08:00
|
|
|
ffStrbufInitWS(&disk->mountpoint, mountpoint);
|
2022-10-09 19:47:13 +08:00
|
|
|
|
2022-10-17 13:19:48 +02:00
|
|
|
uint64_t bytesFree;
|
2023-05-27 22:29:14 +08:00
|
|
|
if(!GetDiskFreeSpaceExW(mountpoint, NULL, (PULARGE_INTEGER)&disk->bytesTotal, (PULARGE_INTEGER)&bytesFree))
|
2022-10-17 13:19:48 +02:00
|
|
|
{
|
|
|
|
|
disk->bytesTotal = 0;
|
|
|
|
|
bytesFree = 0;
|
|
|
|
|
}
|
|
|
|
|
disk->bytesUsed = disk->bytesTotal - bytesFree;
|
2022-10-09 19:47:13 +08:00
|
|
|
|
2022-10-17 13:19:48 +02:00
|
|
|
if(driveType == DRIVE_REMOVABLE || driveType == DRIVE_REMOTE || driveType == DRIVE_CDROM)
|
2023-06-08 21:34:56 +08:00
|
|
|
disk->type = FF_DISK_TYPE_EXTERNAL_BIT;
|
2022-10-17 13:19:48 +02:00
|
|
|
else if(driveType == DRIVE_FIXED)
|
2023-06-08 21:34:56 +08:00
|
|
|
disk->type = FF_DISK_TYPE_REGULAR_BIT;
|
2022-10-17 13:19:48 +02:00
|
|
|
else
|
2023-06-08 21:34:56 +08:00
|
|
|
disk->type = FF_DISK_TYPE_HIDDEN_BIT;
|
2022-10-09 19:47:13 +08:00
|
|
|
|
2023-05-27 22:29:14 +08:00
|
|
|
ffStrbufInit(&disk->filesystem);
|
|
|
|
|
ffStrbufInit(&disk->name);
|
|
|
|
|
wchar_t diskName[MAX_PATH + 1], diskFileSystem[MAX_PATH + 1];
|
|
|
|
|
|
2022-10-18 18:39:28 +08:00
|
|
|
//https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumeinformationa#remarks
|
|
|
|
|
UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
|
2023-05-27 22:29:14 +08:00
|
|
|
|
|
|
|
|
BOOL result = GetVolumeInformationW(mountpoint,
|
|
|
|
|
diskName, sizeof(diskName) / sizeof(*diskName), //Volume name
|
2022-10-17 13:19:48 +02:00
|
|
|
NULL, //Serial number
|
|
|
|
|
NULL, //Max component length
|
|
|
|
|
NULL, //File system flags
|
2023-05-27 22:29:14 +08:00
|
|
|
diskFileSystem, sizeof(diskFileSystem) / sizeof(*diskFileSystem)
|
2022-10-17 13:19:48 +02:00
|
|
|
);
|
2022-10-18 18:39:28 +08:00
|
|
|
SetErrorMode(errorMode);
|
2023-05-27 22:29:14 +08:00
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufSetWS(&disk->filesystem, diskFileSystem);
|
|
|
|
|
ffStrbufSetWS(&disk->name, diskName);
|
|
|
|
|
}
|
2022-10-09 19:47:13 +08:00
|
|
|
|
2023-06-12 22:09:52 +08:00
|
|
|
//Unsupported
|
2022-10-17 13:19:48 +02:00
|
|
|
disk->filesUsed = 0;
|
|
|
|
|
disk->filesTotal = 0;
|
2022-10-09 19:47:13 +08:00
|
|
|
|
2022-10-17 13:19:48 +02:00
|
|
|
i += disk->mountpoint.length;
|
2022-10-09 19:47:13 +08:00
|
|
|
}
|
2023-06-12 22:09:52 +08:00
|
|
|
|
|
|
|
|
return NULL;
|
2022-10-09 19:47:13 +08:00
|
|
|
}
|