utils.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build !windows
  15. // +build !windows
  16. package storageutils
  17. import (
  18. "syscall"
  19. )
  20. func GetTotalSizeMb(path string) (int, error) {
  21. var stat syscall.Statfs_t
  22. err := syscall.Statfs(path, &stat)
  23. if err != nil {
  24. return -1, err
  25. }
  26. return int(stat.Blocks * uint64(stat.Bsize) / 1024 / 1024), nil
  27. }
  28. func GetFreeSizeMb(path string) (int, error) {
  29. var stat syscall.Statfs_t
  30. err := syscall.Statfs(path, &stat)
  31. if err != nil {
  32. return -1, err
  33. }
  34. return int(stat.Bavail * uint64(stat.Bsize) / 1024 / 1024), nil
  35. }
  36. func GetUsedSizeMb(path string) (int, error) {
  37. var stat syscall.Statfs_t
  38. err := syscall.Statfs(path, &stat)
  39. if err != nil {
  40. return -1, err
  41. }
  42. return int((stat.Blocks - stat.Bfree) * uint64(stat.Bsize) / 1024 / 1024), nil
  43. }