Skip to content

Commit

Permalink
cmds/core/du: fix printing subfolders
Browse files Browse the repository at this point in the history
Signed-off-by: Siarhiej Siemianczuk <[email protected]>
  • Loading branch information
binjip978 committed Aug 26, 2024
1 parent 5f07f94 commit 15e37a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions cmds/core/du/du.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ func (c *cmd) du(file string) (int64, error) {
return err
}

// report sub-folders and add number of blocks to overall count
if info.IsDir() && file != path {
dirBlocks, err := c.du(path)
if err != nil {
return err
}

blocks += dirBlocks

fmt.Fprintf(c.stdout, "%d\t%s\n", dirBlocks, path)
return fs.SkipDir
}

st, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return fmt.Errorf("%v: %w", path, errNoStatInfo)
Expand Down
4 changes: 2 additions & 2 deletions cmds/core/du/du_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func TestRun(t *testing.T) {
}
lines := strings.Split(strings.TrimSpace(stdout.String()), "\n")

// should print dir and two files
if len(lines) != 3 {
// should print dir, subdir and two files
if len(lines) != 4 {
t.Errorf("expected file1, file2 and temp dir, but got %d lines", len(lines))
}
})
Expand Down

0 comments on commit 15e37a7

Please sign in to comment.