From 6e804b1ade7f35140ad6ece6ef675f0a8728531a Mon Sep 17 00:00:00 2001 From: CatsDeservePets <145048791+CatsDeservePets@users.noreply.github.com> Date: Tue, 18 Nov 2025 01:55:33 +0100 Subject: [PATCH] Use ls-style permission strings (#2270) --- CHANGELOG.md | 1 + misc.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ ui.go | 6 +++--- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b7002bc..dba47f0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - Logs generated by `-log ` now get appended to `` 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 diff --git a/misc.go b/misc.go index e782fc4b..32fcd321 100644 --- a/misc.go +++ b/misc.go @@ -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 diff --git a/ui.go b/ui.go index 15519138..0207dd92 100644 --- a/ui.go +++ b/ui.go @@ -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),