From 834a6148b07ca55e778f528e0f1a063a3ad6fefd Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Tue, 19 Jan 2021 17:52:24 +0100 Subject: [PATCH] use stat.Blocks and not info.Size --- extern/sector-storage/fsutil/filesize_unix.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/extern/sector-storage/fsutil/filesize_unix.go b/extern/sector-storage/fsutil/filesize_unix.go index e5d220764..aadf93acc 100644 --- a/extern/sector-storage/fsutil/filesize_unix.go +++ b/extern/sector-storage/fsutil/filesize_unix.go @@ -3,6 +3,7 @@ package fsutil import ( "os" "path/filepath" + "syscall" "golang.org/x/xerrors" ) @@ -12,6 +13,7 @@ type SizeInfo struct { } // FileSize returns bytes used by a file or directory on disk +// NOTE: We care about the allocated bytes, not file or directory size func FileSize(path string) (SizeInfo, error) { var size int64 err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { @@ -19,7 +21,11 @@ func FileSize(path string) (SizeInfo, error) { return err } if !info.IsDir() { - size += info.Size() + stat := info.Sys().(*syscall.Stat_t) + + // NOTE: stat.Blocks is in 512B blocks, NOT in stat.Blksize return SizeInfo{size}, nil + // See https://www.gnu.org/software/libc/manual/html_node/Attribute-Meanings.html + size += int64(stat.Blocks) * 512 // nolint NOTE: int64 cast is needed on osx } return err })