Files
run/runtime/metrics_disk_windows.go
2026-08-26 09:56:43 +08:00

30 lines
756 B
Go

//go:build windows
package runtime
import (
"path/filepath"
"syscall"
"unsafe"
)
var getDiskFreeSpaceEx = syscall.NewLazyDLL("kernel32.dll").NewProc("GetDiskFreeSpaceExW")
func workspaceDiskPercent(workspaceRoot string) (float64, error) {
volume := filepath.VolumeName(workspaceRoot)
if volume == "" {
volume = workspaceRoot
} else {
volume += `\`
}
var available, total, free uint64
success, _, callErr := getDiskFreeSpaceEx.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(volume))), uintptr(unsafe.Pointer(&available)), uintptr(unsafe.Pointer(&total)), uintptr(unsafe.Pointer(&free)))
if success == 0 {
return 0, callErr
}
if total == 0 {
return 0, syscall.EINVAL
}
return 100 * float64(total-free) / float64(total), nil
}