fsutil: FileSize util
This commit is contained in:
parent
0bc41d562d
commit
c0a242a1eb
25
fsutil/filesize_unix.go
Normal file
25
fsutil/filesize_unix.go
Normal file
@ -0,0 +1,25 @@
|
||||
package fsutil
|
||||
|
||||
import (
|
||||
"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 {
|
||||
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{
|
||||
stat.Blocks * 512,
|
||||
}, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user