refactor: use *uint64 for dir size and count (#2353)

* refactor: use `*uint64` for dir size and count

* no dircount should be ordered before dircount of 0
This commit is contained in:
Joe Lim authored and GitHub committed 2026-01-22 13:11:18 +11:00
1 parent cc23e323ac
commit c7be3c4434
2 files changed
+41 -54

No files matched your search

+28 -29
View File
@@ -36,8 +36,8 @@ type file struct {
linkState linkState // symlink state linkState linkState // symlink state
linkTarget string // path a symlink points to linkTarget string // path a symlink points to
path string // full path including the name path string // full path including the name
dirCount int // number of items inside the directory (-2: error, -1: unknown) dirCount *uint64 // number of items inside the directory
dirSize int64 // total directory size (needs to be calculated via `calcdirsize`) dirSize *uint64 // total directory size (needs to be calculated via `calcdirsize`)
accessTime time.Time // time of last access accessTime time.Time // time of last access
birthTime time.Time // time of file birth birthTime time.Time // time of file birth
changeTime time.Time // time of last status (inode) change changeTime time.Time // time of last status (inode) change
@@ -53,15 +53,10 @@ func newFile(path string) *file {
return &file{ return &file{
FileInfo: &fakeStat{name: filepath.Base(path)}, FileInfo: &fakeStat{name: filepath.Base(path)},
linkState: notLink, linkState: notLink,
linkTarget: "",
path: path, path: path,
dirCount: -1,
dirSize: -1,
accessTime: time.Unix(0, 0), accessTime: time.Unix(0, 0),
birthTime: time.Unix(0, 0), birthTime: time.Unix(0, 0),
changeTime: time.Unix(0, 0), changeTime: time.Unix(0, 0),
customInfo: "",
ext: "",
err: err, err: err,
} }
} }
@@ -100,19 +95,20 @@ func newFile(path string) *file {
ct = ts.ChangeTime() ct = ts.ChangeTime()
} }
dirCount := -1 var dirCount *uint64
if lstat.IsDir() && getDirCounts(filepath.Dir(path)) { if lstat.IsDir() && getDirCounts(filepath.Dir(path)) {
d, err := os.Open(path) d, err := os.Open(path)
if err != nil { if err != nil {
dirCount = -2 log.Printf("opening file: %s", err)
} else { } else {
names, err := d.Readdirnames(10000) names, err := d.Readdirnames(10000)
d.Close() d.Close()
if names == nil && err != io.EOF { if names == nil && err != io.EOF {
dirCount = -2 log.Printf("reading directory: %s", err)
} else { } else {
dirCount = len(names) v := uint64(len(names))
dirCount = &v
} }
} }
} }
@@ -123,26 +119,13 @@ func newFile(path string) *file {
linkTarget: linkTarget, linkTarget: linkTarget,
path: path, path: path,
dirCount: dirCount, dirCount: dirCount,
dirSize: -1,
accessTime: at, accessTime: at,
birthTime: bt, birthTime: bt,
changeTime: ct, changeTime: ct,
customInfo: "",
ext: getFileExtension(lstat), ext: getFileExtension(lstat),
err: nil,
} }
} }
func (file *file) TotalSize() int64 {
if file.IsDir() {
if file.dirSize >= 0 {
return file.dirSize
}
return 0
}
return file.Size()
}
func (file *file) isPreviewable() bool { func (file *file) isPreviewable() bool {
return !file.IsDir() || gOpts.dirpreviews return !file.IsDir() || gOpts.dirpreviews
} }
@@ -291,14 +274,29 @@ func (dir *dir) sort() {
return cmp.Compare(normalize(f1.Name()), normalize(f2.Name())) return cmp.Compare(normalize(f1.Name()), normalize(f2.Name()))
}) })
case sizeSort: case sizeSort:
sizeVal := func(f *file) int64 { sizeVal := func(f *file) *uint64 {
if f.IsDir() && dir.dircounts { if f.IsDir() && dir.dircounts {
return int64(f.dirCount) return f.dirCount
} }
return f.TotalSize() if f.dirSize != nil {
return f.dirSize
}
v := uint64(f.Size())
return &v
} }
applySort(func(f1, f2 *file) int { applySort(func(f1, f2 *file) int {
return cmp.Compare(sizeVal(f1), sizeVal(f2)) s1 := sizeVal(f1)
s2 := sizeVal(f2)
switch {
case s1 == nil && s2 == nil:
return 0
case s1 == nil && s2 != nil:
return -1
case s1 != nil && s2 == nil:
return 1
default:
return cmp.Compare(*s1, *s2)
}
}) })
case timeSort: case timeSort:
applySort(func(f1, f2 *file) int { applySort(func(f1, f2 *file) int {
@@ -2020,7 +2018,8 @@ func (nav *nav) calcDirSize() error {
if err != nil { if err != nil {
return err return err
} }
f.dirSize = total v := uint64(total)
f.dirSize = &v
} }
return nil return nil
} }
+13 -25
View File
@@ -287,25 +287,23 @@ func fileInfo(f *file, d *dir, userWidth, groupWidth, customWidth int) (string,
case "size": case "size":
if f.IsDir() && getDirCounts(d.path) { if f.IsDir() && getDirCounts(d.path) {
switch { switch {
case f.dirCount < -1: case f.dirCount == nil:
info.WriteString(" !")
case f.dirCount < 0:
info.WriteString(" ?") info.WriteString(" ?")
case f.dirCount < 10000: case *f.dirCount < 10000:
fmt.Fprintf(&info, " %5d", f.dirCount) fmt.Fprintf(&info, " %5d", *f.dirCount)
default: default:
info.WriteString(" 9999+") info.WriteString(" 9999+")
} }
continue
}
var sz string
if f.IsDir() && f.dirSize < 0 {
sz = "-"
} else { } else {
sz = humanize(uint64(f.TotalSize())) switch {
case f.dirSize != nil:
fmt.Fprintf(&info, " %5s", humanize(*f.dirSize))
case f.IsDir():
info.WriteString(" -")
default:
fmt.Fprintf(&info, " %5s", humanize(uint64(f.Size())))
}
} }
fmt.Fprintf(&info, " %5s", sz)
case "time": case "time":
fmt.Fprintf(&info, " %*s", max(len(gOpts.infotimefmtnew), len(gOpts.infotimefmtold)), infotimefmt(f.ModTime())) fmt.Fprintf(&info, " %*s", max(len(gOpts.infotimefmtnew), len(gOpts.infotimefmtold)), infotimefmt(f.ModTime()))
case "atime": case "atime":
@@ -999,23 +997,13 @@ func (ui *ui) drawRulerFile(nav *nav) {
curr := nav.currFile() curr := nav.currFile()
if curr != nil { if curr != nil {
if curr.err == nil { if curr.err == nil {
var dirsize *uint64 = nil
var dircount *uint64 = nil
if curr.dirSize >= 0 {
v := uint64(curr.dirSize)
dirsize = &v
}
if curr.dirCount >= 0 {
v := uint64(curr.dirCount)
dircount = &v
}
stat = &statData{ stat = &statData{
Path: curr.path, Path: curr.path,
Name: curr.Name(), Name: curr.Name(),
Extension: curr.ext, Extension: curr.ext,
Size: uint64(curr.Size()), Size: uint64(curr.Size()),
DirSize: dirsize, DirSize: curr.dirSize,
DirCount: dircount, DirCount: curr.dirCount,
Permissions: permString(curr.Mode()), Permissions: permString(curr.Mode()),
ModTime: curr.ModTime().Format(gOpts.timefmt), ModTime: curr.ModTime().Format(gOpts.timefmt),
AccessTime: curr.accessTime.Format(gOpts.timefmt), AccessTime: curr.accessTime.Format(gOpts.timefmt),