fix: calculate target directory size for symlinks in calcdirsize (#2650)
This commit is contained in:
3 files changed
+66
-8
No files matched your search
@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
### Fixed
|
||||
|
||||
- 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).
|
||||
- Selecting entries with the mouse no longer causes the file list to jump unexpectedly (#2664).
|
||||
|
||||
|
||||
@@ -2007,15 +2007,32 @@ func (nav *nav) currFileOrSelections() ([]string, error) {
|
||||
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 {
|
||||
calc := func(f *file) error {
|
||||
if f.IsDir() {
|
||||
total, err := copySize([]string{f.path})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.dirSize = total
|
||||
if !f.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
total, err := calcSize(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.dirSize = total
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2028,8 +2045,10 @@ func (nav *nav) calcDirSize() error {
|
||||
}
|
||||
|
||||
for sel := range nav.selections {
|
||||
lstat, err := os.Lstat(sel)
|
||||
if err != nil || !lstat.IsDir() {
|
||||
// `os.Stat` is used instead of `os.Lstat` so that symlinks pointing to
|
||||
// directories are not skipped.
|
||||
stat, err := os.Stat(sel)
|
||||
if err != nil || !stat.IsDir() {
|
||||
continue
|
||||
}
|
||||
path, name := filepath.Dir(sel), filepath.Base(sel)
|
||||
|
||||
+38
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user