mirror of
https://github.com/ZoiteChat/zoitechat.git
synced 2026-12-05 06:22:18 +00:00
Windows boot drive capacity
This commit is contained in:
1 file changed
+50
-71
@@ -18,6 +18,7 @@
|
||||
#include <iphlpapi.h>
|
||||
#include <ipifcons.h>
|
||||
#include <mmsystem.h>
|
||||
#include <winioctl.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma comment(lib, "advapi32.lib")
|
||||
@@ -555,94 +556,72 @@ sysinfo_backend_get_memory (void)
|
||||
return format_usage ((guint64) memory.ullTotalPhys, (guint64) memory.ullAvailPhys);
|
||||
}
|
||||
|
||||
static char *
|
||||
get_system_volume_usage (void)
|
||||
static gboolean
|
||||
get_boot_disk_number (DWORD *number)
|
||||
{
|
||||
WCHAR windows_dir[MAX_PATH];
|
||||
WCHAR volume_path[MAX_PATH];
|
||||
ULARGE_INTEGER available, total, total_free;
|
||||
WCHAR volume_name[MAX_PATH];
|
||||
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));
|
||||
if (!length || length >= G_N_ELEMENTS (windows_dir))
|
||||
return NULL;
|
||||
if (!GetVolumePathNameW (windows_dir, volume_path, G_N_ELEMENTS (volume_path)))
|
||||
return NULL;
|
||||
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;
|
||||
result = g_strdup_printf ("%s (system volume only)", usage);
|
||||
g_free (usage);
|
||||
return result;
|
||||
if (!length || length >= G_N_ELEMENTS (windows_dir) ||
|
||||
!GetVolumePathNameW (windows_dir, volume_path, G_N_ELEMENTS (volume_path)) ||
|
||||
GetDriveTypeW (volume_path) != DRIVE_FIXED ||
|
||||
!GetVolumeNameForVolumeMountPointW (volume_path, volume_name, G_N_ELEMENTS (volume_name)))
|
||||
return FALSE;
|
||||
length = lstrlenW (volume_name);
|
||||
if (!length || volume_name[length - 1] != L'\\')
|
||||
return FALSE;
|
||||
volume_name[length - 1] = L'\0';
|
||||
volume = CreateFileW (volume_name, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
NULL, OPEN_EXISTING, 0, NULL);
|
||||
if (volume == INVALID_HANDLE_VALUE)
|
||||
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 *
|
||||
sysinfo_backend_get_disk (void)
|
||||
{
|
||||
DWORD drives = GetLogicalDrives ();
|
||||
DWORD previous_mode = 0;
|
||||
DWORD number, returned, 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;
|
||||
DISK_GEOMETRY_EX geometry;
|
||||
HANDLE disk;
|
||||
char *path, *result = NULL;
|
||||
|
||||
/* 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++)
|
||||
if (get_boot_disk_number (&number))
|
||||
{
|
||||
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;
|
||||
/* The PhysicalDrive handle describes the complete disk, not a
|
||||
* partition or filesystem. Request metadata access only. */
|
||||
path = g_strdup_printf ("\\\\.\\PhysicalDrive%lu", (unsigned long) number);
|
||||
disk = CreateFileA (path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
NULL, OPEN_EXISTING, 0, NULL);
|
||||
g_free (path);
|
||||
if (disk != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
char *lower = g_ascii_strdown (key, -1);
|
||||
g_free (key);
|
||||
key = lower;
|
||||
if (DeviceIoControl (disk, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0,
|
||||
&geometry, sizeof (geometry), &returned, NULL) &&
|
||||
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)
|
||||
SetThreadErrorMode (previous_mode, NULL);
|
||||
|
||||
Reference in new issue
Block a user