From 71c5b0052502d4c20dcd11d67b49c80d70755aa9 Mon Sep 17 00:00:00 2001 From: Max Altgelt Date: Wed, 7 May 2025 15:07:21 +0200 Subject: [PATCH] fix: don't assume GetDriveType returns error GetDriveType does not set an error for GetLastError, see https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdrivetypea. Fix some code that assumed this. Also prefer windows.GetDriveType to a direct call. --- disk/disk_windows.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/disk/disk_windows.go b/disk/disk_windows.go index b9343cb..4e605e2 100644 --- a/disk/disk_windows.go +++ b/disk/disk_windows.go @@ -19,7 +19,6 @@ import ( var ( procGetDiskFreeSpaceExW = common.Modkernel32.NewProc("GetDiskFreeSpaceExW") procGetLogicalDriveStringsW = common.Modkernel32.NewProc("GetLogicalDriveStringsW") - procGetDriveType = common.Modkernel32.NewProc("GetDriveTypeW") procGetVolumeInformation = common.Modkernel32.NewProc("GetVolumeInformationW") ) @@ -109,15 +108,14 @@ func PartitionsWithContext(ctx context.Context, _ bool) ([]PartitionStat, error) if v >= 65 && v <= 90 { path := string(v) + ":" typepath, _ := windows.UTF16PtrFromString(path) - typeret, _, _ := procGetDriveType.Call(uintptr(unsafe.Pointer(typepath))) - if typeret == 0 { - err := windows.GetLastError() - warnings.Add(err) + typeret := windows.GetDriveType(typepath) + switch typeret { + case windows.DRIVE_UNKNOWN: continue - } - // 2: DRIVE_REMOVABLE 3: DRIVE_FIXED 4: DRIVE_REMOTE 5: DRIVE_CDROM - - if typeret == 2 || typeret == 3 || typeret == 4 || typeret == 5 { + case windows.DRIVE_REMOVABLE, + windows.DRIVE_FIXED, + windows.DRIVE_REMOTE, + windows.DRIVE_CDROM: lpVolumeNameBuffer := make([]byte, 256) lpVolumeSerialNumber := int64(0) lpMaximumComponentLength := int64(0) @@ -134,7 +132,8 @@ func PartitionsWithContext(ctx context.Context, _ bool) ([]PartitionStat, error) uintptr(unsafe.Pointer(&lpFileSystemNameBuffer[0])), uintptr(len(lpFileSystemNameBuffer))) if driveret == 0 { - if typeret == 2 || typeret == 4 || typeret == 5 { + switch typeret { + case windows.DRIVE_REMOVABLE, windows.DRIVE_REMOTE, windows.DRIVE_CDROM: continue // device is not ready will happen if there is no disk in the drive } warnings.Add(err) @@ -197,9 +196,6 @@ func IOCountersWithContext(_ context.Context, names ...string) (map[string]IOCou path := string(rune(v)) + ":" typepath, _ := windows.UTF16PtrFromString(path) typeret := windows.GetDriveType(typepath) - if typeret == 0 { - return drivemap, windows.GetLastError() - } if typeret != windows.DRIVE_FIXED { continue }