]> Cypherpunks repositories - gostls13.git/commitdiff
os: use backslashes for DirFS on Windows
authorIan Lance Taylor <iant@golang.org>
Sat, 27 Aug 2022 02:22:50 +0000 (19:22 -0700)
committerGopher Robot <gobot@golang.org>
Sun, 2 Oct 2022 02:31:18 +0000 (02:31 +0000)
Otherwise DirFS of a UNC path does not work.

Fixes #54694

Change-Id: I82c1c436f7c26b3935c2cc4fd238daf094fc4d86
Reviewed-on: https://go-review.googlesource.com/c/go/+/426094
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: hopehook <hopehook@golangcn.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/os/file.go

index e2eef8ec5d30dc3427a047dab66c87e6ff642cd6..78677c2f8fdfd88cec1cd55c1780d1b9a8162879 100644 (file)
@@ -642,7 +642,7 @@ func (dir dirFS) Open(name string) (fs.File, error) {
        if !fs.ValidPath(name) || runtime.GOOS == "windows" && containsAny(name, `\:`) {
                return nil, &PathError{Op: "open", Path: name, Err: ErrInvalid}
        }
-       f, err := Open(string(dir) + "/" + name)
+       f, err := Open(dir.join(name))
        if err != nil {
                return nil, err // nil fs.File
        }
@@ -653,13 +653,28 @@ func (dir dirFS) Stat(name string) (fs.FileInfo, error) {
        if !fs.ValidPath(name) || runtime.GOOS == "windows" && containsAny(name, `\:`) {
                return nil, &PathError{Op: "stat", Path: name, Err: ErrInvalid}
        }
-       f, err := Stat(string(dir) + "/" + name)
+       f, err := Stat(dir.join(name))
        if err != nil {
                return nil, err
        }
        return f, nil
 }
 
+// join returns the path for name in dir. We can't always use "/"
+// because that fails on Windows for UNC paths.
+func (dir dirFS) join(name string) string {
+       if runtime.GOOS == "windows" && containsAny(name, "/") {
+               buf := []byte(name)
+               for i, b := range buf {
+                       if b == '/' {
+                               buf[i] = '\\'
+                       }
+               }
+               name = string(buf)
+       }
+       return string(dir) + string(PathSeparator) + name
+}
+
 // ReadFile reads the named file and returns the contents.
 // A successful call returns err == nil, not err == EOF.
 // Because ReadFile reads the whole file, it does not treat an EOF from Read