forked from airnandez/cluefs
/
user.go
54 lines (48 loc) · 920 Bytes
/
user.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
package main
import (
"fmt"
"os/user"
"sync"
)
// userStore is a map from user ids to user name
var (
userMutex sync.RWMutex
userStore map[uint32]string
groupMutex sync.RWMutex
groupStore map[uint32]string
)
func init() {
userStore = make(map[uint32]string, 512)
groupStore = make(map[uint32]string, 256)
}
func userName(uid uint32) string {
userMutex.RLock()
name, ok := userStore[uid]
userMutex.RUnlock()
if !ok {
name = uidToUserName(uid)
userMutex.Lock()
userStore[uid] = name
userMutex.Unlock()
}
return name
}
func uidToUserName(uid uint32) string {
u, err := user.LookupId(fmt.Sprintf("%d", uid))
if err != nil {
return ""
}
return u.Username
}
func groupName(uid uint32) string {
groupMutex.RLock()
name, ok := groupStore[uid]
groupMutex.RUnlock()
if !ok {
name = gidToGroupName(uid)
groupMutex.Lock()
groupStore[uid] = name
groupMutex.Unlock()
}
return name
}