]> Cypherpunks repositories - gostls13.git/commitdiff
os: don't treat RemoveAll("/x") as RemoveAll("x")
authorIan Lance Taylor <iant@golang.org>
Mon, 15 Apr 2019 17:36:17 +0000 (10:36 -0700)
committerIan Lance Taylor <iant@golang.org>
Mon, 15 Apr 2019 18:28:49 +0000 (18:28 +0000)
Fixes #31468

Change-Id: I5c4e61631b8af35bfc14b0cb9bc77feec100e340
Reviewed-on: https://go-review.googlesource.com/c/go/+/172058
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/os/export_unix_test.go [new file with mode: 0644]
src/os/os_unix_test.go
src/os/path_unix.go

diff --git a/src/os/export_unix_test.go b/src/os/export_unix_test.go
new file mode 100644 (file)
index 0000000..032b1a9
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2019 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.
+
+// +build aix darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris
+
+package os
+
+var SplitPath = splitPath
index 87c3bcd8fa6393f640baa81b830807b6b2e62c4a..fa4c594136aaea5e254335296a9b319820250f3f 100644 (file)
@@ -288,3 +288,28 @@ func TestNewFileNonBlock(t *testing.T) {
        t.Parallel()
        newFileTest(t, false)
 }
+
+func TestSplitPath(t *testing.T) {
+       t.Parallel()
+       for _, tt := range []struct{ path, wantDir, wantBase string }{
+               {"a", ".", "a"},
+               {"a/", ".", "a"},
+               {"a//", ".", "a"},
+               {"a/b", "a", "b"},
+               {"a/b/", "a", "b"},
+               {"a/b/c", "a/b", "c"},
+               {"/a", "/", "a"},
+               {"/a/", "/", "a"},
+               {"/a/b", "/a", "b"},
+               {"/a/b/", "/a", "b"},
+               {"/a/b/c", "/a/b", "c"},
+               {"//a", "/", "a"},
+               {"//a/", "/", "a"},
+               {"///a", "/", "a"},
+               {"///a/", "/", "a"},
+       } {
+               if dir, base := SplitPath(tt.path); dir != tt.wantDir || base != tt.wantBase {
+                       t.Errorf("splitPath(%q) = %q, %q, want %q, %q", tt.path, dir, base, tt.wantDir, tt.wantBase)
+               }
+       }
+}
index a08ddaf6dbdcba9da55e22bd0c09b56c0293f994..df423d2c9daa8ab1f6d45963bcc7a1c1e62d8b41 100644 (file)
@@ -38,20 +38,30 @@ func basename(name string) string {
 func splitPath(path string) (string, string) {
        // if no better parent is found, the path is relative from "here"
        dirname := "."
-       // if no slashes in path, base is path
-       basename := path
+
+       // Remove all but one leading slash.
+       for len(path) > 1 && path[0] == '/' && path[1] == '/' {
+               path = path[1:]
+       }
 
        i := len(path) - 1
 
-       // Remove trailing slashes
+       // Remove trailing slashes.
        for ; i > 0 && path[i] == '/'; i-- {
                path = path[:i]
        }
 
+       // if no slashes in path, base is path
+       basename := path
+
        // Remove leading directory path
        for i--; i >= 0; i-- {
                if path[i] == '/' {
-                       dirname = path[:i]
+                       if i == 0 {
+                               dirname = path[:1]
+                       } else {
+                               dirname = path[:i]
+                       }
                        basename = path[i+1:]
                        break
                }