From e7e45259bad863de8c354d3591a79fbd528947b6 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 5 Aug 2023 05:07:59 +0400 Subject: [PATCH] Oh, in os.Stat() path.Join() is needed, since DirEntry.Name() contains only name of a file itself, not a full path. --- cmd/dwelling-upload-clean/main.go | 2 +- pkg/utils/filesize.go | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cmd/dwelling-upload-clean/main.go b/cmd/dwelling-upload-clean/main.go index 04f1e23..8c9751a 100644 --- a/cmd/dwelling-upload-clean/main.go +++ b/cmd/dwelling-upload-clean/main.go @@ -33,7 +33,7 @@ func main() { } for _, entry := range uploadsDir { - file, err := os.Stat(entry.Name()) + file, err := os.Stat(path.Join(*uploadDir, entry.Name())) if err != nil { log.Printf("failed to stat a file %s: %s", entry.Name(), err) continue diff --git a/pkg/utils/filesize.go b/pkg/utils/filesize.go index 8b74ead..1100a5b 100644 --- a/pkg/utils/filesize.go +++ b/pkg/utils/filesize.go @@ -2,6 +2,7 @@ package utils import ( "os" + "path" "strconv" "strings" @@ -28,14 +29,14 @@ func ConvertFileSize(size int64) (float64, string, string) { strings.Join([]string{fSizeStr, sizeSuffixes[idx]}, " ") } -func DirectorySize(path string) (dirSz int64, err error) { - dir, err := os.ReadDir(path) +func DirectorySize(dirPath string) (dirSz int64, err error) { + dir, err := os.ReadDir(dirPath) if err != nil { - return 0, errors.Wrapf(err, "failed to compute %s directory size", path) + return 0, errors.Wrapf(err, "failed to compute %s directory size", dirPath) } for _, entry := range dir { - file, err := os.Stat(entry.Name()) + file, err := os.Stat(path.Join(dirPath, entry.Name())) if err != nil { return 0, errors.Wrapf(err, "failed to stat a file %s", entry.Name()) }