Improve disk code to return back disk StatFS{} structure

```
StatFS {
Total int64
Free int64
FSType string
}
```

Provides more information in a cross platform way.
This commit is contained in:
Harshavardhana
2015-10-17 19:09:43 -07:00
parent 4b3961e1df
commit a8a935f5fd
8 changed files with 116 additions and 59 deletions

View File

@@ -27,20 +27,14 @@ import (
// It returns free space available to the user (including quota limitations)
//
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx
func Stat(path string) (total, free int64, err error) {
kernel32, err := syscall.LoadLibrary("Kernel32.dll")
if err != nil {
return 0, 0, err
}
defer syscall.FreeLibrary(kernel32)
func Stat(path string) (statfs StatFS, err error) {
dll := syscall.MustLoadDLL("kernel32.dll")
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx
// Retrieves information about the amount of space that is available on a disk volume,
// which is the total amount of space, the total amount of free space, and the total
// amount of free space available to the user that is associated with the calling thread.
GetDiskFreeSpaceEx, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GetDiskFreeSpaceExW")
if err != nil {
return 0, 0, err
}
GetDiskFreeSpaceEx := dll.MustFindProc("GetDiskFreeSpaceExW")
lpFreeBytesAvailable := int64(0)
lpTotalNumberOfBytes := int64(0)
lpTotalNumberOfFreeBytes := int64(0)
@@ -52,18 +46,13 @@ func Stat(path string) (total, free int64, err error) {
// _Out_opt_ PULARGE_INTEGER lpTotalNumberOfBytes,
// _Out_opt_ PULARGE_INTEGER lpTotalNumberOfFreeBytes
// );
r1, _, e1 := syscall.Syscall6(uintptr(GetDiskFreeSpaceEx), 4,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
_, _, _ = GetDiskFreeSpaceEx.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
uintptr(unsafe.Pointer(&lpFreeBytesAvailable)),
uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)),
uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)), 0, 0)
if e1 != 0 {
return 0, 0, error(e1)
}
if r1 == 0 {
return 0, 0, syscall.EINVAL
}
total = int64(lpTotalNumberOfBytes)
free = int64(lpFreeBytesAvailable)
return total, free, nil
uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)))
statfs = StatFS{}
statfs.Total = int64(lpTotalNumberOfBytes)
statfs.Free = int64(lpFreeBytesAvailable)
statfs.FSType = getFSType(path)
return statfs, nil
}