mirror of
https://github.com/octoleo/restic.git
synced 2024-11-11 07:41:03 +00:00
Merge pull request #3016 from greatroar/uid-gid-lookup
Simplify/optimize cached UID/GID lookups
This commit is contained in:
commit
41a45ae908
@ -506,43 +506,29 @@ func (node Node) sameExtendedAttributes(other Node) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (node *Node) fillUser(stat statT) error {
|
func (node *Node) fillUser(stat statT) {
|
||||||
node.UID = stat.uid()
|
uid, gid := stat.uid(), stat.gid()
|
||||||
node.GID = stat.gid()
|
node.UID, node.GID = uid, gid
|
||||||
|
node.User = lookupUsername(uid)
|
||||||
username, err := lookupUsername(strconv.Itoa(int(stat.uid())))
|
node.Group = lookupGroup(gid)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
group, err := lookupGroup(strconv.Itoa(int(stat.gid())))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
node.User = username
|
|
||||||
node.Group = group
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
uidLookupCache = make(map[string]string)
|
uidLookupCache = make(map[uint32]string)
|
||||||
uidLookupCacheMutex = sync.RWMutex{}
|
uidLookupCacheMutex = sync.RWMutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func lookupUsername(uid string) (string, error) {
|
// Cached user name lookup by uid. Returns "" when no name can be found.
|
||||||
|
func lookupUsername(uid uint32) string {
|
||||||
uidLookupCacheMutex.RLock()
|
uidLookupCacheMutex.RLock()
|
||||||
value, ok := uidLookupCache[uid]
|
username, ok := uidLookupCache[uid]
|
||||||
uidLookupCacheMutex.RUnlock()
|
uidLookupCacheMutex.RUnlock()
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
return value, nil
|
return username
|
||||||
}
|
}
|
||||||
|
|
||||||
username := ""
|
u, err := user.LookupId(strconv.Itoa(int(uid)))
|
||||||
|
|
||||||
u, err := user.LookupId(uid)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
username = u.Username
|
username = u.Username
|
||||||
}
|
}
|
||||||
@ -551,26 +537,25 @@ func lookupUsername(uid string) (string, error) {
|
|||||||
uidLookupCache[uid] = username
|
uidLookupCache[uid] = username
|
||||||
uidLookupCacheMutex.Unlock()
|
uidLookupCacheMutex.Unlock()
|
||||||
|
|
||||||
return username, nil
|
return username
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gidLookupCache = make(map[string]string)
|
gidLookupCache = make(map[uint32]string)
|
||||||
gidLookupCacheMutex = sync.RWMutex{}
|
gidLookupCacheMutex = sync.RWMutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func lookupGroup(gid string) (string, error) {
|
// Cached group name lookup by gid. Returns "" when no name can be found.
|
||||||
|
func lookupGroup(gid uint32) string {
|
||||||
gidLookupCacheMutex.RLock()
|
gidLookupCacheMutex.RLock()
|
||||||
value, ok := gidLookupCache[gid]
|
group, ok := gidLookupCache[gid]
|
||||||
gidLookupCacheMutex.RUnlock()
|
gidLookupCacheMutex.RUnlock()
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
return value, nil
|
return group
|
||||||
}
|
}
|
||||||
|
|
||||||
group := ""
|
g, err := user.LookupGroupId(strconv.Itoa(int(gid)))
|
||||||
|
|
||||||
g, err := user.LookupGroupId(gid)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
group = g.Name
|
group = g.Name
|
||||||
}
|
}
|
||||||
@ -579,7 +564,7 @@ func lookupGroup(gid string) (string, error) {
|
|||||||
gidLookupCache[gid] = group
|
gidLookupCache[gid] = group
|
||||||
gidLookupCacheMutex.Unlock()
|
gidLookupCacheMutex.Unlock()
|
||||||
|
|
||||||
return group, nil
|
return group
|
||||||
}
|
}
|
||||||
|
|
||||||
func (node *Node) fillExtra(path string, fi os.FileInfo) error {
|
func (node *Node) fillExtra(path string, fi os.FileInfo) error {
|
||||||
@ -597,11 +582,7 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
|
|||||||
|
|
||||||
node.fillTimes(stat)
|
node.fillTimes(stat)
|
||||||
|
|
||||||
var err error
|
node.fillUser(stat)
|
||||||
|
|
||||||
if err = node.fillUser(stat); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch node.Type {
|
switch node.Type {
|
||||||
case "file":
|
case "file":
|
||||||
@ -609,6 +590,7 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
|
|||||||
node.Links = uint64(stat.nlink())
|
node.Links = uint64(stat.nlink())
|
||||||
case "dir":
|
case "dir":
|
||||||
case "symlink":
|
case "symlink":
|
||||||
|
var err error
|
||||||
node.LinkTarget, err = fs.Readlink(path)
|
node.LinkTarget, err = fs.Readlink(path)
|
||||||
node.Links = uint64(stat.nlink())
|
node.Links = uint64(stat.nlink())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -626,7 +608,7 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
|
|||||||
return errors.Errorf("invalid node type %q", node.Type)
|
return errors.Errorf("invalid node type %q", node.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = node.fillExtendedAttributes(path); err != nil {
|
if err := node.fillExtendedAttributes(path); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user