Skip to content

Commit

Permalink
get size or directory or file
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Sep 17, 2021
1 parent 9cb32df commit dd39f68
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions directory/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,17 @@ func CopyDirectory(name string) error {
return err
}

// GetTreeSize calculates the size of a directory recursively.
func GetTreeSize(path string) (int64, error) {
// GetDirectoryItemSize calculates the size of a directory or file.
func GetDirectoryItemSize(path string) (int64, error) {
curFile, err := os.Stat(path)
if err != nil {
return 0, err
}

if !curFile.IsDir() {
return curFile.Size(), nil
}

entries, err := os.ReadDir(path)
if err != nil {
return 0, err
Expand All @@ -387,7 +396,7 @@ func GetTreeSize(path string) (int64, error) {
var total int64
for _, entry := range entries {
if entry.IsDir() {
size, err := GetTreeSize(filepath.Join(path, entry.Name()))
size, err := GetDirectoryItemSize(filepath.Join(path, entry.Name()))
if err != nil {
return 0, err
}
Expand Down

0 comments on commit dd39f68

Please sign in to comment.