Skip to content

Commit bc5e3ed

Browse files
committed
Fix usage calculation to account for sparse files
The current usage calculation does not account for usage within spares files. To correctly account for the usage, the number of blocks must be used rather than the reported file size. Signed-off-by: Derek McGowan <[email protected]>
1 parent 03c371a commit bc5e3ed

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

fs/du_unix.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ func diskUsage(ctx context.Context, roots ...string) (Usage, error) {
5959
default:
6060
}
6161

62-
inoKey := newInode(fi.Sys().(*syscall.Stat_t))
62+
stat := fi.Sys().(*syscall.Stat_t)
63+
inoKey := newInode(stat)
6364
if _, ok := inodes[inoKey]; !ok {
6465
inodes[inoKey] = struct{}{}
65-
size += fi.Size()
66+
size += stat.Blocks * stat.Blksize
6667
}
6768

6869
return nil
@@ -89,10 +90,11 @@ func diffUsage(ctx context.Context, a, b string) (Usage, error) {
8990
}
9091

9192
if kind == ChangeKindAdd || kind == ChangeKindModify {
92-
inoKey := newInode(fi.Sys().(*syscall.Stat_t))
93+
stat := fi.Sys().(*syscall.Stat_t)
94+
inoKey := newInode(stat)
9395
if _, ok := inodes[inoKey]; !ok {
9496
inodes[inoKey] = struct{}{}
95-
size += fi.Size()
97+
size += stat.Blocks * stat.Blksize
9698
}
9799

98100
return nil

0 commit comments

Comments
 (0)