30 lines
756 B
Go
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
|
|
}
|