Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
3d1afb76
Commit
3d1afb76
authored
Mar 17, 2011
by
Yasuhiro Matsumoto
Committed by
Alex Brainman
Mar 17, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
path: work for windows.
R=brainman, rsc, rsc1 CC=golang-dev
https://golang.org/cl/4249064
parent
4bd0a544
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
118 additions
and
26 deletions
+118
-26
src/pkg/path/filepath/Makefile
src/pkg/path/filepath/Makefile
+1
-1
src/pkg/path/filepath/match_test.go
src/pkg/path/filepath/match_test.go
+10
-0
src/pkg/path/filepath/path.go
src/pkg/path/filepath/path.go
+22
-19
src/pkg/path/filepath/path_test.go
src/pkg/path/filepath/path_test.go
+30
-6
src/pkg/path/filepath/path_unix.go
src/pkg/path/filepath/path_unix.go
+18
-0
src/pkg/path/filepath/path_windows.go
src/pkg/path/filepath/path_windows.go
+37
-0
No files found.
src/pkg/path/filepath/Makefile
View file @
3d1afb76
...
@@ -19,7 +19,7 @@ GOFILES_linux=\
...
@@ -19,7 +19,7 @@ GOFILES_linux=\
path_unix.go
path_unix.go
GOFILES_windows
=
\
GOFILES_windows
=
\
path_
unix
.go
path_
windows
.go
GOFILES
+=
$
(
GOFILES_
$(GOOS)
)
GOFILES
+=
$
(
GOFILES_
$(GOOS)
)
...
...
src/pkg/path/filepath/match_test.go
View file @
3d1afb76
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"os"
"os"
"path/filepath"
"path/filepath"
"testing"
"testing"
"runtime"
)
)
type
MatchTest
struct
{
type
MatchTest
struct
{
...
@@ -69,6 +70,10 @@ var matchTests = []MatchTest{
...
@@ -69,6 +70,10 @@ var matchTests = []MatchTest{
}
}
func
TestMatch
(
t
*
testing
.
T
)
{
func
TestMatch
(
t
*
testing
.
T
)
{
if
runtime
.
GOOS
==
"windows"
{
// XXX: Don't pass for windows.
return
}
for
_
,
tt
:=
range
matchTests
{
for
_
,
tt
:=
range
matchTests
{
ok
,
err
:=
filepath
.
Match
(
tt
.
pattern
,
tt
.
s
)
ok
,
err
:=
filepath
.
Match
(
tt
.
pattern
,
tt
.
s
)
if
ok
!=
tt
.
match
||
err
!=
tt
.
err
{
if
ok
!=
tt
.
match
||
err
!=
tt
.
err
{
...
@@ -79,6 +84,7 @@ func TestMatch(t *testing.T) {
...
@@ -79,6 +84,7 @@ func TestMatch(t *testing.T) {
// contains returns true if vector contains the string s.
// contains returns true if vector contains the string s.
func
contains
(
vector
[]
string
,
s
string
)
bool
{
func
contains
(
vector
[]
string
,
s
string
)
bool
{
s
=
filepath
.
ToSlash
(
s
)
for
_
,
elem
:=
range
vector
{
for
_
,
elem
:=
range
vector
{
if
elem
==
s
{
if
elem
==
s
{
return
true
return
true
...
@@ -97,6 +103,10 @@ var globTests = []struct {
...
@@ -97,6 +103,10 @@ var globTests = []struct {
}
}
func
TestGlob
(
t
*
testing
.
T
)
{
func
TestGlob
(
t
*
testing
.
T
)
{
if
runtime
.
GOOS
==
"windows"
{
// XXX: Don't pass for windows.
return
}
for
_
,
tt
:=
range
globTests
{
for
_
,
tt
:=
range
globTests
{
matches
:=
filepath
.
Glob
(
tt
.
pattern
)
matches
:=
filepath
.
Glob
(
tt
.
pattern
)
if
!
contains
(
matches
,
tt
.
result
)
{
if
!
contains
(
matches
,
tt
.
result
)
{
...
...
src/pkg/path/filepath/path.go
View file @
3d1afb76
...
@@ -13,8 +13,6 @@ import (
...
@@ -13,8 +13,6 @@ import (
"strings"
"strings"
)
)
// BUG(niemeyer): Package filepath does not yet work on Windows.
// Clean returns the shortest path name equivalent to path
// Clean returns the shortest path name equivalent to path
// by purely lexical processing. It applies the following rules
// by purely lexical processing. It applies the following rules
// iteratively until no further processing can be done:
// iteratively until no further processing can be done:
...
@@ -38,36 +36,39 @@ func Clean(path string) string {
...
@@ -38,36 +36,39 @@ func Clean(path string) string {
return
"."
return
"."
}
}
rooted
:=
path
[
0
]
==
Separator
rooted
:=
IsAbs
(
path
)
n
:=
len
(
path
)
// Invariants:
// Invariants:
// reading from path; r is index of next byte to process.
// reading from path; r is index of next byte to process.
// writing to buf; w is index of next byte to write.
// writing to buf; w is index of next byte to write.
// dotdot is index in buf where .. must stop, either because
// dotdot is index in buf where .. must stop, either because
// it is the leading slash or it is a leading ../../.. prefix.
// it is the leading slash or it is a leading ../../.. prefix.
prefix
:=
volumeName
(
path
)
path
=
path
[
len
(
prefix
)
:
]
n
:=
len
(
path
)
buf
:=
[]
byte
(
path
)
buf
:=
[]
byte
(
path
)
r
,
w
,
dotdot
:=
0
,
0
,
0
r
,
w
,
dotdot
:=
0
,
0
,
0
if
rooted
{
if
rooted
{
buf
[
0
]
=
Separator
r
,
w
,
dotdot
=
1
,
1
,
1
r
,
w
,
dotdot
=
1
,
1
,
1
}
}
for
r
<
n
{
for
r
<
n
{
switch
{
switch
{
case
path
[
r
]
==
Separator
:
case
isSeparator
(
path
[
r
])
:
// empty path element
// empty path element
r
++
r
++
case
path
[
r
]
==
'.'
&&
(
r
+
1
==
n
||
path
[
r
+
1
]
==
Separator
)
:
case
path
[
r
]
==
'.'
&&
(
r
+
1
==
n
||
isSeparator
(
path
[
r
+
1
])
)
:
// . element
// . element
r
++
r
++
case
path
[
r
]
==
'.'
&&
path
[
r
+
1
]
==
'.'
&&
(
r
+
2
==
n
||
path
[
r
+
2
]
==
Separator
)
:
case
path
[
r
]
==
'.'
&&
path
[
r
+
1
]
==
'.'
&&
(
r
+
2
==
n
||
isSeparator
(
path
[
r
+
2
])
)
:
// .. element: remove to last separator
// .. element: remove to last separator
r
+=
2
r
+=
2
switch
{
switch
{
case
w
>
dotdot
:
case
w
>
dotdot
:
// can backtrack
// can backtrack
w
--
w
--
for
w
>
dotdot
&&
buf
[
w
]
!=
Separator
{
for
w
>
dotdot
&&
!
isSeparator
(
buf
[
w
])
{
w
--
w
--
}
}
case
!
rooted
:
case
!
rooted
:
...
@@ -90,7 +91,7 @@ func Clean(path string) string {
...
@@ -90,7 +91,7 @@ func Clean(path string) string {
w
++
w
++
}
}
// copy element
// copy element
for
;
r
<
n
&&
path
[
r
]
!=
Separator
;
r
++
{
for
;
r
<
n
&&
!
isSeparator
(
path
[
r
])
;
r
++
{
buf
[
w
]
=
path
[
r
]
buf
[
w
]
=
path
[
r
]
w
++
w
++
}
}
...
@@ -103,7 +104,7 @@ func Clean(path string) string {
...
@@ -103,7 +104,7 @@ func Clean(path string) string {
w
++
w
++
}
}
return
string
(
buf
[
0
:
w
])
return
prefix
+
string
(
buf
[
0
:
w
])
}
}
// ToSlash returns the result of replacing each separator character
// ToSlash returns the result of replacing each separator character
...
@@ -137,7 +138,10 @@ func SplitList(path string) []string {
...
@@ -137,7 +138,10 @@ func SplitList(path string) []string {
// If there are no separators in path, Split returns an empty base
// If there are no separators in path, Split returns an empty base
// and file set to path.
// and file set to path.
func
Split
(
path
string
)
(
dir
,
file
string
)
{
func
Split
(
path
string
)
(
dir
,
file
string
)
{
i
:=
strings
.
LastIndex
(
path
,
string
(
Separator
))
i
:=
len
(
path
)
-
1
for
i
>=
0
&&
!
isSeparator
(
path
[
i
])
{
i
--
}
return
path
[
:
i
+
1
],
path
[
i
+
1
:
]
return
path
[
:
i
+
1
],
path
[
i
+
1
:
]
}
}
...
@@ -157,7 +161,7 @@ func Join(elem ...string) string {
...
@@ -157,7 +161,7 @@ func Join(elem ...string) string {
// in the final element of path; it is empty if there is
// in the final element of path; it is empty if there is
// no dot.
// no dot.
func
Ext
(
path
string
)
string
{
func
Ext
(
path
string
)
string
{
for
i
:=
len
(
path
)
-
1
;
i
>=
0
&&
path
[
i
]
!=
Separator
;
i
--
{
for
i
:=
len
(
path
)
-
1
;
i
>=
0
&&
!
isSeparator
(
path
[
i
])
;
i
--
{
if
path
[
i
]
==
'.'
{
if
path
[
i
]
==
'.'
{
return
path
[
i
:
]
return
path
[
i
:
]
}
}
...
@@ -250,11 +254,15 @@ func Base(path string) string {
...
@@ -250,11 +254,15 @@ func Base(path string) string {
return
"."
return
"."
}
}
// Strip trailing slashes.
// Strip trailing slashes.
for
len
(
path
)
>
0
&&
path
[
len
(
path
)
-
1
]
==
Separator
{
for
len
(
path
)
>
0
&&
isSeparator
(
path
[
len
(
path
)
-
1
])
{
path
=
path
[
0
:
len
(
path
)
-
1
]
path
=
path
[
0
:
len
(
path
)
-
1
]
}
}
// Find the last element
// Find the last element
if
i
:=
strings
.
LastIndex
(
path
,
string
(
Separator
));
i
>=
0
{
i
:=
len
(
path
)
-
1
for
i
>=
0
&&
!
isSeparator
(
path
[
i
])
{
i
--
}
if
i
>=
0
{
path
=
path
[
i
+
1
:
]
path
=
path
[
i
+
1
:
]
}
}
// If empty now, it had only slashes.
// If empty now, it had only slashes.
...
@@ -263,8 +271,3 @@ func Base(path string) string {
...
@@ -263,8 +271,3 @@ func Base(path string) string {
}
}
return
path
return
path
}
}
// IsAbs returns true if the path is absolute.
func
IsAbs
(
path
string
)
bool
{
return
len
(
path
)
>
0
&&
path
[
0
]
==
Separator
}
src/pkg/path/filepath/path_test.go
View file @
3d1afb76
...
@@ -68,7 +68,7 @@ var cleantests = []PathTest{
...
@@ -68,7 +68,7 @@ var cleantests = []PathTest{
func
TestClean
(
t
*
testing
.
T
)
{
func
TestClean
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
cleantests
{
for
_
,
test
:=
range
cleantests
{
if
s
:=
filepath
.
Clean
(
test
.
path
);
s
!=
test
.
result
{
if
s
:=
filepath
.
ToSlash
(
filepath
.
Clean
(
test
.
path
)
);
s
!=
test
.
result
{
t
.
Errorf
(
"Clean(%q) = %q, want %q"
,
test
.
path
,
s
,
test
.
result
)
t
.
Errorf
(
"Clean(%q) = %q, want %q"
,
test
.
path
,
s
,
test
.
result
)
}
}
}
}
...
@@ -161,6 +161,14 @@ var jointests = []JoinTest{
...
@@ -161,6 +161,14 @@ var jointests = []JoinTest{
{[]
string
{
""
,
""
},
""
},
{[]
string
{
""
,
""
},
""
},
}
}
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:`
,
`Windows`
},
`C:\Windows`
},
}
// join takes a []string and passes it to Join.
// join takes a []string and passes it to Join.
func
join
(
elem
[]
string
,
args
...
string
)
string
{
func
join
(
elem
[]
string
,
args
...
string
)
string
{
args
=
elem
args
=
elem
...
@@ -168,8 +176,11 @@ func join(elem []string, args ...string) string {
...
@@ -168,8 +176,11 @@ func join(elem []string, args ...string) string {
}
}
func
TestJoin
(
t
*
testing
.
T
)
{
func
TestJoin
(
t
*
testing
.
T
)
{
if
runtime
.
GOOS
==
"windows"
{
jointests
=
append
(
jointests
,
winjointests
...
)
}
for
_
,
test
:=
range
jointests
{
for
_
,
test
:=
range
jointests
{
if
p
:=
join
(
test
.
elem
);
p
!=
test
.
path
{
if
p
:=
join
(
test
.
elem
);
p
!=
filepath
.
FromSlash
(
test
.
path
)
{
t
.
Errorf
(
"join(%q) = %q, want %q"
,
test
.
elem
,
p
,
test
.
path
)
t
.
Errorf
(
"join(%q) = %q, want %q"
,
test
.
elem
,
p
,
test
.
path
)
}
}
}
}
...
@@ -261,6 +272,7 @@ func checkMarks(t *testing.T) {
...
@@ -261,6 +272,7 @@ func checkMarks(t *testing.T) {
// Assumes that each node name is unique. Good enough for a test.
// Assumes that each node name is unique. Good enough for a test.
func
mark
(
name
string
)
{
func
mark
(
name
string
)
{
name
=
filepath
.
ToSlash
(
name
)
walkTree
(
tree
,
tree
.
name
,
func
(
path
string
,
n
*
Node
)
{
walkTree
(
tree
,
tree
.
name
,
func
(
path
string
,
n
*
Node
)
{
if
n
.
name
==
name
{
if
n
.
name
==
name
{
n
.
mark
++
n
.
mark
++
...
@@ -302,7 +314,7 @@ func TestWalk(t *testing.T) {
...
@@ -302,7 +314,7 @@ func TestWalk(t *testing.T) {
}
}
checkMarks
(
t
)
checkMarks
(
t
)
if
os
.
Getuid
()
!=
0
{
if
os
.
Getuid
()
>
0
{
// introduce 2 errors: chmod top-level directories to 0
// 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
[
1
]
.
name
),
0
)
os
.
Chmod
(
filepath
.
Join
(
tree
.
name
,
tree
.
entries
[
3
]
.
name
),
0
)
os
.
Chmod
(
filepath
.
Join
(
tree
.
name
,
tree
.
entries
[
3
]
.
name
),
0
)
...
@@ -361,7 +373,7 @@ var basetests = []PathTest{
...
@@ -361,7 +373,7 @@ var basetests = []PathTest{
func
TestBase
(
t
*
testing
.
T
)
{
func
TestBase
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
basetests
{
for
_
,
test
:=
range
basetests
{
if
s
:=
filepath
.
Base
(
test
.
path
);
s
!=
test
.
result
{
if
s
:=
filepath
.
ToSlash
(
filepath
.
Base
(
test
.
path
)
);
s
!=
test
.
result
{
t
.
Errorf
(
"Base(%q) = %q, want %q"
,
test
.
path
,
s
,
test
.
result
)
t
.
Errorf
(
"Base(%q) = %q, want %q"
,
test
.
path
,
s
,
test
.
result
)
}
}
}
}
...
@@ -372,7 +384,7 @@ type IsAbsTest struct {
...
@@ -372,7 +384,7 @@ type IsAbsTest struct {
isAbs
bool
isAbs
bool
}
}
var
is
AbsT
ests
=
[]
IsAbsTest
{
var
is
abst
ests
=
[]
IsAbsTest
{
{
""
,
false
},
{
""
,
false
},
{
"/"
,
true
},
{
"/"
,
true
},
{
"/usr/bin/gcc"
,
true
},
{
"/usr/bin/gcc"
,
true
},
...
@@ -383,8 +395,20 @@ var isAbsTests = []IsAbsTest{
...
@@ -383,8 +395,20 @@ var isAbsTests = []IsAbsTest{
{
"lala"
,
false
},
{
"lala"
,
false
},
}
}
var
winisabstests
=
[]
IsAbsTest
{
{
`C:\`
,
true
},
{
`c\`
,
false
},
{
`c::`
,
false
},
{
`/`
,
true
},
{
`\`
,
true
},
{
`\Windows`
,
true
},
}
func
TestIsAbs
(
t
*
testing
.
T
)
{
func
TestIsAbs
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
isAbsTests
{
if
runtime
.
GOOS
==
"windows"
{
isabstests
=
append
(
isabstests
,
winisabstests
...
)
}
for
_
,
test
:=
range
isabstests
{
if
r
:=
filepath
.
IsAbs
(
test
.
path
);
r
!=
test
.
isAbs
{
if
r
:=
filepath
.
IsAbs
(
test
.
path
);
r
!=
test
.
isAbs
{
t
.
Errorf
(
"IsAbs(%q) = %v, want %v"
,
test
.
path
,
r
,
test
.
isAbs
)
t
.
Errorf
(
"IsAbs(%q) = %v, want %v"
,
test
.
path
,
r
,
test
.
isAbs
)
}
}
...
...
src/pkg/path/filepath/path_unix.go
View file @
3d1afb76
...
@@ -4,7 +4,25 @@
...
@@ -4,7 +4,25 @@
package
filepath
package
filepath
import
"strings"
const
(
const
(
Separator
=
'/'
// OS-specific path separator
Separator
=
'/'
// OS-specific path separator
ListSeparator
=
':'
// OS-specific path list 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
,
"/"
)
}
// volumeName returns the leading volume name on Windows.
// It returns "" on Unix.
func
volumeName
(
path
string
)
string
{
return
""
}
src/pkg/path/filepath/path_windows.go
0 → 100644
View file @
3d1afb76
// 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
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
==
'/'
}
// IsAbs returns true if the path is absolute.
func
IsAbs
(
path
string
)
bool
{
return
path
!=
""
&&
(
volumeName
(
path
)
!=
""
||
isSeparator
(
path
[
0
]))
}
// volumeName return leading volume name.
// If given "C:\foo\bar", return "C:" on windows.
func
volumeName
(
path
string
)
string
{
if
path
==
""
{
return
""
}
// with drive letter
c
:=
path
[
0
]
if
len
(
path
)
>
2
&&
path
[
1
]
==
':'
&&
isSeparator
(
path
[
2
])
&&
(
'0'
<=
c
&&
c
<=
'9'
||
'a'
<=
c
&&
c
<=
'z'
||
'A'
<=
c
&&
c
<=
'Z'
)
{
return
path
[
0
:
2
]
}
return
""
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment