128 lines
3.7 KiB
Go
128 lines
3.7 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"` // storage device is not confirmed internal and non-removable
|
|
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
|
|
}
|
|
guid := volumeGUID(root)
|
|
internal, known := volumeInternalFixed(strings.TrimSuffix(guid, `\`))
|
|
out = append(out, Drive{
|
|
Root: root,
|
|
FreeGB: free,
|
|
System: strings.EqualFold(root[:2], sys),
|
|
External: !known || !internal,
|
|
VolumeGUID: guid,
|
|
})
|
|
}
|
|
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)
|
|
}
|
|
|
|
// volumeInternalFixed asks the volume's storage device for its bus type and removable flag
|
|
// (IOCTL_STORAGE_QUERY_PROPERTY, StorageDeviceProperty). Opening with no
|
|
// access rights needs no elevation.
|
|
func volumeInternalFixed(devicePath string) (internal, known bool) {
|
|
if devicePath == "" {
|
|
return false, false
|
|
}
|
|
path, err := windows.UTF16PtrFromString(devicePath)
|
|
if err != nil {
|
|
return false, 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, false
|
|
}
|
|
defer windows.CloseHandle(h)
|
|
|
|
const ioctlStorageQueryProperty = 0x002D1400
|
|
// 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, false
|
|
}
|
|
return internalStorageDevice(out[:n])
|
|
}
|
|
|
|
func internalStorageDevice(descriptor []byte) (internal, known bool) {
|
|
// STORAGE_DEVICE_DESCRIPTOR.RemovableMedia is at offset 10 and BusType at 28.
|
|
if len(descriptor) < 32 {
|
|
return false, false
|
|
}
|
|
switch binary.LittleEndian.Uint32(descriptor[28:32]) {
|
|
case 1, 2, 3, 8, 10, 11, 16, 17, 18, 19:
|
|
return descriptor[10] == 0, true
|
|
default:
|
|
return false, true
|
|
}
|
|
}
|
|
|
|
// FreeGB is the free space on the volume holding path.
|
|
func FreeGB(path string) (float64, bool) { return probeFreeDiskGB(path) }
|