fix!: use int64 for file sizes (#2397)
* fix!: use `int64` for file sizes Unfortunately Go templates cannot unconditionally dereference pointers, so we use a magic value of -1 instead of a nil pointer to indicate a file size that doesn't exist. This also means having to change surrounding code, for example the `humanize` function. * Update `CHANGELOG.md`
This commit is contained in:
1 parent
9ab0fd4de0
commit
3637f5f3f8
11 files changed
+44
-51
No files matched your search
@@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
|
||||
## Unreleased
|
||||
|
||||
### Changed
|
||||
|
||||
- `.Stat.DirSize` and `.Stat.DirCount` in the ruler file no longer have a pointer type, and will be set to `-1` instead of `nil` if the corresponding value cannot be determined (#2397).
|
||||
|
||||
### Added
|
||||
|
||||
- A new field `.All` is added to the `ruler` file to display the number of all files (i.e. visible + hidden) in the current working directory (#2376).
|
||||
|
||||
+1
-1
@@ -15,5 +15,5 @@ func diskFree(wd string) string {
|
||||
}
|
||||
|
||||
// Available blocks * size per block = available space in bytes
|
||||
return "df: " + humanize(uint64(stat.F_bavail)*uint64(stat.F_bsize))
|
||||
return "df: " + humanize(int64(stat.F_bavail)*int64(stat.F_bsize))
|
||||
}
|
||||
+1
-1
@@ -17,5 +17,5 @@ func diskFree(wd string) string {
|
||||
}
|
||||
|
||||
// Available blocks * size per block = available space in bytes
|
||||
return "df: " + humanize(uint64(stat.Bavail)*uint64(stat.Bsize))
|
||||
return "df: " + humanize(int64(stat.Bavail)*int64(stat.Bsize))
|
||||
}
|
||||
+1
-1
@@ -17,5 +17,5 @@ func diskFree(wd string) string {
|
||||
}
|
||||
|
||||
// Available blocks * size per block = available space in bytes
|
||||
return "df: " + humanize(uint64(stat.Bavail)*uint64(stat.Bsize))
|
||||
return "df: " + humanize(int64(stat.Bavail)*int64(stat.Bsize))
|
||||
}
|
||||
+1
-1
@@ -19,5 +19,5 @@ func diskFree(wd string) string {
|
||||
log.Printf("diskfree: %s", err)
|
||||
return ""
|
||||
}
|
||||
return "df: " + humanize(free)
|
||||
return "df: " + humanize(int64(free))
|
||||
}
|
||||
@@ -2241,9 +2241,9 @@ The following data fields are exported:
|
||||
.Stat.Path string Path of the current file
|
||||
.Stat.Name string Name of the current file
|
||||
.Stat.Extension string Extension of the current file
|
||||
.Stat.Size uint64 Size of the current file
|
||||
.Stat.DirSize *uint64 Total size of the current directory if calculated via `calcdirsize`
|
||||
.Stat.DirCount *uint64 Number of items in the current directory if the `dircounts` option is enabled
|
||||
.Stat.Size int64 Size of the current file
|
||||
.Stat.DirSize int64 Total size of the current directory if calculated via `calcdirsize` (`-1` if not calculated)
|
||||
.Stat.DirCount int Number of items in the current directory if the `dircounts` option is enabled (`-1` if the directory cannot be read)
|
||||
.Stat.Permissions string Permissions of the current file
|
||||
.Stat.ModTime string Last modified time of the current file (formatted based on the `timefmt` option)
|
||||
.Stat.AccessTime string Last access time of the current file (formatted based on the `timefmt` option)
|
||||
@@ -2259,7 +2259,7 @@ The following functions are exported:
|
||||
|
||||
df func() string Get an indicator representing the amount of free disk space available
|
||||
env func(string) string Get the value of an environment variable
|
||||
humanize func(uint64) string Express a file size in a human-readable format
|
||||
humanize func(int64) string Express a file size in a human-readable format
|
||||
join func([]string, string) string Join a string array by a separator
|
||||
lower func(string) string Convert a string to lowercase
|
||||
substr func(string, int, int) string Get a substring based on starting index and length
|
||||
|
||||
@@ -246,8 +246,8 @@ func readPairs(r io.Reader) ([][]string, error) {
|
||||
// humanize converts a size in bytes to a human-readable form using
|
||||
// prefixes for either binary (1 KiB = 1024 B) or decimal (1 KB = 1000 B)
|
||||
// multiples. The output should be no more than 5 characters long.
|
||||
func humanize(size uint64) string {
|
||||
var base uint64 = 1024
|
||||
func humanize(size int64) string {
|
||||
var base int64 = 1024
|
||||
if gOpts.sizeunits == "decimal" {
|
||||
base = 1000
|
||||
}
|
||||
@@ -271,7 +271,7 @@ func humanize(size uint64) string {
|
||||
"Q", // quebi (2^100) or quetta (10^30)
|
||||
}
|
||||
|
||||
curr := big.NewRat(int64(size), int64(base))
|
||||
curr := big.NewRat(size, base)
|
||||
|
||||
for _, prefix := range prefixes {
|
||||
// if curr < 99.95 then round to 1 decimal place
|
||||
@@ -280,11 +280,11 @@ func humanize(size uint64) string {
|
||||
}
|
||||
|
||||
// if curr < base-0.5 then round to the nearest integer
|
||||
if curr.Cmp(new(big.Rat).Sub(big.NewRat(int64(base), 1), big.NewRat(1, 2))) < 0 {
|
||||
if curr.Cmp(new(big.Rat).Sub(big.NewRat(base, 1), big.NewRat(1, 2))) < 0 {
|
||||
return fmt.Sprintf("%s%s", curr.FloatString(0), prefix)
|
||||
}
|
||||
|
||||
curr.Quo(curr, big.NewRat(int64(base), 1))
|
||||
curr.Quo(curr, big.NewRat(base, 1))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("+999%s", prefixes[len(prefixes)-1])
|
||||
|
||||
+2
-2
@@ -255,7 +255,7 @@ func TestReadArrays(t *testing.T) {
|
||||
|
||||
func TestHumanize(t *testing.T) {
|
||||
tests := []struct {
|
||||
size uint64
|
||||
size int64
|
||||
expected string
|
||||
}{
|
||||
{0, "0B"},
|
||||
@@ -298,7 +298,7 @@ func TestHumanize(t *testing.T) {
|
||||
}
|
||||
|
||||
tests = []struct {
|
||||
size uint64
|
||||
size int64
|
||||
expected string
|
||||
}{
|
||||
{0, "0B"},
|
||||
|
||||
@@ -37,8 +37,8 @@ type file struct {
|
||||
linkState linkState // symlink state
|
||||
linkTarget string // path a symlink points to
|
||||
path string // full path including the name
|
||||
dirCount *uint64 // number of items inside the directory
|
||||
dirSize *uint64 // total directory size (needs to be calculated via `calcdirsize`)
|
||||
dirCount int // number of items inside the directory
|
||||
dirSize int64 // total directory size (needs to be calculated via `calcdirsize`)
|
||||
accessTime time.Time // time of last access
|
||||
birthTime time.Time // time of file birth
|
||||
changeTime time.Time // time of last status (inode) change
|
||||
@@ -55,6 +55,8 @@ func newFile(path string) *file {
|
||||
FileInfo: &fakeStat{name: filepath.Base(path)},
|
||||
linkState: notLink,
|
||||
path: path,
|
||||
dirCount: -1,
|
||||
dirSize: -1,
|
||||
accessTime: time.Unix(0, 0),
|
||||
birthTime: time.Unix(0, 0),
|
||||
changeTime: time.Unix(0, 0),
|
||||
@@ -96,7 +98,7 @@ func newFile(path string) *file {
|
||||
ct = ts.ChangeTime()
|
||||
}
|
||||
|
||||
var dirCount *uint64
|
||||
dirCount := -1
|
||||
if lstat.IsDir() && getDirCounts(filepath.Dir(path)) {
|
||||
d, err := os.Open(path)
|
||||
if err != nil {
|
||||
@@ -108,8 +110,7 @@ func newFile(path string) *file {
|
||||
if names == nil && err != io.EOF {
|
||||
log.Printf("reading directory: %s", err)
|
||||
} else {
|
||||
v := uint64(len(names))
|
||||
dirCount = &v
|
||||
dirCount = len(names)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,6 +121,7 @@ func newFile(path string) *file {
|
||||
linkTarget: linkTarget,
|
||||
path: path,
|
||||
dirCount: dirCount,
|
||||
dirSize: -1,
|
||||
accessTime: at,
|
||||
birthTime: bt,
|
||||
changeTime: ct,
|
||||
@@ -275,29 +277,17 @@ func (dir *dir) sort() {
|
||||
return cmp.Compare(normalize(f1.Name()), normalize(f2.Name()))
|
||||
})
|
||||
case sizeSort:
|
||||
sizeVal := func(f *file) *uint64 {
|
||||
sizeVal := func(f *file) int64 {
|
||||
if f.IsDir() && dir.dircounts {
|
||||
return f.dirCount
|
||||
return int64(f.dirCount)
|
||||
}
|
||||
if f.dirSize != nil {
|
||||
if f.dirSize >= 0 {
|
||||
return f.dirSize
|
||||
}
|
||||
v := uint64(f.Size())
|
||||
return &v
|
||||
return f.Size()
|
||||
}
|
||||
applySort(func(f1, f2 *file) int {
|
||||
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)
|
||||
}
|
||||
return cmp.Compare(sizeVal(f1), sizeVal(f2))
|
||||
})
|
||||
case timeSort:
|
||||
applySort(func(f1, f2 *file) int {
|
||||
@@ -2020,8 +2010,7 @@ func (nav *nav) calcDirSize() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v := uint64(total)
|
||||
f.dirSize = &v
|
||||
f.dirSize = total
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@ type statData struct {
|
||||
Path string
|
||||
Name string
|
||||
Extension string
|
||||
Size uint64
|
||||
DirSize *uint64
|
||||
DirCount *uint64
|
||||
Size int64
|
||||
DirSize int64
|
||||
DirCount int
|
||||
Permissions string
|
||||
ModTime string
|
||||
AccessTime string
|
||||
|
||||
@@ -299,21 +299,21 @@ func fileInfo(f *file, d *dir, userWidth, groupWidth, customWidth int) (string,
|
||||
case "size":
|
||||
if f.IsDir() && getDirCounts(d.path) {
|
||||
switch {
|
||||
case f.dirCount == nil:
|
||||
case f.dirCount < 0:
|
||||
info.WriteString(" !")
|
||||
case *f.dirCount < 10000:
|
||||
fmt.Fprintf(&info, " %5d", *f.dirCount)
|
||||
case f.dirCount < 10000:
|
||||
fmt.Fprintf(&info, " %5d", f.dirCount)
|
||||
default:
|
||||
info.WriteString(" 9999+")
|
||||
}
|
||||
} else {
|
||||
switch {
|
||||
case f.dirSize != nil:
|
||||
fmt.Fprintf(&info, " %5s", humanize(*f.dirSize))
|
||||
case f.dirSize >= 0:
|
||||
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", humanize(f.Size()))
|
||||
}
|
||||
}
|
||||
case "time":
|
||||
@@ -886,8 +886,8 @@ func (ui *ui) drawStat(nav *nav) {
|
||||
replace("%c", linkCount(curr))
|
||||
replace("%u", userName(curr))
|
||||
replace("%g", groupName(curr))
|
||||
replace("%s", humanize(uint64(curr.Size())))
|
||||
replace("%S", fmt.Sprintf("%5s", humanize(uint64(curr.Size()))))
|
||||
replace("%s", humanize(curr.Size()))
|
||||
replace("%S", fmt.Sprintf("%5s", humanize(curr.Size())))
|
||||
replace("%t", curr.ModTime().Format(gOpts.timefmt))
|
||||
replace("%l", curr.linkTarget)
|
||||
|
||||
@@ -1018,7 +1018,7 @@ func (ui *ui) drawRulerFile(nav *nav) {
|
||||
Path: curr.path,
|
||||
Name: curr.Name(),
|
||||
Extension: curr.ext,
|
||||
Size: uint64(curr.Size()),
|
||||
Size: curr.Size(),
|
||||
DirSize: curr.dirSize,
|
||||
DirCount: curr.dirCount,
|
||||
Permissions: permString(curr.Mode()),
|
||||
|
||||
Reference in new issue
Block a user