Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inaccurate disk size on windows #325

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions pkg/block/block_windows.go
Expand Up @@ -6,7 +6,10 @@
package block

import (
"os"
"strings"
"syscall"
"unsafe"

"github.com/StackExchange/wmi"

Expand Down Expand Up @@ -158,6 +161,26 @@ func getDiskDrives() ([]win32DiskDrive, error) {
if err := wmi.Query(wqlDiskDrive, &win3232DiskDriveDescriptions); err != nil {
return nil, err
}
for _, disk := range win3232DiskDriveDescriptions {
if disk.DeviceID == nil {
continue
}
volume, err := os.OpenFile(*disk.DeviceID, os.O_RDWR, 0)
if err != nil {
continue
}
// Get the volume's underlying partition size in NTFS clusters.
var (
partitionSize int64
bytes uint32
)
const _IOCTL_DISK_GET_LENGTH_INFO = 0x0007405C
err = syscall.DeviceIoControl(syscall.Handle(volume.Fd()), _IOCTL_DISK_GET_LENGTH_INFO, nil, 0, (*byte)(unsafe.Pointer(&partitionSize)), 8, &bytes, nil)
volume.Close()
if err == nil {
*disk.Size = uint64(partitionSize)
}
}
return win3232DiskDriveDescriptions, nil
}

Expand Down