Source file
src/os/user/lookup.go
1
2
3
4
5 package user
6
7 import "sync"
8
9 const (
10 userFile = "/etc/passwd"
11 groupFile = "/etc/group"
12 )
13
14 var colon = []byte{':'}
15
16
17
18
19
20
21 func Current() (*User, error) {
22 cache.Do(func() { cache.u, cache.err = current() })
23 if cache.err != nil {
24 return nil, cache.err
25 }
26 u := *cache.u
27 return &u, nil
28 }
29
30
31 var cache struct {
32 sync.Once
33 u *User
34 err error
35 }
36
37
38
39 func Lookup(username string) (*User, error) {
40 if u, err := Current(); err == nil && u.Username == username {
41 return u, err
42 }
43 return lookupUser(username)
44 }
45
46
47
48 func LookupId(uid string) (*User, error) {
49 if u, err := Current(); err == nil && u.Uid == uid {
50 return u, err
51 }
52 return lookupUserId(uid)
53 }
54
55
56
57 func LookupGroup(name string) (*Group, error) {
58 return lookupGroup(name)
59 }
60
61
62
63 func LookupGroupId(gid string) (*Group, error) {
64 return lookupGroupId(gid)
65 }
66
67
68 func (u *User) GroupIds() ([]string, error) {
69 return listGroups(u)
70 }
71
View as plain text