fs_windows.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "os"
  18. "syscall"
  19. "unsafe"
  20. "golang.org/x/sys/windows"
  21. "k8s.io/apimachinery/pkg/api/resource"
  22. )
  23. var (
  24. modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
  25. procGetDiskFreeSpaceEx = modkernel32.NewProc("GetDiskFreeSpaceExW")
  26. )
  27. // FSInfo returns (available bytes, byte capacity, byte usage, total inodes, inodes free, inode usage, error)
  28. // for the filesystem that path resides upon.
  29. func FsInfo(path string) (int64, int64, int64, int64, int64, int64, error) {
  30. var freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes int64
  31. var err error
  32. ret, _, err := syscall.Syscall6(
  33. procGetDiskFreeSpaceEx.Addr(),
  34. 4,
  35. uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
  36. uintptr(unsafe.Pointer(&freeBytesAvailable)),
  37. uintptr(unsafe.Pointer(&totalNumberOfBytes)),
  38. uintptr(unsafe.Pointer(&totalNumberOfFreeBytes)),
  39. 0,
  40. 0,
  41. )
  42. if ret == 0 {
  43. return 0, 0, 0, 0, 0, 0, err
  44. }
  45. return freeBytesAvailable, totalNumberOfBytes, totalNumberOfBytes - freeBytesAvailable, 0, 0, 0, nil
  46. }
  47. // DiskUsage gets disk usage of specified path.
  48. func DiskUsage(path string) (*resource.Quantity, error) {
  49. info, err := os.Lstat(path)
  50. if err != nil {
  51. return nil, err
  52. }
  53. usage, err := diskUsage(path, info)
  54. if err != nil {
  55. return nil, err
  56. }
  57. used, err := resource.ParseQuantity(fmt.Sprintf("%d", usage))
  58. if err != nil {
  59. return nil, fmt.Errorf("failed to parse fs usage %d due to %v", usage, err)
  60. }
  61. used.Format = resource.BinarySI
  62. return &used, nil
  63. }
  64. // Always return zero since inodes is not supported on Windows.
  65. func Find(path string) (int64, error) {
  66. return 0, nil
  67. }
  68. func diskUsage(currPath string, info os.FileInfo) (int64, error) {
  69. var size int64
  70. if info.Mode()&os.ModeSymlink != 0 {
  71. return size, nil
  72. }
  73. size += info.Size()
  74. if !info.IsDir() {
  75. return size, nil
  76. }
  77. dir, err := os.Open(currPath)
  78. if err != nil {
  79. return size, err
  80. }
  81. defer dir.Close()
  82. files, err := dir.Readdir(-1)
  83. if err != nil {
  84. return size, err
  85. }
  86. for _, file := range files {
  87. if file.IsDir() {
  88. s, err := diskUsage(fmt.Sprintf("%s/%s", currPath, file.Name()), file)
  89. if err != nil {
  90. return size, err
  91. }
  92. size += s
  93. } else {
  94. size += file.Size()
  95. }
  96. }
  97. return size, nil
  98. }