fix FileSize to return correct disk usage recursively

This commit is contained in:
Anton Evangelatov 2021-01-19 14:43:19 +01:00
parent e9989d0e46
commit 16d07d3f18

View File

@ -2,7 +2,7 @@ package fsutil
import (
"os"
"syscall"
"path/filepath"
"golang.org/x/xerrors"
)
@ -11,19 +11,24 @@ 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
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() {
size += info.Size()
}
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
}