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
|
## 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
|
### 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).
|
- 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
|
// 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
|
// 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
|
// 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)
|
log.Printf("diskfree: %s", err)
|
||||||
return ""
|
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.Path string Path of the current file
|
||||||
.Stat.Name string Name of the current file
|
.Stat.Name string Name of the current file
|
||||||
.Stat.Extension string Extension of the current file
|
.Stat.Extension string Extension of the current file
|
||||||
.Stat.Size uint64 Size of the current file
|
.Stat.Size int64 Size of the current file
|
||||||
.Stat.DirSize *uint64 Total size of the current directory if calculated via `calcdirsize`
|
.Stat.DirSize int64 Total size of the current directory if calculated via `calcdirsize` (`-1` if not calculated)
|
||||||
.Stat.DirCount *uint64 Number of items in the current directory if the `dircounts` option is enabled
|
.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.Permissions string Permissions of the current file
|
||||||
.Stat.ModTime string Last modified time of the current file (formatted based on the `timefmt` option)
|
.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)
|
.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
|
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
|
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
|
join func([]string, string) string Join a string array by a separator
|
||||||
lower func(string) string Convert a string to lowercase
|
lower func(string) string Convert a string to lowercase
|
||||||
substr func(string, int, int) string Get a substring based on starting index and length
|
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
|
// 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)
|
// 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.
|
// multiples. The output should be no more than 5 characters long.
|
||||||
func humanize(size uint64) string {
|
func humanize(size int64) string {
|
||||||
var base uint64 = 1024
|
var base int64 = 1024
|
||||||
if gOpts.sizeunits == "decimal" {
|
if gOpts.sizeunits == "decimal" {
|
||||||
base = 1000
|
base = 1000
|
||||||
}
|
}
|
||||||
@@ -271,7 +271,7 @@ func humanize(size uint64) string {
|
|||||||
"Q", // quebi (2^100) or quetta (10^30)
|
"Q", // quebi (2^100) or quetta (10^30)
|
||||||
}
|
}
|
||||||
|
|
||||||
curr := big.NewRat(int64(size), int64(base))
|
curr := big.NewRat(size, base)
|
||||||
|
|
||||||
for _, prefix := range prefixes {
|
for _, prefix := range prefixes {
|
||||||
// if curr < 99.95 then round to 1 decimal place
|
// 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 < 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)
|
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])
|
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) {
|
func TestHumanize(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
size uint64
|
size int64
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{0, "0B"},
|
{0, "0B"},
|
||||||
@@ -298,7 +298,7 @@ func TestHumanize(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tests = []struct {
|
tests = []struct {
|
||||||
size uint64
|
size int64
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{0, "0B"},
|
{0, "0B"},
|
||||||
|
|||||||
@@ -37,8 +37,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 *uint64 // number of items inside the directory
|
dirCount int // number of items inside the directory
|
||||||
dirSize *uint64 // total directory size (needs to be calculated via `calcdirsize`)
|
dirSize int64 // 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
|
||||||
@@ -55,6 +55,8 @@ func newFile(path string) *file {
|
|||||||
FileInfo: &fakeStat{name: filepath.Base(path)},
|
FileInfo: &fakeStat{name: filepath.Base(path)},
|
||||||
linkState: notLink,
|
linkState: notLink,
|
||||||
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),
|
||||||
@@ -96,7 +98,7 @@ func newFile(path string) *file {
|
|||||||
ct = ts.ChangeTime()
|
ct = ts.ChangeTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
var dirCount *uint64
|
dirCount := -1
|
||||||
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 {
|
||||||
@@ -108,8 +110,7 @@ func newFile(path string) *file {
|
|||||||
if names == nil && err != io.EOF {
|
if names == nil && err != io.EOF {
|
||||||
log.Printf("reading directory: %s", err)
|
log.Printf("reading directory: %s", err)
|
||||||
} else {
|
} else {
|
||||||
v := uint64(len(names))
|
dirCount = len(names)
|
||||||
dirCount = &v
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,6 +121,7 @@ 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,
|
||||||
@@ -275,29 +277,17 @@ 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) *uint64 {
|
sizeVal := func(f *file) int64 {
|
||||||
if f.IsDir() && dir.dircounts {
|
if f.IsDir() && dir.dircounts {
|
||||||
return f.dirCount
|
return int64(f.dirCount)
|
||||||
}
|
}
|
||||||
if f.dirSize != nil {
|
if f.dirSize >= 0 {
|
||||||
return f.dirSize
|
return f.dirSize
|
||||||
}
|
}
|
||||||
v := uint64(f.Size())
|
return f.Size()
|
||||||
return &v
|
|
||||||
}
|
}
|
||||||
applySort(func(f1, f2 *file) int {
|
applySort(func(f1, f2 *file) int {
|
||||||
s1 := sizeVal(f1)
|
return cmp.Compare(sizeVal(f1), sizeVal(f2))
|
||||||
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,8 +2010,7 @@ func (nav *nav) calcDirSize() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v := uint64(total)
|
f.dirSize = total
|
||||||
f.dirSize = &v
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ type statData struct {
|
|||||||
Path string
|
Path string
|
||||||
Name string
|
Name string
|
||||||
Extension string
|
Extension string
|
||||||
Size uint64
|
Size int64
|
||||||
DirSize *uint64
|
DirSize int64
|
||||||
DirCount *uint64
|
DirCount int
|
||||||
Permissions string
|
Permissions string
|
||||||
ModTime string
|
ModTime string
|
||||||
AccessTime string
|
AccessTime string
|
||||||
|
|||||||
@@ -299,21 +299,21 @@ 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 == nil:
|
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+")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch {
|
switch {
|
||||||
case f.dirSize != nil:
|
case f.dirSize >= 0:
|
||||||
fmt.Fprintf(&info, " %5s", humanize(*f.dirSize))
|
fmt.Fprintf(&info, " %5s", humanize(f.dirSize))
|
||||||
case f.IsDir():
|
case f.IsDir():
|
||||||
info.WriteString(" -")
|
info.WriteString(" -")
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(&info, " %5s", humanize(uint64(f.Size())))
|
fmt.Fprintf(&info, " %5s", humanize(f.Size()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "time":
|
case "time":
|
||||||
@@ -886,8 +886,8 @@ func (ui *ui) drawStat(nav *nav) {
|
|||||||
replace("%c", linkCount(curr))
|
replace("%c", linkCount(curr))
|
||||||
replace("%u", userName(curr))
|
replace("%u", userName(curr))
|
||||||
replace("%g", groupName(curr))
|
replace("%g", groupName(curr))
|
||||||
replace("%s", humanize(uint64(curr.Size())))
|
replace("%s", humanize(curr.Size()))
|
||||||
replace("%S", fmt.Sprintf("%5s", humanize(uint64(curr.Size()))))
|
replace("%S", fmt.Sprintf("%5s", humanize(curr.Size())))
|
||||||
replace("%t", curr.ModTime().Format(gOpts.timefmt))
|
replace("%t", curr.ModTime().Format(gOpts.timefmt))
|
||||||
replace("%l", curr.linkTarget)
|
replace("%l", curr.linkTarget)
|
||||||
|
|
||||||
@@ -1018,7 +1018,7 @@ func (ui *ui) drawRulerFile(nav *nav) {
|
|||||||
Path: curr.path,
|
Path: curr.path,
|
||||||
Name: curr.Name(),
|
Name: curr.Name(),
|
||||||
Extension: curr.ext,
|
Extension: curr.ext,
|
||||||
Size: uint64(curr.Size()),
|
Size: curr.Size(),
|
||||||
DirSize: curr.dirSize,
|
DirSize: curr.dirSize,
|
||||||
DirCount: curr.dirCount,
|
DirCount: curr.dirCount,
|
||||||
Permissions: permString(curr.Mode()),
|
Permissions: permString(curr.Mode()),
|
||||||
|
|||||||
Reference in new issue
Block a user