10000 improve previous commit's user/group lookups · etherscan-io/sftp@6d8540e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d8540e

Browse files
committed
improve previous commit's user/group lookups
Mostly fixing an inefficiency w/ calculating the gid string twice if lookups fail (but also overall a bit cleaner).
1 parent 5bf2a17 commit 6d8540e

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

server_unix.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"os/user"
1010
"path"
11-
"strconv"
1211
"syscall"
1312
"time"
1413
)
@@ -21,23 +20,14 @@ func runLsStatt(dirent os.FileInfo, statt *syscall.Stat_t) string {
2120

2221
typeword := runLsTypeWord(dirent)
2322
numLinks := statt.Nlink
24-
uid := statt.Uid
25-
usr, err := user.LookupId(strconv.Itoa(int(uid)))
26-
var username string
27-
if err == nil {
23+
username := fmt.Sprintf("%d", statt.Uid)
24+
if usr, err := user.LookupId(username); err == nil {
2825
username = usr.Username
29-
} else {
30-
username = fmt.Sprintf("%d", uid)
3126
}
32-
gid := statt.Gid
33-
grp, err := user.LookupGroupId(strconv.Itoa(int(gid)))
34-
var groupname string
35-
if err == nil {
27+
groupname := fmt.Sprintf("%d", statt.Gid)
28+
if grp, err := user.LookupGroupId(groupname); err == nil {
3629
groupname = grp.Name
37-
} else {
38-
groupname = fmt.Sprintf("%d", gid)
3930
}
40-
4131
mtime := dirent.ModTime()
4232
monthStr := mtime.Month().String()[0:3]
4333
day := mtime.Day()
@@ -50,7 +40,9 @@ func runLsStatt(dirent os.FileInfo, statt *syscall.Stat_t) string {
5040
yearOrTime = fmt.Sprintf("%d", year)
5141
}
5242

53-
return fmt.Sprintf("%s %4d %-8s %-8s %8d %s %2d %5s %s", typeword, numLinks, username, groupname, dirent.Size(), monthStr, day, yearOrTime, dirent.Name())
43+
return fmt.Sprintf("%s %4d %-8s %-8s %8d %s %2d %5s %s", typeword,
44+
numLinks, username, groupname, dirent.Size(), monthStr, day,
45+
yearOrTime, dirent.Name())
5446
}
5547

5648
// ls -l style output for a file, which is in the 'long output' section of a readdir response packet

0 commit comments

Comments
 (0)
0