Skip to content

Commit 8b9b584

Browse files
committed
Fix defaultFs relative path check for windows
1 parent da5f798 commit 8b9b584

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

echo_fs_go1.16.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/url"
1111
"os"
1212
"path/filepath"
13+
"runtime"
1314
"strings"
1415
)
1516

@@ -127,7 +128,7 @@ func subFS(currentFs fs.FS, root string) (fs.FS, error) {
127128
// we need to make exception for `defaultFS` instances as it interprets root prefix differently from fs.FS.
128129
// fs.Fs.Open does not like relative paths ("./", "../") and absolute paths at all but prior echo.Filesystem we
129130
// were able to use paths like `./myfile.log`, `/etc/hosts` and these would work fine with `os.Open` but not with fs.Fs
130-
if len(root) > 0 && root[0] != '/' {
131+
if isRelativePath(root) {
131132
root = filepath.Join(dFS.prefix, root)
132133
}
133134
return &defaultFS{
@@ -138,6 +139,21 @@ func subFS(currentFs fs.FS, root string) (fs.FS, error) {
138139
return fs.Sub(currentFs, root)
139140
}
140141

142+
func isRelativePath(path string) bool {
143+
if path == "" {
144+
return true
145+
}
146+
if path[0] == '/' {
147+
return false
148+
}
149+
if runtime.GOOS == "windows" && strings.IndexByte(path, ':') != -1 {
150+
// https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#file_and_directory_names
151+
// https://docs.microsoft.com/en-us/dotnet/standard/io/file-path-formats
152+
return false
153+
}
154+
return true
155+
}
156+
141157
// MustSubFS creates sub FS from current filesystem or panic on failure.
142158
// Panic happens when `fsRoot` contains invalid path according to `fs.ValidPath` rules.
143159
//

0 commit comments

Comments
 (0)