lotus/extern/sector-storage/fsutil/filesize_unix.go

30 lines
654 B
Go
Raw Normal View History

2020-07-08 15:09:35 +00:00
package fsutil
import (
2020-08-04 14:20:59 +00:00
"os"
2020-07-08 15:09:35 +00:00
"syscall"
"golang.org/x/xerrors"
)
type SizeInfo struct {
OnDisk int64
}
// FileSize returns bytes used by a file on disk
func FileSize(path string) (SizeInfo, error) {
var stat syscall.Stat_t
if err := syscall.Stat(path, &stat); err != nil {
2020-08-04 14:20:59 +00:00
if err == syscall.ENOENT {
return SizeInfo{}, os.ErrNotExist
}
2020-07-08 15:09:35 +00:00
return SizeInfo{}, xerrors.Errorf("stat: %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{
2020-08-17 16:21:33 +00:00
int64(stat.Blocks) * 512, // nolint NOTE: int64 cast is needed on osx
2020-07-08 15:09:35 +00:00
}, nil
2020-07-08 17:51:26 +00:00
}