1
0
Fork 0

Oh, in os.Stat() path.Join() is needed, since DirEntry.Name() contains only name of a file itself, not a full path.

This commit is contained in:
Alexander Andreev 2023-08-05 05:07:59 +04:00
parent df8baf153b
commit e7e45259ba
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 6 additions and 5 deletions

View File

@ -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

View File

@ -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())
}