Files
bj_power/bj_power_wms/internal/handler/disk_space_windows.go
T

23 lines
624 B
Go
Raw Normal View History

//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
}