From 487b7d8721f5a93192df1b290c9dc9c79ddc6bcd Mon Sep 17 00:00:00 2001 From: deepend Date: Fri, 4 Dec 2026 18:27:33 -0600 Subject: [PATCH] Windows fixed volume reporting --- plugins/sysinfo/win32/backend.c | 107 +++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 28 deletions(-) diff --git a/plugins/sysinfo/win32/backend.c b/plugins/sysinfo/win32/backend.c index 8cec67c4..958472ca 100644 --- a/plugins/sysinfo/win32/backend.c +++ b/plugins/sysinfo/win32/backend.c @@ -544,49 +544,100 @@ sysinfo_backend_get_memory (void) return format_usage ((guint64) memory.ullTotalPhys, (guint64) memory.ullAvailPhys); } -char * -sysinfo_backend_get_disk (void) +static char * +get_system_volume_usage (void) { WCHAR windows_dir[MAX_PATH]; WCHAR volume_path[MAX_PATH]; - ULARGE_INTEGER available; - ULARGE_INTEGER total; - ULARGE_INTEGER total_free; - char *usage; - char *volume_utf8; - char *result; + ULARGE_INTEGER available, total, total_free; + UINT length; + char *usage, *result; - if (GetWindowsDirectoryW (windows_dir, G_N_ELEMENTS (windows_dir)) == 0) - { + length = GetWindowsDirectoryW (windows_dir, G_N_ELEMENTS (windows_dir)); + if (!length || length >= G_N_ELEMENTS (windows_dir)) return NULL; - } if (!GetVolumePathNameW (windows_dir, volume_path, G_N_ELEMENTS (volume_path))) - { return NULL; - } - if (!GetDiskFreeSpaceExW (volume_path, &available, &total, &total_free)) - { + if (GetDriveTypeW (volume_path) != DRIVE_FIXED || + !GetDiskFreeSpaceExW (volume_path, &available, &total, &total_free)) return NULL; - } - usage = format_usage ((guint64) total.QuadPart, (guint64) available.QuadPart); if (!usage) - { return NULL; - } - - volume_utf8 = wide_to_utf8 (volume_path); - if (!volume_utf8) - { - return usage; - } - g_strchomp (volume_utf8); - result = g_strdup_printf ("%s" SYSINFO_SEP "%s", usage, volume_utf8); - g_free (volume_utf8); + result = g_strdup_printf ("%s (system volume only)", usage); g_free (usage); return result; } +char * +sysinfo_backend_get_disk (void) +{ + DWORD drives = GetLogicalDrives (); + DWORD previous_mode = 0; + BOOL error_mode_changed; + guint i; + guint64 total = 0, available = 0; + GHashTable *seen = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, 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); + for (i = 0; i < 26; i++) + { + WCHAR root[] = L"A:\\"; + WCHAR volume[MAX_PATH]; + ULARGE_INTEGER caller_free, caller_total, total_free; + char *key; + + if (!(drives & (1UL << i))) + continue; + 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); + g_free (key); + key = lower; + } + 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) + SetThreadErrorMode (previous_mode, NULL); + return result; +} + char * sysinfo_backend_get_uptime (void) {