summaryrefslogtreecommitdiff
path: root/src/pkg/path
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/path')
-rw-r--r--src/pkg/path/filepath/match.go7
-rw-r--r--src/pkg/path/filepath/match_test.go9
-rw-r--r--src/pkg/path/filepath/path.go45
-rw-r--r--src/pkg/path/filepath/path_plan9.go10
-rw-r--r--src/pkg/path/filepath/path_test.go65
-rw-r--r--src/pkg/path/filepath/path_unix.go10
-rw-r--r--src/pkg/path/filepath/path_windows.go15
7 files changed, 85 insertions, 76 deletions
diff --git a/src/pkg/path/filepath/match.go b/src/pkg/path/filepath/match.go
index a05bb5f7e..9c344309d 100644
--- a/src/pkg/path/filepath/match.go
+++ b/src/pkg/path/filepath/match.go
@@ -124,9 +124,8 @@ func matchChunk(chunk, s string) (rest string, ok bool, err os.Error) {
s = s[n:]
chunk = chunk[1:]
// possibly negated
- notNegated := true
- if len(chunk) > 0 && chunk[0] == '^' {
- notNegated = false
+ negated := chunk[0] == '^'
+ if negated {
chunk = chunk[1:]
}
// parse all ranges
@@ -152,7 +151,7 @@ func matchChunk(chunk, s string) (rest string, ok bool, err os.Error) {
}
nrange++
}
- if match != notNegated {
+ if match == negated {
return
}
diff --git a/src/pkg/path/filepath/match_test.go b/src/pkg/path/filepath/match_test.go
index 43e1c1cc2..a1c8333f3 100644
--- a/src/pkg/path/filepath/match_test.go
+++ b/src/pkg/path/filepath/match_test.go
@@ -69,6 +69,13 @@ var matchTests = []MatchTest{
{"*x", "xxx", true, nil},
}
+func errp(e os.Error) string {
+ if e == nil {
+ return "<nil>"
+ }
+ return e.String()
+}
+
func TestMatch(t *testing.T) {
if runtime.GOOS == "windows" {
// XXX: Don't pass for windows.
@@ -77,7 +84,7 @@ 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, nil", tt.pattern, tt.s, ok, err, tt.match)
+ t.Errorf("Match(%#q, %#q) = %v, %q want %v, %q", tt.pattern, tt.s, ok, errp(err), tt.match, errp(tt.err))
}
}
}
diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go
index 541a23306..dcd8017ad 100644
--- a/src/pkg/path/filepath/path.go
+++ b/src/pkg/path/filepath/path.go
@@ -9,13 +9,14 @@ package filepath
import (
"bytes"
"os"
+ "runtime"
"sort"
"strings"
)
const (
- SeparatorString = string(Separator)
- ListSeparatorString = string(ListSeparator)
+ Separator = os.PathSeparator
+ ListSeparator = os.PathListSeparator
)
// Clean returns the shortest path name equivalent to path
@@ -60,20 +61,20 @@ func Clean(path string) string {
for r < n {
switch {
- case isSeparator(path[r]):
+ case os.IsPathSeparator(path[r]):
// empty path element
r++
- case path[r] == '.' && (r+1 == n || isSeparator(path[r+1])):
+ case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])):
// . element
r++
- case path[r] == '.' && path[r+1] == '.' && (r+2 == n || isSeparator(path[r+2])):
+ case path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])):
// .. element: remove to last separator
r += 2
switch {
case w > dotdot:
// can backtrack
w--
- for w > dotdot && !isSeparator(buf[w]) {
+ for w > dotdot && !os.IsPathSeparator(buf[w]) {
w--
}
case !rooted:
@@ -96,7 +97,7 @@ func Clean(path string) string {
w++
}
// copy element
- for ; r < n && !isSeparator(path[r]); r++ {
+ for ; r < n && !os.IsPathSeparator(path[r]); r++ {
buf[w] = path[r]
w++
}
@@ -118,7 +119,7 @@ func ToSlash(path string) string {
if Separator == '/' {
return path
}
- return strings.Replace(path, SeparatorString, "/", -1)
+ return strings.Replace(path, string(Separator), "/", -1)
}
// FromSlash returns the result of replacing each slash ('/') character
@@ -127,7 +128,7 @@ func FromSlash(path string) string {
if Separator == '/' {
return path
}
- return strings.Replace(path, "/", SeparatorString, -1)
+ return strings.Replace(path, "/", string(Separator), -1)
}
// SplitList splits a list of paths joined by the OS-specific ListSeparator.
@@ -135,7 +136,7 @@ func SplitList(path string) []string {
if path == "" {
return []string{}
}
- return strings.Split(path, ListSeparatorString, -1)
+ return strings.Split(path, string(ListSeparator), -1)
}
// Split splits path immediately following the final Separator,
@@ -144,7 +145,7 @@ func SplitList(path string) []string {
// and file set to path.
func Split(path string) (dir, file string) {
i := len(path) - 1
- for i >= 0 && !isSeparator(path[i]) {
+ for i >= 0 && !os.IsPathSeparator(path[i]) {
i--
}
return path[:i+1], path[i+1:]
@@ -155,7 +156,7 @@ func Split(path string) (dir, file string) {
func Join(elem ...string) string {
for i, e := range elem {
if e != "" {
- return Clean(strings.Join(elem[i:], SeparatorString))
+ return Clean(strings.Join(elem[i:], string(Separator)))
}
}
return ""
@@ -166,7 +167,7 @@ func Join(elem ...string) string {
// 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 && !isSeparator(path[i]); i-- {
+ for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- {
if path[i] == '.' {
return path[i:]
}
@@ -178,6 +179,14 @@ func Ext(path string) string {
// links.
// If path is relative it will be evaluated relative to the current directory.
func EvalSymlinks(path string) (string, os.Error) {
+ if runtime.GOOS == "windows" {
+ // Symlinks are not supported under windows.
+ _, err := os.Lstat(path)
+ if err != nil {
+ return "", err
+ }
+ return Clean(path), nil
+ }
const maxIter = 255
originalPath := path
// consume path by taking each frontmost path element,
@@ -225,7 +234,7 @@ func EvalSymlinks(path string) (string, os.Error) {
if IsAbs(dest) {
b.Reset()
}
- path = dest + SeparatorString + path
+ path = dest + string(Separator) + path
}
return Clean(b.String()), nil
}
@@ -236,7 +245,7 @@ func EvalSymlinks(path string) (string, os.Error) {
// path name for a given file is not guaranteed to be unique.
func Abs(path string) (string, os.Error) {
if IsAbs(path) {
- return path, nil
+ return Clean(path), nil
}
wd, err := os.Getwd()
if err != nil {
@@ -330,12 +339,12 @@ func Base(path string) string {
return "."
}
// Strip trailing slashes.
- for len(path) > 0 && isSeparator(path[len(path)-1]) {
+ for len(path) > 0 && os.IsPathSeparator(path[len(path)-1]) {
path = path[0 : len(path)-1]
}
// Find the last element
i := len(path) - 1
- for i >= 0 && !isSeparator(path[i]) {
+ for i >= 0 && !os.IsPathSeparator(path[i]) {
i--
}
if i >= 0 {
@@ -343,7 +352,7 @@ func Base(path string) string {
}
// If empty now, it had only slashes.
if path == "" {
- return SeparatorString
+ return string(Separator)
}
return path
}
diff --git a/src/pkg/path/filepath/path_plan9.go b/src/pkg/path/filepath/path_plan9.go
index e40008364..47990e0fe 100644
--- a/src/pkg/path/filepath/path_plan9.go
+++ b/src/pkg/path/filepath/path_plan9.go
@@ -6,16 +6,6 @@ package filepath
import "strings"
-const (
- Separator = '/' // OS-specific path separator
- ListSeparator = 0 // OS-specific path list separator
-)
-
-// isSeparator returns true if c is a directory separator character.
-func isSeparator(c uint8) bool {
- return Separator == c
-}
-
// IsAbs returns true if the path is absolute.
func IsAbs(path string) bool {
return strings.HasPrefix(path, "/") || strings.HasPrefix(path, "#")
diff --git a/src/pkg/path/filepath/path_test.go b/src/pkg/path/filepath/path_test.go
index b3b6eb5ab..6a5dd5b00 100644
--- a/src/pkg/path/filepath/path_test.go
+++ b/src/pkg/path/filepath/path_test.go
@@ -315,7 +315,10 @@ func TestWalk(t *testing.T) {
}
checkMarks(t)
- if os.Getuid() > 0 {
+ // 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 gotest -short.
+ if os.Getuid() > 0 && !testing.Short() {
// 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)
@@ -440,48 +443,64 @@ var EvalSymlinksTests = []EvalSymlinksTest{
{"test/link2/link3/test", "test"},
}
-func TestEvalSymlinks(t *testing.T) {
- // Symlinks are not supported under windows.
- if runtime.GOOS == "windows" {
- return
+var EvalSymlinksAbsWindowsTests = []EvalSymlinksTest{
+ {`c:\`, `c:\`},
+}
+
+func testEvalSymlinks(t *testing.T, tests []EvalSymlinksTest) {
+ for _, d := range tests {
+ if p, err := filepath.EvalSymlinks(d.path); err != nil {
+ t.Errorf("EvalSymlinks(%q) error: %v", d.path, err)
+ } else if filepath.Clean(p) != filepath.Clean(d.dest) {
+ t.Errorf("EvalSymlinks(%q)=%q, want %q", d.path, p, d.dest)
+ }
}
+}
+
+func TestEvalSymlinks(t *testing.T) {
defer os.RemoveAll("test")
for _, d := range EvalSymlinksTestDirs {
var err os.Error
if d.dest == "" {
err = os.Mkdir(d.path, 0755)
} else {
- err = os.Symlink(d.dest, d.path)
+ if runtime.GOOS != "windows" {
+ err = os.Symlink(d.dest, d.path)
+ }
}
if err != nil {
t.Fatal(err)
}
}
- // relative
- for _, d := range EvalSymlinksTests {
- if p, err := filepath.EvalSymlinks(d.path); err != nil {
- t.Errorf("EvalSymlinks(%q) error: %v", d.path, err)
- } else if p != d.dest {
- t.Errorf("EvalSymlinks(%q)=%q, want %q", d.path, p, d.dest)
+ var tests []EvalSymlinksTest
+ if runtime.GOOS == "windows" {
+ for _, d := range EvalSymlinksTests {
+ if d.path == d.dest {
+ // will test only real files and directories
+ tests = append(tests, d)
+ }
}
+ } else {
+ tests = EvalSymlinksTests
}
+ // relative
+ testEvalSymlinks(t, tests)
// absolute
goroot, err := filepath.EvalSymlinks(os.Getenv("GOROOT"))
if err != nil {
t.Fatalf("EvalSymlinks(%q) error: %v", os.Getenv("GOROOT"), err)
}
testroot := filepath.Join(goroot, "src", "pkg", "path", "filepath")
- for _, d := range EvalSymlinksTests {
- a := EvalSymlinksTest{
- filepath.Join(testroot, d.path),
- filepath.Join(testroot, d.dest),
- }
- if p, err := filepath.EvalSymlinks(a.path); err != nil {
- t.Errorf("EvalSymlinks(%q) error: %v", a.path, err)
- } else if p != a.dest {
- t.Errorf("EvalSymlinks(%q)=%q, want %q", a.path, p, a.dest)
+ for i, d := range tests {
+ tests[i].path = filepath.Join(testroot, d.path)
+ tests[i].dest = filepath.Join(testroot, d.dest)
+ }
+ if runtime.GOOS == "windows" {
+ for _, d := range EvalSymlinksAbsWindowsTests {
+ tests = append(tests, d)
}
}
+ testEvalSymlinks(t, tests)
}
// Test paths relative to $GOROOT/src
@@ -493,6 +512,7 @@ var abstests = []string{
// Already absolute
"$GOROOT/src/Make.pkg",
+ "$GOROOT/src/../src/Make.pkg",
}
func TestAbs(t *testing.T) {
@@ -521,5 +541,8 @@ func TestAbs(t *testing.T) {
if !filepath.IsAbs(abspath) {
t.Errorf("Abs(%q)=%q, not an absolute path", path, abspath)
}
+ if filepath.IsAbs(path) && abspath != filepath.Clean(path) {
+ t.Errorf("Abs(%q)=%q, isn't clean", path, abspath)
+ }
}
}
diff --git a/src/pkg/path/filepath/path_unix.go b/src/pkg/path/filepath/path_unix.go
index f8ac248fb..ea555fc0e 100644
--- a/src/pkg/path/filepath/path_unix.go
+++ b/src/pkg/path/filepath/path_unix.go
@@ -6,16 +6,6 @@ package filepath
import "strings"
-const (
- Separator = '/' // OS-specific path separator
- ListSeparator = ':' // OS-specific path list separator
-)
-
-// isSeparator returns true if c is a directory separator character.
-func isSeparator(c uint8) bool {
- return Separator == c
-}
-
// IsAbs returns true if the path is absolute.
func IsAbs(path string) bool {
return strings.HasPrefix(path, "/")
diff --git a/src/pkg/path/filepath/path_windows.go b/src/pkg/path/filepath/path_windows.go
index dbd1c1e40..35302eb1a 100644
--- a/src/pkg/path/filepath/path_windows.go
+++ b/src/pkg/path/filepath/path_windows.go
@@ -4,20 +4,11 @@
package filepath
-const (
- Separator = '\\' // OS-specific path separator
- ListSeparator = ':' // OS-specific path list separator
-)
-
-// isSeparator returns true if c is a directory separator character.
-func isSeparator(c uint8) bool {
- // NOTE: Windows accept / as path separator.
- return c == '\\' || c == '/'
-}
+import "os"
// IsAbs returns true if the path is absolute.
func IsAbs(path string) bool {
- return path != "" && (volumeName(path) != "" || isSeparator(path[0]))
+ return path != "" && (volumeName(path) != "" || os.IsPathSeparator(path[0]))
}
// volumeName return leading volume name.
@@ -28,7 +19,7 @@ func volumeName(path string) string {
}
// with drive letter
c := path[0]
- if len(path) > 2 && path[1] == ':' && isSeparator(path[2]) &&
+ if len(path) > 2 && path[1] == ':' && os.IsPathSeparator(path[2]) &&
('0' <= c && c <= '9' || 'a' <= c && c <= 'z' ||
'A' <= c && c <= 'Z') {
return path[0:2]