Merge pull request #5384 from filecoin-project/fix-storage-size-estimation

fix FileSize to return correct disk usage recursively
This commit is contained in:
Łukasz Magiera 2021-01-19 19:22:47 +01:00 committed by GitHub
commit 48c6c55f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package fsutil
import (
"os"
"path/filepath"
"syscall"
"golang.org/x/xerrors"
@ -11,19 +12,32 @@ type SizeInfo struct {
OnDisk int64
}
// FileSize returns bytes used by a file on disk
// 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 stat syscall.Stat_t
if err := syscall.Stat(path, &stat); err != nil {
if err == syscall.ENOENT {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return xerrors.New("FileInfo.Sys of wrong type")
}
// 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
})
if err != nil {
if os.IsNotExist(err) {
return SizeInfo{}, os.ErrNotExist
}
return SizeInfo{}, xerrors.Errorf("stat: %w", err)
return SizeInfo{}, xerrors.Errorf("filepath.Walk err: %w", err)
}
// NOTE: stat.Blocks is in 512B blocks, NOT in stat.Blksize
// See https://www.gnu.org/software/libc/manual/html_node/Attribute-Meanings.html
return SizeInfo{
int64(stat.Blocks) * 512, // nolint NOTE: int64 cast is needed on osx
}, nil
return SizeInfo{size}, nil
}