Skip to content

Commit

Permalink
os: use backslashes for DirFS on Windows
Browse files Browse the repository at this point in the history
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 <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
Reviewed-by: Alex Brainman <[email protected]>
Reviewed-by: hopehook <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Oct 2, 2022
1 parent 4585bf9 commit 82e357d
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/os/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 82e357d

Please sign in to comment.