diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 13:14:23 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 13:14:23 +0000 |
commit | 73df946d56c74384511a194dd01dbe099584fd1a (patch) | |
tree | fd0bcea490dd81327ddfbb31e215439672c9a068 /src/path | |
parent | Initial commit. (diff) | |
download | golang-1.16-73df946d56c74384511a194dd01dbe099584fd1a.tar.xz golang-1.16-73df946d56c74384511a194dd01dbe099584fd1a.zip |
Adding upstream version 1.16.10.upstream/1.16.10upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/path')
-rw-r--r-- | src/path/example_test.go | 121 | ||||
-rw-r--r-- | src/path/filepath/example_test.go | 20 | ||||
-rw-r--r-- | src/path/filepath/example_unix_test.go | 171 | ||||
-rw-r--r-- | src/path/filepath/example_unix_walk_test.go | 66 | ||||
-rw-r--r-- | src/path/filepath/export_test.go | 7 | ||||
-rw-r--r-- | src/path/filepath/export_windows_test.go | 10 | ||||
-rw-r--r-- | src/path/filepath/match.go | 360 | ||||
-rw-r--r-- | src/path/filepath/match_test.go | 392 | ||||
-rw-r--r-- | src/path/filepath/path.go | 596 | ||||
-rw-r--r-- | src/path/filepath/path_plan9.go | 51 | ||||
-rw-r--r-- | src/path/filepath/path_test.go | 1537 | ||||
-rw-r--r-- | src/path/filepath/path_unix.go | 53 | ||||
-rw-r--r-- | src/path/filepath/path_windows.go | 200 | ||||
-rw-r--r-- | src/path/filepath/path_windows_test.go | 586 | ||||
-rw-r--r-- | src/path/filepath/symlink.go | 147 | ||||
-rw-r--r-- | src/path/filepath/symlink_unix.go | 7 | ||||
-rw-r--r-- | src/path/filepath/symlink_windows.go | 119 | ||||
-rw-r--r-- | src/path/match.go | 231 | ||||
-rw-r--r-- | src/path/match_test.go | 84 | ||||
-rw-r--r-- | src/path/path.go | 233 | ||||
-rw-r--r-- | src/path/path_test.go | 236 |
21 files changed, 5227 insertions, 0 deletions
diff --git a/src/path/example_test.go b/src/path/example_test.go new file mode 100644 index 0000000..e30ebd1 --- /dev/null +++ b/src/path/example_test.go @@ -0,0 +1,121 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package path_test + +import ( + "fmt" + "path" +) + +func ExampleBase() { + fmt.Println(path.Base("/a/b")) + fmt.Println(path.Base("/")) + fmt.Println(path.Base("")) + // Output: + // b + // / + // . +} + +func ExampleClean() { + paths := []string{ + "a/c", + "a//c", + "a/c/.", + "a/c/b/..", + "/../a/c", + "/../a/b/../././/c", + "", + } + + for _, p := range paths { + fmt.Printf("Clean(%q) = %q\n", p, path.Clean(p)) + } + + // Output: + // Clean("a/c") = "a/c" + // Clean("a//c") = "a/c" + // Clean("a/c/.") = "a/c" + // Clean("a/c/b/..") = "a/c" + // Clean("/../a/c") = "/a/c" + // Clean("/../a/b/../././/c") = "/a/c" + // Clean("") = "." +} + +func ExampleDir() { + fmt.Println(path.Dir("/a/b/c")) + fmt.Println(path.Dir("a/b/c")) + fmt.Println(path.Dir("/a/")) + fmt.Println(path.Dir("a/")) + fmt.Println(path.Dir("/")) + fmt.Println(path.Dir("")) + // Output: + // /a/b + // a/b + // /a + // a + // / + // . +} + +func ExampleExt() { + fmt.Println(path.Ext("/a/b/c/bar.css")) + fmt.Println(path.Ext("/")) + fmt.Println(path.Ext("")) + // Output: + // .css + // + // +} + +func ExampleIsAbs() { + fmt.Println(path.IsAbs("/dev/null")) + // Output: true +} + +func ExampleJoin() { + fmt.Println(path.Join("a", "b", "c")) + fmt.Println(path.Join("a", "b/c")) + fmt.Println(path.Join("a/b", "c")) + + fmt.Println(path.Join("a/b", "../../../xyz")) + + fmt.Println(path.Join("", "")) + fmt.Println(path.Join("a", "")) + fmt.Println(path.Join("", "a")) + + // Output: + // a/b/c + // a/b/c + // a/b/c + // ../xyz + // + // a + // a +} + +func ExampleMatch() { + fmt.Println(path.Match("abc", "abc")) + fmt.Println(path.Match("a*", "abc")) + fmt.Println(path.Match("a*/b", "a/c/b")) + // Output: + // true <nil> + // true <nil> + // false <nil> +} + +func ExampleSplit() { + split := func(s string) { + dir, file := path.Split(s) + fmt.Printf("path.Split(%q) = dir: %q, file: %q\n", s, dir, file) + } + split("static/myfile.css") + split("myfile.css") + split("") + // Output: + // path.Split("static/myfile.css") = dir: "static/", file: "myfile.css" + // path.Split("myfile.css") = dir: "", file: "myfile.css" + // path.Split("") = dir: "", file: "" +} diff --git a/src/path/filepath/example_test.go b/src/path/filepath/example_test.go new file mode 100644 index 0000000..a1d680e --- /dev/null +++ b/src/path/filepath/example_test.go @@ -0,0 +1,20 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath_test + +import ( + "fmt" + "path/filepath" +) + +func ExampleExt() { + fmt.Printf("No dots: %q\n", filepath.Ext("index")) + fmt.Printf("One dot: %q\n", filepath.Ext("index.js")) + fmt.Printf("Two dots: %q\n", filepath.Ext("main.test.js")) + // Output: + // No dots: "" + // One dot: ".js" + // Two dots: ".js" +} diff --git a/src/path/filepath/example_unix_test.go b/src/path/filepath/example_unix_test.go new file mode 100644 index 0000000..c9d6944 --- /dev/null +++ b/src/path/filepath/example_unix_test.go @@ -0,0 +1,171 @@ +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build !windows,!plan9 + +package filepath_test + +import ( + "fmt" + "path/filepath" +) + +func ExampleSplitList() { + fmt.Println("On Unix:", filepath.SplitList("/a/b/c:/usr/bin")) + // Output: + // On Unix: [/a/b/c /usr/bin] +} + +func ExampleRel() { + paths := []string{ + "/a/b/c", + "/b/c", + "./b/c", + } + base := "/a" + + fmt.Println("On Unix:") + for _, p := range paths { + rel, err := filepath.Rel(base, p) + fmt.Printf("%q: %q %v\n", p, rel, err) + } + + // Output: + // On Unix: + // "/a/b/c": "b/c" <nil> + // "/b/c": "../b/c" <nil> + // "./b/c": "" Rel: can't make ./b/c relative to /a +} + +func ExampleSplit() { + paths := []string{ + "/home/arnie/amelia.jpg", + "/mnt/photos/", + "rabbit.jpg", + "/usr/local//go", + } + fmt.Println("On Unix:") + for _, p := range paths { + dir, file := filepath.Split(p) + fmt.Printf("input: %q\n\tdir: %q\n\tfile: %q\n", p, dir, file) + } + // Output: + // On Unix: + // input: "/home/arnie/amelia.jpg" + // dir: "/home/arnie/" + // file: "amelia.jpg" + // input: "/mnt/photos/" + // dir: "/mnt/photos/" + // file: "" + // input: "rabbit.jpg" + // dir: "" + // file: "rabbit.jpg" + // input: "/usr/local//go" + // dir: "/usr/local//" + // file: "go" +} + +func ExampleJoin() { + fmt.Println("On Unix:") + fmt.Println(filepath.Join("a", "b", "c")) + fmt.Println(filepath.Join("a", "b/c")) + fmt.Println(filepath.Join("a/b", "c")) + fmt.Println(filepath.Join("a/b", "/c")) + + fmt.Println(filepath.Join("a/b", "../../../xyz")) + + // Output: + // On Unix: + // a/b/c + // a/b/c + // a/b/c + // a/b/c + // ../xyz +} + +func ExampleMatch() { + fmt.Println("On Unix:") + fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo")) + fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo/bar")) + fmt.Println(filepath.Match("/home/?opher", "/home/gopher")) + fmt.Println(filepath.Match("/home/\\*", "/home/*")) + + // Output: + // On Unix: + // true <nil> + // false <nil> + // true <nil> + // true <nil> +} + +func ExampleBase() { + fmt.Println("On Unix:") + fmt.Println(filepath.Base("/foo/bar/baz.js")) + fmt.Println(filepath.Base("/foo/bar/baz")) + fmt.Println(filepath.Base("/foo/bar/baz/")) + fmt.Println(filepath.Base("dev.txt")) + fmt.Println(filepath.Base("../todo.txt")) + fmt.Println(filepath.Base("..")) + fmt.Println(filepath.Base(".")) + fmt.Println(filepath.Base("/")) + fmt.Println(filepath.Base("")) + + // Output: + // On Unix: + // baz.js + // baz + // baz + // dev.txt + // todo.txt + // .. + // . + // / + // . +} + +func ExampleDir() { + fmt.Println("On Unix:") + fmt.Println(filepath.Dir("/foo/bar/baz.js")) + fmt.Println(filepath.Dir("/foo/bar/baz")) + fmt.Println(filepath.Dir("/foo/bar/baz/")) + fmt.Println(filepath.Dir("/dirty//path///")) + fmt.Println(filepath.Dir("dev.txt")) + fmt.Println(filepath.Dir("../todo.txt")) + fmt.Println(filepath.Dir("..")) + fmt.Println(filepath.Dir(".")) + fmt.Println(filepath.Dir("/")) + fmt.Println(filepath.Dir("")) + + // Output: + // On Unix: + // /foo/bar + // /foo/bar + // /foo/bar/baz + // /dirty/path + // . + // .. + // . + // . + // / + // . +} + +func ExampleIsAbs() { + fmt.Println("On Unix:") + fmt.Println(filepath.IsAbs("/home/gopher")) + fmt.Println(filepath.IsAbs(".bashrc")) + fmt.Println(filepath.IsAbs("..")) + fmt.Println(filepath.IsAbs(".")) + fmt.Println(filepath.IsAbs("/")) + fmt.Println(filepath.IsAbs("")) + + // Output: + // On Unix: + // true + // false + // false + // false + // true + // false +} diff --git a/src/path/filepath/example_unix_walk_test.go b/src/path/filepath/example_unix_walk_test.go new file mode 100644 index 0000000..c8a818f --- /dev/null +++ b/src/path/filepath/example_unix_walk_test.go @@ -0,0 +1,66 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build !windows,!plan9 + +package filepath_test + +import ( + "fmt" + "io/fs" + "os" + "path/filepath" +) + +func prepareTestDirTree(tree string) (string, error) { + tmpDir, err := os.MkdirTemp("", "") + if err != nil { + return "", fmt.Errorf("error creating temp directory: %v\n", err) + } + + err = os.MkdirAll(filepath.Join(tmpDir, tree), 0755) + if err != nil { + os.RemoveAll(tmpDir) + return "", err + } + + return tmpDir, nil +} + +func ExampleWalk() { + tmpDir, err := prepareTestDirTree("dir/to/walk/skip") + if err != nil { + fmt.Printf("unable to create test dir tree: %v\n", err) + return + } + defer os.RemoveAll(tmpDir) + os.Chdir(tmpDir) + + subDirToSkip := "skip" + + fmt.Println("On Unix:") + err = filepath.Walk(".", func(path string, info fs.FileInfo, err error) error { + if err != nil { + fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err) + return err + } + if info.IsDir() && info.Name() == subDirToSkip { + fmt.Printf("skipping a dir without errors: %+v \n", info.Name()) + return filepath.SkipDir + } + fmt.Printf("visited file or dir: %q\n", path) + return nil + }) + if err != nil { + fmt.Printf("error walking the path %q: %v\n", tmpDir, err) + return + } + // Output: + // On Unix: + // visited file or dir: "." + // visited file or dir: "dir" + // visited file or dir: "dir/to" + // visited file or dir: "dir/to/walk" + // skipping a dir without errors: skip +} diff --git a/src/path/filepath/export_test.go b/src/path/filepath/export_test.go new file mode 100644 index 0000000..0cf9e3b --- /dev/null +++ b/src/path/filepath/export_test.go @@ -0,0 +1,7 @@ +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath + +var LstatP = &lstat diff --git a/src/path/filepath/export_windows_test.go b/src/path/filepath/export_windows_test.go new file mode 100644 index 0000000..a7e2e64 --- /dev/null +++ b/src/path/filepath/export_windows_test.go @@ -0,0 +1,10 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath + +var ( + ToNorm = toNorm + NormBase = normBase +) diff --git a/src/path/filepath/match.go b/src/path/filepath/match.go new file mode 100644 index 0000000..c77a269 --- /dev/null +++ b/src/path/filepath/match.go @@ -0,0 +1,360 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath + +import ( + "errors" + "os" + "runtime" + "sort" + "strings" + "unicode/utf8" +) + +// ErrBadPattern indicates a pattern was malformed. +var ErrBadPattern = errors.New("syntax error in pattern") + +// Match reports whether name matches the shell file name pattern. +// The pattern syntax is: +// +// pattern: +// { term } +// term: +// '*' matches any sequence of non-Separator characters +// '?' matches any single non-Separator character +// '[' [ '^' ] { character-range } ']' +// character class (must be non-empty) +// c matches character c (c != '*', '?', '\\', '[') +// '\\' c matches character c +// +// character-range: +// c matches character c (c != '\\', '-', ']') +// '\\' c matches character c +// lo '-' hi matches character c for lo <= c <= hi +// +// Match requires pattern to match all of name, not just a substring. +// The only possible returned error is ErrBadPattern, when pattern +// is malformed. +// +// On Windows, escaping is disabled. Instead, '\\' is treated as +// path separator. +// +func Match(pattern, name string) (matched bool, err error) { +Pattern: + for len(pattern) > 0 { + var star bool + var chunk string + star, chunk, pattern = scanChunk(pattern) + if star && chunk == "" { + // Trailing * matches rest of string unless it has a /. + return !strings.Contains(name, string(Separator)), nil + } + // Look for match at current position. + t, ok, err := matchChunk(chunk, name) + // if we're the last chunk, make sure we've exhausted the name + // otherwise we'll give a false result even if we could still match + // using the star + if ok && (len(t) == 0 || len(pattern) > 0) { + name = t + continue + } + if err != nil { + return false, err + } + if star { + // Look for match skipping i+1 bytes. + // Cannot skip /. + for i := 0; i < len(name) && name[i] != Separator; i++ { + t, ok, err := matchChunk(chunk, name[i+1:]) + if ok { + // if we're the last chunk, make sure we exhausted the name + if len(pattern) == 0 && len(t) > 0 { + continue + } + name = t + continue Pattern + } + if err != nil { + return false, err + } + } + } + return false, nil + } + return len(name) == 0, nil +} + +// scanChunk gets the next segment of pattern, which is a non-star string +// possibly preceded by a star. +func scanChunk(pattern string) (star bool, chunk, rest string) { + for len(pattern) > 0 && pattern[0] == '*' { + pattern = pattern[1:] + star = true + } + inrange := false + var i int +Scan: + for i = 0; i < len(pattern); i++ { + switch pattern[i] { + case '\\': + if runtime.GOOS != "windows" { + // error check handled in matchChunk: bad pattern. + if i+1 < len(pattern) { + i++ + } + } + case '[': + inrange = true + case ']': + inrange = false + case '*': + if !inrange { + break Scan + } + } + } + return star, pattern[0:i], pattern[i:] +} + +// matchChunk checks whether chunk matches the beginning of s. +// If so, it returns the remainder of s (after the match). +// Chunk is all single-character operators: literals, char classes, and ?. +func matchChunk(chunk, s string) (rest string, ok bool, err error) { + // failed records whether the match has failed. + // After the match fails, the loop continues on processing chunk, + // checking that the pattern is well-formed but no longer reading s. + failed := false + for len(chunk) > 0 { + if !failed && len(s) == 0 { + failed = true + } + switch chunk[0] { + case '[': + // character class + var r rune + if !failed { + var n int + r, n = utf8.DecodeRuneInString(s) + s = s[n:] + } + chunk = chunk[1:] + // possibly negated + negated := false + if len(chunk) > 0 && chunk[0] == '^' { + negated = true + chunk = chunk[1:] + } + // parse all ranges + match := false + nrange := 0 + for { + if len(chunk) > 0 && chunk[0] == ']' && nrange > 0 { + chunk = chunk[1:] + break + } + var lo, hi rune + if lo, chunk, err = getEsc(chunk); err != nil { + return "", false, err + } + hi = lo + if chunk[0] == '-' { + if hi, chunk, err = getEsc(chunk[1:]); err != nil { + return "", false, err + } + } + if lo <= r && r <= hi { + match = true + } + nrange++ + } + if match == negated { + failed = true + } + + case '?': + if !failed { + if s[0] == Separator { + failed = true + } + _, n := utf8.DecodeRuneInString(s) + s = s[n:] + } + chunk = chunk[1:] + + case '\\': + if runtime.GOOS != "windows" { + chunk = chunk[1:] + if len(chunk) == 0 { + return "", false, ErrBadPattern + } + } + fallthrough + + default: + if !failed { + if chunk[0] != s[0] { + failed = true + } + s = s[1:] + } + chunk = chunk[1:] + } + } + if failed { + return "", false, nil + } + return s, true, nil +} + +// getEsc gets a possibly-escaped character from chunk, for a character class. +func getEsc(chunk string) (r rune, nchunk string, err error) { + if len(chunk) == 0 || chunk[0] == '-' || chunk[0] == ']' { + err = ErrBadPattern + return + } + if chunk[0] == '\\' && runtime.GOOS != "windows" { + chunk = chunk[1:] + if len(chunk) == 0 { + err = ErrBadPattern + return + } + } + r, n := utf8.DecodeRuneInString(chunk) + if r == utf8.RuneError && n == 1 { + err = ErrBadPattern + } + nchunk = chunk[n:] + if len(nchunk) == 0 { + err = ErrBadPattern + } + return +} + +// Glob returns the names of all files matching pattern or nil +// if there is no matching file. The syntax of patterns is the same +// as in Match. The pattern may describe hierarchical names such as +// /usr/*/bin/ed (assuming the Separator is '/'). +// +// Glob ignores file system errors such as I/O errors reading directories. +// The only possible returned error is ErrBadPattern, when pattern +// is malformed. +func Glob(pattern string) (matches []string, err error) { + // Check pattern is well-formed. + if _, err := Match(pattern, ""); err != nil { + return nil, err + } + if !hasMeta(pattern) { + if _, err = os.Lstat(pattern); err != nil { + return nil, nil + } + return []string{pattern}, nil + } + + dir, file := Split(pattern) + volumeLen := 0 + if runtime.GOOS == "windows" { + volumeLen, dir = cleanGlobPathWindows(dir) + } else { + dir = cleanGlobPath(dir) + } + + if !hasMeta(dir[volumeLen:]) { + return glob(dir, file, nil) + } + + // Prevent infinite recursion. See issue 15879. + if dir == pattern { + return nil, ErrBadPattern + } + + var m []string + m, err = Glob(dir) + if err != nil { + return + } + for _, d := range m { + matches, err = glob(d, file, matches) + if err != nil { + return + } + } + return +} + +// cleanGlobPath prepares path for glob matching. +func cleanGlobPath(path string) string { + switch path { + case "": + return "." + case string(Separator): + // do nothing to the path + return path + default: + return path[0 : len(path)-1] // chop off trailing separator + } +} + +// cleanGlobPathWindows is windows version of cleanGlobPath. +func cleanGlobPathWindows(path string) (prefixLen int, cleaned string) { + vollen := volumeNameLen(path) + switch { + case path == "": + return 0, "." + case vollen+1 == len(path) && os.IsPathSeparator(path[len(path)-1]): // /, \, C:\ and C:/ + // do nothing to the path + return vollen + 1, path + case vollen == len(path) && len(path) == 2: // C: + return vollen, path + "." // convert C: into C:. + default: + if vollen >= len(path) { + vollen = len(path) - 1 + } + return vollen, path[0 : len(path)-1] // chop off trailing separator + } +} + +// glob searches for files matching pattern in the directory dir +// and appends them to matches. If the directory cannot be +// opened, it returns the existing matches. New matches are +// added in lexicographical order. +func glob(dir, pattern string, matches []string) (m []string, e error) { + m = matches + fi, err := os.Stat(dir) + if err != nil { + return // ignore I/O error + } + if !fi.IsDir() { + return // ignore I/O error + } + d, err := os.Open(dir) + if err != nil { + return // ignore I/O error + } + defer d.Close() + + names, _ := d.Readdirnames(-1) + sort.Strings(names) + + for _, n := range names { + matched, err := Match(pattern, n) + if err != nil { + return m, err + } + if matched { + m = append(m, Join(dir, n)) + } + } + return +} + +// hasMeta reports whether path contains any of the magic characters +// recognized by Match. +func hasMeta(path string) bool { + magicChars := `*?[` + if runtime.GOOS != "windows" { + magicChars = `*?[\` + } + return strings.ContainsAny(path, magicChars) +} diff --git a/src/path/filepath/match_test.go b/src/path/filepath/match_test.go new file mode 100644 index 0000000..48880ea --- /dev/null +++ b/src/path/filepath/match_test.go @@ -0,0 +1,392 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath_test + +import ( + "fmt" + "internal/testenv" + "os" + . "path/filepath" + "reflect" + "runtime" + "sort" + "strings" + "testing" +) + +type MatchTest struct { + pattern, s string + match bool + err error +} + +var matchTests = []MatchTest{ + {"abc", "abc", true, nil}, + {"*", "abc", true, nil}, + {"*c", "abc", true, nil}, + {"a*", "a", true, nil}, + {"a*", "abc", true, nil}, + {"a*", "ab/c", false, nil}, + {"a*/b", "abc/b", true, nil}, + {"a*/b", "a/c/b", false, nil}, + {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil}, + {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil}, + {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil}, + {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil}, + {"a*b?c*x", "abxbbxdbxebxczzx", true, nil}, + {"a*b?c*x", "abxbbxdbxebxczzy", false, nil}, + {"ab[c]", "abc", true, nil}, + {"ab[b-d]", "abc", true, nil}, + {"ab[e-g]", "abc", false, nil}, + {"ab[^c]", "abc", false, nil}, + {"ab[^b-d]", "abc", false, nil}, + {"ab[^e-g]", "abc", true, nil}, + {"a\\*b", "a*b", true, nil}, + {"a\\*b", "ab", false, nil}, + {"a?b", "a☺b", true, nil}, + {"a[^a]b", "a☺b", true, nil}, + {"a???b", "a☺b", false, nil}, + {"a[^a][^a][^a]b", "a☺b", false, nil}, + {"[a-ζ]*", "α", true, nil}, + {"*[a-ζ]", "A", false, nil}, + {"a?b", "a/b", false, nil}, + {"a*b", "a/b", false, nil}, + {"[\\]a]", "]", true, nil}, + {"[\\-]", "-", true, nil}, + {"[x\\-]", "x", true, nil}, + {"[x\\-]", "-", true, nil}, + {"[x\\-]", "z", false, nil}, + {"[\\-x]", "x", true, nil}, + {"[\\-x]", "-", true, nil}, + {"[\\-x]", "a", false, nil}, + {"[]a]", "]", false, ErrBadPattern}, + {"[-]", "-", false, ErrBadPattern}, + {"[x-]", "x", false, ErrBadPattern}, + {"[x-]", "-", false, ErrBadPattern}, + {"[x-]", "z", false, ErrBadPattern}, + {"[-x]", "x", false, ErrBadPattern}, + {"[-x]", "-", false, ErrBadPattern}, + {"[-x]", "a", false, ErrBadPattern}, + {"\\", "a", false, ErrBadPattern}, + {"[a-b-c]", "a", false, ErrBadPattern}, + {"[", "a", false, ErrBadPattern}, + {"[^", "a", false, ErrBadPattern}, + {"[^bc", "a", false, ErrBadPattern}, + {"a[", "a", false, ErrBadPattern}, + {"a[", "ab", false, ErrBadPattern}, + {"a[", "x", false, ErrBadPattern}, + {"a/b[", "x", false, ErrBadPattern}, + {"*x", "xxx", true, nil}, +} + +func errp(e error) string { + if e == nil { + return "<nil>" + } + return e.Error() +} + +func TestMatch(t *testing.T) { + for _, tt := range matchTests { + pattern := tt.pattern + s := tt.s + if runtime.GOOS == "windows" { + if strings.Contains(pattern, "\\") { + // no escape allowed on windows. + continue + } + pattern = Clean(pattern) + s = Clean(s) + } + ok, err := Match(pattern, s) + if ok != tt.match || err != tt.err { + t.Errorf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err)) + } + } +} + +// contains reports whether vector contains the string s. +func contains(vector []string, s string) bool { + for _, elem := range vector { + if elem == s { + return true + } + } + return false +} + +var globTests = []struct { + pattern, result string +}{ + {"match.go", "match.go"}, + {"mat?h.go", "match.go"}, + {"*", "match.go"}, + {"../*/match.go", "../filepath/match.go"}, +} + +func TestGlob(t *testing.T) { + for _, tt := range globTests { + pattern := tt.pattern + result := tt.result + if runtime.GOOS == "windows" { + pattern = Clean(pattern) + result = Clean(result) + } + matches, err := Glob(pattern) + if err != nil { + t.Errorf("Glob error for %q: %s", pattern, err) + continue + } + if !contains(matches, result) { + t.Errorf("Glob(%#q) = %#v want %v", pattern, matches, result) + } + } + for _, pattern := range []string{"no_match", "../*/no_match"} { + matches, err := Glob(pattern) + if err != nil { + t.Errorf("Glob error for %q: %s", pattern, err) + continue + } + if len(matches) != 0 { + t.Errorf("Glob(%#q) = %#v want []", pattern, matches) + } + } +} + +func TestGlobError(t *testing.T) { + bad := []string{`[]`, `nonexist/[]`} + for _, pattern := range bad { + if _, err := Glob(pattern); err != ErrBadPattern { + t.Errorf("Glob(%#q) returned err=%v, want ErrBadPattern", pattern, err) + } + } +} + +func TestGlobUNC(t *testing.T) { + // Just make sure this runs without crashing for now. + // See issue 15879. + Glob(`\\?\C:\*`) +} + +var globSymlinkTests = []struct { + path, dest string + brokenLink bool +}{ + {"test1", "link1", false}, + {"test2", "link2", true}, +} + +func TestGlobSymlink(t *testing.T) { + testenv.MustHaveSymlink(t) + + tmpDir, err := os.MkdirTemp("", "globsymlink") + if err != nil { + t.Fatal("creating temp dir:", err) + } + defer os.RemoveAll(tmpDir) + + for _, tt := range globSymlinkTests { + path := Join(tmpDir, tt.path) + dest := Join(tmpDir, tt.dest) + f, err := os.Create(path) + if err != nil { + t.Fatal(err) + } + if err := f.Close(); err != nil { + t.Fatal(err) + } + err = os.Symlink(path, dest) + if err != nil { + t.Fatal(err) + } + if tt.brokenLink { + // Break the symlink. + os.Remove(path) + } + matches, err := Glob(dest) + if err != nil { + t.Errorf("GlobSymlink error for %q: %s", dest, err) + } + if !contains(matches, dest) { + t.Errorf("Glob(%#q) = %#v want %v", dest, matches, dest) + } + } +} + +type globTest struct { + pattern string + matches []string +} + +func (test *globTest) buildWant(root string) []string { + want := make([]string, 0) + for _, m := range test.matches { + want = append(want, root+FromSlash(m)) + } + sort.Strings(want) + return want +} + +func (test *globTest) globAbs(root, rootPattern string) error { + p := FromSlash(rootPattern + `\` + test.pattern) + have, err := Glob(p) + if err != nil { + return err + } + sort.Strings(have) + want := test.buildWant(root + `\`) + if strings.Join(want, "_") == strings.Join(have, "_") { + return nil + } + return fmt.Errorf("Glob(%q) returns %q, but %q expected", p, have, want) +} + +func (test *globTest) globRel(root string) error { + p := root + FromSlash(test.pattern) + have, err := Glob(p) + if err != nil { + return err + } + sort.Strings(have) + want := test.buildWant(root) + if strings.Join(want, "_") == strings.Join(have, "_") { + return nil + } + // try also matching version without root prefix + wantWithNoRoot := test.buildWant("") + if strings.Join(wantWithNoRoot, "_") == strings.Join(have, "_") { + return nil + } + return fmt.Errorf("Glob(%q) returns %q, but %q expected", p, have, want) +} + +func TestWindowsGlob(t *testing.T) { + if runtime.GOOS != "windows" { + t.Skipf("skipping windows specific test") + } + + tmpDir, err := os.MkdirTemp("", "TestWindowsGlob") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // /tmp may itself be a symlink + tmpDir, err = EvalSymlinks(tmpDir) + if err != nil { + t.Fatal("eval symlink for tmp dir:", err) + } + + if len(tmpDir) < 3 { + t.Fatalf("tmpDir path %q is too short", tmpDir) + } + if tmpDir[1] != ':' { + t.Fatalf("tmpDir path %q must have drive letter in it", tmpDir) + } + + dirs := []string{ + "a", + "b", + "dir/d/bin", + } + files := []string{ + "dir/d/bin/git.exe", + } + for _, dir := range dirs { + err := os.MkdirAll(Join(tmpDir, dir), 0777) + if err != nil { + t.Fatal(err) + } + } + for _, file := range files { + err := os.WriteFile(Join(tmpDir, file), nil, 0666) + if err != nil { + t.Fatal(err) + } + } + + tests := []globTest{ + {"a", []string{"a"}}, + {"b", []string{"b"}}, + {"c", []string{}}, + {"*", []string{"a", "b", "dir"}}, + {"d*", []string{"dir"}}, + {"*i*", []string{"dir"}}, + {"*r", []string{"dir"}}, + {"?ir", []string{"dir"}}, + {"?r", []string{}}, + {"d*/*/bin/git.exe", []string{"dir/d/bin/git.exe"}}, + } + + // test absolute paths + for _, test := range tests { + var p string + err = test.globAbs(tmpDir, tmpDir) + if err != nil { + t.Error(err) + } + // test C:\*Documents and Settings\... + p = tmpDir + p = strings.Replace(p, `:\`, `:\*`, 1) + err = test.globAbs(tmpDir, p) + if err != nil { + t.Error(err) + } + // test C:\Documents and Settings*\... + p = tmpDir + p = strings.Replace(p, `:\`, `:`, 1) + p = strings.Replace(p, `\`, `*\`, 1) + p = strings.Replace(p, `:`, `:\`, 1) + err = test.globAbs(tmpDir, p) + if err != nil { + t.Error(err) + } + } + + // test relative paths + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + err = os.Chdir(tmpDir) + if err != nil { + t.Fatal(err) + } + defer func() { + err := os.Chdir(wd) + if err != nil { + t.Fatal(err) + } + }() + for _, test := range tests { + err := test.globRel("") + if err != nil { + t.Error(err) + } + err = test.globRel(`.\`) + if err != nil { + t.Error(err) + } + err = test.globRel(tmpDir[:2]) // C: + if err != nil { + t.Error(err) + } + } +} + +func TestNonWindowsGlobEscape(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skipf("skipping non-windows specific test") + } + pattern := `\match.go` + want := []string{"match.go"} + matches, err := Glob(pattern) + if err != nil { + t.Fatalf("Glob error for %q: %s", pattern, err) + } + if !reflect.DeepEqual(matches, want) { + t.Fatalf("Glob(%#q) = %v want %v", pattern, matches, want) + } +} diff --git a/src/path/filepath/path.go b/src/path/filepath/path.go new file mode 100644 index 0000000..2e7b439 --- /dev/null +++ b/src/path/filepath/path.go @@ -0,0 +1,596 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package filepath implements utility routines for manipulating filename paths +// in a way compatible with the target operating system-defined file paths. +// +// The filepath package uses either forward slashes or backslashes, +// depending on the operating system. To process paths such as URLs +// that always use forward slashes regardless of the operating +// system, see the path package. +package filepath + +import ( + "errors" + "io/fs" + "os" + "sort" + "strings" +) + +// A lazybuf is a lazily constructed path buffer. +// It supports append, reading previously appended bytes, +// and retrieving the final string. It does not allocate a buffer +// to hold the output until that output diverges from s. +type lazybuf struct { + path string + buf []byte + w int + volAndPath string + volLen int +} + +func (b *lazybuf) index(i int) byte { + if b.buf != nil { + return b.buf[i] + } + return b.path[i] +} + +func (b *lazybuf) append(c byte) { + if b.buf == nil { + if b.w < len(b.path) && b.path[b.w] == c { + b.w++ + return + } + b.buf = make([]byte, len(b.path)) + copy(b.buf, b.path[:b.w]) + } + b.buf[b.w] = c + b.w++ +} + +func (b *lazybuf) string() string { + if b.buf == nil { + return b.volAndPath[:b.volLen+b.w] + } + return b.volAndPath[:b.volLen] + string(b.buf[:b.w]) +} + +const ( + Separator = os.PathSeparator + ListSeparator = os.PathListSeparator +) + +// Clean returns the shortest path name equivalent to path +// by purely lexical processing. It applies the following rules +// iteratively until no further processing can be done: +// +// 1. Replace multiple Separator elements with a single one. +// 2. Eliminate each . path name element (the current directory). +// 3. Eliminate each inner .. path name element (the parent directory) +// along with the non-.. element that precedes it. +// 4. Eliminate .. elements that begin a rooted path: +// that is, replace "/.." by "/" at the beginning of a path, +// assuming Separator is '/'. +// +// The returned path ends in a slash only if it represents a root directory, +// such as "/" on Unix or `C:\` on Windows. +// +// Finally, any occurrences of slash are replaced by Separator. +// +// If the result of this process is an empty string, Clean +// returns the string ".". +// +// See also Rob Pike, ``Lexical File Names in Plan 9 or +// Getting Dot-Dot Right,'' +// https://9p.io/sys/doc/lexnames.html +func Clean(path string) string { + originalPath := path + volLen := volumeNameLen(path) + path = path[volLen:] + if path == "" { + if volLen > 1 && originalPath[1] != ':' { + // should be UNC + return FromSlash(originalPath) + } + return originalPath + "." + } + 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 + // 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 { + out.append(Separator) + r, dotdot = 1, 1 + } + + for r < n { + switch { + case os.IsPathSeparator(path[r]): + // empty path element + r++ + case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])): + // . element + r++ + case path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])): + // .. element: remove to last separator + r += 2 + switch { + case out.w > dotdot: + // can backtrack + out.w-- + for out.w > dotdot && !os.IsPathSeparator(out.index(out.w)) { + out.w-- + } + case !rooted: + // cannot backtrack, but not rooted, so append .. element. + if out.w > 0 { + out.append(Separator) + } + out.append('.') + out.append('.') + dotdot = out.w + } + default: + // real path element. + // add slash if needed + if rooted && out.w != 1 || !rooted && out.w != 0 { + out.append(Separator) + } + // copy element + for ; r < n && !os.IsPathSeparator(path[r]); r++ { + out.append(path[r]) + } + } + } + + // Turn empty string into "." + if out.w == 0 { + out.append('.') + } + + return FromSlash(out.string()) +} + +// ToSlash returns the result of replacing each separator character +// in path with a slash ('/') character. Multiple separators are +// replaced by multiple slashes. +func ToSlash(path string) string { + if Separator == '/' { + return path + } + return strings.ReplaceAll(path, string(Separator), "/") +} + +// FromSlash returns the result of replacing each slash ('/') character +// in path with a separator character. Multiple slashes are replaced +// by multiple separators. +func FromSlash(path string) string { + if Separator == '/' { + return path + } + return strings.ReplaceAll(path, "/", string(Separator)) +} + +// SplitList splits a list of paths joined by the OS-specific ListSeparator, +// usually found in PATH or GOPATH environment variables. +// Unlike strings.Split, SplitList returns an empty slice when passed an empty +// string. +func SplitList(path string) []string { + return splitList(path) +} + +// Split splits path immediately following the final Separator, +// separating it into a directory and file name component. +// If there is no Separator in path, Split returns an empty dir +// and file set to path. +// The returned values have the property that path = dir+file. +func Split(path string) (dir, file string) { + vol := VolumeName(path) + i := len(path) - 1 + for i >= len(vol) && !os.IsPathSeparator(path[i]) { + i-- + } + return path[:i+1], path[i+1:] +} + +// Join joins any number of path elements into a single path, +// separating them with an OS specific Separator. Empty elements +// are ignored. The result is Cleaned. However, if the argument +// list is empty or all its elements are empty, Join returns +// an empty string. +// On Windows, the result will only be a UNC path if the first +// non-empty element is a UNC path. +func Join(elem ...string) string { + return join(elem) +} + +// Ext returns the file name extension used by path. +// The extension is the suffix beginning at the final dot +// in the final element of path; it is empty if there is +// no dot. +func Ext(path string) string { + for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- { + if path[i] == '.' { + return path[i:] + } + } + return "" +} + +// EvalSymlinks returns the path name after the evaluation of any symbolic +// links. +// If path is relative the result will be relative to the current directory, +// unless one of the components is an absolute symbolic link. +// EvalSymlinks calls Clean on the result. +func EvalSymlinks(path string) (string, error) { + return evalSymlinks(path) +} + +// Abs returns an absolute representation of path. +// If the path is not absolute it will be joined with the current +// working directory to turn it into an absolute path. The absolute +// path name for a given file is not guaranteed to be unique. +// Abs calls Clean on the result. +func Abs(path string) (string, error) { + return abs(path) +} + +func unixAbs(path string) (string, error) { + if IsAbs(path) { + return Clean(path), nil + } + wd, err := os.Getwd() + if err != nil { + return "", err + } + return Join(wd, path), nil +} + +// Rel returns a relative path that is lexically equivalent to targpath when +// joined to basepath with an intervening separator. That is, +// Join(basepath, Rel(basepath, targpath)) is equivalent to targpath itself. +// On success, the returned path will always be relative to basepath, +// even if basepath and targpath share no elements. +// An error is returned if targpath can't be made relative to basepath or if +// knowing the current working directory would be necessary to compute it. +// Rel calls Clean on the result. +func Rel(basepath, targpath string) (string, error) { + baseVol := VolumeName(basepath) + targVol := VolumeName(targpath) + base := Clean(basepath) + targ := Clean(targpath) + if sameWord(targ, base) { + return ".", nil + } + base = base[len(baseVol):] + targ = targ[len(targVol):] + if base == "." { + base = "" + } + // Can't use IsAbs - `\a` and `a` are both relative in Windows. + baseSlashed := len(base) > 0 && base[0] == Separator + targSlashed := len(targ) > 0 && targ[0] == Separator + if baseSlashed != targSlashed || !sameWord(baseVol, targVol) { + return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath) + } + // Position base[b0:bi] and targ[t0:ti] at the first differing elements. + bl := len(base) + tl := len(targ) + var b0, bi, t0, ti int + for { + for bi < bl && base[bi] != Separator { + bi++ + } + for ti < tl && targ[ti] != Separator { + ti++ + } + if !sameWord(targ[t0:ti], base[b0:bi]) { + break + } + if bi < bl { + bi++ + } + if ti < tl { + ti++ + } + b0 = bi + t0 = ti + } + if base[b0:bi] == ".." { + return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath) + } + if b0 != bl { + // Base elements left. Must go up before going down. + seps := strings.Count(base[b0:bl], string(Separator)) + size := 2 + seps*3 + if tl != t0 { + size += 1 + tl - t0 + } + buf := make([]byte, size) + n := copy(buf, "..") + for i := 0; i < seps; i++ { + buf[n] = Separator + copy(buf[n+1:], "..") + n += 3 + } + if t0 != tl { + buf[n] = Separator + copy(buf[n+1:], targ[t0:]) + } + return string(buf), nil + } + return targ[t0:], nil +} + +// SkipDir is used as a return value from WalkFuncs to indicate that +// the directory named in the call is to be skipped. It is not returned +// as an error by any function. +var SkipDir error = fs.SkipDir + +// WalkFunc is the type of the function called by Walk to visit each each +// file or directory. +// +// The path argument contains the argument to Walk as a prefix. +// That is, if Walk is called with root argument "dir" and finds a file +// named "a" in that directory, the walk function will be called with +// argument "dir/a". +// +// The directory and file are joined with Join, which may clean the +// directory name: if Walk is called with the root argument "x/../dir" +// and finds a file named "a" in that directory, the walk function will +// be called with argument "dir/a", not "x/../dir/a". +// +// The info argument is the fs.FileInfo for the named path. +// +// The error result returned by the function controls how Walk continues. +// If the function returns the special value SkipDir, Walk skips the +// current directory (path if info.IsDir() is true, otherwise path's +// parent directory). Otherwise, if the function returns a non-nil error, +// Walk stops entirely and returns that error. +// +// The err argument reports an error related to path, signaling that Walk +// will not walk into that directory. The function can decide how to +// handle that error; as described earlier, returning the error will +// cause Walk to stop walking the entire tree. +// +// Walk calls the function with a non-nil err argument in two cases. +// +// First, if an os.Lstat on the root directory or any directory or file +// in the tree fails, Walk calls the function with path set to that +// directory or file's path, info set to nil, and err set to the error +// from os.Lstat. +// +// Second, if a directory's Readdirnames method fails, Walk calls the +// function with path set to the directory's path, info, set to an +// fs.FileInfo describing the directory, and err set to the error from +// Readdirnames. +type WalkFunc func(path string, info fs.FileInfo, err error) error + +var lstat = os.Lstat // for testing + +// walkDir recursively descends path, calling walkDirFn. +func walkDir(path string, d fs.DirEntry, walkDirFn fs.WalkDirFunc) error { + if err := walkDirFn(path, d, nil); err != nil || !d.IsDir() { + if err == SkipDir && d.IsDir() { + // Successfully skipped directory. + err = nil + } + return err + } + + dirs, err := readDir(path) + if err != nil { + // Second call, to report ReadDir error. + err = walkDirFn(path, d, err) + if err != nil { + return err + } + } + + for _, d1 := range dirs { + path1 := Join(path, d1.Name()) + if err := walkDir(path1, d1, walkDirFn); err != nil { + if err == SkipDir { + break + } + return err + } + } + return nil +} + +// walk recursively descends path, calling walkFn. +func walk(path string, info fs.FileInfo, walkFn WalkFunc) error { + if !info.IsDir() { + return walkFn(path, info, nil) + } + + names, err := readDirNames(path) + err1 := walkFn(path, info, err) + // If err != nil, walk can't walk into this directory. + // err1 != nil means walkFn want walk to skip this directory or stop walking. + // Therefore, if one of err and err1 isn't nil, walk will return. + if err != nil || err1 != nil { + // The caller's behavior is controlled by the return value, which is decided + // by walkFn. walkFn may ignore err and return nil. + // If walkFn returns SkipDir, it will be handled by the caller. + // So walk should return whatever walkFn returns. + return err1 + } + + for _, name := range names { + filename := Join(path, name) + fileInfo, err := lstat(filename) + if err != nil { + if err := walkFn(filename, fileInfo, err); err != nil && err != SkipDir { + return err + } + } else { + err = walk(filename, fileInfo, walkFn) + if err != nil { + if !fileInfo.IsDir() || err != SkipDir { + return err + } + } + } + } + return nil +} + +// WalkDir walks the file tree rooted at root, calling fn for each file or +// directory in the tree, including root. +// +// All errors that arise visiting files and directories are filtered by fn: +// see the fs.WalkDirFunc documentation for details. +// +// The files are walked in lexical order, which makes the output deterministic +// but requires WalkDir to read an entire directory into memory before proceeding +// to walk that directory. +// +// WalkDir does not follow symbolic links. +func WalkDir(root string, fn fs.WalkDirFunc) error { + info, err := os.Lstat(root) + if err != nil { + err = fn(root, nil, err) + } else { + err = walkDir(root, &statDirEntry{info}, fn) + } + if err == SkipDir { + return nil + } + return err +} + +type statDirEntry struct { + info fs.FileInfo +} + +func (d *statDirEntry) Name() string { return d.info.Name() } +func (d *statDirEntry) IsDir() bool { return d.info.IsDir() } +func (d *statDirEntry) Type() fs.FileMode { return d.info.Mode().Type() } +func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil } + +// Walk walks the file tree rooted at root, calling fn for each file or +// directory in the tree, including root. +// +// All errors that arise visiting files and directories are filtered by fn: +// see the WalkFunc documentation for details. +// +// The files are walked in lexical order, which makes the output deterministic +// but requires Walk to read an entire directory into memory before proceeding +// to walk that directory. +// +// Walk does not follow symbolic links. +// +// Walk is less efficient than WalkDir, introduced in Go 1.16, +// which avoids calling os.Lstat on every visited file or directory. +func Walk(root string, fn WalkFunc) error { + info, err := os.Lstat(root) + if err != nil { + err = fn(root, nil, err) + } else { + err = walk(root, info, fn) + } + if err == SkipDir { + return nil + } + return err +} + +// readDir reads the directory named by dirname and returns +// a sorted list of directory entries. +func readDir(dirname string) ([]fs.DirEntry, error) { + f, err := os.Open(dirname) + if err != nil { + return nil, err + } + dirs, err := f.ReadDir(-1) + f.Close() + if err != nil { + return nil, err + } + sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() }) + return dirs, nil +} + +// readDirNames reads the directory named by dirname and returns +// a sorted list of directory entry names. +func readDirNames(dirname string) ([]string, error) { + f, err := os.Open(dirname) + if err != nil { + return nil, err + } + names, err := f.Readdirnames(-1) + f.Close() + if err != nil { + return nil, err + } + sort.Strings(names) + return names, nil +} + +// Base returns the last element of path. +// Trailing path separators are removed before extracting the last element. +// If the path is empty, Base returns ".". +// If the path consists entirely of separators, Base returns a single separator. +func Base(path string) string { + if path == "" { + return "." + } + // Strip trailing slashes. + for len(path) > 0 && os.IsPathSeparator(path[len(path)-1]) { + path = path[0 : len(path)-1] + } + // Throw away volume name + path = path[len(VolumeName(path)):] + // Find the last element + i := len(path) - 1 + for i >= 0 && !os.IsPathSeparator(path[i]) { + i-- + } + if i >= 0 { + path = path[i+1:] + } + // If empty now, it had only slashes. + if path == "" { + return string(Separator) + } + return path +} + +// Dir returns all but the last element of path, typically the path's directory. +// After dropping the final element, Dir calls Clean on the path and trailing +// slashes are removed. +// If the path is empty, Dir returns ".". +// If the path consists entirely of separators, Dir returns a single separator. +// The returned path does not end in a separator unless it is the root directory. +func Dir(path string) string { + vol := VolumeName(path) + i := len(path) - 1 + for i >= len(vol) && !os.IsPathSeparator(path[i]) { + i-- + } + dir := Clean(path[len(vol) : i+1]) + if dir == "." && len(vol) > 2 { + // must be UNC + return vol + } + return vol + dir +} + +// VolumeName returns leading volume name. +// Given "C:\foo\bar" it returns "C:" on Windows. +// Given "\\host\share\foo" it returns "\\host\share". +// On other platforms it returns "". +func VolumeName(path string) string { + return path[:volumeNameLen(path)] +} diff --git a/src/path/filepath/path_plan9.go b/src/path/filepath/path_plan9.go new file mode 100644 index 0000000..ec792fc --- /dev/null +++ b/src/path/filepath/path_plan9.go @@ -0,0 +1,51 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath + +import "strings" + +// IsAbs reports whether the path is absolute. +func IsAbs(path string) bool { + return strings.HasPrefix(path, "/") || strings.HasPrefix(path, "#") +} + +// volumeNameLen returns length of the leading volume name on Windows. +// It returns 0 elsewhere. +func volumeNameLen(path string) int { + return 0 +} + +// HasPrefix exists for historical compatibility and should not be used. +// +// Deprecated: HasPrefix does not respect path boundaries and +// does not ignore case when required. +func HasPrefix(p, prefix string) bool { + return strings.HasPrefix(p, prefix) +} + +func splitList(path string) []string { + if path == "" { + return []string{} + } + return strings.Split(path, string(ListSeparator)) +} + +func abs(path string) (string, error) { + return unixAbs(path) +} + +func join(elem []string) string { + // If there's a bug here, fix the logic in ./path_unix.go too. + for i, e := range elem { + if e != "" { + return Clean(strings.Join(elem[i:], string(Separator))) + } + } + return "" +} + +func sameWord(a, b string) bool { + return a == b +} diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go new file mode 100644 index 0000000..8616256 --- /dev/null +++ b/src/path/filepath/path_test.go @@ -0,0 +1,1537 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath_test + +import ( + "errors" + "fmt" + "internal/testenv" + "io/fs" + "os" + "path/filepath" + "reflect" + "runtime" + "sort" + "strings" + "syscall" + "testing" +) + +type PathTest struct { + path, result string +} + +var cleantests = []PathTest{ + // Already clean + {"abc", "abc"}, + {"abc/def", "abc/def"}, + {"a/b/c", "a/b/c"}, + {".", "."}, + {"..", ".."}, + {"../..", "../.."}, + {"../../abc", "../../abc"}, + {"/abc", "/abc"}, + {"/", "/"}, + + // Empty is current dir + {"", "."}, + + // Remove trailing slash + {"abc/", "abc"}, + {"abc/def/", "abc/def"}, + {"a/b/c/", "a/b/c"}, + {"./", "."}, + {"../", ".."}, + {"../../", "../.."}, + {"/abc/", "/abc"}, + + // Remove doubled slash + {"abc//def//ghi", "abc/def/ghi"}, + {"//abc", "/abc"}, + {"///abc", "/abc"}, + {"//abc//", "/abc"}, + {"abc//", "abc"}, + + // Remove . elements + {"abc/./def", "abc/def"}, + {"/./abc/def", "/abc/def"}, + {"abc/.", "abc"}, + + // Remove .. elements + {"abc/def/ghi/../jkl", "abc/def/jkl"}, + {"abc/def/../ghi/../jkl", "abc/jkl"}, + {"abc/def/..", "abc"}, + {"abc/def/../..", "."}, + {"/abc/def/../..", "/"}, + {"abc/def/../../..", ".."}, + {"/abc/def/../../..", "/"}, + {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"}, + {"/../abc", "/abc"}, + + // Combinations + {"abc/./../def", "def"}, + {"abc//./../def", "def"}, + {"abc/../../././../def", "../../def"}, +} + +var wincleantests = []PathTest{ + {`c:`, `c:.`}, + {`c:\`, `c:\`}, + {`c:\abc`, `c:\abc`}, + {`c:abc\..\..\.\.\..\def`, `c:..\..\def`}, + {`c:\abc\def\..\..`, `c:\`}, + {`c:\..\abc`, `c:\abc`}, + {`c:..\abc`, `c:..\abc`}, + {`\`, `\`}, + {`/`, `\`}, + {`\\i\..\c$`, `\c$`}, + {`\\i\..\i\c$`, `\i\c$`}, + {`\\i\..\I\c$`, `\I\c$`}, + {`\\host\share\foo\..\bar`, `\\host\share\bar`}, + {`//host/share/foo/../baz`, `\\host\share\baz`}, + {`\\a\b\..\c`, `\\a\b\c`}, + {`\\a\b`, `\\a\b`}, +} + +func TestClean(t *testing.T) { + tests := cleantests + if runtime.GOOS == "windows" { + for i := range tests { + tests[i].result = filepath.FromSlash(tests[i].result) + } + tests = append(tests, wincleantests...) + } + for _, test := range tests { + if s := filepath.Clean(test.path); s != test.result { + t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result) + } + if s := filepath.Clean(test.result); s != test.result { + t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result) + } + } + + if testing.Short() { + t.Skip("skipping malloc count in short mode") + } + if runtime.GOMAXPROCS(0) > 1 { + t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1") + return + } + + for _, test := range tests { + allocs := testing.AllocsPerRun(100, func() { filepath.Clean(test.result) }) + if allocs > 0 { + t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs) + } + } +} + +const sep = filepath.Separator + +var slashtests = []PathTest{ + {"", ""}, + {"/", string(sep)}, + {"/a/b", string([]byte{sep, 'a', sep, 'b'})}, + {"a//b", string([]byte{'a', sep, sep, 'b'})}, +} + +func TestFromAndToSlash(t *testing.T) { + for _, test := range slashtests { + if s := filepath.FromSlash(test.path); s != test.result { + t.Errorf("FromSlash(%q) = %q, want %q", test.path, s, test.result) + } + if s := filepath.ToSlash(test.result); s != test.path { + t.Errorf("ToSlash(%q) = %q, want %q", test.result, s, test.path) + } + } +} + +type SplitListTest struct { + list string + result []string +} + +const lsep = filepath.ListSeparator + +var splitlisttests = []SplitListTest{ + {"", []string{}}, + {string([]byte{'a', lsep, 'b'}), []string{"a", "b"}}, + {string([]byte{lsep, 'a', lsep, 'b'}), []string{"", "a", "b"}}, +} + +var winsplitlisttests = []SplitListTest{ + // quoted + {`"a"`, []string{`a`}}, + + // semicolon + {`";"`, []string{`;`}}, + {`"a;b"`, []string{`a;b`}}, + {`";";`, []string{`;`, ``}}, + {`;";"`, []string{``, `;`}}, + + // partially quoted + {`a";"b`, []string{`a;b`}}, + {`a; ""b`, []string{`a`, ` b`}}, + {`"a;b`, []string{`a;b`}}, + {`""a;b`, []string{`a`, `b`}}, + {`"""a;b`, []string{`a;b`}}, + {`""""a;b`, []string{`a`, `b`}}, + {`a";b`, []string{`a;b`}}, + {`a;b";c`, []string{`a`, `b;c`}}, + {`"a";b";c`, []string{`a`, `b;c`}}, +} + +func TestSplitList(t *testing.T) { + tests := splitlisttests + if runtime.GOOS == "windows" { + tests = append(tests, winsplitlisttests...) + } + for _, test := range tests { + if l := filepath.SplitList(test.list); !reflect.DeepEqual(l, test.result) { + t.Errorf("SplitList(%#q) = %#q, want %#q", test.list, l, test.result) + } + } +} + +type SplitTest struct { + path, dir, file string +} + +var unixsplittests = []SplitTest{ + {"a/b", "a/", "b"}, + {"a/b/", "a/b/", ""}, + {"a/", "a/", ""}, + {"a", "", "a"}, + {"/", "/", ""}, +} + +var winsplittests = []SplitTest{ + {`c:`, `c:`, ``}, + {`c:/`, `c:/`, ``}, + {`c:/foo`, `c:/`, `foo`}, + {`c:/foo/bar`, `c:/foo/`, `bar`}, + {`//host/share`, `//host/share`, ``}, + {`//host/share/`, `//host/share/`, ``}, + {`//host/share/foo`, `//host/share/`, `foo`}, + {`\\host\share`, `\\host\share`, ``}, + {`\\host\share\`, `\\host\share\`, ``}, + {`\\host\share\foo`, `\\host\share\`, `foo`}, +} + +func TestSplit(t *testing.T) { + var splittests []SplitTest + splittests = unixsplittests + if runtime.GOOS == "windows" { + splittests = append(splittests, winsplittests...) + } + for _, test := range splittests { + if d, f := filepath.Split(test.path); d != test.dir || f != test.file { + t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file) + } + } +} + +type JoinTest struct { + elem []string + path string +} + +var jointests = []JoinTest{ + // zero parameters + {[]string{}, ""}, + + // one parameter + {[]string{""}, ""}, + {[]string{"/"}, "/"}, + {[]string{"a"}, "a"}, + + // two parameters + {[]string{"a", "b"}, "a/b"}, + {[]string{"a", ""}, "a"}, + {[]string{"", "b"}, "b"}, + {[]string{"/", "a"}, "/a"}, + {[]string{"/", "a/b"}, "/a/b"}, + {[]string{"/", ""}, "/"}, + {[]string{"//", "a"}, "/a"}, + {[]string{"/a", "b"}, "/a/b"}, + {[]string{"a/", "b"}, "a/b"}, + {[]string{"a/", ""}, "a"}, + {[]string{"", ""}, ""}, + + // three parameters + {[]string{"/", "a", "b"}, "/a/b"}, +} + +var winjointests = []JoinTest{ + {[]string{`directory`, `file`}, `directory\file`}, + {[]string{`C:\Windows\`, `System32`}, `C:\Windows\System32`}, + {[]string{`C:\Windows\`, ``}, `C:\Windows`}, + {[]string{`C:\`, `Windows`}, `C:\Windows`}, + {[]string{`C:`, `a`}, `C:a`}, + {[]string{`C:`, `a\b`}, `C:a\b`}, + {[]string{`C:`, `a`, `b`}, `C:a\b`}, + {[]string{`C:`, ``, `b`}, `C:b`}, + {[]string{`C:`, ``, ``, `b`}, `C:b`}, + {[]string{`C:`, ``}, `C:.`}, + {[]string{`C:`, ``, ``}, `C:.`}, + {[]string{`C:.`, `a`}, `C:a`}, + {[]string{`C:a`, `b`}, `C:a\b`}, + {[]string{`C:a`, `b`, `d`}, `C:a\b\d`}, + {[]string{`\\host\share`, `foo`}, `\\host\share\foo`}, + {[]string{`\\host\share\foo`}, `\\host\share\foo`}, + {[]string{`//host/share`, `foo/bar`}, `\\host\share\foo\bar`}, + {[]string{`\`}, `\`}, + {[]string{`\`, ``}, `\`}, + {[]string{`\`, `a`}, `\a`}, + {[]string{`\\`, `a`}, `\a`}, + {[]string{`\`, `a`, `b`}, `\a\b`}, + {[]string{`\\`, `a`, `b`}, `\a\b`}, + {[]string{`\`, `\\a\b`, `c`}, `\a\b\c`}, + {[]string{`\\a`, `b`, `c`}, `\a\b\c`}, + {[]string{`\\a\`, `b`, `c`}, `\a\b\c`}, +} + +func TestJoin(t *testing.T) { + if runtime.GOOS == "windows" { + jointests = append(jointests, winjointests...) + } + for _, test := range jointests { + expected := filepath.FromSlash(test.path) + if p := filepath.Join(test.elem...); p != expected { + t.Errorf("join(%q) = %q, want %q", test.elem, p, expected) + } + } +} + +type ExtTest struct { + path, ext string +} + +var exttests = []ExtTest{ + {"path.go", ".go"}, + {"path.pb.go", ".go"}, + {"a.dir/b", ""}, + {"a.dir/b.go", ".go"}, + {"a.dir/", ""}, +} + +func TestExt(t *testing.T) { + for _, test := range exttests { + if x := filepath.Ext(test.path); x != test.ext { + t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext) + } + } +} + +type Node struct { + name string + entries []*Node // nil if the entry is a file + mark int +} + +var tree = &Node{ + "testdata", + []*Node{ + {"a", nil, 0}, + {"b", []*Node{}, 0}, + {"c", nil, 0}, + { + "d", + []*Node{ + {"x", nil, 0}, + {"y", []*Node{}, 0}, + { + "z", + []*Node{ + {"u", nil, 0}, + {"v", nil, 0}, + }, + 0, + }, + }, + 0, + }, + }, + 0, +} + +func walkTree(n *Node, path string, f func(path string, n *Node)) { + f(path, n) + for _, e := range n.entries { + walkTree(e, filepath.Join(path, e.name), f) + } +} + +func makeTree(t *testing.T) { + walkTree(tree, tree.name, func(path string, n *Node) { + if n.entries == nil { + fd, err := os.Create(path) + if err != nil { + t.Errorf("makeTree: %v", err) + return + } + fd.Close() + } else { + os.Mkdir(path, 0770) + } + }) +} + +func markTree(n *Node) { walkTree(n, "", func(path string, n *Node) { n.mark++ }) } + +func checkMarks(t *testing.T, report bool) { + walkTree(tree, tree.name, func(path string, n *Node) { + if n.mark != 1 && report { + t.Errorf("node %s mark = %d; expected 1", path, n.mark) + } + n.mark = 0 + }) +} + +// Assumes that each node name is unique. Good enough for a test. +// If clear is true, any incoming error is cleared before return. The errors +// are always accumulated, though. +func mark(d fs.DirEntry, err error, errors *[]error, clear bool) error { + name := d.Name() + walkTree(tree, tree.name, func(path string, n *Node) { + if n.name == name { + n.mark++ + } + }) + if err != nil { + *errors = append(*errors, err) + if clear { + return nil + } + return err + } + return nil +} + +func chtmpdir(t *testing.T) (restore func()) { + oldwd, err := os.Getwd() + if err != nil { + t.Fatalf("chtmpdir: %v", err) + } + d, err := os.MkdirTemp("", "test") + if err != nil { + t.Fatalf("chtmpdir: %v", err) + } + if err := os.Chdir(d); err != nil { + t.Fatalf("chtmpdir: %v", err) + } + return func() { + if err := os.Chdir(oldwd); err != nil { + t.Fatalf("chtmpdir: %v", err) + } + os.RemoveAll(d) + } +} + +func TestWalk(t *testing.T) { + walk := func(root string, fn fs.WalkDirFunc) error { + return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error { + return fn(path, &statDirEntry{info}, err) + }) + } + testWalk(t, walk, 1) +} + +type statDirEntry struct { + info fs.FileInfo +} + +func (d *statDirEntry) Name() string { return d.info.Name() } +func (d *statDirEntry) IsDir() bool { return d.info.IsDir() } +func (d *statDirEntry) Type() fs.FileMode { return d.info.Mode().Type() } +func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil } + +func TestWalkDir(t *testing.T) { + testWalk(t, filepath.WalkDir, 2) +} + +func testWalk(t *testing.T, walk func(string, fs.WalkDirFunc) error, errVisit int) { + if runtime.GOOS == "ios" { + restore := chtmpdir(t) + defer restore() + } + + tmpDir, err := os.MkdirTemp("", "TestWalk") + if err != nil { + t.Fatal("creating temp dir:", err) + } + defer os.RemoveAll(tmpDir) + + origDir, err := os.Getwd() + if err != nil { + t.Fatal("finding working dir:", err) + } + if err = os.Chdir(tmpDir); err != nil { + t.Fatal("entering temp dir:", err) + } + defer os.Chdir(origDir) + + makeTree(t) + errors := make([]error, 0, 10) + clear := true + markFn := func(path string, d fs.DirEntry, err error) error { + return mark(d, err, &errors, clear) + } + // Expect no errors. + err = walk(tree.name, markFn) + if err != nil { + t.Fatalf("no error expected, found: %s", err) + } + if len(errors) != 0 { + t.Fatalf("unexpected errors: %s", errors) + } + checkMarks(t, true) + errors = errors[0:0] + + t.Run("PermErr", func(t *testing.T) { + // Test permission errors. Only possible if we're not root + // and only on some file systems (AFS, FAT). To avoid errors during + // all.bash on those file systems, skip during go test -short. + if runtime.GOOS == "windows" { + t.Skip("skipping on Windows") + } + if os.Getuid() == 0 { + t.Skip("skipping as root") + } + if testing.Short() { + t.Skip("skipping in short mode") + } + + // introduce 2 errors: chmod top-level directories to 0 + os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0) + os.Chmod(filepath.Join(tree.name, tree.entries[3].name), 0) + + // 3) capture errors, expect two. + // mark respective subtrees manually + markTree(tree.entries[1]) + markTree(tree.entries[3]) + // correct double-marking of directory itself + tree.entries[1].mark -= errVisit + tree.entries[3].mark -= errVisit + err := walk(tree.name, markFn) + if err != nil { + t.Fatalf("expected no error return from Walk, got %s", err) + } + if len(errors) != 2 { + t.Errorf("expected 2 errors, got %d: %s", len(errors), errors) + } + // the inaccessible subtrees were marked manually + checkMarks(t, true) + errors = errors[0:0] + + // 4) capture errors, stop after first error. + // mark respective subtrees manually + markTree(tree.entries[1]) + markTree(tree.entries[3]) + // correct double-marking of directory itself + tree.entries[1].mark -= errVisit + tree.entries[3].mark -= errVisit + clear = false // error will stop processing + err = walk(tree.name, markFn) + if err == nil { + t.Fatalf("expected error return from Walk") + } + if len(errors) != 1 { + t.Errorf("expected 1 error, got %d: %s", len(errors), errors) + } + // the inaccessible subtrees were marked manually + checkMarks(t, false) + errors = errors[0:0] + + // restore permissions + os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0770) + os.Chmod(filepath.Join(tree.name, tree.entries[3].name), 0770) + }) +} + +func touch(t *testing.T, name string) { + f, err := os.Create(name) + if err != nil { + t.Fatal(err) + } + if err := f.Close(); err != nil { + t.Fatal(err) + } +} + +func TestWalkSkipDirOnFile(t *testing.T) { + td, err := os.MkdirTemp("", "walktest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(td) + + if err := os.MkdirAll(filepath.Join(td, "dir"), 0755); err != nil { + t.Fatal(err) + } + touch(t, filepath.Join(td, "dir/foo1")) + touch(t, filepath.Join(td, "dir/foo2")) + + sawFoo2 := false + walker := func(path string) error { + if strings.HasSuffix(path, "foo2") { + sawFoo2 = true + } + if strings.HasSuffix(path, "foo1") { + return filepath.SkipDir + } + return nil + } + walkFn := func(path string, _ fs.FileInfo, _ error) error { return walker(path) } + walkDirFn := func(path string, _ fs.DirEntry, _ error) error { return walker(path) } + + check := func(t *testing.T, walk func(root string) error, root string) { + t.Helper() + sawFoo2 = false + err = walk(root) + if err != nil { + t.Fatal(err) + } + if sawFoo2 { + t.Errorf("SkipDir on file foo1 did not block processing of foo2") + } + } + + t.Run("Walk", func(t *testing.T) { + Walk := func(root string) error { return filepath.Walk(td, walkFn) } + check(t, Walk, td) + check(t, Walk, filepath.Join(td, "dir")) + }) + t.Run("WalkDir", func(t *testing.T) { + WalkDir := func(root string) error { return filepath.WalkDir(td, walkDirFn) } + check(t, WalkDir, td) + check(t, WalkDir, filepath.Join(td, "dir")) + }) +} + +func TestWalkFileError(t *testing.T) { + td, err := os.MkdirTemp("", "walktest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(td) + + touch(t, filepath.Join(td, "foo")) + touch(t, filepath.Join(td, "bar")) + dir := filepath.Join(td, "dir") + if err := os.MkdirAll(filepath.Join(td, "dir"), 0755); err != nil { + t.Fatal(err) + } + touch(t, filepath.Join(dir, "baz")) + touch(t, filepath.Join(dir, "stat-error")) + defer func() { + *filepath.LstatP = os.Lstat + }() + statErr := errors.New("some stat error") + *filepath.LstatP = func(path string) (fs.FileInfo, error) { + if strings.HasSuffix(path, "stat-error") { + return nil, statErr + } + return os.Lstat(path) + } + got := map[string]error{} + err = filepath.Walk(td, func(path string, fi fs.FileInfo, err error) error { + rel, _ := filepath.Rel(td, path) + got[filepath.ToSlash(rel)] = err + return nil + }) + if err != nil { + t.Errorf("Walk error: %v", err) + } + want := map[string]error{ + ".": nil, + "foo": nil, + "bar": nil, + "dir": nil, + "dir/baz": nil, + "dir/stat-error": statErr, + } + if !reflect.DeepEqual(got, want) { + t.Errorf("Walked %#v; want %#v", got, want) + } +} + +var basetests = []PathTest{ + {"", "."}, + {".", "."}, + {"/.", "."}, + {"/", "/"}, + {"////", "/"}, + {"x/", "x"}, + {"abc", "abc"}, + {"abc/def", "def"}, + {"a/b/.x", ".x"}, + {"a/b/c.", "c."}, + {"a/b/c.x", "c.x"}, +} + +var winbasetests = []PathTest{ + {`c:\`, `\`}, + {`c:.`, `.`}, + {`c:\a\b`, `b`}, + {`c:a\b`, `b`}, + {`c:a\b\c`, `c`}, + {`\\host\share\`, `\`}, + {`\\host\share\a`, `a`}, + {`\\host\share\a\b`, `b`}, +} + +func TestBase(t *testing.T) { + tests := basetests + if runtime.GOOS == "windows" { + // make unix tests work on windows + for i := range tests { + tests[i].result = filepath.Clean(tests[i].result) + } + // add windows specific tests + tests = append(tests, winbasetests...) + } + for _, test := range tests { + if s := filepath.Base(test.path); s != test.result { + t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result) + } + } +} + +var dirtests = []PathTest{ + {"", "."}, + {".", "."}, + {"/.", "/"}, + {"/", "/"}, + {"////", "/"}, + {"/foo", "/"}, + {"x/", "x"}, + {"abc", "."}, + {"abc/def", "abc"}, + {"a/b/.x", "a/b"}, + {"a/b/c.", "a/b"}, + {"a/b/c.x", "a/b"}, +} + +var windirtests = []PathTest{ + {`c:\`, `c:\`}, + {`c:.`, `c:.`}, + {`c:\a\b`, `c:\a`}, + {`c:a\b`, `c:a`}, + {`c:a\b\c`, `c:a\b`}, + {`\\host\share`, `\\host\share`}, + {`\\host\share\`, `\\host\share\`}, + {`\\host\share\a`, `\\host\share\`}, + {`\\host\share\a\b`, `\\host\share\a`}, +} + +func TestDir(t *testing.T) { + tests := dirtests + if runtime.GOOS == "windows" { + // make unix tests work on windows + for i := range tests { + tests[i].result = filepath.Clean(tests[i].result) + } + // add windows specific tests + tests = append(tests, windirtests...) + } + for _, test := range tests { + if s := filepath.Dir(test.path); s != test.result { + t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result) + } + } +} + +type IsAbsTest struct { + path string + isAbs bool +} + +var isabstests = []IsAbsTest{ + {"", false}, + {"/", true}, + {"/usr/bin/gcc", true}, + {"..", false}, + {"/a/../bb", true}, + {".", false}, + {"./", false}, + {"lala", false}, +} + +var winisabstests = []IsAbsTest{ + {`C:\`, true}, + {`c\`, false}, + {`c::`, false}, + {`c:`, false}, + {`/`, false}, + {`\`, false}, + {`\Windows`, false}, + {`c:a\b`, false}, + {`c:\a\b`, true}, + {`c:/a/b`, true}, + {`\\host\share\foo`, true}, + {`//host/share/foo/bar`, true}, +} + +func TestIsAbs(t *testing.T) { + var tests []IsAbsTest + if runtime.GOOS == "windows" { + tests = append(tests, winisabstests...) + // All non-windows tests should fail, because they have no volume letter. + for _, test := range isabstests { + tests = append(tests, IsAbsTest{test.path, false}) + } + // All non-windows test should work as intended if prefixed with volume letter. + for _, test := range isabstests { + tests = append(tests, IsAbsTest{"c:" + test.path, test.isAbs}) + } + // Test reserved names. + tests = append(tests, IsAbsTest{os.DevNull, true}) + tests = append(tests, IsAbsTest{"NUL", true}) + tests = append(tests, IsAbsTest{"nul", true}) + tests = append(tests, IsAbsTest{"CON", true}) + } else { + tests = isabstests + } + + for _, test := range tests { + if r := filepath.IsAbs(test.path); r != test.isAbs { + t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs) + } + } +} + +type EvalSymlinksTest struct { + // If dest is empty, the path is created; otherwise the dest is symlinked to the path. + path, dest string +} + +var EvalSymlinksTestDirs = []EvalSymlinksTest{ + {"test", ""}, + {"test/dir", ""}, + {"test/dir/link3", "../../"}, + {"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{ + {"test", "test"}, + {"test/dir", "test/dir"}, + {"test/dir/../..", "."}, + {"test/link1", "test"}, + {"test/link2", "test/dir"}, + {"test/link1/dir", "test/dir"}, + {"test/link2/..", "test"}, + {"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. +// It does not use Join because we don't want ".." to be evaluated. +func simpleJoin(dir, path string) string { + return dir + string(filepath.Separator) + path +} + +func testEvalSymlinks(t *testing.T, path, want string) { + have, err := filepath.EvalSymlinks(path) + if err != nil { + t.Errorf("EvalSymlinks(%q) error: %v", path, err) + return + } + if filepath.Clean(have) != filepath.Clean(want) { + t.Errorf("EvalSymlinks(%q) returns %q, want %q", path, have, want) + } +} + +func testEvalSymlinksAfterChdir(t *testing.T, wd, path, want string) { + cwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + defer func() { + err := os.Chdir(cwd) + if err != nil { + t.Fatal(err) + } + }() + + err = os.Chdir(wd) + if err != nil { + t.Fatal(err) + } + + have, err := filepath.EvalSymlinks(path) + if err != nil { + t.Errorf("EvalSymlinks(%q) in %q directory error: %v", path, wd, err) + return + } + if filepath.Clean(have) != filepath.Clean(want) { + t.Errorf("EvalSymlinks(%q) in %q directory returns %q, want %q", path, wd, have, want) + } +} + +func TestEvalSymlinks(t *testing.T) { + testenv.MustHaveSymlink(t) + + tmpDir, err := os.MkdirTemp("", "evalsymlink") + if err != nil { + t.Fatal("creating temp dir:", err) + } + defer os.RemoveAll(tmpDir) + + // /tmp may itself be a symlink! Avoid the confusion, although + // it means trusting the thing we're testing. + tmpDir, err = filepath.EvalSymlinks(tmpDir) + if err != nil { + t.Fatal("eval symlink for tmp dir:", err) + } + + // Create the symlink farm using relative paths. + for _, d := range EvalSymlinksTestDirs { + var err error + path := simpleJoin(tmpDir, d.path) + if d.dest == "" { + err = os.Mkdir(path, 0755) + } else { + err = os.Symlink(d.dest, path) + } + if err != nil { + t.Fatal(err) + } + } + + // Evaluate the symlink farm. + for _, test := range EvalSymlinksTests { + path := simpleJoin(tmpDir, test.path) + + dest := simpleJoin(tmpDir, test.dest) + if filepath.IsAbs(test.dest) || os.IsPathSeparator(test.dest[0]) { + dest = test.dest + } + testEvalSymlinks(t, path, dest) + + // test EvalSymlinks(".") + testEvalSymlinksAfterChdir(t, path, ".", ".") + + // test EvalSymlinks("C:.") on Windows + if runtime.GOOS == "windows" { + volDot := filepath.VolumeName(tmpDir) + "." + testEvalSymlinksAfterChdir(t, path, volDot, volDot) + } + + // test EvalSymlinks(".."+path) + dotdotPath := simpleJoin("..", test.dest) + if filepath.IsAbs(test.dest) || os.IsPathSeparator(test.dest[0]) { + dotdotPath = test.dest + } + testEvalSymlinksAfterChdir(t, + simpleJoin(tmpDir, "test"), + simpleJoin("..", test.path), + dotdotPath) + + // test EvalSymlinks(p) where p is relative path + testEvalSymlinksAfterChdir(t, tmpDir, test.path, test.dest) + } +} + +func TestEvalSymlinksIsNotExist(t *testing.T) { + testenv.MustHaveSymlink(t) + + defer chtmpdir(t)() + + _, err := filepath.EvalSymlinks("notexist") + if !os.IsNotExist(err) { + t.Errorf("expected the file is not found, got %v\n", err) + } + + err = os.Symlink("notexist", "link") + if err != nil { + t.Fatal(err) + } + defer os.Remove("link") + + _, err = filepath.EvalSymlinks("link") + if !os.IsNotExist(err) { + t.Errorf("expected the file is not found, got %v\n", err) + } +} + +func TestIssue13582(t *testing.T) { + testenv.MustHaveSymlink(t) + + tmpDir, err := os.MkdirTemp("", "issue13582") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + dir := filepath.Join(tmpDir, "dir") + err = os.Mkdir(dir, 0755) + if err != nil { + t.Fatal(err) + } + linkToDir := filepath.Join(tmpDir, "link_to_dir") + err = os.Symlink(dir, linkToDir) + if err != nil { + t.Fatal(err) + } + file := filepath.Join(linkToDir, "file") + err = os.WriteFile(file, nil, 0644) + if err != nil { + t.Fatal(err) + } + link1 := filepath.Join(linkToDir, "link1") + err = os.Symlink(file, link1) + if err != nil { + t.Fatal(err) + } + link2 := filepath.Join(linkToDir, "link2") + err = os.Symlink(link1, link2) + if err != nil { + t.Fatal(err) + } + + // /tmp may itself be a symlink! + realTmpDir, err := filepath.EvalSymlinks(tmpDir) + if err != nil { + t.Fatal(err) + } + realDir := filepath.Join(realTmpDir, "dir") + realFile := filepath.Join(realDir, "file") + + tests := []struct { + path, want string + }{ + {dir, realDir}, + {linkToDir, realDir}, + {file, realFile}, + {link1, realFile}, + {link2, realFile}, + } + for i, test := range tests { + have, err := filepath.EvalSymlinks(test.path) + if err != nil { + t.Fatal(err) + } + if have != test.want { + t.Errorf("test#%d: EvalSymlinks(%q) returns %q, want %q", i, test.path, have, test.want) + } + } +} + +// Test directories relative to temporary directory. +// The tests are run in absTestDirs[0]. +var absTestDirs = []string{ + "a", + "a/b", + "a/b/c", +} + +// Test paths relative to temporary directory. $ expands to the directory. +// The tests are run in absTestDirs[0]. +// We create absTestDirs first. +var absTests = []string{ + ".", + "b", + "b/", + "../a", + "../a/b", + "../a/b/./c/../../.././a", + "../a/b/./c/../../.././a/", + "$", + "$/.", + "$/a/../a/b", + "$/a/b/c/../../.././a", + "$/a/b/c/../../.././a/", +} + +func TestAbs(t *testing.T) { + root, err := os.MkdirTemp("", "TestAbs") + if err != nil { + t.Fatal("TempDir failed: ", err) + } + defer os.RemoveAll(root) + + wd, err := os.Getwd() + if err != nil { + t.Fatal("getwd failed: ", err) + } + err = os.Chdir(root) + if err != nil { + t.Fatal("chdir failed: ", err) + } + defer os.Chdir(wd) + + for _, dir := range absTestDirs { + err = os.Mkdir(dir, 0777) + if err != nil { + t.Fatal("Mkdir failed: ", err) + } + } + + if runtime.GOOS == "windows" { + vol := filepath.VolumeName(root) + var extra []string + for _, path := range absTests { + if strings.Contains(path, "$") { + continue + } + path = vol + path + extra = append(extra, path) + } + absTests = append(absTests, extra...) + } + + err = os.Chdir(absTestDirs[0]) + if err != nil { + t.Fatal("chdir failed: ", err) + } + + for _, path := range absTests { + path = strings.ReplaceAll(path, "$", root) + info, err := os.Stat(path) + if err != nil { + t.Errorf("%s: %s", path, err) + continue + } + + abspath, err := filepath.Abs(path) + if err != nil { + t.Errorf("Abs(%q) error: %v", path, err) + continue + } + absinfo, err := os.Stat(abspath) + if err != nil || !os.SameFile(absinfo, info) { + t.Errorf("Abs(%q)=%q, not the same file", path, abspath) + } + if !filepath.IsAbs(abspath) { + t.Errorf("Abs(%q)=%q, not an absolute path", path, abspath) + } + if filepath.IsAbs(abspath) && abspath != filepath.Clean(abspath) { + t.Errorf("Abs(%q)=%q, isn't clean", path, abspath) + } + } +} + +// Empty path needs to be special-cased on Windows. See golang.org/issue/24441. +// We test it separately from all other absTests because the empty string is not +// a valid path, so it can't be used with os.Stat. +func TestAbsEmptyString(t *testing.T) { + root, err := os.MkdirTemp("", "TestAbsEmptyString") + if err != nil { + t.Fatal("TempDir failed: ", err) + } + defer os.RemoveAll(root) + + wd, err := os.Getwd() + if err != nil { + t.Fatal("getwd failed: ", err) + } + err = os.Chdir(root) + if err != nil { + t.Fatal("chdir failed: ", err) + } + defer os.Chdir(wd) + + info, err := os.Stat(root) + if err != nil { + t.Fatalf("%s: %s", root, err) + } + + abspath, err := filepath.Abs("") + if err != nil { + t.Fatalf(`Abs("") error: %v`, err) + } + absinfo, err := os.Stat(abspath) + if err != nil || !os.SameFile(absinfo, info) { + t.Errorf(`Abs("")=%q, not the same file`, abspath) + } + if !filepath.IsAbs(abspath) { + t.Errorf(`Abs("")=%q, not an absolute path`, abspath) + } + if filepath.IsAbs(abspath) && abspath != filepath.Clean(abspath) { + t.Errorf(`Abs("")=%q, isn't clean`, abspath) + } +} + +type RelTests struct { + root, path, want string +} + +var reltests = []RelTests{ + {"a/b", "a/b", "."}, + {"a/b/.", "a/b", "."}, + {"a/b", "a/b/.", "."}, + {"./a/b", "a/b", "."}, + {"a/b", "./a/b", "."}, + {"ab/cd", "ab/cde", "../cde"}, + {"ab/cd", "ab/c", "../c"}, + {"a/b", "a/b/c/d", "c/d"}, + {"a/b", "a/b/../c", "../c"}, + {"a/b/../c", "a/b", "../b"}, + {"a/b/c", "a/c/d", "../../c/d"}, + {"a/b", "c/d", "../../c/d"}, + {"a/b/c/d", "a/b", "../.."}, + {"a/b/c/d", "a/b/", "../.."}, + {"a/b/c/d/", "a/b", "../.."}, + {"a/b/c/d/", "a/b/", "../.."}, + {"../../a/b", "../../a/b/c/d", "c/d"}, + {"/a/b", "/a/b", "."}, + {"/a/b/.", "/a/b", "."}, + {"/a/b", "/a/b/.", "."}, + {"/ab/cd", "/ab/cde", "../cde"}, + {"/ab/cd", "/ab/c", "../c"}, + {"/a/b", "/a/b/c/d", "c/d"}, + {"/a/b", "/a/b/../c", "../c"}, + {"/a/b/../c", "/a/b", "../b"}, + {"/a/b/c", "/a/c/d", "../../c/d"}, + {"/a/b", "/c/d", "../../c/d"}, + {"/a/b/c/d", "/a/b", "../.."}, + {"/a/b/c/d", "/a/b/", "../.."}, + {"/a/b/c/d/", "/a/b", "../.."}, + {"/a/b/c/d/", "/a/b/", "../.."}, + {"/../../a/b", "/../../a/b/c/d", "c/d"}, + {".", "a/b", "a/b"}, + {".", "..", ".."}, + + // can't do purely lexically + {"..", ".", "err"}, + {"..", "a", "err"}, + {"../..", "..", "err"}, + {"a", "/a", "err"}, + {"/a", "a", "err"}, +} + +var winreltests = []RelTests{ + {`C:a\b\c`, `C:a/b/d`, `..\d`}, + {`C:\`, `D:\`, `err`}, + {`C:`, `D:`, `err`}, + {`C:\Projects`, `c:\projects\src`, `src`}, + {`C:\Projects`, `c:\projects`, `.`}, + {`C:\Projects\a\..`, `c:\projects`, `.`}, +} + +func TestRel(t *testing.T) { + tests := append([]RelTests{}, reltests...) + if runtime.GOOS == "windows" { + for i := range tests { + tests[i].want = filepath.FromSlash(tests[i].want) + } + tests = append(tests, winreltests...) + } + for _, test := range tests { + got, err := filepath.Rel(test.root, test.path) + if test.want == "err" { + if err == nil { + t.Errorf("Rel(%q, %q)=%q, want error", test.root, test.path, got) + } + continue + } + if err != nil { + t.Errorf("Rel(%q, %q): want %q, got error: %s", test.root, test.path, test.want, err) + } + if got != test.want { + t.Errorf("Rel(%q, %q)=%q, want %q", test.root, test.path, got, test.want) + } + } +} + +type VolumeNameTest struct { + path string + vol string +} + +var volumenametests = []VolumeNameTest{ + {`c:/foo/bar`, `c:`}, + {`c:`, `c:`}, + {`2:`, ``}, + {``, ``}, + {`\\\host`, ``}, + {`\\\host\`, ``}, + {`\\\host\share`, ``}, + {`\\\host\\share`, ``}, + {`\\host`, ``}, + {`//host`, ``}, + {`\\host\`, ``}, + {`//host/`, ``}, + {`\\host\share`, `\\host\share`}, + {`//host/share`, `//host/share`}, + {`\\host\share\`, `\\host\share`}, + {`//host/share/`, `//host/share`}, + {`\\host\share\foo`, `\\host\share`}, + {`//host/share/foo`, `//host/share`}, + {`\\host\share\\foo\\\bar\\\\baz`, `\\host\share`}, + {`//host/share//foo///bar////baz`, `//host/share`}, + {`\\host\share\foo\..\bar`, `\\host\share`}, + {`//host/share/foo/../bar`, `//host/share`}, +} + +func TestVolumeName(t *testing.T) { + if runtime.GOOS != "windows" { + return + } + for _, v := range volumenametests { + if vol := filepath.VolumeName(v.path); vol != v.vol { + t.Errorf("VolumeName(%q)=%q, want %q", v.path, vol, v.vol) + } + } +} + +func TestDriveLetterInEvalSymlinks(t *testing.T) { + if runtime.GOOS != "windows" { + return + } + wd, _ := os.Getwd() + if len(wd) < 3 { + t.Errorf("Current directory path %q is too short", wd) + } + lp := strings.ToLower(wd) + up := strings.ToUpper(wd) + flp, err := filepath.EvalSymlinks(lp) + if err != nil { + t.Fatalf("EvalSymlinks(%q) failed: %q", lp, err) + } + fup, err := filepath.EvalSymlinks(up) + if err != nil { + t.Fatalf("EvalSymlinks(%q) failed: %q", up, err) + } + if flp != fup { + t.Errorf("Results of EvalSymlinks do not match: %q and %q", flp, fup) + } +} + +func TestBug3486(t *testing.T) { // https://golang.org/issue/3486 + if runtime.GOOS == "ios" { + t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH) + } + root, err := filepath.EvalSymlinks(runtime.GOROOT() + "/test") + if err != nil { + t.Fatal(err) + } + bugs := filepath.Join(root, "fixedbugs") + ken := filepath.Join(root, "ken") + seenBugs := false + seenKen := false + err = filepath.Walk(root, func(pth string, info fs.FileInfo, err error) error { + if err != nil { + t.Fatal(err) + } + + switch pth { + case bugs: + seenBugs = true + return filepath.SkipDir + case ken: + if !seenBugs { + t.Fatal("filepath.Walk out of order - ken before fixedbugs") + } + seenKen = true + } + return nil + }) + if err != nil { + t.Fatal(err) + } + if !seenKen { + t.Fatalf("%q not seen", ken) + } +} + +func testWalkSymlink(t *testing.T, mklink func(target, link string) error) { + tmpdir, err := os.MkdirTemp("", "testWalkSymlink") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpdir) + + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + defer os.Chdir(wd) + + err = os.Chdir(tmpdir) + if err != nil { + t.Fatal(err) + } + + err = mklink(tmpdir, "link") + if err != nil { + t.Fatal(err) + } + + var visited []string + err = filepath.Walk(tmpdir, func(path string, info fs.FileInfo, err error) error { + if err != nil { + t.Fatal(err) + } + rel, err := filepath.Rel(tmpdir, path) + if err != nil { + t.Fatal(err) + } + visited = append(visited, rel) + return nil + }) + if err != nil { + t.Fatal(err) + } + sort.Strings(visited) + want := []string{".", "link"} + if fmt.Sprintf("%q", visited) != fmt.Sprintf("%q", want) { + t.Errorf("unexpected paths visited %q, want %q", visited, want) + } +} + +func TestWalkSymlink(t *testing.T) { + testenv.MustHaveSymlink(t) + testWalkSymlink(t, os.Symlink) +} + +func TestIssue29372(t *testing.T) { + tmpDir, err := os.MkdirTemp("", "TestIssue29372") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + path := filepath.Join(tmpDir, "file.txt") + err = os.WriteFile(path, nil, 0644) + if err != nil { + t.Fatal(err) + } + + pathSeparator := string(filepath.Separator) + tests := []string{ + path + strings.Repeat(pathSeparator, 1), + path + strings.Repeat(pathSeparator, 2), + path + strings.Repeat(pathSeparator, 1) + ".", + path + strings.Repeat(pathSeparator, 2) + ".", + path + strings.Repeat(pathSeparator, 1) + "..", + path + strings.Repeat(pathSeparator, 2) + "..", + } + + for i, test := range tests { + _, err = filepath.EvalSymlinks(test) + if err != syscall.ENOTDIR { + t.Fatalf("test#%d: want %q, got %q", i, syscall.ENOTDIR, err) + } + } +} + +// Issue 30520 part 1. +func TestEvalSymlinksAboveRoot(t *testing.T) { + testenv.MustHaveSymlink(t) + + t.Parallel() + + tmpDir, err := os.MkdirTemp("", "TestEvalSymlinksAboveRoot") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + evalTmpDir, err := filepath.EvalSymlinks(tmpDir) + if err != nil { + t.Fatal(err) + } + + if err := os.Mkdir(filepath.Join(evalTmpDir, "a"), 0777); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(evalTmpDir, "a"), filepath.Join(evalTmpDir, "b")); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(evalTmpDir, "a", "file"), nil, 0666); err != nil { + t.Fatal(err) + } + + // Count the number of ".." elements to get to the root directory. + vol := filepath.VolumeName(evalTmpDir) + c := strings.Count(evalTmpDir[len(vol):], string(os.PathSeparator)) + var dd []string + for i := 0; i < c+2; i++ { + dd = append(dd, "..") + } + + wantSuffix := strings.Join([]string{"a", "file"}, string(os.PathSeparator)) + + // Try different numbers of "..". + for _, i := range []int{c, c + 1, c + 2} { + check := strings.Join([]string{evalTmpDir, strings.Join(dd[:i], string(os.PathSeparator)), evalTmpDir[len(vol)+1:], "b", "file"}, string(os.PathSeparator)) + if resolved, err := filepath.EvalSymlinks(check); err != nil { + t.Errorf("EvalSymlinks(%q) failed: %v", check, err) + } else if !strings.HasSuffix(resolved, wantSuffix) { + t.Errorf("EvalSymlinks(%q) = %q does not end with %q", check, resolved, wantSuffix) + } else { + t.Logf("EvalSymlinks(%q) = %q", check, resolved) + } + } +} + +// Issue 30520 part 2. +func TestEvalSymlinksAboveRootChdir(t *testing.T) { + testenv.MustHaveSymlink(t) + + tmpDir, err := os.MkdirTemp("", "TestEvalSymlinksAboveRootChdir") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + defer os.Chdir(wd) + + if err := os.Chdir(tmpDir); err != nil { + t.Fatal(err) + } + + subdir := filepath.Join("a", "b") + if err := os.MkdirAll(subdir, 0777); err != nil { + t.Fatal(err) + } + if err := os.Symlink(subdir, "c"); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(subdir, "file"), nil, 0666); err != nil { + t.Fatal(err) + } + + subdir = filepath.Join("d", "e", "f") + if err := os.MkdirAll(subdir, 0777); err != nil { + t.Fatal(err) + } + if err := os.Chdir(subdir); err != nil { + t.Fatal(err) + } + + check := filepath.Join("..", "..", "..", "c", "file") + wantSuffix := filepath.Join("a", "b", "file") + if resolved, err := filepath.EvalSymlinks(check); err != nil { + t.Errorf("EvalSymlinks(%q) failed: %v", check, err) + } else if !strings.HasSuffix(resolved, wantSuffix) { + t.Errorf("EvalSymlinks(%q) = %q does not end with %q", check, resolved, wantSuffix) + } else { + t.Logf("EvalSymlinks(%q) = %q", check, resolved) + } +} diff --git a/src/path/filepath/path_unix.go b/src/path/filepath/path_unix.go new file mode 100644 index 0000000..ec497d9 --- /dev/null +++ b/src/path/filepath/path_unix.go @@ -0,0 +1,53 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris + +package filepath + +import "strings" + +// IsAbs reports whether the path is absolute. +func IsAbs(path string) bool { + return strings.HasPrefix(path, "/") +} + +// volumeNameLen returns length of the leading volume name on Windows. +// It returns 0 elsewhere. +func volumeNameLen(path string) int { + return 0 +} + +// HasPrefix exists for historical compatibility and should not be used. +// +// Deprecated: HasPrefix does not respect path boundaries and +// does not ignore case when required. +func HasPrefix(p, prefix string) bool { + return strings.HasPrefix(p, prefix) +} + +func splitList(path string) []string { + if path == "" { + return []string{} + } + return strings.Split(path, string(ListSeparator)) +} + +func abs(path string) (string, error) { + return unixAbs(path) +} + +func join(elem []string) string { + // If there's a bug here, fix the logic in ./path_plan9.go too. + for i, e := range elem { + if e != "" { + return Clean(strings.Join(elem[i:], string(Separator))) + } + } + return "" +} + +func sameWord(a, b string) bool { + return a == b +} diff --git a/src/path/filepath/path_windows.go b/src/path/filepath/path_windows.go new file mode 100644 index 0000000..445c868 --- /dev/null +++ b/src/path/filepath/path_windows.go @@ -0,0 +1,200 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath + +import ( + "strings" + "syscall" +) + +func isSlash(c uint8) bool { + return c == '\\' || c == '/' +} + +// reservedNames lists reserved Windows names. Search for PRN in +// https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file +// for details. +var reservedNames = []string{ + "CON", "PRN", "AUX", "NUL", + "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", + "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", +} + +// isReservedName returns true, if path is Windows reserved name. +// See reservedNames for the full list. +func isReservedName(path string) bool { + if len(path) == 0 { + return false + } + for _, reserved := range reservedNames { + if strings.EqualFold(path, reserved) { + return true + } + } + return false +} + +// IsAbs reports whether the path is absolute. +func IsAbs(path string) (b bool) { + if isReservedName(path) { + return true + } + l := volumeNameLen(path) + if l == 0 { + return false + } + path = path[l:] + if path == "" { + return false + } + return isSlash(path[0]) +} + +// volumeNameLen returns length of the leading volume name on Windows. +// It returns 0 elsewhere. +func volumeNameLen(path string) int { + if len(path) < 2 { + return 0 + } + // with drive letter + c := path[0] + if path[1] == ':' && ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') { + return 2 + } + // is it UNC? https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx + if l := len(path); l >= 5 && isSlash(path[0]) && isSlash(path[1]) && + !isSlash(path[2]) && path[2] != '.' { + // first, leading `\\` and next shouldn't be `\`. its server name. + for n := 3; n < l-1; n++ { + // second, next '\' shouldn't be repeated. + if isSlash(path[n]) { + n++ + // third, following something characters. its share name. + if !isSlash(path[n]) { + if path[n] == '.' { + break + } + for ; n < l; n++ { + if isSlash(path[n]) { + break + } + } + return n + } + break + } + } + } + return 0 +} + +// HasPrefix exists for historical compatibility and should not be used. +// +// Deprecated: HasPrefix does not respect path boundaries and +// does not ignore case when required. +func HasPrefix(p, prefix string) bool { + if strings.HasPrefix(p, prefix) { + return true + } + return strings.HasPrefix(strings.ToLower(p), strings.ToLower(prefix)) +} + +func splitList(path string) []string { + // The same implementation is used in LookPath in os/exec; + // consider changing os/exec when changing this. + + if path == "" { + return []string{} + } + + // Split path, respecting but preserving quotes. + list := []string{} + start := 0 + quo := false + for i := 0; i < len(path); i++ { + switch c := path[i]; { + case c == '"': + quo = !quo + case c == ListSeparator && !quo: + list = append(list, path[start:i]) + start = i + 1 + } + } + list = append(list, path[start:]) + + // Remove quotes. + for i, s := range list { + list[i] = strings.ReplaceAll(s, `"`, ``) + } + + return list +} + +func abs(path string) (string, error) { + if path == "" { + // syscall.FullPath returns an error on empty path, because it's not a valid path. + // To implement Abs behavior of returning working directory on empty string input, + // special-case empty path by changing it to "." path. See golang.org/issue/24441. + path = "." + } + fullPath, err := syscall.FullPath(path) + if err != nil { + return "", err + } + return Clean(fullPath), nil +} + +func join(elem []string) string { + for i, e := range elem { + if e != "" { + return joinNonEmpty(elem[i:]) + } + } + return "" +} + +// joinNonEmpty is like join, but it assumes that the first element is non-empty. +func joinNonEmpty(elem []string) string { + if len(elem[0]) == 2 && elem[0][1] == ':' { + // First element is drive letter without terminating slash. + // Keep path relative to current directory on that drive. + // Skip empty elements. + i := 1 + for ; i < len(elem); i++ { + if elem[i] != "" { + break + } + } + return Clean(elem[0] + strings.Join(elem[i:], string(Separator))) + } + // The following logic prevents Join from inadvertently creating a + // UNC path on Windows. Unless the first element is a UNC path, Join + // shouldn't create a UNC path. See golang.org/issue/9167. + p := Clean(strings.Join(elem, string(Separator))) + if !isUNC(p) { + return p + } + // p == UNC only allowed when the first element is a UNC path. + head := Clean(elem[0]) + if isUNC(head) { + return p + } + // head + tail == UNC, but joining two non-UNC paths should not result + // in a UNC path. Undo creation of UNC path. + tail := Clean(strings.Join(elem[1:], string(Separator))) + if head[len(head)-1] == Separator { + return head + tail + } + return head + string(Separator) + tail +} + +// isUNC reports whether path is a UNC path. +func isUNC(path string) bool { + return volumeNameLen(path) > 2 +} + +func sameWord(a, b string) bool { + return strings.EqualFold(a, b) +} diff --git a/src/path/filepath/path_windows_test.go b/src/path/filepath/path_windows_test.go new file mode 100644 index 0000000..1c3d84c --- /dev/null +++ b/src/path/filepath/path_windows_test.go @@ -0,0 +1,586 @@ +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath_test + +import ( + "flag" + "fmt" + "internal/testenv" + "io/fs" + "os" + "os/exec" + "path/filepath" + "reflect" + "runtime/debug" + "strings" + "testing" +) + +func TestWinSplitListTestsAreValid(t *testing.T) { + comspec := os.Getenv("ComSpec") + if comspec == "" { + t.Fatal("%ComSpec% must be set") + } + + for ti, tt := range winsplitlisttests { + testWinSplitListTestIsValid(t, ti, tt, comspec) + } +} + +func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest, + comspec string) { + + const ( + cmdfile = `printdir.cmd` + perm fs.FileMode = 0700 + ) + + tmp, err := os.MkdirTemp("", "testWinSplitListTestIsValid") + if err != nil { + t.Fatalf("TempDir failed: %v", err) + } + defer os.RemoveAll(tmp) + + for i, d := range tt.result { + if d == "" { + continue + } + if cd := filepath.Clean(d); filepath.VolumeName(cd) != "" || + cd[0] == '\\' || cd == ".." || (len(cd) >= 3 && cd[0:3] == `..\`) { + t.Errorf("%d,%d: %#q refers outside working directory", ti, i, d) + return + } + dd := filepath.Join(tmp, d) + if _, err := os.Stat(dd); err == nil { + t.Errorf("%d,%d: %#q already exists", ti, i, d) + return + } + if err = os.MkdirAll(dd, perm); err != nil { + t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err) + return + } + fn, data := filepath.Join(dd, cmdfile), []byte("@echo "+d+"\r\n") + if err = os.WriteFile(fn, data, perm); err != nil { + t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err) + return + } + } + + // on some systems, SystemRoot is required for cmd to work + systemRoot := os.Getenv("SystemRoot") + + for i, d := range tt.result { + if d == "" { + continue + } + exp := []byte(d + "\r\n") + cmd := &exec.Cmd{ + Path: comspec, + Args: []string{`/c`, cmdfile}, + Env: []string{`Path=` + systemRoot + "/System32;" + tt.list, `SystemRoot=` + systemRoot}, + Dir: tmp, + } + out, err := cmd.CombinedOutput() + switch { + case err != nil: + t.Errorf("%d,%d: execution error %v\n%q", ti, i, err, out) + return + case !reflect.DeepEqual(out, exp): + t.Errorf("%d,%d: expected %#q, got %#q", ti, i, exp, out) + return + default: + // unshadow cmdfile in next directory + err = os.Remove(filepath.Join(tmp, d, cmdfile)) + if err != nil { + t.Fatalf("Remove test command failed: %v", err) + } + } + } +} + +func TestWindowsEvalSymlinks(t *testing.T) { + testenv.MustHaveSymlink(t) + + tmpDir, err := os.MkdirTemp("", "TestWindowsEvalSymlinks") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // /tmp may itself be a symlink! Avoid the confusion, although + // it means trusting the thing we're testing. + tmpDir, err = filepath.EvalSymlinks(tmpDir) + if err != nil { + t.Fatal(err) + } + + if len(tmpDir) < 3 { + t.Fatalf("tmpDir path %q is too short", tmpDir) + } + if tmpDir[1] != ':' { + t.Fatalf("tmpDir path %q must have drive letter in it", tmpDir) + } + test := EvalSymlinksTest{"test/linkabswin", tmpDir[:3]} + + // Create the symlink farm using relative paths. + testdirs := append(EvalSymlinksTestDirs, test) + for _, d := range testdirs { + var err error + path := simpleJoin(tmpDir, d.path) + if d.dest == "" { + err = os.Mkdir(path, 0755) + } else { + err = os.Symlink(d.dest, path) + } + if err != nil { + t.Fatal(err) + } + } + + path := simpleJoin(tmpDir, test.path) + + testEvalSymlinks(t, path, test.dest) + + testEvalSymlinksAfterChdir(t, path, ".", test.dest) + + testEvalSymlinksAfterChdir(t, + path, + filepath.VolumeName(tmpDir)+".", + test.dest) + + testEvalSymlinksAfterChdir(t, + simpleJoin(tmpDir, "test"), + simpleJoin("..", test.path), + test.dest) + + testEvalSymlinksAfterChdir(t, tmpDir, test.path, test.dest) +} + +// TestEvalSymlinksCanonicalNames verify that EvalSymlinks +// returns "canonical" path names on windows. +func TestEvalSymlinksCanonicalNames(t *testing.T) { + tmp, err := os.MkdirTemp("", "evalsymlinkcanonical") + if err != nil { + t.Fatal("creating temp dir:", err) + } + defer os.RemoveAll(tmp) + + // os.MkdirTemp might return "non-canonical" name. + cTmpName, err := filepath.EvalSymlinks(tmp) + if err != nil { + t.Errorf("EvalSymlinks(%q) error: %v", tmp, err) + } + + dirs := []string{ + "test", + "test/dir", + "testing_long_dir", + "TEST2", + } + + for _, d := range dirs { + dir := filepath.Join(cTmpName, d) + err := os.Mkdir(dir, 0755) + if err != nil { + t.Fatal(err) + } + cname, err := filepath.EvalSymlinks(dir) + if err != nil { + t.Errorf("EvalSymlinks(%q) error: %v", dir, err) + continue + } + if dir != cname { + t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", dir, cname, dir) + continue + } + // test non-canonical names + test := strings.ToUpper(dir) + p, err := filepath.EvalSymlinks(test) + if err != nil { + t.Errorf("EvalSymlinks(%q) error: %v", test, err) + continue + } + if p != cname { + t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname) + continue + } + // another test + test = strings.ToLower(dir) + p, err = filepath.EvalSymlinks(test) + if err != nil { + t.Errorf("EvalSymlinks(%q) error: %v", test, err) + continue + } + if p != cname { + t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname) + continue + } + } +} + +// checkVolume8dot3Setting runs "fsutil 8dot3name query c:" command +// (where c: is vol parameter) to discover "8dot3 name creation state". +// The state is combination of 2 flags. The global flag controls if it +// is per volume or global setting: +// 0 - Enable 8dot3 name creation on all volumes on the system +// 1 - Disable 8dot3 name creation on all volumes on the system +// 2 - Set 8dot3 name creation on a per volume basis +// 3 - Disable 8dot3 name creation on all volumes except the system volume +// If global flag is set to 2, then per-volume flag needs to be examined: +// 0 - Enable 8dot3 name creation on this volume +// 1 - Disable 8dot3 name creation on this volume +// checkVolume8dot3Setting verifies that "8dot3 name creation" flags +// are set to 2 and 0, if enabled parameter is true, or 2 and 1, if enabled +// is false. Otherwise checkVolume8dot3Setting returns error. +func checkVolume8dot3Setting(vol string, enabled bool) error { + // It appears, on some systems "fsutil 8dot3name query ..." command always + // exits with error. Ignore exit code, and look at fsutil output instead. + out, _ := exec.Command("fsutil", "8dot3name", "query", vol).CombinedOutput() + // Check that system has "Volume level setting" set. + expected := "The registry state of NtfsDisable8dot3NameCreation is 2, the default (Volume level setting)" + if !strings.Contains(string(out), expected) { + // Windows 10 version of fsutil has different output message. + expectedWindow10 := "The registry state is: 2 (Per volume setting - the default)" + if !strings.Contains(string(out), expectedWindow10) { + return fmt.Errorf("fsutil output should contain %q, but is %q", expected, string(out)) + } + } + // Now check the volume setting. + expected = "Based on the above two settings, 8dot3 name creation is %s on %s" + if enabled { + expected = fmt.Sprintf(expected, "enabled", vol) + } else { + expected = fmt.Sprintf(expected, "disabled", vol) + } + if !strings.Contains(string(out), expected) { + return fmt.Errorf("unexpected fsutil output: %q", string(out)) + } + return nil +} + +func setVolume8dot3Setting(vol string, enabled bool) error { + cmd := []string{"fsutil", "8dot3name", "set", vol} + if enabled { + cmd = append(cmd, "0") + } else { + cmd = append(cmd, "1") + } + // It appears, on some systems "fsutil 8dot3name set ..." command always + // exits with error. Ignore exit code, and look at fsutil output instead. + out, _ := exec.Command(cmd[0], cmd[1:]...).CombinedOutput() + if string(out) != "\r\nSuccessfully set 8dot3name behavior.\r\n" { + // Windows 10 version of fsutil has different output message. + expectedWindow10 := "Successfully %s 8dot3name generation on %s\r\n" + if enabled { + expectedWindow10 = fmt.Sprintf(expectedWindow10, "enabled", vol) + } else { + expectedWindow10 = fmt.Sprintf(expectedWindow10, "disabled", vol) + } + if string(out) != expectedWindow10 { + return fmt.Errorf("%v command failed: %q", cmd, string(out)) + } + } + return nil +} + +var runFSModifyTests = flag.Bool("run_fs_modify_tests", false, "run tests which modify filesystem parameters") + +// This test assumes registry state of NtfsDisable8dot3NameCreation is 2, +// the default (Volume level setting). +func TestEvalSymlinksCanonicalNamesWith8dot3Disabled(t *testing.T) { + if !*runFSModifyTests { + t.Skip("skipping test that modifies file system setting; enable with -run_fs_modify_tests") + } + tempVol := filepath.VolumeName(os.TempDir()) + if len(tempVol) != 2 { + t.Fatalf("unexpected temp volume name %q", tempVol) + } + + err := checkVolume8dot3Setting(tempVol, true) + if err != nil { + t.Fatal(err) + } + err = setVolume8dot3Setting(tempVol, false) + if err != nil { + t.Fatal(err) + } + defer func() { + err := setVolume8dot3Setting(tempVol, true) + if err != nil { + t.Fatal(err) + } + err = checkVolume8dot3Setting(tempVol, true) + if err != nil { + t.Fatal(err) + } + }() + err = checkVolume8dot3Setting(tempVol, false) + if err != nil { + t.Fatal(err) + } + TestEvalSymlinksCanonicalNames(t) +} + +func TestToNorm(t *testing.T) { + stubBase := func(path string) (string, error) { + vol := filepath.VolumeName(path) + path = path[len(vol):] + + if strings.Contains(path, "/") { + return "", fmt.Errorf("invalid path is given to base: %s", vol+path) + } + + if path == "" || path == "." || path == `\` { + return "", fmt.Errorf("invalid path is given to base: %s", vol+path) + } + + i := strings.LastIndexByte(path, filepath.Separator) + if i == len(path)-1 { // trailing '\' is invalid + return "", fmt.Errorf("invalid path is given to base: %s", vol+path) + } + if i == -1 { + return strings.ToUpper(path), nil + } + + return strings.ToUpper(path[i+1:]), nil + } + + // On this test, toNorm should be same as string.ToUpper(filepath.Clean(path)) except empty string. + tests := []struct { + arg string + want string + }{ + {"", ""}, + {".", "."}, + {"./foo/bar", `FOO\BAR`}, + {"/", `\`}, + {"/foo/bar", `\FOO\BAR`}, + {"/foo/bar/baz/qux", `\FOO\BAR\BAZ\QUX`}, + {"foo/bar", `FOO\BAR`}, + {"C:/foo/bar", `C:\FOO\BAR`}, + {"C:foo/bar", `C:FOO\BAR`}, + {"c:/foo/bar", `C:\FOO\BAR`}, + {"C:/foo/bar", `C:\FOO\BAR`}, + {"C:/foo/bar/", `C:\FOO\BAR`}, + {`C:\foo\bar`, `C:\FOO\BAR`}, + {`C:\foo/bar\`, `C:\FOO\BAR`}, + {"C:/ふー/バー", `C:\ふー\バー`}, + } + + for _, test := range tests { + got, err := filepath.ToNorm(test.arg, stubBase) + if err != nil { + t.Errorf("toNorm(%s) failed: %v\n", test.arg, err) + } else if got != test.want { + t.Errorf("toNorm(%s) returns %s, but %s expected\n", test.arg, got, test.want) + } + } + + testPath := `{{tmp}}\test\foo\bar` + + testsDir := []struct { + wd string + arg string + want string + }{ + // test absolute paths + {".", `{{tmp}}\test\foo\bar`, `{{tmp}}\test\foo\bar`}, + {".", `{{tmp}}\.\test/foo\bar`, `{{tmp}}\test\foo\bar`}, + {".", `{{tmp}}\test\..\test\foo\bar`, `{{tmp}}\test\foo\bar`}, + {".", `{{tmp}}\TEST\FOO\BAR`, `{{tmp}}\test\foo\bar`}, + + // test relative paths begin with drive letter + {`{{tmp}}\test`, `{{tmpvol}}.`, `{{tmpvol}}.`}, + {`{{tmp}}\test`, `{{tmpvol}}..`, `{{tmpvol}}..`}, + {`{{tmp}}\test`, `{{tmpvol}}foo\bar`, `{{tmpvol}}foo\bar`}, + {`{{tmp}}\test`, `{{tmpvol}}.\foo\bar`, `{{tmpvol}}foo\bar`}, + {`{{tmp}}\test`, `{{tmpvol}}foo\..\foo\bar`, `{{tmpvol}}foo\bar`}, + {`{{tmp}}\test`, `{{tmpvol}}FOO\BAR`, `{{tmpvol}}foo\bar`}, + + // test relative paths begin with '\' + {"{{tmp}}", `{{tmpnovol}}\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`}, + {"{{tmp}}", `{{tmpnovol}}\.\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`}, + {"{{tmp}}", `{{tmpnovol}}\test\..\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`}, + {"{{tmp}}", `{{tmpnovol}}\TEST\FOO\BAR`, `{{tmpnovol}}\test\foo\bar`}, + + // test relative paths begin without '\' + {`{{tmp}}\test`, ".", `.`}, + {`{{tmp}}\test`, "..", `..`}, + {`{{tmp}}\test`, `foo\bar`, `foo\bar`}, + {`{{tmp}}\test`, `.\foo\bar`, `foo\bar`}, + {`{{tmp}}\test`, `foo\..\foo\bar`, `foo\bar`}, + {`{{tmp}}\test`, `FOO\BAR`, `foo\bar`}, + + // test UNC paths + {".", `\\localhost\c$`, `\\localhost\c$`}, + } + + tmp, err := os.MkdirTemp("", "testToNorm") + if err != nil { + t.Fatal(err) + } + defer func() { + err := os.RemoveAll(tmp) + if err != nil { + t.Fatal(err) + } + }() + + // os.MkdirTemp might return "non-canonical" name. + ctmp, err := filepath.EvalSymlinks(tmp) + if err != nil { + t.Fatal(err) + } + + err = os.MkdirAll(strings.ReplaceAll(testPath, "{{tmp}}", ctmp), 0777) + if err != nil { + t.Fatal(err) + } + + cwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + defer func() { + err := os.Chdir(cwd) + if err != nil { + t.Fatal(err) + } + }() + + tmpVol := filepath.VolumeName(ctmp) + if len(tmpVol) != 2 { + t.Fatalf("unexpected temp volume name %q", tmpVol) + } + + tmpNoVol := ctmp[len(tmpVol):] + + replacer := strings.NewReplacer("{{tmp}}", ctmp, "{{tmpvol}}", tmpVol, "{{tmpnovol}}", tmpNoVol) + + for _, test := range testsDir { + wd := replacer.Replace(test.wd) + arg := replacer.Replace(test.arg) + want := replacer.Replace(test.want) + + if test.wd == "." { + err := os.Chdir(cwd) + if err != nil { + t.Error(err) + + continue + } + } else { + err := os.Chdir(wd) + if err != nil { + t.Error(err) + + continue + } + } + + got, err := filepath.ToNorm(arg, filepath.NormBase) + if err != nil { + t.Errorf("toNorm(%s) failed: %v (wd=%s)\n", arg, err, wd) + } else if got != want { + t.Errorf("toNorm(%s) returns %s, but %s expected (wd=%s)\n", arg, got, want, wd) + } + } +} + +func TestUNC(t *testing.T) { + // Test that this doesn't go into an infinite recursion. + // See golang.org/issue/15879. + defer debug.SetMaxStack(debug.SetMaxStack(1e6)) + filepath.Glob(`\\?\c:\*`) +} + +func testWalkMklink(t *testing.T, linktype string) { + output, _ := exec.Command("cmd", "/c", "mklink", "/?").Output() + if !strings.Contains(string(output), fmt.Sprintf(" /%s ", linktype)) { + t.Skipf(`skipping test; mklink does not supports /%s parameter`, linktype) + } + testWalkSymlink(t, func(target, link string) error { + output, err := exec.Command("cmd", "/c", "mklink", "/"+linktype, link, target).CombinedOutput() + if err != nil { + return fmt.Errorf(`"mklink /%s %v %v" command failed: %v\n%v`, linktype, link, target, err, string(output)) + } + return nil + }) +} + +func TestWalkDirectoryJunction(t *testing.T) { + testenv.MustHaveSymlink(t) + testWalkMklink(t, "J") +} + +func TestWalkDirectorySymlink(t *testing.T) { + testenv.MustHaveSymlink(t) + testWalkMklink(t, "D") +} + +func TestNTNamespaceSymlink(t *testing.T) { + output, _ := exec.Command("cmd", "/c", "mklink", "/?").Output() + if !strings.Contains(string(output), " /J ") { + t.Skip("skipping test because mklink command does not support junctions") + } + + tmpdir, err := os.MkdirTemp("", "TestNTNamespaceSymlink") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpdir) + + // Make sure tmpdir is not a symlink, otherwise tests will fail. + tmpdir, err = filepath.EvalSymlinks(tmpdir) + if err != nil { + t.Fatal(err) + } + + vol := filepath.VolumeName(tmpdir) + output, err = exec.Command("cmd", "/c", "mountvol", vol, "/L").CombinedOutput() + if err != nil { + t.Fatalf("failed to run mountvol %v /L: %v %q", vol, err, output) + } + target := strings.Trim(string(output), " \n\r") + + dirlink := filepath.Join(tmpdir, "dirlink") + output, err = exec.Command("cmd", "/c", "mklink", "/J", dirlink, target).CombinedOutput() + if err != nil { + t.Fatalf("failed to run mklink %v %v: %v %q", dirlink, target, err, output) + } + + got, err := filepath.EvalSymlinks(dirlink) + if err != nil { + t.Fatal(err) + } + if want := vol + `\`; got != want { + t.Errorf(`EvalSymlinks(%q): got %q, want %q`, dirlink, got, want) + } + + // Make sure we have sufficient privilege to run mklink command. + testenv.MustHaveSymlink(t) + + file := filepath.Join(tmpdir, "file") + err = os.WriteFile(file, []byte(""), 0666) + if err != nil { + t.Fatal(err) + } + + target += file[len(filepath.VolumeName(file)):] + + filelink := filepath.Join(tmpdir, "filelink") + output, err = exec.Command("cmd", "/c", "mklink", filelink, target).CombinedOutput() + if err != nil { + t.Fatalf("failed to run mklink %v %v: %v %q", filelink, target, err, output) + } + + got, err = filepath.EvalSymlinks(filelink) + if err != nil { + t.Fatal(err) + } + if want := file; got != want { + t.Errorf(`EvalSymlinks(%q): got %q, want %q`, filelink, got, want) + } +} diff --git a/src/path/filepath/symlink.go b/src/path/filepath/symlink.go new file mode 100644 index 0000000..6fefd15 --- /dev/null +++ b/src/path/filepath/symlink.go @@ -0,0 +1,147 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath + +import ( + "errors" + "io/fs" + "os" + "runtime" + "syscall" +) + +func walkSymlinks(path string) (string, error) { + volLen := volumeNameLen(path) + pathSeparator := string(os.PathSeparator) + + if volLen < len(path) && os.IsPathSeparator(path[volLen]) { + volLen++ + } + 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++ + } + + // 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):] == "." + + // 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. + + // Set r to the index of the last slash in dest, + // after the volume. + var r int + for r = len(dest) - 1; r >= volLen; r-- { + if os.IsPathSeparator(dest[r]) { + break + } + } + if r < volLen || dest[r+1:] == ".." { + // Either path has no slashes + // (it's empty or just "C:") + // or it ends in a ".." we had to keep. + // Either way, keep this "..". + if len(dest) > volLen { + dest += pathSeparator + } + dest += ".." + } else { + // Discard everything since the last slash. + dest = dest[:r] + } + continue + } + + // Ordinary path component. Add it to result. + + if len(dest) > volumeNameLen(dest) && !os.IsPathSeparator(dest[len(dest)-1]) { + dest += pathSeparator + } + + dest += path[start:end] + + // Resolve symlink. + + fi, err := os.Lstat(dest) + if err != nil { + return "", err + } + + if fi.Mode()&fs.ModeSymlink == 0 { + if !fi.Mode().IsDir() && end < len(path) { + return "", syscall.ENOTDIR + } + continue + } + + // Found symlink. + + linksWalked++ + if linksWalked > 255 { + return "", errors.New("EvalSymlinks: too many links") + } + + link, err := os.Readlink(dest) + if err != nil { + return "", err + } + + if isWindowsDot && !IsAbs(link) { + // On Windows, if "." is a relative symlink, + // just return ".". + break + } + + 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 + } + } + return Clean(dest), nil +} diff --git a/src/path/filepath/symlink_unix.go b/src/path/filepath/symlink_unix.go new file mode 100644 index 0000000..d20e63a --- /dev/null +++ b/src/path/filepath/symlink_unix.go @@ -0,0 +1,7 @@ +// +build !windows + +package filepath + +func evalSymlinks(path string) (string, error) { + return walkSymlinks(path) +} diff --git a/src/path/filepath/symlink_windows.go b/src/path/filepath/symlink_windows.go new file mode 100644 index 0000000..d72279e --- /dev/null +++ b/src/path/filepath/symlink_windows.go @@ -0,0 +1,119 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package filepath + +import ( + "strings" + "syscall" +) + +// normVolumeName is like VolumeName, but makes drive letter upper case. +// result of EvalSymlinks must be unique, so we have +// EvalSymlinks(`c:\a`) == EvalSymlinks(`C:\a`). +func normVolumeName(path string) string { + volume := VolumeName(path) + + if len(volume) > 2 { // isUNC + return volume + } + + return strings.ToUpper(volume) +} + +// normBase returns the last element of path with correct case. +func normBase(path string) (string, error) { + p, err := syscall.UTF16PtrFromString(path) + if err != nil { + return "", err + } + + var data syscall.Win32finddata + + h, err := syscall.FindFirstFile(p, &data) + if err != nil { + return "", err + } + syscall.FindClose(h) + + return syscall.UTF16ToString(data.FileName[:]), nil +} + +// baseIsDotDot reports whether the last element of path is "..". +// The given path should be 'Clean'-ed in advance. +func baseIsDotDot(path string) bool { + i := strings.LastIndexByte(path, Separator) + return path[i+1:] == ".." +} + +// toNorm returns the normalized path that is guaranteed to be unique. +// It should accept the following formats: +// * UNC paths (e.g \\server\share\foo\bar) +// * absolute paths (e.g C:\foo\bar) +// * relative paths begin with drive letter (e.g C:foo\bar, C:..\foo\bar, C:.., C:.) +// * relative paths begin with '\' (e.g \foo\bar) +// * relative paths begin without '\' (e.g foo\bar, ..\foo\bar, .., .) +// The returned normalized path will be in the same form (of 5 listed above) as the input path. +// If two paths A and B are indicating the same file with the same format, toNorm(A) should be equal to toNorm(B). +// The normBase parameter should be equal to the normBase func, except for in tests. See docs on the normBase func. +func toNorm(path string, normBase func(string) (string, error)) (string, error) { + if path == "" { + return path, nil + } + + path = Clean(path) + + volume := normVolumeName(path) + path = path[len(volume):] + + // skip special cases + if path == "" || path == "." || path == `\` { + return volume + path, nil + } + + var normPath string + + for { + if baseIsDotDot(path) { + normPath = path + `\` + normPath + + break + } + + name, err := normBase(volume + path) + if err != nil { + return "", err + } + + normPath = name + `\` + normPath + + i := strings.LastIndexByte(path, Separator) + if i == -1 { + break + } + if i == 0 { // `\Go` or `C:\Go` + normPath = `\` + normPath + + break + } + + path = path[:i] + } + + normPath = normPath[:len(normPath)-1] // remove trailing '\' + + return volume + normPath, nil +} + +func evalSymlinks(path string) (string, error) { + newpath, err := walkSymlinks(path) + if err != nil { + return "", err + } + newpath, err = toNorm(newpath, normBase) + if err != nil { + return "", err + } + return newpath, nil +} diff --git a/src/path/match.go b/src/path/match.go new file mode 100644 index 0000000..918624c --- /dev/null +++ b/src/path/match.go @@ -0,0 +1,231 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package path + +import ( + "errors" + "internal/bytealg" + "unicode/utf8" +) + +// ErrBadPattern indicates a pattern was malformed. +var ErrBadPattern = errors.New("syntax error in pattern") + +// Match reports whether name matches the shell pattern. +// The pattern syntax is: +// +// pattern: +// { term } +// term: +// '*' matches any sequence of non-/ characters +// '?' matches any single non-/ character +// '[' [ '^' ] { character-range } ']' +// character class (must be non-empty) +// c matches character c (c != '*', '?', '\\', '[') +// '\\' c matches character c +// +// character-range: +// c matches character c (c != '\\', '-', ']') +// '\\' c matches character c +// lo '-' hi matches character c for lo <= c <= hi +// +// Match requires pattern to match all of name, not just a substring. +// The only possible returned error is ErrBadPattern, when pattern +// is malformed. +// +func Match(pattern, name string) (matched bool, err error) { +Pattern: + for len(pattern) > 0 { + var star bool + var chunk string + star, chunk, pattern = scanChunk(pattern) + if star && chunk == "" { + // Trailing * matches rest of string unless it has a /. + return bytealg.IndexByteString(name, '/') < 0, nil + } + // Look for match at current position. + t, ok, err := matchChunk(chunk, name) + // if we're the last chunk, make sure we've exhausted the name + // otherwise we'll give a false result even if we could still match + // using the star + if ok && (len(t) == 0 || len(pattern) > 0) { + name = t + continue + } + if err != nil { + return false, err + } + if star { + // Look for match skipping i+1 bytes. + // Cannot skip /. + for i := 0; i < len(name) && name[i] != '/'; i++ { + t, ok, err := matchChunk(chunk, name[i+1:]) + if ok { + // if we're the last chunk, make sure we exhausted the name + if len(pattern) == 0 && len(t) > 0 { + continue + } + name = t + continue Pattern + } + if err != nil { + return false, err + } + } + } + // Before returning false with no error, + // check that the remainder of the pattern is syntactically valid. + for len(pattern) > 0 { + _, chunk, pattern = scanChunk(pattern) + if _, _, err := matchChunk(chunk, ""); err != nil { + return false, err + } + } + return false, nil + } + return len(name) == 0, nil +} + +// scanChunk gets the next segment of pattern, which is a non-star string +// possibly preceded by a star. +func scanChunk(pattern string) (star bool, chunk, rest string) { + for len(pattern) > 0 && pattern[0] == '*' { + pattern = pattern[1:] + star = true + } + inrange := false + var i int +Scan: + for i = 0; i < len(pattern); i++ { + switch pattern[i] { + case '\\': + // error check handled in matchChunk: bad pattern. + if i+1 < len(pattern) { + i++ + } + case '[': + inrange = true + case ']': + inrange = false + case '*': + if !inrange { + break Scan + } + } + } + return star, pattern[0:i], pattern[i:] +} + +// matchChunk checks whether chunk matches the beginning of s. +// If so, it returns the remainder of s (after the match). +// Chunk is all single-character operators: literals, char classes, and ?. +func matchChunk(chunk, s string) (rest string, ok bool, err error) { + // failed records whether the match has failed. + // After the match fails, the loop continues on processing chunk, + // checking that the pattern is well-formed but no longer reading s. + failed := false + for len(chunk) > 0 { + if !failed && len(s) == 0 { + failed = true + } + switch chunk[0] { + case '[': + // character class + var r rune + if !failed { + var n int + r, n = utf8.DecodeRuneInString(s) + s = s[n:] + } + chunk = chunk[1:] + // possibly negated + negated := false + if len(chunk) > 0 && chunk[0] == '^' { + negated = true + chunk = chunk[1:] + } + // parse all ranges + match := false + nrange := 0 + for { + if len(chunk) > 0 && chunk[0] == ']' && nrange > 0 { + chunk = chunk[1:] + break + } + var lo, hi rune + if lo, chunk, err = getEsc(chunk); err != nil { + return "", false, err + } + hi = lo + if chunk[0] == '-' { + if hi, chunk, err = getEsc(chunk[1:]); err != nil { + return "", false, err + } + } + if lo <= r && r <= hi { + match = true + } + nrange++ + } + if match == negated { + failed = true + } + + case '?': + if !failed { + if s[0] == '/' { + failed = true + } + _, n := utf8.DecodeRuneInString(s) + s = s[n:] + } + chunk = chunk[1:] + + case '\\': + chunk = chunk[1:] + if len(chunk) == 0 { + return "", false, ErrBadPattern + } + fallthrough + + default: + if !failed { + if chunk[0] != s[0] { + failed = true + } + s = s[1:] + } + chunk = chunk[1:] + } + } + if failed { + return "", false, nil + } + return s, true, nil +} + +// getEsc gets a possibly-escaped character from chunk, for a character class. +func getEsc(chunk string) (r rune, nchunk string, err error) { + if len(chunk) == 0 || chunk[0] == '-' || chunk[0] == ']' { + err = ErrBadPattern + return + } + if chunk[0] == '\\' { + chunk = chunk[1:] + if len(chunk) == 0 { + err = ErrBadPattern + return + } + } + r, n := utf8.DecodeRuneInString(chunk) + if r == utf8.RuneError && n == 1 { + err = ErrBadPattern + } + nchunk = chunk[n:] + if len(nchunk) == 0 { + err = ErrBadPattern + } + return +} diff --git a/src/path/match_test.go b/src/path/match_test.go new file mode 100644 index 0000000..996bd69 --- /dev/null +++ b/src/path/match_test.go @@ -0,0 +1,84 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package path_test + +import ( + . "path" + "testing" +) + +type MatchTest struct { + pattern, s string + match bool + err error +} + +var matchTests = []MatchTest{ + {"abc", "abc", true, nil}, + {"*", "abc", true, nil}, + {"*c", "abc", true, nil}, + {"a*", "a", true, nil}, + {"a*", "abc", true, nil}, + {"a*", "ab/c", false, nil}, + {"a*/b", "abc/b", true, nil}, + {"a*/b", "a/c/b", false, nil}, + {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil}, + {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil}, + {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil}, + {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil}, + {"a*b?c*x", "abxbbxdbxebxczzx", true, nil}, + {"a*b?c*x", "abxbbxdbxebxczzy", false, nil}, + {"ab[c]", "abc", true, nil}, + {"ab[b-d]", "abc", true, nil}, + {"ab[e-g]", "abc", false, nil}, + {"ab[^c]", "abc", false, nil}, + {"ab[^b-d]", "abc", false, nil}, + {"ab[^e-g]", "abc", true, nil}, + {"a\\*b", "a*b", true, nil}, + {"a\\*b", "ab", false, nil}, + {"a?b", "a☺b", true, nil}, + {"a[^a]b", "a☺b", true, nil}, + {"a???b", "a☺b", false, nil}, + {"a[^a][^a][^a]b", "a☺b", false, nil}, + {"[a-ζ]*", "α", true, nil}, + {"*[a-ζ]", "A", false, nil}, + {"a?b", "a/b", false, nil}, + {"a*b", "a/b", false, nil}, + {"[\\]a]", "]", true, nil}, + {"[\\-]", "-", true, nil}, + {"[x\\-]", "x", true, nil}, + {"[x\\-]", "-", true, nil}, + {"[x\\-]", "z", false, nil}, + {"[\\-x]", "x", true, nil}, + {"[\\-x]", "-", true, nil}, + {"[\\-x]", "a", false, nil}, + {"[]a]", "]", false, ErrBadPattern}, + {"[-]", "-", false, ErrBadPattern}, + {"[x-]", "x", false, ErrBadPattern}, + {"[x-]", "-", false, ErrBadPattern}, + {"[x-]", "z", false, ErrBadPattern}, + {"[-x]", "x", false, ErrBadPattern}, + {"[-x]", "-", false, ErrBadPattern}, + {"[-x]", "a", false, ErrBadPattern}, + {"\\", "a", false, ErrBadPattern}, + {"[a-b-c]", "a", false, ErrBadPattern}, + {"[", "a", false, ErrBadPattern}, + {"[^", "a", false, ErrBadPattern}, + {"[^bc", "a", false, ErrBadPattern}, + {"a[", "a", false, ErrBadPattern}, + {"a[", "ab", false, ErrBadPattern}, + {"a[", "x", false, ErrBadPattern}, + {"a/b[", "x", false, ErrBadPattern}, + {"*x", "xxx", true, nil}, +} + +func TestMatch(t *testing.T) { + for _, tt := range matchTests { + ok, err := Match(tt.pattern, tt.s) + if ok != tt.match || err != tt.err { + t.Errorf("Match(%#q, %#q) = %v, %v want %v, %v", tt.pattern, tt.s, ok, err, tt.match, tt.err) + } + } +} diff --git a/src/path/path.go b/src/path/path.go new file mode 100644 index 0000000..f1f3499 --- /dev/null +++ b/src/path/path.go @@ -0,0 +1,233 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package path implements utility routines for manipulating slash-separated +// paths. +// +// The path package should only be used for paths separated by forward +// slashes, such as the paths in URLs. This package does not deal with +// Windows paths with drive letters or backslashes; to manipulate +// operating system paths, use the path/filepath package. +package path + +// A lazybuf is a lazily constructed path buffer. +// It supports append, reading previously appended bytes, +// and retrieving the final string. It does not allocate a buffer +// to hold the output until that output diverges from s. +type lazybuf struct { + s string + buf []byte + w int +} + +func (b *lazybuf) index(i int) byte { + if b.buf != nil { + return b.buf[i] + } + return b.s[i] +} + +func (b *lazybuf) append(c byte) { + if b.buf == nil { + if b.w < len(b.s) && b.s[b.w] == c { + b.w++ + return + } + b.buf = make([]byte, len(b.s)) + copy(b.buf, b.s[:b.w]) + } + b.buf[b.w] = c + b.w++ +} + +func (b *lazybuf) string() string { + if b.buf == nil { + return b.s[:b.w] + } + return string(b.buf[:b.w]) +} + +// Clean returns the shortest path name equivalent to path +// by purely lexical processing. It applies the following rules +// iteratively until no further processing can be done: +// +// 1. Replace multiple slashes with a single slash. +// 2. Eliminate each . path name element (the current directory). +// 3. Eliminate each inner .. path name element (the parent directory) +// along with the non-.. element that precedes it. +// 4. Eliminate .. elements that begin a rooted path: +// that is, replace "/.." by "/" at the beginning of a path. +// +// The returned path ends in a slash only if it is the root "/". +// +// If the result of this process is an empty string, Clean +// returns the string ".". +// +// See also Rob Pike, ``Lexical File Names in Plan 9 or +// Getting Dot-Dot Right,'' +// https://9p.io/sys/doc/lexnames.html +func Clean(path string) string { + if path == "" { + return "." + } + + rooted := path[0] == '/' + n := len(path) + + // 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 + // it is the leading slash or it is a leading ../../.. prefix. + out := lazybuf{s: path} + r, dotdot := 0, 0 + if rooted { + out.append('/') + r, dotdot = 1, 1 + } + + for r < n { + switch { + case path[r] == '/': + // empty path element + r++ + case path[r] == '.' && (r+1 == n || path[r+1] == '/'): + // . element + r++ + case path[r] == '.' && path[r+1] == '.' && (r+2 == n || path[r+2] == '/'): + // .. element: remove to last / + r += 2 + switch { + case out.w > dotdot: + // can backtrack + out.w-- + for out.w > dotdot && out.index(out.w) != '/' { + out.w-- + } + case !rooted: + // cannot backtrack, but not rooted, so append .. element. + if out.w > 0 { + out.append('/') + } + out.append('.') + out.append('.') + dotdot = out.w + } + default: + // real path element. + // add slash if needed + if rooted && out.w != 1 || !rooted && out.w != 0 { + out.append('/') + } + // copy element + for ; r < n && path[r] != '/'; r++ { + out.append(path[r]) + } + } + } + + // Turn empty string into "." + if out.w == 0 { + return "." + } + + return out.string() +} + +// lastSlash(s) is strings.LastIndex(s, "/") but we can't import strings. +func lastSlash(s string) int { + i := len(s) - 1 + for i >= 0 && s[i] != '/' { + i-- + } + return i +} + +// Split splits path immediately following the final slash, +// separating it into a directory and file name component. +// If there is no slash in path, Split returns an empty dir and +// file set to path. +// The returned values have the property that path = dir+file. +func Split(path string) (dir, file string) { + i := lastSlash(path) + return path[:i+1], path[i+1:] +} + +// Join joins any number of path elements into a single path, +// separating them with slashes. Empty elements are ignored. +// The result is Cleaned. However, if the argument list is +// empty or all its elements are empty, Join returns +// an empty string. +func Join(elem ...string) string { + size := 0 + for _, e := range elem { + size += len(e) + } + if size == 0 { + return "" + } + buf := make([]byte, 0, size+len(elem)-1) + for _, e := range elem { + if len(buf) > 0 || e != "" { + if len(buf) > 0 { + buf = append(buf, '/') + } + buf = append(buf, e...) + } + } + return Clean(string(buf)) +} + +// Ext returns the file name extension used by path. +// The extension is the suffix beginning at the final dot +// in the final slash-separated element of path; +// it is empty if there is no dot. +func Ext(path string) string { + for i := len(path) - 1; i >= 0 && path[i] != '/'; i-- { + if path[i] == '.' { + return path[i:] + } + } + return "" +} + +// Base returns the last element of path. +// Trailing slashes are removed before extracting the last element. +// If the path is empty, Base returns ".". +// If the path consists entirely of slashes, Base returns "/". +func Base(path string) string { + if path == "" { + return "." + } + // Strip trailing slashes. + for len(path) > 0 && path[len(path)-1] == '/' { + path = path[0 : len(path)-1] + } + // Find the last element + if i := lastSlash(path); i >= 0 { + path = path[i+1:] + } + // If empty now, it had only slashes. + if path == "" { + return "/" + } + return path +} + +// IsAbs reports whether the path is absolute. +func IsAbs(path string) bool { + return len(path) > 0 && path[0] == '/' +} + +// Dir returns all but the last element of path, typically the path's directory. +// After dropping the final element using Split, the path is Cleaned and trailing +// slashes are removed. +// If the path is empty, Dir returns ".". +// If the path consists entirely of slashes followed by non-slash bytes, Dir +// returns a single slash. In any other case, the returned path does not end in a +// slash. +func Dir(path string) string { + dir, _ := Split(path) + return Clean(dir) +} diff --git a/src/path/path_test.go b/src/path/path_test.go new file mode 100644 index 0000000..a57286f --- /dev/null +++ b/src/path/path_test.go @@ -0,0 +1,236 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package path_test + +import ( + . "path" + "runtime" + "testing" +) + +type PathTest struct { + path, result string +} + +var cleantests = []PathTest{ + // Already clean + {"", "."}, + {"abc", "abc"}, + {"abc/def", "abc/def"}, + {"a/b/c", "a/b/c"}, + {".", "."}, + {"..", ".."}, + {"../..", "../.."}, + {"../../abc", "../../abc"}, + {"/abc", "/abc"}, + {"/", "/"}, + + // Remove trailing slash + {"abc/", "abc"}, + {"abc/def/", "abc/def"}, + {"a/b/c/", "a/b/c"}, + {"./", "."}, + {"../", ".."}, + {"../../", "../.."}, + {"/abc/", "/abc"}, + + // Remove doubled slash + {"abc//def//ghi", "abc/def/ghi"}, + {"//abc", "/abc"}, + {"///abc", "/abc"}, + {"//abc//", "/abc"}, + {"abc//", "abc"}, + + // Remove . elements + {"abc/./def", "abc/def"}, + {"/./abc/def", "/abc/def"}, + {"abc/.", "abc"}, + + // Remove .. elements + {"abc/def/ghi/../jkl", "abc/def/jkl"}, + {"abc/def/../ghi/../jkl", "abc/jkl"}, + {"abc/def/..", "abc"}, + {"abc/def/../..", "."}, + {"/abc/def/../..", "/"}, + {"abc/def/../../..", ".."}, + {"/abc/def/../../..", "/"}, + {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"}, + + // Combinations + {"abc/./../def", "def"}, + {"abc//./../def", "def"}, + {"abc/../../././../def", "../../def"}, +} + +func TestClean(t *testing.T) { + for _, test := range cleantests { + if s := Clean(test.path); s != test.result { + t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result) + } + if s := Clean(test.result); s != test.result { + t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result) + } + } +} + +func TestCleanMallocs(t *testing.T) { + if testing.Short() { + t.Skip("skipping malloc count in short mode") + } + if runtime.GOMAXPROCS(0) > 1 { + t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1") + return + } + + for _, test := range cleantests { + allocs := testing.AllocsPerRun(100, func() { Clean(test.result) }) + if allocs > 0 { + t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs) + } + } +} + +type SplitTest struct { + path, dir, file string +} + +var splittests = []SplitTest{ + {"a/b", "a/", "b"}, + {"a/b/", "a/b/", ""}, + {"a/", "a/", ""}, + {"a", "", "a"}, + {"/", "/", ""}, +} + +func TestSplit(t *testing.T) { + for _, test := range splittests { + if d, f := Split(test.path); d != test.dir || f != test.file { + t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file) + } + } +} + +type JoinTest struct { + elem []string + path string +} + +var jointests = []JoinTest{ + // zero parameters + {[]string{}, ""}, + + // one parameter + {[]string{""}, ""}, + {[]string{"a"}, "a"}, + + // two parameters + {[]string{"a", "b"}, "a/b"}, + {[]string{"a", ""}, "a"}, + {[]string{"", "b"}, "b"}, + {[]string{"/", "a"}, "/a"}, + {[]string{"/", ""}, "/"}, + {[]string{"a/", "b"}, "a/b"}, + {[]string{"a/", ""}, "a"}, + {[]string{"", ""}, ""}, +} + +func TestJoin(t *testing.T) { + for _, test := range jointests { + if p := Join(test.elem...); p != test.path { + t.Errorf("Join(%q) = %q, want %q", test.elem, p, test.path) + } + } +} + +type ExtTest struct { + path, ext string +} + +var exttests = []ExtTest{ + {"path.go", ".go"}, + {"path.pb.go", ".go"}, + {"a.dir/b", ""}, + {"a.dir/b.go", ".go"}, + {"a.dir/", ""}, +} + +func TestExt(t *testing.T) { + for _, test := range exttests { + if x := Ext(test.path); x != test.ext { + t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext) + } + } +} + +var basetests = []PathTest{ + // Already clean + {"", "."}, + {".", "."}, + {"/.", "."}, + {"/", "/"}, + {"////", "/"}, + {"x/", "x"}, + {"abc", "abc"}, + {"abc/def", "def"}, + {"a/b/.x", ".x"}, + {"a/b/c.", "c."}, + {"a/b/c.x", "c.x"}, +} + +func TestBase(t *testing.T) { + for _, test := range basetests { + if s := Base(test.path); s != test.result { + t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result) + } + } +} + +var dirtests = []PathTest{ + {"", "."}, + {".", "."}, + {"/.", "/"}, + {"/", "/"}, + {"////", "/"}, + {"/foo", "/"}, + {"x/", "x"}, + {"abc", "."}, + {"abc/def", "abc"}, + {"abc////def", "abc"}, + {"a/b/.x", "a/b"}, + {"a/b/c.", "a/b"}, + {"a/b/c.x", "a/b"}, +} + +func TestDir(t *testing.T) { + for _, test := range dirtests { + if s := Dir(test.path); s != test.result { + t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result) + } + } +} + +type IsAbsTest struct { + path string + isAbs bool +} + +var isAbsTests = []IsAbsTest{ + {"", false}, + {"/", true}, + {"/usr/bin/gcc", true}, + {"..", false}, + {"/a/../bb", true}, + {".", false}, + {"./", false}, + {"lala", false}, +} + +func TestIsAbs(t *testing.T) { + for _, test := range isAbsTests { + if r := IsAbs(test.path); r != test.isAbs { + t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs) + } + } +} |