Make sure utils.GetCwd() does not return a symlink

Always resolve the symlinks before returning the current working
directory name. Functions like filepath.Walk do not handle symlinks
well.

Found out the hard way by @kroepke - Thanks!
This commit is contained in:
Bernd Ahlers
2017-06-12 16:26:02 +02:00
parent d4cee55d72
commit d822dee18e

View File

@@ -17,7 +17,14 @@ func GetCwd() string {
logger.Fatal("Unable to get current directory: %v", err)
}
return currentDir
// Make sure to resolve any symlinks and get the real directory.
// Functions like filepath.Walk do not handle symlinks well...
dir, err := filepath.EvalSymlinks(currentDir)
if err != nil {
logger.Fatal("Unable to eval symlink for %v: %v", currentDir, err)
}
return dir
}
func Chdir(path string) {
@@ -31,10 +38,7 @@ func GetRelativePath(path string) string {
return path
}
cwd, err := os.Getwd()
if err != nil {
logger.Fatal("Unable to get current working directory")
}
cwd := GetCwd()
relPath, err := filepath.Rel(cwd, path)
if err != nil {
logger.Fatal("Unable to get relative path for %v", path)