In DirectorySize() filepath.Walk was replaced by ioutil.ReadDir to shorten the code.
This commit is contained in:
parent
da001dbe39
commit
54f87951c1
@ -29,19 +29,15 @@ func ConvertFileSize(size int64) (float64, string, string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DirectorySize(path string) (dirSz int64, err error) {
|
func DirectorySize(path string) (dirSz int64, err error) {
|
||||||
err = filepath.Walk(path, func(_ string, info fs.FileInfo, err error) error {
|
dir, err := ioutil.ReadDir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to compute %s directory size", path)
|
return 0, errors.Wrapf(err, "failed to compute %s directory size", path)
|
||||||
}
|
}
|
||||||
|
|
||||||
dirSz += info.Size()
|
for _, entry := range dir {
|
||||||
|
dirSz += entry.Size()
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Here we subtract a size of a directory's inode to get size of files only.
|
||||||
return dirSz - 4096, nil
|
return dirSz - 4096, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user