aboutsummaryrefslogtreecommitdiff
path: root/src/path/filepath
diff options
context:
space:
mode:
Diffstat (limited to 'src/path/filepath')
-rw-r--r--src/path/filepath/path.go15
-rw-r--r--src/path/filepath/path_test.go19
-rw-r--r--src/path/filepath/path_windows.go2
-rw-r--r--src/path/filepath/path_windows_test.go2
-rw-r--r--src/path/filepath/symlink.go191
5 files changed, 134 insertions, 95 deletions
diff --git a/src/path/filepath/path.go b/src/path/filepath/path.go
index 1508137a33..bbb90306a7 100644
--- a/src/path/filepath/path.go
+++ b/src/path/filepath/path.go
@@ -96,14 +96,19 @@ func Clean(path string) string {
}
return originalPath + "."
}
+
+ n := len(path)
+ if volLen > 2 && n == 1 && os.IsPathSeparator(path[0]) {
+ // UNC volume name with trailing slash.
+ return FromSlash(originalPath[:volLen])
+ }
rooted := os.IsPathSeparator(path[0])
// Invariants:
// reading from path; r is index of next byte to process.
- // writing to buf; w is index of next byte to write.
- // dotdot is index in buf where .. must stop, either because
+ // writing to out; w is index of next byte to write.
+ // dotdot is index in out where .. must stop, either because
// it is the leading slash or it is a leading ../../.. prefix.
- n := len(path)
out := lazybuf{path: path, volAndPath: originalPath, volLen: volLen}
r, dotdot := 0, 0
if rooted {
@@ -166,7 +171,7 @@ func ToSlash(path string) string {
if Separator == '/' {
return path
}
- return strings.Replace(path, string(Separator), "/", -1)
+ return strings.ReplaceAll(path, string(Separator), "/")
}
// FromSlash returns the result of replacing each slash ('/') character
@@ -176,7 +181,7 @@ func FromSlash(path string) string {
if Separator == '/' {
return path
}
- return strings.Replace(path, "/", string(Separator), -1)
+ return strings.ReplaceAll(path, "/", string(Separator))
}
// SplitList splits a list of paths joined by the OS-specific ListSeparator,
diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go
index e50ee97bcb..eddae4755b 100644
--- a/src/path/filepath/path_test.go
+++ b/src/path/filepath/path_test.go
@@ -92,6 +92,9 @@ var wincleantests = []PathTest{
{`//host/share/foo/../baz`, `\\host\share\baz`},
{`\\a\b\..\c`, `\\a\b\c`},
{`\\a\b`, `\\a\b`},
+ {`\\a\b\`, `\\a\b`},
+ {`\\folder\share\foo`, `\\folder\share\foo`},
+ {`\\folder\share\foo\`, `\\folder\share\foo`},
}
func TestClean(t *testing.T) {
@@ -771,6 +774,18 @@ var EvalSymlinksTestDirs = []EvalSymlinksTest{
{"test/link1", "../test"},
{"test/link2", "dir"},
{"test/linkabs", "/"},
+ {"test/link4", "../test2"},
+ {"test2", "test/dir"},
+ // Issue 23444.
+ {"src", ""},
+ {"src/pool", ""},
+ {"src/pool/test", ""},
+ {"src/versions", ""},
+ {"src/versions/current", "../../version"},
+ {"src/versions/v1", ""},
+ {"src/versions/v1/modules", ""},
+ {"src/versions/v1/modules/test", "../../../pool/test"},
+ {"version", "src/versions/v1"},
}
var EvalSymlinksTests = []EvalSymlinksTest{
@@ -784,6 +799,8 @@ var EvalSymlinksTests = []EvalSymlinksTest{
{"test/dir/link3", "."},
{"test/link2/link3/test", "test"},
{"test/linkabs", "/"},
+ {"test/link4/..", "test"},
+ {"src/versions/current/modules/test", "src/pool/test"},
}
// simpleJoin builds a file name from the directory and path.
@@ -1048,7 +1065,7 @@ func TestAbs(t *testing.T) {
}
for _, path := range absTests {
- path = strings.Replace(path, "$", root, -1)
+ path = strings.ReplaceAll(path, "$", root)
info, err := os.Stat(path)
if err != nil {
t.Errorf("%s: %s", path, err)
diff --git a/src/path/filepath/path_windows.go b/src/path/filepath/path_windows.go
index 519b6ebc32..6a144d9e0b 100644
--- a/src/path/filepath/path_windows.go
+++ b/src/path/filepath/path_windows.go
@@ -100,7 +100,7 @@ func splitList(path string) []string {
// Remove quotes.
for i, s := range list {
- list[i] = strings.Replace(s, `"`, ``, -1)
+ list[i] = strings.ReplaceAll(s, `"`, ``)
}
return list
diff --git a/src/path/filepath/path_windows_test.go b/src/path/filepath/path_windows_test.go
index e36a3c9b64..63eab18116 100644
--- a/src/path/filepath/path_windows_test.go
+++ b/src/path/filepath/path_windows_test.go
@@ -431,7 +431,7 @@ func TestToNorm(t *testing.T) {
t.Fatal(err)
}
- err = os.MkdirAll(strings.Replace(testPath, "{{tmp}}", ctmp, -1), 0777)
+ err = os.MkdirAll(strings.ReplaceAll(testPath, "{{tmp}}", ctmp), 0777)
if err != nil {
t.Fatal(err)
}
diff --git a/src/path/filepath/symlink.go b/src/path/filepath/symlink.go
index 824aee4e49..98a92357be 100644
--- a/src/path/filepath/symlink.go
+++ b/src/path/filepath/symlink.go
@@ -10,109 +10,126 @@ import (
"runtime"
)
-// isRoot returns true if path is root of file system
-// (`/` on unix and `/`, `\`, `c:\` or `c:/` on windows).
-func isRoot(path string) bool {
- if runtime.GOOS != "windows" {
- return path == "/"
- }
- switch len(path) {
- case 1:
- return os.IsPathSeparator(path[0])
- case 3:
- return path[1] == ':' && os.IsPathSeparator(path[2])
+func walkSymlinks(path string) (string, error) {
+ volLen := volumeNameLen(path)
+ if volLen < len(path) && os.IsPathSeparator(path[volLen]) {
+ volLen++
}
- return false
-}
+ vol := path[:volLen]
+ dest := vol
+ linksWalked := 0
+ for start, end := volLen, volLen; start < len(path); start = end {
+ for start < len(path) && os.IsPathSeparator(path[start]) {
+ start++
+ }
+ end = start
+ for end < len(path) && !os.IsPathSeparator(path[end]) {
+ end++
+ }
-// isDriveLetter returns true if path is Windows drive letter (like "c:").
-func isDriveLetter(path string) bool {
- if runtime.GOOS != "windows" {
- return false
- }
- return len(path) == 2 && path[1] == ':'
-}
+ // On Windows, "." can be a symlink.
+ // We look it up, and use the value if it is absolute.
+ // If not, we just return ".".
+ isWindowsDot := runtime.GOOS == "windows" && path[volumeNameLen(path):] == "."
-func walkLink(path string, linksWalked *int) (newpath string, islink bool, err error) {
- if *linksWalked > 255 {
- return "", false, errors.New("EvalSymlinks: too many links")
- }
- fi, err := os.Lstat(path)
- if err != nil {
- return "", false, err
- }
- if fi.Mode()&os.ModeSymlink == 0 {
- return path, false, nil
- }
- newpath, err = os.Readlink(path)
- if err != nil {
- return "", false, err
- }
- *linksWalked++
- return newpath, true, nil
-}
-
-func walkLinks(path string, linksWalked *int) (string, error) {
- switch dir, file := Split(path); {
- case dir == "":
- newpath, _, err := walkLink(file, linksWalked)
- return newpath, err
- case file == "":
- if isDriveLetter(dir) {
- return dir, nil
- }
- if os.IsPathSeparator(dir[len(dir)-1]) {
- if isRoot(dir) {
- return dir, nil
+ // The next path component is in path[start:end].
+ if end == start {
+ // No more path components.
+ break
+ } else if path[start:end] == "." && !isWindowsDot {
+ // Ignore path component ".".
+ continue
+ } else if path[start:end] == ".." {
+ // Back up to previous component if possible.
+ // Note that volLen includes any leading slash.
+ var r int
+ for r = len(dest) - 1; r >= volLen; r-- {
+ if os.IsPathSeparator(dest[r]) {
+ break
+ }
+ }
+ if r < volLen {
+ if len(dest) > volLen {
+ dest += string(os.PathSeparator)
+ }
+ dest += ".."
+ } else {
+ dest = dest[:r]
}
- return walkLinks(dir[:len(dir)-1], linksWalked)
+ continue
}
- newpath, _, err := walkLink(dir, linksWalked)
- return newpath, err
- default:
- newdir, err := walkLinks(dir, linksWalked)
- if err != nil {
- return "", err
+
+ // Ordinary path component. Add it to result.
+
+ if len(dest) > volumeNameLen(dest) && !os.IsPathSeparator(dest[len(dest)-1]) {
+ dest += string(os.PathSeparator)
}
- newpath, islink, err := walkLink(Join(newdir, file), linksWalked)
+
+ dest += path[start:end]
+
+ // Resolve symlink.
+
+ fi, err := os.Lstat(dest)
if err != nil {
return "", err
}
- if !islink {
- return newpath, nil
+
+ if fi.Mode()&os.ModeSymlink == 0 {
+ if !fi.Mode().IsDir() && end < len(path) {
+ return "", os.ErrNotExist
+ }
+ continue
}
- if IsAbs(newpath) || os.IsPathSeparator(newpath[0]) {
- return newpath, nil
+
+ // Found symlink.
+
+ linksWalked++
+ if linksWalked > 255 {
+ return "", errors.New("EvalSymlinks: too many links")
}
- return Join(newdir, newpath), nil
- }
-}
-func walkSymlinks(path string) (string, error) {
- if path == "" {
- return path, nil
- }
- var linksWalked int // to protect against cycles
- for {
- i := linksWalked
- newpath, err := walkLinks(path, &linksWalked)
+ link, err := os.Readlink(dest)
if err != nil {
return "", err
}
- if runtime.GOOS == "windows" {
- // walkLinks(".", ...) always returns "." on unix.
- // But on windows it returns symlink target, if current
- // directory is a symlink. Stop the walk, if symlink
- // target is not absolute path, and return "."
- // to the caller (just like unix does).
- // Same for "C:.".
- if path[volumeNameLen(path):] == "." && !IsAbs(newpath) {
- return path, nil
- }
+
+ if isWindowsDot && !IsAbs(link) {
+ // On Windows, if "." is a relative symlink,
+ // just return ".".
+ break
}
- if i == linksWalked {
- return Clean(newpath), nil
+
+ path = link + path[end:]
+
+ v := volumeNameLen(link)
+ if v > 0 {
+ // Symlink to drive name is an absolute path.
+ if v < len(link) && os.IsPathSeparator(link[v]) {
+ v++
+ }
+ vol = link[:v]
+ dest = vol
+ end = len(vol)
+ } else if len(link) > 0 && os.IsPathSeparator(link[0]) {
+ // Symlink to absolute path.
+ dest = link[:1]
+ end = 1
+ } else {
+ // Symlink to relative path; replace last
+ // path component in dest.
+ var r int
+ for r = len(dest) - 1; r >= volLen; r-- {
+ if os.IsPathSeparator(dest[r]) {
+ break
+ }
+ }
+ if r < volLen {
+ dest = vol
+ } else {
+ dest = dest[:r]
+ }
+ end = 0
}
- path = newpath
}
+ return Clean(dest), nil
}