du_unix.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //go:build !windows
  2. /*
  3. Copyright The containerd 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. "context"
  17. "os"
  18. "path/filepath"
  19. "syscall"
  20. )
  21. // blocksUnitSize is the unit used by `st_blocks` in `stat` in bytes.
  22. // See https://man7.org/linux/man-pages/man2/stat.2.html
  23. //
  24. // st_blocks
  25. // This field indicates the number of blocks allocated to the
  26. // file, in 512-byte units. (This may be smaller than
  27. // st_size/512 when the file has holes.)
  28. const blocksUnitSize = 512
  29. type inode struct {
  30. // TODO(stevvooe): Can probably reduce memory usage by not tracking
  31. // device, but we can leave this right for now.
  32. dev, ino uint64
  33. }
  34. func newInode(stat *syscall.Stat_t) inode {
  35. return inode{
  36. dev: uint64(stat.Dev), //nolint: unconvert // dev is uint32 on darwin/bsd, uint64 on linux/solaris/freebsd
  37. ino: uint64(stat.Ino), //nolint: unconvert // ino is uint32 on bsd, uint64 on darwin/linux/solaris/freebsd
  38. }
  39. }
  40. func diskUsage(ctx context.Context, roots ...string) (Usage, error) {
  41. var (
  42. size int64
  43. inodes = map[inode]struct{}{} // expensive!
  44. )
  45. for _, root := range roots {
  46. if err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
  47. if err != nil {
  48. return err
  49. }
  50. select {
  51. case <-ctx.Done():
  52. return ctx.Err()
  53. default:
  54. }
  55. stat := fi.Sys().(*syscall.Stat_t)
  56. inoKey := newInode(stat)
  57. if _, ok := inodes[inoKey]; !ok {
  58. inodes[inoKey] = struct{}{}
  59. size += stat.Blocks * blocksUnitSize
  60. }
  61. return nil
  62. }); err != nil {
  63. return Usage{}, err
  64. }
  65. }
  66. return Usage{
  67. Inodes: int64(len(inodes)),
  68. Size: size,
  69. }, nil
  70. }
  71. func diffUsage(ctx context.Context, a, b string) (Usage, error) {
  72. var (
  73. size int64
  74. inodes = map[inode]struct{}{} // expensive!
  75. )
  76. if err := Changes(ctx, a, b, func(kind ChangeKind, _ string, fi os.FileInfo, err error) error {
  77. if err != nil {
  78. return err
  79. }
  80. if kind == ChangeKindAdd || kind == ChangeKindModify {
  81. stat := fi.Sys().(*syscall.Stat_t)
  82. inoKey := newInode(stat)
  83. if _, ok := inodes[inoKey]; !ok {
  84. inodes[inoKey] = struct{}{}
  85. size += stat.Blocks * blocksUnitSize
  86. }
  87. return nil
  88. }
  89. return nil
  90. }); err != nil {
  91. return Usage{}, err
  92. }
  93. return Usage{
  94. Inodes: int64(len(inodes)),
  95. Size: size,
  96. }, nil
  97. }