From 6e20eba85262021920b9794893c1f9bfb6866c8f Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Fri, 1 May 2015 17:26:40 -0400 Subject: [PATCH 1/2] uid lookup cache --- node.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/node.go b/node.go index b29f2fe45..9b669f4ee 100644 --- a/node.go +++ b/node.go @@ -6,6 +6,7 @@ import ( "os" "os/user" "strconv" + "sync" "syscall" "time" @@ -339,15 +340,41 @@ func (node *Node) fillUser(stat *syscall.Stat_t) error { node.UID = stat.Uid node.GID = stat.Gid - u, err := user.LookupId(strconv.Itoa(int(stat.Uid))) + username, err := lookupUsername(strconv.Itoa(int(stat.Uid))) if err != nil { return err } - node.User = u.Username + node.User = username return nil } +var ( + uidLookupCache = make(map[string]string) + uidLookupCacheMutex = sync.RWMutex{} +) + +func lookupUsername(uid string) (string, error) { + uidLookupCacheMutex.RLock() + value, ok := uidLookupCache[uid] + uidLookupCacheMutex.RUnlock() + + if ok { + return value, nil + } + + u, err := user.LookupId(uid) + if err != nil { + return "", err + } + + uidLookupCacheMutex.Lock() + uidLookupCache[uid] = u.Username + uidLookupCacheMutex.Unlock() + + return u.Username, nil +} + func (node *Node) fillExtra(path string, fi os.FileInfo) error { stat, ok := fi.Sys().(*syscall.Stat_t) if !ok { From e9009358a954fdbd0f57cfa1686a7567ca84df2a Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Fri, 1 May 2015 17:31:02 -0400 Subject: [PATCH 2/2] catch err --- node.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/node.go b/node.go index 9b669f4ee..5c9a5768c 100644 --- a/node.go +++ b/node.go @@ -383,11 +383,14 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error { node.Inode = stat.Ino - node.fillUser(stat) node.fillTimes(stat) var err error + if err = node.fillUser(stat); err != nil { + return err + } + switch node.Type { case "file": node.Size = uint64(stat.Size)