diff --git a/disk/disk_windows.go b/disk/disk_windows.go index 3d22a3d..edce1f9 100644 --- a/disk/disk_windows.go +++ b/disk/disk_windows.go @@ -19,8 +19,10 @@ import ( "github.com/shirou/gopsutil/v4/internal/common" ) -const volumeNameBufferLength = uint32(windows.MAX_PATH + 1) -const volumePathBufferLength = volumeNameBufferLength +const ( + volumeNameBufferLength = uint32(windows.MAX_PATH + 1) + volumePathBufferLength = volumeNameBufferLength +) var ( procGetDiskFreeSpaceExW = common.Modkernel32.NewProc("GetDiskFreeSpaceExW") @@ -165,7 +167,8 @@ func PartitionsWithContext(ctx context.Context, _ bool) ([]PartitionStat, error) nextVolHandle, uintptr(unsafe.Pointer(&volNameBuf[0])), uintptr(volumeNameBufferLength)); err != nil && volRet == 0 { - if errno, ok := err.(syscall.Errno); ok && errno == windows.ERROR_NO_MORE_FILES { + var errno syscall.Errno + if errors.As(err, &errno) && errno == windows.ERROR_NO_MORE_FILES { break } warnings.Add(fmt.Errorf("failed to find next volume: %w", err)) @@ -257,40 +260,41 @@ func IOCountersWithContext(_ context.Context, names ...string) (map[string]IOCou return drivemap, err } for _, v := range lpBuffer[:lpBufferLen] { - if 'A' <= v && v <= 'Z' { - path := string(rune(v)) + ":" - typepath, _ := windows.UTF16PtrFromString(path) - typeret := windows.GetDriveType(typepath) - if typeret != windows.DRIVE_FIXED { + if v < 'A' || v > 'Z' { + continue + } + path := string(rune(v)) + ":" + typepath, _ := windows.UTF16PtrFromString(path) + typeret := windows.GetDriveType(typepath) + if typeret != windows.DRIVE_FIXED { + continue + } + szDevice := `\\.\` + path + const IOCTL_DISK_PERFORMANCE = 0x70020 + h, err := windows.CreateFile(syscall.StringToUTF16Ptr(szDevice), 0, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0) + if err != nil { + if errors.Is(err, windows.ERROR_FILE_NOT_FOUND) { continue } - szDevice := `\\.\` + path - const IOCTL_DISK_PERFORMANCE = 0x70020 - h, err := windows.CreateFile(syscall.StringToUTF16Ptr(szDevice), 0, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0) - if err != nil { - if errors.Is(err, windows.ERROR_FILE_NOT_FOUND) { - continue - } - return drivemap, err - } - defer windows.CloseHandle(h) + return drivemap, err + } + defer windows.CloseHandle(h) - var diskPerformanceSize uint32 - err = windows.DeviceIoControl(h, IOCTL_DISK_PERFORMANCE, nil, 0, (*byte)(unsafe.Pointer(&diskPerformance)), uint32(unsafe.Sizeof(diskPerformance)), &diskPerformanceSize, nil) - if err != nil { - return drivemap, err - } + var diskPerformanceSize uint32 + err = windows.DeviceIoControl(h, IOCTL_DISK_PERFORMANCE, nil, 0, (*byte)(unsafe.Pointer(&diskPerformance)), uint32(unsafe.Sizeof(diskPerformance)), &diskPerformanceSize, nil) + if err != nil { + return drivemap, err + } - if len(names) == 0 || common.StringsHas(names, path) { - drivemap[path] = IOCountersStat{ - ReadBytes: uint64(diskPerformance.BytesRead), - WriteBytes: uint64(diskPerformance.BytesWritten), - ReadCount: uint64(diskPerformance.ReadCount), - WriteCount: uint64(diskPerformance.WriteCount), - ReadTime: uint64(diskPerformance.ReadTime / 10000 / 1000), // convert to ms: https://github.com/giampaolo/psutil/issues/1012 - WriteTime: uint64(diskPerformance.WriteTime / 10000 / 1000), - Name: path, - } + if len(names) == 0 || common.StringsHas(names, path) { + drivemap[path] = IOCountersStat{ + ReadBytes: uint64(diskPerformance.BytesRead), + WriteBytes: uint64(diskPerformance.BytesWritten), + ReadCount: uint64(diskPerformance.ReadCount), + WriteCount: uint64(diskPerformance.WriteCount), + ReadTime: uint64(diskPerformance.ReadTime / 10000 / 1000), // convert to ms: https://github.com/giampaolo/psutil/issues/1012 + WriteTime: uint64(diskPerformance.WriteTime / 10000 / 1000), + Name: path, } } } diff --git a/go.mod b/go.mod index 9f6989a..8662ed2 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/ebitengine/purego v0.8.4 github.com/google/go-cmp v0.7.0 github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 - github.com/pkg/errors v0.9.1 github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c github.com/stretchr/testify v1.10.0 github.com/tklauser/go-sysconf v0.3.12 diff --git a/go.sum b/go.sum index a38601e..b14b648 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,6 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=