fs_windows.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // +build windows
  2. /*
  3. Copyright 2014 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package fs
  15. import (
  16. "fmt"
  17. "syscall"
  18. "unsafe"
  19. "golang.org/x/sys/windows"
  20. "k8s.io/apimachinery/pkg/api/resource"
  21. )
  22. var (
  23. modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
  24. procGetDiskFreeSpaceEx = modkernel32.NewProc("GetDiskFreeSpaceExW")
  25. )
  26. // FSInfo returns (available bytes, byte capacity, byte usage, total inodes, inodes free, inode usage, error)
  27. // for the filesystem that path resides upon.
  28. func FsInfo(path string) (int64, int64, int64, int64, int64, int64, error) {
  29. var freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes int64
  30. var err error
  31. ret, _, err := syscall.Syscall6(
  32. procGetDiskFreeSpaceEx.Addr(),
  33. 4,
  34. uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
  35. uintptr(unsafe.Pointer(&freeBytesAvailable)),
  36. uintptr(unsafe.Pointer(&totalNumberOfBytes)),
  37. uintptr(unsafe.Pointer(&totalNumberOfFreeBytes)),
  38. 0,
  39. 0,
  40. )
  41. if ret == 0 {
  42. return 0, 0, 0, 0, 0, 0, err
  43. }
  44. return freeBytesAvailable, totalNumberOfBytes, totalNumberOfBytes - freeBytesAvailable, 0, 0, 0, nil
  45. }
  46. // DiskUsage gets disk usage of specified path.
  47. func DiskUsage(path string) (*resource.Quantity, error) {
  48. _, _, usage, _, _, _, err := FsInfo(path)
  49. if err != nil {
  50. return nil, err
  51. }
  52. used, err := resource.ParseQuantity(fmt.Sprintf("%d", usage))
  53. if err != nil {
  54. return nil, fmt.Errorf("failed to parse fs usage %d due to %v", usage, err)
  55. }
  56. used.Format = resource.BinarySI
  57. return &used, nil
  58. }
  59. // Always return zero since inodes is not supported on Windows.
  60. func Find(path string) (int64, error) {
  61. return 0, nil
  62. }