]> Cypherpunks repositories - gostls13.git/commitdiff
path: use OS-specific function in MkdirAll, don't always keep trailing slash
authorIan Lance Taylor <iant@golang.org>
Mon, 19 Feb 2018 21:26:01 +0000 (13:26 -0800)
committerIan Lance Taylor <iant@golang.org>
Fri, 23 Feb 2018 18:37:09 +0000 (18:37 +0000)
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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David du Colombier <0intro@gmail.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
src/os/path.go
src/os/path_plan9.go
src/os/path_unix.go
src/os/path_windows.go

index ec6a7938b2b856848b19cb638ab409763bbe8e53..5c5350670d8cd4bb185289bc557827ef9be95afc 100644 (file)
@@ -40,9 +40,7 @@ func MkdirAll(path string, perm FileMode) error {
 
        if j > 1 {
                // Create parent.
-               // Pass trailing path separator to MkdirAll, so our
-               // algorithm works for paths, like \\?\c:\
-               err = MkdirAll(path[0:j], perm)
+               err = MkdirAll(fixRootDirectory(path[:j-1]), perm)
                if err != nil {
                        return err
                }
index b09b53a3d828cb500f8ba6dc5c396c4a7e5fbafc..a54b4b98f1220a97cdb54c99515a8d5685aa4abb 100644 (file)
@@ -13,3 +13,7 @@ const (
 func IsPathSeparator(c uint8) bool {
        return PathSeparator == c
 }
+
+func fixRootDirectory(p string) string {
+       return p
+}
index ecf098c46125c882eac7f2821b26a09b8d08f2c1..9117ad0ef668107ed45bf998f17c13a0ac8f12dd 100644 (file)
@@ -33,3 +33,7 @@ func basename(name string) string {
 
        return name
 }
+
+func fixRootDirectory(p string) string {
+       return p
+}
index 101b026dc9e1b89bf09cfcab8a8e377f65a35103..87b1cac531790db476cb81094ff5bcfc2bd20ed8 100644 (file)
@@ -207,3 +207,14 @@ func fixLongPath(path string) string {
        }
        return string(pathbuf[:w])
 }
+
+// fixRootDirectory fixes a reference to a drive's root directory to
+// have the required trailing slash.
+func fixRootDirectory(p string) string {
+       if len(p) == len(`\\?\c:`) {
+               if IsPathSeparator(p[0]) && IsPathSeparator(p[1]) && p[2] == '?' && IsPathSeparator(p[3]) && p[5] == ':' {
+                       return p + `\`
+               }
+       }
+       return p
+}