diff options
| author | Eric Biggers <ebiggers@google.com> | 2022-01-18 23:43:35 -0800 |
|---|---|---|
| committer | Eric Biggers <ebiggers@google.com> | 2022-01-19 11:12:37 -0800 |
| commit | 65a445d4d01c09f43676180d779abbff0de40f1e (patch) | |
| tree | 0ad79f4a80722fc630438be84e85ff7f6ef1db6c /filesystem | |
| parent | bf17c3e80daa975ac15d6146964ca294327d8fd9 (diff) | |
filesystem: add back canonicalizePath()
Restore the canonicalizePath() function from before commit
f2eb79fb5fb10275c014b55c13e28ff02d3b70a8, since it's needed again.
Update https://github.com/google/fscrypt/issues/339
Diffstat (limited to 'filesystem')
| -rw-r--r-- | filesystem/path.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/filesystem/path.go b/filesystem/path.go index 274dc0a..fa38701 100644 --- a/filesystem/path.go +++ b/filesystem/path.go @@ -23,6 +23,7 @@ import ( "fmt" "log" "os" + "path/filepath" "golang.org/x/sys/unix" @@ -40,6 +41,22 @@ func OpenFileOverridingUmask(name string, flag int, perm os.FileMode) (*os.File, // We only check the unix permissions and the sticky bit const permMask = os.ModeSticky | os.ModePerm +// canonicalizePath turns path into an absolute path without symlinks. +func canonicalizePath(path string) (string, error) { + path, err := filepath.Abs(path) + if err != nil { + return "", err + } + path, err = filepath.EvalSymlinks(path) + + // Get a better error if we have an invalid path + if pathErr, ok := err.(*os.PathError); ok { + err = errors.Wrap(pathErr.Err, pathErr.Path) + } + + return path, err +} + // loggedStat runs os.Stat, but it logs the error if stat returns any error // other than nil or IsNotExist. func loggedStat(name string) (os.FileInfo, error) { |