Skip to content

Commit 65a445d

Browse files
committed
filesystem: add back canonicalizePath()
Restore the canonicalizePath() function from before commit f2eb79f, since it's needed again. Update #339
1 parent bf17c3e commit 65a445d

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

filesystem/path.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"log"
2525
"os"
26+
"path/filepath"
2627

2728
"golang.org/x/sys/unix"
2829

@@ -40,6 +41,22 @@ func OpenFileOverridingUmask(name string, flag int, perm os.FileMode) (*os.File,
4041
// We only check the unix permissions and the sticky bit
4142
const permMask = os.ModeSticky | os.ModePerm
4243

44+
// canonicalizePath turns path into an absolute path without symlinks.
45+
func canonicalizePath(path string) (string, error) {
46+
path, err := filepath.Abs(path)
47+
if err != nil {
48+
return "", err
49+
}
50+
path, err = filepath.EvalSymlinks(path)
51+
52+
// Get a better error if we have an invalid path
53+
if pathErr, ok := err.(*os.PathError); ok {
54+
err = errors.Wrap(pathErr.Err, pathErr.Path)
55+
}
56+
57+
return path, err
58+
}
59+
4360
// loggedStat runs os.Stat, but it logs the error if stat returns any error
4461
// other than nil or IsNotExist.
4562
func loggedStat(name string) (os.FileInfo, error) {

0 commit comments

Comments
 (0)