Skip to content

Commit 5616ab6

Browse files
qmuntalneild
authored andcommitted
os: use filepathlite.VolumeName
It is better to have a single implementation of VolumeName, which is quite tricky to get right on Windows. Change-Id: Ibba82dd71fe10b594cb6f782582430aa422e7078 Reviewed-on: https://go-review.googlesource.com/c/go/+/582499 LUCI-TryBot-Result: Go LUCI <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Damien Neil <[email protected]> Run-TryBot: Quim Muntal <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent d95fa7a commit 5616ab6

5 files changed

Lines changed: 5 additions & 50 deletions

File tree

src/os/file_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ func Symlink(oldname, newname string) error {
292292

293293
// need the exact location of the oldname when it's relative to determine if it's a directory
294294
destpath := oldname
295-
if v := volumeName(oldname); v == "" {
295+
if v := filepathlite.VolumeName(oldname); v == "" {
296296
if len(oldname) > 0 && IsPathSeparator(oldname[0]) {
297297
// oldname is relative to the volume containing newname.
298-
if v = volumeName(newname); v != "" {
298+
if v = filepathlite.VolumeName(newname); v != "" {
299299
// Prepend the volume explicitly, because it may be different from the
300300
// volume of the current working directory.
301301
destpath = v + oldname

src/os/path.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package os
66

77
import (
8+
"internal/filepathlite"
89
"syscall"
910
)
1011

@@ -43,7 +44,7 @@ func MkdirAll(path string, perm FileMode) error {
4344

4445
// If there is a parent directory, and it is not the volume name,
4546
// recurse to ensure parent directory exists.
46-
if parent := path[:i]; len(parent) > len(volumeName(path)) {
47+
if parent := path[:i]; len(parent) > len(filepathlite.VolumeName(path)) {
4748
err = MkdirAll(parent, perm)
4849
if err != nil {
4950
return err

src/os/path_plan9.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,3 @@ const (
1313
func IsPathSeparator(c uint8) bool {
1414
return PathSeparator == c
1515
}
16-
17-
func volumeName(p string) string {
18-
return ""
19-
}

src/os/path_unix.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,3 @@ func splitPath(path string) (string, string) {
6969

7070
return dirname, basename
7171
}
72-
73-
func volumeName(p string) string {
74-
return ""
75-
}

src/os/path_windows.go

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,6 @@ func basename(name string) string {
4545
return name
4646
}
4747

48-
func volumeName(path string) (v string) {
49-
if len(path) < 2 {
50-
return ""
51-
}
52-
// with drive letter
53-
c := path[0]
54-
if path[1] == ':' &&
55-
('0' <= c && c <= '9' || 'a' <= c && c <= 'z' ||
56-
'A' <= c && c <= 'Z') {
57-
return path[:2]
58-
}
59-
// is it UNC
60-
if l := len(path); l >= 5 && IsPathSeparator(path[0]) && IsPathSeparator(path[1]) &&
61-
!IsPathSeparator(path[2]) && path[2] != '.' {
62-
// first, leading `\\` and next shouldn't be `\`. its server name.
63-
for n := 3; n < l-1; n++ {
64-
// second, next '\' shouldn't be repeated.
65-
if IsPathSeparator(path[n]) {
66-
n++
67-
// third, following something characters. its share name.
68-
if !IsPathSeparator(path[n]) {
69-
if path[n] == '.' {
70-
break
71-
}
72-
for ; n < l; n++ {
73-
if IsPathSeparator(path[n]) {
74-
break
75-
}
76-
}
77-
return path[:n]
78-
}
79-
break
80-
}
81-
}
82-
}
83-
return ""
84-
}
85-
8648
func fromSlash(path string) string {
8749
// Replace each '/' with '\\' if present
8850
var pathbuf []byte
@@ -106,7 +68,7 @@ func fromSlash(path string) string {
10668
}
10769

10870
func dirname(path string) string {
109-
vol := volumeName(path)
71+
vol := filepathlite.VolumeName(path)
11072
i := len(path) - 1
11173
for i >= len(vol) && !IsPathSeparator(path[i]) {
11274
i--

0 commit comments

Comments
 (0)