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.
pull/1848/head
Max Altgelt 6 days ago
parent 7411b4e266
commit 71c5b00525
No known key found for this signature in database
GPG Key ID: D76CC9A61CFEB041

@ -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
}

Loading…
Cancel
Save