Use ls-style permission strings (#2270)

This commit is contained in:
CatsDeservePets authored and GitHub committed 2025-11-18 01:55:33 +01:00
1 parent 5749c9cd93
commit 6e804b1ade
3 files changed
+52 -3

No files matched your search

+1
View File
@@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
- Logs generated by `-log <path>` now get appended to `<path>` instead of overwriting it (#2215).
- The `showbinds` menu now hides the redundant `mode` column (#2226) (#2228) and omits already typed keys (#2249).
- The `setlocal` command no longer requires absolute paths (#2253).
- The string representation of file permissions now matches the traditional `Unix` format instead of the one used by `Go` (#2270).
### Added
+48
View File
@@ -8,6 +8,7 @@ import (
"io"
"io/fs"
"math/big"
"os"
"path/filepath"
"regexp"
"strconv"
@@ -289,6 +290,53 @@ func humanize(size uint64) string {
return fmt.Sprintf("+999%s", prefixes[len(prefixes)-1])
}
// This function returns an ls(1)-style string representation of the given file
// mode, to avoid using Go's implementation, which slightly differs.
func permString(m os.FileMode) string {
// re-use Perm()'s "-rwxrwxrwx" output and write type into b[0]
b := []byte(m.Perm().String())
switch {
case m&os.ModeSymlink != 0:
b[0] = 'l'
case m&os.ModeDir != 0:
b[0] = 'd'
case m&os.ModeNamedPipe != 0:
b[0] = 'p'
case m&os.ModeSocket != 0:
b[0] = 's'
case m&os.ModeCharDevice != 0:
b[0] = 'c'
case m&os.ModeDevice != 0:
b[0] = 'b'
default:
b[0] = '-'
}
// patch exec slots with suid/sgid/sticky flags
if m&os.ModeSetuid != 0 {
if b[3] == 'x' {
b[3] = 's'
} else {
b[3] = 'S'
}
}
if m&os.ModeSetgid != 0 {
if b[6] == 'x' {
b[6] = 's'
} else {
b[6] = 'S'
}
}
if m&os.ModeSticky != 0 {
if b[9] == 'x' {
b[9] = 't'
} else {
b[9] = 'T'
}
}
return string(b)
}
// This function compares two strings for natural sorting which takes into
// account the values of numbers in strings. For example, '2' is ordered before
// '10', and similarly 'foo2bar' ordered before 'foo10bar'. When comparing
+3 -3
View File
@@ -320,7 +320,7 @@ func fileInfo(f *file, d *dir, userWidth int, groupWidth int, customWidth int) (
case "ctime":
fmt.Fprintf(&info, " %*s", max(len(gOpts.infotimefmtnew), len(gOpts.infotimefmtold)), infotimefmt(f.changeTime))
case "perm":
info.WriteString(" " + f.FileInfo.Mode().String())
info.WriteString(" " + permString(f.Mode()))
case "user":
fmt.Fprintf(&info, " %-*s", userWidth, userName(f.FileInfo))
case "group":
@@ -896,7 +896,7 @@ func (ui *ui) drawStat(nav *nav) {
replace("%m", "")
replace("%M", "NORMAL")
}
replace("%p", curr.Mode().String())
replace("%p", permString(curr.Mode()))
replace("%c", linkCount(curr))
replace("%u", userName(curr))
replace("%g", groupName(curr))
@@ -1032,7 +1032,7 @@ func (ui *ui) drawRulerFile(nav *nav) {
Path: curr.path,
Name: curr.Name(),
Size: uint64(curr.Size()),
Permissions: curr.Mode().String(),
Permissions: permString(curr.Mode()),
ModTime: curr.ModTime().Format(gOpts.timefmt),
LinkCount: linkCount(curr),
User: userName(curr),