diskusage_windows.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package du
  2. import (
  3. "syscall"
  4. "unsafe"
  5. )
  6. type DiskUsage struct {
  7. freeBytes int64
  8. totalBytes int64
  9. availBytes int64
  10. }
  11. // NewDiskUsages returns an object holding the disk usage of volumePath
  12. // or nil in case of error (invalid path, etc)
  13. func NewDiskUsage(volumePath string) *DiskUsage {
  14. h := syscall.MustLoadDLL("kernel32.dll")
  15. c := h.MustFindProc("GetDiskFreeSpaceExW")
  16. du := &DiskUsage{}
  17. c.Call(
  18. uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(volumePath))),
  19. uintptr(unsafe.Pointer(&du.freeBytes)),
  20. uintptr(unsafe.Pointer(&du.totalBytes)),
  21. uintptr(unsafe.Pointer(&du.availBytes)))
  22. return du
  23. }
  24. // Free returns total free bytes on file system
  25. func (du *DiskUsage) Free() uint64 {
  26. return uint64(du.freeBytes)
  27. }
  28. // Available returns total available bytes on file system to an unprivileged user
  29. func (du *DiskUsage) Available() uint64 {
  30. return uint64(du.availBytes)
  31. }
  32. // Size returns total size of the file system
  33. func (du *DiskUsage) Size() uint64 {
  34. return uint64(du.totalBytes)
  35. }
  36. // Used returns total bytes used in file system
  37. func (du *DiskUsage) Used() uint64 {
  38. return du.Size() - du.Free()
  39. }
  40. // Usage returns percentage of use on the file system
  41. func (du *DiskUsage) Usage() float32 {
  42. return float32(du.Used()) / float32(du.Size())
  43. }