Avoids the dot-dot-based algorithm on repeated calls
when the directory hasn't changed.
R=golang-dev, iant, bradfitz
CC=golang-dev
https://golang.org/cl/
7340043
package os
import (
+ "sync"
"syscall"
)
+var getwdCache struct {
+ sync.Mutex
+ dir string
+}
+
// Getwd returns a rooted path name corresponding to the
// current directory. If the current directory can be
// reached via multiple paths (due to symbolic links),
}
}
+ // Apply same kludge but to cached dir instead of $PWD.
+ getwdCache.Lock()
+ pwd = getwdCache.dir
+ getwdCache.Unlock()
+ if len(pwd) > 0 {
+ d, err := Stat(pwd)
+ if err == nil && SameFile(dot, d) {
+ return pwd, nil
+ }
+ }
+
// Root is a special case because it has no parent
// and ends in a slash.
root, err := Stat("/")
// Set up for next round.
dot = pd
}
+
+ // Save answer as hint to avoid the expensive path next time.
+ getwdCache.Lock()
+ getwdCache.dir = pwd
+ getwdCache.Unlock()
+
return pwd, nil
}