Skip to content

Commit 495f892

Browse files
committed
Merged the user and group retrieval logic into a single function
1 parent d14ac9a commit 495f892

File tree

6 files changed

+57
-77
lines changed

6 files changed

+57
-77
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
gaze
2+
gaze.exe
23
*.out
34
*.test
45
.vscode/

entry/entry.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@ func (e Entry) Permission() string {
1818
return e.info.Mode().String()
1919
}
2020

21-
// User returns the user name associated with the fileInfo inside the Entry.
22-
func (e Entry) User() string {
23-
return FileOwner(e)
24-
}
25-
26-
// Group returns the group name associated with the fileInfo inside the Entry.
27-
func (e Entry) Group() string {
28-
return FileGroup(e)
21+
// User returns the user and group name associated with the fileInfo inside the Entry.
22+
func (e Entry) UserAndGroup() string {
23+
return FileUserGroup(e)
2924
}
3025

3126
// Time returns the formatted modification time of the Entry.

entry/fs_unix.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@ package entry
44

55
import (
66
"fmt"
7-
"log"
87
"os/user"
98
"syscall"
109
)
1110

12-
func FileOwner(e Entry) string {
13-
stat := e.info.Sys().(*syscall.Stat_t)
14-
u, err := user.LookupId(fmt.Sprint(stat.Uid))
15-
if err != nil {
16-
log.Fatal(err)
11+
// FileUserGroup retrieves the file owner and group names for the given Entry.
12+
func FileUserGroup(e Entry) string {
13+
stat, ok := e.info.Sys().(*syscall.Stat_t)
14+
if !ok {
15+
return "0 0"
16+
}
17+
18+
uidStr := fmt.Sprint(stat.Uid)
19+
gidStr := fmt.Sprint(stat.Gid)
20+
21+
owner := uidStr
22+
if u, err := user.LookupId(uidStr); err == nil {
23+
owner = u.Username
1724
}
18-
return u.Username
19-
}
2025

21-
func FileGroup(e Entry) string {
22-
stat := e.info.Sys().(*syscall.Stat_t)
23-
g, err := user.LookupGroupId(fmt.Sprint(stat.Gid))
24-
if err != nil {
25-
log.Fatal(err)
26+
group := gidStr
27+
if g, err := user.LookupGroupId(gidStr); err == nil {
28+
group = g.Name
2629
}
27-
return g.Name
30+
31+
return fmt.Sprintf("%s %s", owner, group)
2832
}

entry/fs_windows.go

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,50 @@
33
package entry
44

55
import (
6-
"syscall"
7-
"unsafe"
8-
)
6+
"fmt"
7+
"path/filepath"
98

10-
var (
11-
modadvapi32 = syscall.NewLazyDLL("advapi32.dll")
12-
procGetSecurityInfo = modadvapi32.NewProc("GetSecurityInfo")
9+
"golang.org/x/sys/windows"
1310
)
1411

15-
// Get file owner using Windows SID
16-
func FileOwner(e Entry) string {
17-
path, err := syscall.UTF16PtrFromString(e.info.Name())
12+
// FileUserGroup retrieves the file's owner and group names for the given Entry.
13+
func FileUserGroup(e Entry) string {
14+
securityFlags := windows.OWNER_SECURITY_INFORMATION | windows.GROUP_SECURITY_INFORMATION
15+
sd, err := windows.GetNamedSecurityInfo(
16+
filepath.Clean(e.info.Name()),
17+
windows.SE_FILE_OBJECT,
18+
windows.SECURITY_INFORMATION(securityFlags),
19+
)
1820
if err != nil {
19-
return ""
21+
return "unknown unknown"
2022
}
2123

22-
var sid *syscall.SID
23-
var secDesc uintptr
24-
25-
err = syscall.GetNamedSecurityInfo(
26-
path,
27-
syscall.SE_FILE_OBJECT,
28-
syscall.OWNER_SECURITY_INFORMATION,
29-
&sid,
30-
nil,
31-
nil,
32-
nil,
33-
&secDesc,
34-
)
35-
36-
if err != nil {
37-
return ""
24+
owner := "unknown"
25+
if ownerSid, _, err := sd.Owner(); err == nil && ownerSid != nil {
26+
owner = sidToName(ownerSid)
27+
} else if ownerSid != nil {
28+
owner = ownerSid.String()
3829
}
3930

40-
sidStr, _ := sid.String()
41-
return sidStr
31+
group := "unknown"
32+
if groupSid, _, err := sd.Group(); err == nil && groupSid != nil {
33+
group = sidToName(groupSid)
34+
} else if groupSid != nil {
35+
group = groupSid.String()
36+
}
37+
38+
return fmt.Sprintf("%s %s", owner, group)
4239
}
4340

44-
// Get file group using Windows SID
45-
func FileGroup(e Entry) string {
46-
path, err := syscall.UTF16PtrFromString(e.info.Name())
47-
if err != nil {
48-
return ""
41+
// sidToName converts a Windows SID into a human-readable account name ("DOMAIN\User").
42+
func sidToName(sid *windows.SID) string {
43+
if sid == nil {
44+
return "unknown"
4945
}
5046

51-
var sid *syscall.SID
52-
var secDesc uintptr
53-
54-
err = syscall.GetNamedSecurityInfo(
55-
path,
56-
syscall.SE_FILE_OBJECT,
57-
syscall.GROUP_SECURITY_INFORMATION,
58-
nil,
59-
&sid,
60-
nil,
61-
nil,
62-
&secDesc,
63-
)
64-
47+
name, domain, _, err := sid.LookupAccount("")
6548
if err != nil {
66-
return ""
49+
return sid.String()
6750
}
68-
69-
sidStr, _ := sid.String()
70-
return sidStr
51+
return fmt.Sprintf("%s\\%s", domain, name)
7152
}

entry/long.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ func Long(displayEntries []Entry) string {
1111
sb.WriteString(line)
1212
const pad = 2
1313
for _, e := range displayEntries {
14-
line = fmt.Sprintf("%s%*s%s%*s%s%*s%s%*s%s%*s%s\n",
14+
line = fmt.Sprintf("%s%*s%s%*s%s%*s%s%*s%s\n",
1515
e.Permission(), pad, "",
16-
e.User(), pad, "",
17-
e.Group(), pad, "",
16+
e.UserAndGroup(), pad, "",
1817
e.Time(), pad, "",
1918
HumanReadableSize((e.Size())), pad, "",
2019
e.Name(),

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ go 1.23
44

55
require golang.org/x/term v0.28.0
66

7-
require golang.org/x/sys v0.29.0 // indirect
7+
require golang.org/x/sys v0.29.0

0 commit comments

Comments
 (0)