fix: calculate target directory size for symlinks in calcdirsize (#2650)

This commit is contained in:
ivan authored and GitHub committed 2026-08-15 04:07:20 +02:00
1 parent e6789ab125
commit 252ca3e425
3 files changed
+66 -8

No files matched your search

+1
View File
@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
### Fixed ### Fixed
- Dead client connections no longer prevent the `quit` server command from terminating the server (#2643). - Dead client connections no longer prevent the `quit` server command from terminating the server (#2643).
- The `calcdirsize` command now calculates the size of the target directory for symbolic links instead of the size of the link itself (#2650).
- Readline markers in `promptfmt` are no longer rendered as visible characters (#2657). - Readline markers in `promptfmt` are no longer rendered as visible characters (#2657).
- Selecting entries with the mouse no longer causes the file list to jump unexpectedly (#2664). - Selecting entries with the mouse no longer causes the file list to jump unexpectedly (#2664).
+27 -8
View File
@@ -2007,15 +2007,32 @@ func (nav *nav) currFileOrSelections() ([]string, error) {
return nil, errors.New("no file selected") return nil, errors.New("no file selected")
} }
// calcSize returns the total size of the given directory. Symlinks pointing to
// directories are resolved beforehand, since `copySize` does not follow them.
func calcSize(f *file) (int64, error) {
path := f.path
if f.linkState == working {
var err error
path, err = filepath.EvalSymlinks(f.path)
if err != nil {
return 0, err
}
}
return copySize([]string{path})
}
func (nav *nav) calcDirSize() error { func (nav *nav) calcDirSize() error {
calc := func(f *file) error { calc := func(f *file) error {
if f.IsDir() { if !f.IsDir() {
total, err := copySize([]string{f.path}) return nil
if err != nil {
return err
}
f.dirSize = total
} }
total, err := calcSize(f)
if err != nil {
return err
}
f.dirSize = total
return nil return nil
} }
@@ -2028,8 +2045,10 @@ func (nav *nav) calcDirSize() error {
} }
for sel := range nav.selections { for sel := range nav.selections {
lstat, err := os.Lstat(sel) // `os.Stat` is used instead of `os.Lstat` so that symlinks pointing to
if err != nil || !lstat.IsDir() { // directories are not skipped.
stat, err := os.Stat(sel)
if err != nil || !stat.IsDir() {
continue continue
} }
path, name := filepath.Dir(sel), filepath.Base(sel) path, name := filepath.Dir(sel), filepath.Base(sel)
+38
View File
@@ -0,0 +1,38 @@
package main
import (
"os"
"path/filepath"
"testing"
)
func TestDirSize(t *testing.T) {
tmp := t.TempDir()
dir := filepath.Join(tmp, "dir")
if err := os.Mkdir(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "file"), make([]byte, 4096), 0o644); err != nil {
t.Fatal(err)
}
link := filepath.Join(tmp, "link")
if err := os.Symlink(dir, link); err != nil {
t.Skipf("creating symlink: %s", err)
}
expected, err := calcSize(newFile(dir))
if err != nil {
t.Fatal(err)
}
got, err := calcSize(newFile(link))
if err != nil {
t.Fatal(err)
}
if got != expected {
t.Errorf("at symlink to directory expected %d but got %d", expected, got)
}
}