aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/path
diff options
context:
space:
mode:
authorIan Lance Taylor <ian@gcc.gnu.org>2015-10-31 00:59:47 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2015-10-31 00:59:47 +0000
commitaf146490bb04205107cb23e301ec7a8ff927b5fc (patch)
tree13beeaed3698c61903fe93fb1ce70bd9b18d4e7f /libgo/go/path
parent725e1be3406315d9bcc8195d7eef0a7082b3c7cc (diff)
downloadgcc-af146490bb04205107cb23e301ec7a8ff927b5fc.zip
gcc-af146490bb04205107cb23e301ec7a8ff927b5fc.tar.gz
gcc-af146490bb04205107cb23e301ec7a8ff927b5fc.tar.bz2
runtime: Remove now unnecessary pad field from ParFor.
It is not needed due to the removal of the ctx field. Reviewed-on: https://go-review.googlesource.com/16525 From-SVN: r229616
Diffstat (limited to 'libgo/go/path')
-rw-r--r--libgo/go/path/filepath/example_unix_test.go28
-rw-r--r--libgo/go/path/filepath/match.go4
-rw-r--r--libgo/go/path/filepath/path.go28
-rw-r--r--libgo/go/path/filepath/path_plan9.go12
-rw-r--r--libgo/go/path/filepath/path_test.go98
-rw-r--r--libgo/go/path/filepath/path_unix.go12
-rw-r--r--libgo/go/path/filepath/path_windows.go39
-rw-r--r--libgo/go/path/filepath/symlink_windows.go27
-rw-r--r--libgo/go/path/match.go2
-rw-r--r--libgo/go/path/path.go4
10 files changed, 204 insertions, 50 deletions
diff --git a/libgo/go/path/filepath/example_unix_test.go b/libgo/go/path/filepath/example_unix_test.go
index f3fe076..27d85d1 100644
--- a/libgo/go/path/filepath/example_unix_test.go
+++ b/libgo/go/path/filepath/example_unix_test.go
@@ -37,3 +37,31 @@ func ExampleRel() {
// "/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"
+}
diff --git a/libgo/go/path/filepath/match.go b/libgo/go/path/filepath/match.go
index ecc07aa..89f16de 100644
--- a/libgo/go/path/filepath/match.go
+++ b/libgo/go/path/filepath/match.go
@@ -16,7 +16,7 @@ import (
// ErrBadPattern indicates a globbing pattern was malformed.
var ErrBadPattern = errors.New("syntax error in pattern")
-// Match returns true if name matches the shell file name pattern.
+// Match reports whether name matches the shell file name pattern.
// The pattern syntax is:
//
// pattern:
@@ -301,7 +301,7 @@ func glob(dir, pattern string, matches []string) (m []string, e error) {
return
}
-// hasMeta returns true if path contains any of the magic characters
+// hasMeta reports whether path contains any of the magic characters
// recognized by Match.
func hasMeta(path string) bool {
// TODO(niemeyer): Should other magic characters be added here?
diff --git a/libgo/go/path/filepath/path.go b/libgo/go/path/filepath/path.go
index d37fc9d..5dc5cfd 100644
--- a/libgo/go/path/filepath/path.go
+++ b/libgo/go/path/filepath/path.go
@@ -4,6 +4,9 @@
// Package filepath implements utility routines for manipulating filename paths
// in a way compatible with the target operating system-defined file paths.
+//
+// Functions in this package replace any occurrences of the slash ('/') character
+// with os.PathSeparator when returning paths unless otherwise specified.
package filepath
import (
@@ -174,7 +177,8 @@ func FromSlash(path string) string {
// 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.
+// Unlike strings.Split, SplitList returns an empty slice when passed an empty
+// string. SplitList does not replace slash characters in the returned paths.
func SplitList(path string) []string {
return splitList(path)
}
@@ -196,13 +200,10 @@ func Split(path string) (dir, file string) {
// Join joins any number of path elements into a single path, adding
// a Separator if necessary. The result is Cleaned, in particular
// all empty strings are ignored.
+// On Windows, the result is a UNC path if and only if the first path
+// element is a UNC path.
func Join(elem ...string) string {
- for i, e := range elem {
- if e != "" {
- return Clean(strings.Join(elem[i:], string(Separator)))
- }
- }
- return ""
+ return join(elem)
}
// Ext returns the file name extension used by path.
@@ -334,10 +335,11 @@ var SkipDir = errors.New("skip this directory")
// If there was a problem walking to the file or directory named by path, the
// incoming error will describe the problem and the function can decide how
// to handle that error (and Walk will not descend into that directory). If
-// an error is returned, processing stops. The sole exception is that if path
-// is a directory and the function returns the special value SkipDir, the
-// contents of the directory are skipped and processing continues as usual on
-// the next file.
+// an error is returned, processing stops. The sole exception is when the function
+// returns the special value SkipDir. If the function returns SkipDir when invoked
+// on a directory, Walk skips the directory's contents entirely.
+// If the function returns SkipDir when invoked on a non-directory file,
+// Walk skips the remaining files in the containing directory.
type WalkFunc func(path string, info os.FileInfo, err error) error
var lstat = os.Lstat // for testing
@@ -456,9 +458,9 @@ func Dir(path string) string {
}
// VolumeName returns leading volume name.
-// Given "C:\foo\bar" it returns "C:" under windows.
+// 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) (v string) {
+func VolumeName(path string) string {
return path[:volumeNameLen(path)]
}
diff --git a/libgo/go/path/filepath/path_plan9.go b/libgo/go/path/filepath/path_plan9.go
index ee8912d..962774e 100644
--- a/libgo/go/path/filepath/path_plan9.go
+++ b/libgo/go/path/filepath/path_plan9.go
@@ -6,7 +6,7 @@ package filepath
import "strings"
-// IsAbs returns true if the path is absolute.
+// IsAbs reports whether the path is absolute.
func IsAbs(path string) bool {
return strings.HasPrefix(path, "/") || strings.HasPrefix(path, "#")
}
@@ -32,3 +32,13 @@ func splitList(path string) []string {
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 ""
+}
diff --git a/libgo/go/path/filepath/path_test.go b/libgo/go/path/filepath/path_test.go
index c3ee0cb..b2536cb 100644
--- a/libgo/go/path/filepath/path_test.go
+++ b/libgo/go/path/filepath/path_test.go
@@ -245,6 +245,7 @@ var jointests = []JoinTest{
// one parameter
{[]string{""}, ""},
+ {[]string{"/"}, "/"},
{[]string{"a"}, "a"},
// two parameters
@@ -252,10 +253,16 @@ var jointests = []JoinTest{
{[]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{
@@ -265,13 +272,17 @@ var winjointests = []JoinTest{
{[]string{`C:\`, `Windows`}, `C:\Windows`},
{[]string{`C:`, `Windows`}, `C:\Windows`},
{[]string{`\\host\share`, `foo`}, `\\host\share\foo`},
+ {[]string{`\\host\share\foo`}, `\\host\share\foo`},
{[]string{`//host/share`, `foo/bar`}, `\\host\share\foo\bar`},
-}
-
-// join takes a []string and passes it to Join.
-func join(elem []string, args ...string) string {
- args = elem
- return filepath.Join(args...)
+ {[]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) {
@@ -279,8 +290,9 @@ func TestJoin(t *testing.T) {
jointests = append(jointests, winjointests...)
}
for _, test := range jointests {
- if p := join(test.elem); p != filepath.FromSlash(test.path) {
- t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path)
+ expected := filepath.FromSlash(test.path)
+ if p := filepath.Join(test.elem...); p != expected {
+ t.Errorf("join(%q) = %q, want %q", test.elem, p, expected)
}
}
}
@@ -390,7 +402,34 @@ func mark(path string, info os.FileInfo, err error, errors *[]error, clear bool)
return nil
}
+func chtmpdir(t *testing.T) (restore func()) {
+ oldwd, err := os.Getwd()
+ if err != nil {
+ t.Fatal("chtmpdir: %v", err)
+ }
+ d, err := ioutil.TempDir("", "test")
+ if err != nil {
+ t.Fatal("chtmpdir: %v", err)
+ }
+ if err := os.Chdir(d); err != nil {
+ t.Fatal("chtmpdir: %v", err)
+ }
+ return func() {
+ if err := os.Chdir(oldwd); err != nil {
+ t.Fatal("chtmpdir: %v", err)
+ }
+ os.RemoveAll(d)
+ }
+}
+
func TestWalk(t *testing.T) {
+ if runtime.GOOS == "darwin" {
+ switch runtime.GOARCH {
+ case "arm", "arm64":
+ restore := chtmpdir(t)
+ defer restore()
+ }
+ }
makeTree(t)
errors := make([]error, 0, 10)
clear := true
@@ -474,6 +513,35 @@ func touch(t *testing.T, name string) {
}
}
+func TestWalkSkipDirOnFile(t *testing.T) {
+ td, err := ioutil.TempDir("", "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
+ filepath.Walk(td, func(path string, info os.FileInfo, err error) error {
+ if strings.HasSuffix(path, "foo2") {
+ sawFoo2 = true
+ }
+ if strings.HasSuffix(path, "foo1") {
+ return filepath.SkipDir
+ }
+ return nil
+ })
+
+ if sawFoo2 {
+ t.Errorf("SkipDir on file foo1 did not block processing of foo2")
+ }
+}
+
func TestWalkFileError(t *testing.T) {
td, err := ioutil.TempDir("", "walktest")
if err != nil {
@@ -999,10 +1067,14 @@ func TestDriveLetterInEvalSymlinks(t *testing.T) {
}
}
-/* This test does not work gccgo, since the sources are arranged
- differently.
-
-func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486
+func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
+ t.Skip("skipping test because gccgo sources are arranged differently.")
+ if runtime.GOOS == "darwin" {
+ switch runtime.GOARCH {
+ case "arm", "arm64":
+ t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH)
+ }
+ }
root, err := filepath.EvalSymlinks(runtime.GOROOT() + "/test")
if err != nil {
t.Fatal(err)
@@ -1032,5 +1104,3 @@ func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id
t.Fatalf("%q not seen", ken)
}
}
-
-*/
diff --git a/libgo/go/path/filepath/path_unix.go b/libgo/go/path/filepath/path_unix.go
index 4e7d0d1..d241d78 100644
--- a/libgo/go/path/filepath/path_unix.go
+++ b/libgo/go/path/filepath/path_unix.go
@@ -8,7 +8,7 @@ package filepath
import "strings"
-// IsAbs returns true if the path is absolute.
+// IsAbs reports whether the path is absolute.
func IsAbs(path string) bool {
return strings.HasPrefix(path, "/")
}
@@ -34,3 +34,13 @@ func splitList(path string) []string {
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 ""
+}
diff --git a/libgo/go/path/filepath/path_windows.go b/libgo/go/path/filepath/path_windows.go
index ec50f6b..bcfe0a3 100644
--- a/libgo/go/path/filepath/path_windows.go
+++ b/libgo/go/path/filepath/path_windows.go
@@ -13,7 +13,7 @@ func isSlash(c uint8) bool {
return c == '\\' || c == '/'
}
-// IsAbs returns true if the path is absolute.
+// IsAbs reports whether the path is absolute.
func IsAbs(path string) (b bool) {
l := volumeNameLen(path)
if l == 0 {
@@ -108,3 +108,40 @@ func splitList(path string) []string {
func abs(path string) (string, error) {
return syscall.FullPath(path)
}
+
+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 {
+ // 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
+}
diff --git a/libgo/go/path/filepath/symlink_windows.go b/libgo/go/path/filepath/symlink_windows.go
index 327c2c8..4b38f6f 100644
--- a/libgo/go/path/filepath/symlink_windows.go
+++ b/libgo/go/path/filepath/symlink_windows.go
@@ -14,18 +14,17 @@ func toShort(path string) (string, error) {
return "", err
}
b := p // GetShortPathName says we can reuse buffer
- n, err := syscall.GetShortPathName(&p[0], &b[0], uint32(len(b)))
- if err != nil {
- return "", err
- }
- if n > uint32(len(b)) {
- b = make([]uint16, n)
+ n := uint32(len(b))
+ for {
n, err = syscall.GetShortPathName(&p[0], &b[0], uint32(len(b)))
if err != nil {
return "", err
}
+ if n <= uint32(len(b)) {
+ return syscall.UTF16ToString(b[:n]), nil
+ }
+ b = make([]uint16, n)
}
- return syscall.UTF16ToString(b), nil
}
func toLong(path string) (string, error) {
@@ -34,19 +33,17 @@ func toLong(path string) (string, error) {
return "", err
}
b := p // GetLongPathName says we can reuse buffer
- n, err := syscall.GetLongPathName(&p[0], &b[0], uint32(len(b)))
- if err != nil {
- return "", err
- }
- if n > uint32(len(b)) {
- b = make([]uint16, n)
+ n := uint32(len(b))
+ for {
n, err = syscall.GetLongPathName(&p[0], &b[0], uint32(len(b)))
if err != nil {
return "", err
}
+ if n <= uint32(len(b)) {
+ return syscall.UTF16ToString(b[:n]), nil
+ }
+ b = make([]uint16, n)
}
- b = b[:n]
- return syscall.UTF16ToString(b), nil
}
func evalSymlinks(path string) (string, error) {
diff --git a/libgo/go/path/match.go b/libgo/go/path/match.go
index 8154bf6..75dd3b3 100644
--- a/libgo/go/path/match.go
+++ b/libgo/go/path/match.go
@@ -13,7 +13,7 @@ import (
// ErrBadPattern indicates a globbing pattern was malformed.
var ErrBadPattern = errors.New("syntax error in pattern")
-// Match returns true if name matches the shell file name pattern.
+// Match reports whether name matches the shell file name pattern.
// The pattern syntax is:
//
// pattern:
diff --git a/libgo/go/path/path.go b/libgo/go/path/path.go
index 98a6d52..77f2185 100644
--- a/libgo/go/path/path.go
+++ b/libgo/go/path/path.go
@@ -134,7 +134,7 @@ func Clean(path string) string {
return out.string()
}
-// Split splits path immediately following the final slash.
+// Split splits path immediately following the final slash,
// separating it into a directory and file name component.
// If there is no slash path, Split returns an empty dir and
// file set to path.
@@ -192,7 +192,7 @@ func Base(path string) string {
return path
}
-// IsAbs returns true if the path is absolute.
+// IsAbs reports whether the path is absolute.
func IsAbs(path string) bool {
return len(path) > 0 && path[0] == '/'
}