]> Cypherpunks repositories - gostls13.git/commitdiff
os: handle backslash and slash both in the path on Windows
authorYasuhiro Matsumoto <mattn.jp@gmail.com>
Wed, 13 Nov 2019 01:02:17 +0000 (10:02 +0900)
committerIan Lance Taylor <iant@golang.org>
Thu, 14 Nov 2019 00:58:33 +0000 (00:58 +0000)
Fixes #35492

Change-Id: I00dce8fd1228f809e0c61013ac4de7a5953cbbf9
Reviewed-on: https://go-review.googlesource.com/c/go/+/206997
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/os/file_windows.go
src/os/path_windows_test.go

index 1e78f4e8670012e6dff3c42b5f2025a9b770b21d..96f934d039917ff7262996899ee32e8ed1c3f76d 100644 (file)
@@ -111,10 +111,17 @@ func openDir(name string) (file *File, err error) {
 
        path := fixLongPath(name)
 
-       if len(path) == 2 && path[1] == ':' || (len(path) > 0 && path[len(path)-1] == '\\') { // it is a drive letter, like C:
+       if len(path) == 2 && path[1] == ':' { // it is a drive letter, like C:
                mask = path + `*`
+       } else if len(path) > 0 {
+               lc := path[len(path)-1]
+               if lc == '/' || lc == '\\' {
+                       mask = path + `*`
+               } else {
+                       mask = path + `\*`
+               }
        } else {
-               mask = path + `\*`
+               mask = `\*`
        }
        maskp, e := syscall.UTF16PtrFromString(mask)
        if e != nil {
index f1745ad132e764b568cb6e5b4efce66db925a3c0..862b4043624b6f7329f84edf566c8220e81b9c6b 100644 (file)
@@ -74,3 +74,18 @@ func TestMkdirAllExtendedLength(t *testing.T) {
                t.Fatalf("MkdirAll(%q) should have failed, but did not", path)
        }
 }
+
+func TestOpenRootSlash(t *testing.T) {
+       tests := []string{
+               `/`,
+               `\`,
+       }
+
+       for _, test := range tests {
+               dir, err := os.Open(test)
+               if err != nil {
+                       t.Fatalf("Open(%q) failed: %v", test, err)
+               }
+               dir.Close()
+       }
+}