statvfs_fallback.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. //go:build !darwin && !linux && !freebsd
  15. package vfs
  16. import (
  17. "github.com/pkg/sftp"
  18. "github.com/shirou/gopsutil/v3/disk"
  19. )
  20. const bsize = uint64(4096)
  21. func getStatFS(path string) (*sftp.StatVFS, error) {
  22. usage, err := disk.Usage(path)
  23. if err != nil {
  24. return nil, err
  25. }
  26. // we assume block size = 4096
  27. blocks := usage.Total / bsize
  28. bfree := usage.Free / bsize
  29. files := usage.InodesTotal
  30. ffree := usage.InodesFree
  31. if files == 0 {
  32. // these assumptions are wrong but still better than returning 0
  33. files = blocks / 4
  34. ffree = bfree / 4
  35. }
  36. return &sftp.StatVFS{
  37. Bsize: bsize,
  38. Frsize: bsize,
  39. Blocks: blocks,
  40. Bfree: bfree,
  41. Bavail: bfree,
  42. Files: files,
  43. Ffree: ffree,
  44. Favail: ffree,
  45. Namemax: 255,
  46. }, nil
  47. }