Skip to content

Commit

Permalink
Follow symlinks (#519)
Browse files Browse the repository at this point in the history
Fixes #518
  • Loading branch information
dominikschulz authored Dec 14, 2017
1 parent 511b766 commit d2eb5be
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions utils/fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ import (

// CleanPath resolves common aliases in a path and cleans it as much as possible
func CleanPath(path string) string {
if fi, err := os.Lstat(path); err == nil {
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
resolvedPath, err := filepath.EvalSymlinks(path)
if err != nil {
path = resolvedPath
}
}
resolvedPath, err := filepath.EvalSymlinks(path)
if err == nil {
path = resolvedPath
}
// http://stackoverflow.com/questions/17609732/expand-tilde-to-home-directory
if len(path) > 1 && path[:2] == "~/" {
Expand All @@ -38,7 +34,7 @@ func CleanPath(path string) string {
// IsDir checks if a certain path exists and is a directory
// https://stackoverflow.com/questions/10510691/how-to-check-whether-a-file-or-directory-denoted-by-a-path-exists-in-golang
func IsDir(path string) bool {
fi, err := os.Lstat(path)
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
// not found
Expand All @@ -47,17 +43,13 @@ func IsDir(path string) bool {
fmt.Printf("failed to check dir %s: %s\n", path, err)
return false
}
if fi.Mode()&os.ModeSymlink != 0 {
fmt.Printf("dir %s is a symlink. ignoring", path)
return false
}

return fi.IsDir()
}

// IsFile checks if a certain path is actually a file
func IsFile(path string) bool {
fi, err := os.Lstat(path)
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
// not found
Expand All @@ -66,10 +58,6 @@ func IsFile(path string) bool {
fmt.Printf("failed to check dir %s: %s\n", path, err)
return false
}
if fi.Mode()&os.ModeSymlink != 0 {
fmt.Printf("dir %s is a symlink. ignoring", path)
return false
}

return fi.Mode().IsRegular()
}
Expand Down

0 comments on commit d2eb5be

Please sign in to comment.