From 26df48b2aaa8275ae14583761a5098b21377aced Mon Sep 17 00:00:00 2001 From: Anton Lindstrom Date: Wed, 4 Oct 2017 20:11:54 +0200 Subject: [PATCH] Add group name in fillUser This does a lookup of the group name from the GID and adds it to the Node. --- internal/restic/node.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/internal/restic/node.go b/internal/restic/node.go index 73518bf8e..0ae773e98 100644 --- a/internal/restic/node.go +++ b/internal/restic/node.go @@ -541,7 +541,14 @@ func (node *Node) fillUser(stat statT) error { return err } + group, err := lookupGroup(strconv.Itoa(int(stat.gid()))) + if err != nil { + return err + } + node.User = username + node.Group = group + return nil } @@ -573,6 +580,34 @@ func lookupUsername(uid string) (string, error) { return username, nil } +var ( + gidLookupCache = make(map[string]string) + gidLookupCacheMutex = sync.RWMutex{} +) + +func lookupGroup(gid string) (string, error) { + gidLookupCacheMutex.RLock() + value, ok := gidLookupCache[gid] + gidLookupCacheMutex.RUnlock() + + if ok { + return value, nil + } + + group := "" + + g, err := user.LookupGroupId(gid) + if err == nil { + group = g.Name + } + + gidLookupCacheMutex.Lock() + gidLookupCache[gid] = group + gidLookupCacheMutex.Unlock() + + return group, nil +} + func (node *Node) fillExtra(path string, fi os.FileInfo) error { stat, ok := toStatT(fi.Sys()) if !ok {