Windows boot drive capacity

This commit is contained in:
deepend committed 2026-12-04 19:24:34 -06:00
1 parent 38e7a5c763
commit 3394faae04
1 file changed
+50 -71
+50 -71
View File
@@ -18,6 +18,7 @@
#include <iphlpapi.h> #include <iphlpapi.h>
#include <ipifcons.h> #include <ipifcons.h>
#include <mmsystem.h> #include <mmsystem.h>
#include <winioctl.h>
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma comment(lib, "advapi32.lib") #pragma comment(lib, "advapi32.lib")
@@ -555,94 +556,72 @@ sysinfo_backend_get_memory (void)
return format_usage ((guint64) memory.ullTotalPhys, (guint64) memory.ullAvailPhys); return format_usage ((guint64) memory.ullTotalPhys, (guint64) memory.ullAvailPhys);
} }
static char * static gboolean
get_system_volume_usage (void) get_boot_disk_number (DWORD *number)
{ {
WCHAR windows_dir[MAX_PATH]; WCHAR windows_dir[MAX_PATH];
WCHAR volume_path[MAX_PATH]; WCHAR volume_path[MAX_PATH];
ULARGE_INTEGER available, total, total_free; WCHAR volume_name[MAX_PATH];
UINT length; UINT length;
char *usage, *result; HANDLE volume;
VOLUME_DISK_EXTENTS extents;
DWORD returned;
gboolean valid = FALSE;
/* Windows calls the volume containing Windows the boot volume. Resolve
* it to a volume GUID instead of assuming Windows is installed on C:. */
length = GetWindowsDirectoryW (windows_dir, G_N_ELEMENTS (windows_dir)); length = GetWindowsDirectoryW (windows_dir, G_N_ELEMENTS (windows_dir));
if (!length || length >= G_N_ELEMENTS (windows_dir)) if (!length || length >= G_N_ELEMENTS (windows_dir) ||
return NULL; !GetVolumePathNameW (windows_dir, volume_path, G_N_ELEMENTS (volume_path)) ||
if (!GetVolumePathNameW (windows_dir, volume_path, G_N_ELEMENTS (volume_path))) GetDriveTypeW (volume_path) != DRIVE_FIXED ||
return NULL; !GetVolumeNameForVolumeMountPointW (volume_path, volume_name, G_N_ELEMENTS (volume_name)))
if (GetDriveTypeW (volume_path) != DRIVE_FIXED || return FALSE;
!GetDiskFreeSpaceExW (volume_path, &available, &total, &total_free)) length = lstrlenW (volume_name);
return NULL; if (!length || volume_name[length - 1] != L'\\')
usage = format_usage ((guint64) total.QuadPart, (guint64) available.QuadPart); return FALSE;
if (!usage) volume_name[length - 1] = L'\0';
return NULL; volume = CreateFileW (volume_name, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
result = g_strdup_printf ("%s (system volume only)", usage); NULL, OPEN_EXISTING, 0, NULL);
g_free (usage); if (volume == INVALID_HANDLE_VALUE)
return result; return FALSE;
/* Refuse spanned volumes rather than choosing an arbitrary disk. */
if (DeviceIoControl (volume, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0,
&extents, sizeof (extents), &returned, NULL) && returned >= sizeof (extents) &&
extents.NumberOfDiskExtents == 1)
{
*number = extents.Extents[0].DiskNumber;
valid = TRUE;
}
CloseHandle (volume);
return valid;
} }
char * char *
sysinfo_backend_get_disk (void) sysinfo_backend_get_disk (void)
{ {
DWORD drives = GetLogicalDrives (); DWORD number, returned, previous_mode = 0;
DWORD previous_mode = 0;
BOOL error_mode_changed; BOOL error_mode_changed;
guint i; DISK_GEOMETRY_EX geometry;
guint64 total = 0, available = 0; HANDLE disk;
GHashTable *seen = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); char *path, *result = NULL;
char *usage, *result;
/* Offline or empty local devices must not open a system error dialog. */
error_mode_changed = SetThreadErrorMode (GetThreadErrorMode () | SEM_FAILCRITICALERRORS, &previous_mode); error_mode_changed = SetThreadErrorMode (GetThreadErrorMode () | SEM_FAILCRITICALERRORS, &previous_mode);
for (i = 0; i < 26; i++) if (get_boot_disk_number (&number))
{ {
WCHAR root[] = L"A:\\"; /* The PhysicalDrive handle describes the complete disk, not a
WCHAR volume[MAX_PATH]; * partition or filesystem. Request metadata access only. */
ULARGE_INTEGER caller_free, caller_total, total_free; path = g_strdup_printf ("\\\\.\\PhysicalDrive%lu", (unsigned long) number);
char *key; disk = CreateFileA (path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (!(drives & (1UL << i))) g_free (path);
continue; if (disk != INVALID_HANDLE_VALUE)
root[0] = (WCHAR) (L'A' + i);
/* Don't query mapped network shares or removable media. */
if (GetDriveTypeW (root) != DRIVE_FIXED)
continue;
/* A stable volume GUID deduplicates aliases. SUBST paths without a
* volume GUID are deliberately skipped rather than double-counted. */
if (!GetVolumeNameForVolumeMountPointW (root, volume, G_N_ELEMENTS (volume)))
continue;
key = wide_to_utf8 (volume);
if (!key)
continue;
{ {
char *lower = g_ascii_strdown (key, -1); if (DeviceIoControl (disk, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0,
g_free (key); &geometry, sizeof (geometry), &returned, NULL) &&
key = lower; returned >= FIELD_OFFSET (DISK_GEOMETRY_EX, Data) && geometry.DiskSize.QuadPart > 0)
result = g_format_size ((guint64) geometry.DiskSize.QuadPart);
CloseHandle (disk);
} }
if (g_hash_table_contains (seen, key) ||
!GetDiskFreeSpaceExW (root, &caller_free, &caller_total, &total_free) ||
!caller_total.QuadPart)
{
g_free (key);
continue;
}
if ((guint64) caller_total.QuadPart > G_MAXUINT64 - total)
{
g_free (key);
total = 0;
break;
}
g_hash_table_add (seen, key);
total += (guint64) caller_total.QuadPart;
available += MIN ((guint64) caller_free.QuadPart, (guint64) caller_total.QuadPart);
}
g_hash_table_unref (seen);
usage = format_usage (total, available);
if (!usage)
result = get_system_volume_usage ();
else
{
result = g_strdup_printf ("%s (fixed volumes)", usage);
g_free (usage);
} }
if (error_mode_changed) if (error_mode_changed)
SetThreadErrorMode (previous_mode, NULL); SetThreadErrorMode (previous_mode, NULL);