23 lines
624 B
Go
23 lines
624 B
Go
//go:build windows
|
|||
|
|
|
||
|
|
package handler
|
||
|
|
|
||
|
|
import "golang.org/x/sys/windows"
|
||
|
|
|
||
|
|
// diskUsage 返回指定目录所在磁盘的已用百分比(0-100)、可用字节、总字节。
|
||
|
|
func diskUsage(path string) (usedPercent int, freeBytes, totalBytes uint64, err error) {
|
||
|
|
p, e := windows.UTF16PtrFromString(path)
|
||
|
|
if e != nil {
|
||
|
|
return 0, 0, 0, e
|
||
|
|
}
|
||
|
|
var freeAvail, total, totalFree uint64
|
||
|
|
if e := windows.GetDiskFreeSpaceEx(p, &freeAvail, &total, &totalFree); e != nil {
|
||
|
|
return 0, 0, 0, e
|
||
|
|
}
|
||
|
|
if total == 0 {
|
||
|
|
return 0, totalFree, total, nil
|
||
|
|
}
|
||
|
|
used := total - totalFree
|
||
|
|
return int(used * 100 / total), totalFree, total, nil
|
||
|
|
}
|