8000 lookup username/group-name for longname · etherscan-io/sftp@5bf2a17 · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit 5bf2a17

Browse files
committed
lookup username/group-name for longname
When returning file data, the longname field is assigned fileinfo formatted as a long list entry (like `ls -l`). This adds username and groupname lookup to that output instead of just using uid/gid values. Falls back to uid/gid if there is an issue with the lookup.
1 parent 43ec6c6 commit 5bf2a17

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

server_unix.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package sftp
66
import (
77
"fmt"
88
"os"
9+
"os/user"
910
"path"
11+
"strconv"
1012
"syscall"
1113
"time"
1214
)
@@ -20,10 +22,21 @@ func runLsStatt(dirent os.FileInfo, statt *syscall.Stat_t) string {
2022
typeword := runLsTypeWord(dirent)
2123
numLinks := statt.Nlink
2224
uid := statt.Uid
25+
usr, err := user.LookupId(strconv.Itoa(int(uid)))
26+
var username string
27+
if err == nil {
28+
username = usr.Username
29+
} else {
30+
username = fmt.Sprintf("%d", uid)
31+
}
2332
gid := statt.Gid
24-
username := fmt.Sprintf("%d", uid)
25-
groupname := fmt.Sprintf("%d", gid)
26-
// TODO FIXME: uid -> username, gid -> groupname lookup for ls -l format output
33+
grp, err := user.LookupGroupId(strconv.Itoa(int(gid)))
34+
var groupname string
35+
if err == nil { 5E16
36+
groupname = grp.Name
37+
} else {
38+
groupname = fmt.Sprintf("%d", gid)
39+
}
2740

2841
mtime := dirent.ModTime()
2942
monthStr := mtime.Month().String()[0:3]

0 commit comments

Comments
 (0)
0