Skip to content

Commit b86e766

Browse files
path: use OS-specific function in MkdirAll, don't always keep trailing slash
CL 86295 changed MkdirAll to always pass a trailing path separator to support extended-length paths on Windows. However, when Stat is called on an existing file followed by trailing slash, it will return a "not a directory" error, skipping the fast path at the beginning of MkdirAll. This change fixes MkdirAll to only pass the trailing path separator where required on Windows, by using an OS-specific function fixRootDirectory. Updates #23918 Change-Id: I23f84a20e65ccce556efa743d026d352b4812c34 Reviewed-on: https://go-review.googlesource.com/95255 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: David du Colombier <[email protected]> Reviewed-by: Alex Brainman <[email protected]>
1 parent bae3fd6 commit b86e766

4 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/os/path.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ func MkdirAll(path string, perm FileMode) error {
4040

4141
if j > 1 {
4242
// Create parent.
43-
// Pass trailing path separator to MkdirAll, so our
44-
// algorithm works for paths, like \\?\c:\
45-
err = MkdirAll(path[0:j], perm)
43+
err = MkdirAll(fixRootDirectory(path[:j-1]), perm)
4644
if err != nil {
4745
return err
4846
}

src/os/path_plan9.go

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

src/os/path_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ func basename(name string) string {
3333

3434
return name
3535
}
36+
37+
func fixRootDirectory(p string) string {
38+
return p
39+
}

src/os/path_windows.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,14 @@ func fixLongPath(path string) string {
207207
}
208208
return string(pathbuf[:w])
209209
}
210+
211+
// fixRootDirectory fixes a reference to a drive's root directory to
212+
// have the required trailing slash.
213+
func fixRootDirectory(p string) string {
214+
if len(p) == len(`\\?\c:`) {
215+
if IsPathSeparator(p[0]) && IsPathSeparator(p[1]) && p[2] == '?' && IsPathSeparator(p[3]) && p[5] == ':' {
216+
return p + `\`
217+
}
218+
}
219+
return p
220+
}

0 commit comments

Comments
 (0)