-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
63 lines (53 loc) · 1.17 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"io/fs"
"os"
"os/user"
"strings"
"syscall"
"time"
)
type file struct {
name string
isHidden bool
mode string
user string
group string
size int64
modificationTime time.Time
}
func newFile(entry fs.DirEntry) (*file, error) {
info, err := entry.Info()
if err != nil {
return nil, err
}
user, group := getUserAndGroupOwners(&info)
return &file{
name: entry.Name(),
isHidden: strings.HasPrefix(entry.Name(), "."),
mode: info.Mode().String(),
user: user,
group: group,
size: info.Size(),
modificationTime: info.ModTime(),
}, nil
}
func getUserAndGroupOwners(fileInfo *os.FileInfo) (userName, groupName string) {
statSyscall := (*fileInfo).Sys().(*syscall.Stat_t)
userName = fmt.Sprint(statSyscall.Uid)
groupName = fmt.Sprint(statSyscall.Gid)
// Getting the user
owner, err := user.LookupId(userName)
if err != nil {
return
}
userName = owner.Username
// Getting the group
group, err := user.LookupGroupId(groupName)
if err != nil {
return
}
groupName = group.Name
return
}