From 54f87951c1c1f7ca7395b0e6216b5710df3ac51d Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Fri, 26 May 2023 17:22:21 +0400 Subject: [PATCH] In DirectorySize() filepath.Walk was replaced by ioutil.ReadDir to shorten the code. --- pkg/utils/filesize.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/pkg/utils/filesize.go b/pkg/utils/filesize.go index d58a0c4..be9f553 100644 --- a/pkg/utils/filesize.go +++ b/pkg/utils/filesize.go @@ -29,19 +29,15 @@ func ConvertFileSize(size int64) (float64, string, string) { } func DirectorySize(path string) (dirSz int64, err error) { - err = filepath.Walk(path, func(_ string, info fs.FileInfo, err error) error { - if err != nil { - return errors.Wrapf(err, "failed to compute %s directory size", path) - } - - dirSz += info.Size() - - return nil - }) - + dir, err := ioutil.ReadDir(path) if err != nil { - return 0, err + return 0, errors.Wrapf(err, "failed to compute %s directory size", path) } + for _, entry := range dir { + dirSz += entry.Size() + } + + // Here we subtract a size of a directory's inode to get size of files only. return dirSz - 4096, nil }