2015-07-16 10:52:36 +00:00
|
|
|
package du
|
|
|
|
|
|
|
|
import (
|
2017-04-04 12:53:24 +00:00
|
|
|
"runtime"
|
2015-07-16 10:52:36 +00:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get returns the Usage of a given path, or an error if usage data is
|
|
|
|
// unavailable.
|
|
|
|
func Get(path string) (Usage, error) {
|
|
|
|
h := syscall.MustLoadDLL("kernel32.dll")
|
|
|
|
c := h.MustFindProc("GetDiskFreeSpaceExW")
|
|
|
|
|
|
|
|
var u Usage
|
|
|
|
|
2017-04-04 12:53:24 +00:00
|
|
|
pathw, err := syscall.UTF16PtrFromString(path)
|
|
|
|
if err != nil {
|
|
|
|
return Usage{}, err
|
|
|
|
}
|
|
|
|
|
2015-07-16 10:52:36 +00:00
|
|
|
ret, _, err := c.Call(
|
2017-04-04 12:53:24 +00:00
|
|
|
uintptr(unsafe.Pointer(pathw)),
|
2015-07-16 10:52:36 +00:00
|
|
|
uintptr(unsafe.Pointer(&u.FreeBytes)),
|
|
|
|
uintptr(unsafe.Pointer(&u.TotalBytes)),
|
|
|
|
uintptr(unsafe.Pointer(&u.AvailBytes)))
|
2017-04-04 12:53:24 +00:00
|
|
|
runtime.KeepAlive(pathw)
|
2015-07-16 10:52:36 +00:00
|
|
|
|
|
|
|
if ret == 0 {
|
2017-04-04 12:53:24 +00:00
|
|
|
return Usage{}, err
|
2015-07-16 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|