Files
Reims-Go/internal/host/drives_windows.go
T

115 lines
3.2 KiB
Go

//go:build windows
package host
import (
"encoding/binary"
"os"
"strings"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
var (
procGetLogicalDrives = kernel32.NewProc("GetLogicalDrives")
procGetDriveTypeW = kernel32.NewProc("GetDriveTypeW")
procGetVolumeNameForVolumeMountPointW = kernel32.NewProc("GetVolumeNameForVolumeMountPointW")
)
const driveFixed = 3 // DRIVE_FIXED
// Drive is a local fixed disk a data folder could go on.
type Drive struct {
Root string `json:"root"` // `D:\`
FreeGB float64 `json:"free_gb"` // bytes available to this user
System bool `json:"system"` // holds Windows
External bool `json:"external"` // on a USB bus (Windows still calls USB disks "fixed")
VolumeGUID string `json:"volume_guid,omitempty"` // stable volume identity; letters can be reassigned
}
// ListDrives returns local fixed drives (no removable, network or optical).
func ListDrives() []Drive {
mask, _, _ := procGetLogicalDrives.Call()
sys := strings.ToUpper(strings.TrimSuffix(os.Getenv("SystemDrive"), `\`))
if sys == "" {
sys = "C:"
}
var out []Drive
for i := 0; i < 26; i++ {
if mask&(1<<uint(i)) == 0 {
continue
}
root := string(rune('A'+i)) + `:\`
p, err := syscall.UTF16PtrFromString(root)
if err != nil {
continue
}
t, _, _ := procGetDriveTypeW.Call(uintptr(unsafe.Pointer(p)))
if t != driveFixed {
continue
}
free, ok := probeFreeDiskGB(root)
if !ok {
continue
}
out = append(out, Drive{
Root: root,
FreeGB: free,
System: strings.EqualFold(root[:2], sys),
External: isUSB(root[:2]),
VolumeGUID: volumeGUID(root),
})
}
return out
}
func volumeGUID(root string) string {
p, err := syscall.UTF16PtrFromString(root)
if err != nil {
return ""
}
buf := make([]uint16, 50)
r, _, _ := procGetVolumeNameForVolumeMountPointW.Call(
uintptr(unsafe.Pointer(p)), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)),
)
if r == 0 {
return ""
}
return syscall.UTF16ToString(buf)
}
// isUSB asks the volume's storage device for its bus type
// (IOCTL_STORAGE_QUERY_PROPERTY, StorageDeviceProperty). Opening with no
// access rights needs no elevation.
func isUSB(drive string) bool {
path, err := windows.UTF16PtrFromString(`\\.\` + drive)
if err != nil {
return false
}
h, err := windows.CreateFile(path, 0, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0)
if err != nil {
return false
}
defer windows.CloseHandle(h)
const ioctlStorageQueryProperty = 0x002D1400
const busTypeUSB = 7
// STORAGE_PROPERTY_QUERY{PropertyId: StorageDeviceProperty(0), QueryType: PropertyStandardQuery(0)}
query := make([]byte, 12)
out := make([]byte, 1024)
var n uint32
if err := windows.DeviceIoControl(h, ioctlStorageQueryProperty, &query[0], uint32(len(query)), &out[0], uint32(len(out)), &n, nil); err != nil {
return false
}
// STORAGE_DEVICE_DESCRIPTOR.BusType is the DWORD at offset 28.
if n < 32 {
return false
}
return binary.LittleEndian.Uint32(out[28:32]) == busTypeUSB
}
// FreeGB is the free space on the volume holding path.
func FreeGB(path string) (float64, bool) { return probeFreeDiskGB(path) }