Skip to content

Commit

Permalink
Fix defaultFs relative path check for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
aldas committed Mar 13, 2022
1 parent da5f798 commit 8b9b584
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion echo_fs_go1.16.go
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
)

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

func isRelativePath(path string) bool {
if path == "" {
return true
}
if path[0] == '/' {
return false
}
if runtime.GOOS == "windows" && strings.IndexByte(path, ':') != -1 {
// https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#file_and_directory_names
// https://docs.microsoft.com/en-us/dotnet/standard/io/file-path-formats
return false
}
return true
}

// MustSubFS creates sub FS from current filesystem or panic on failure.
// Panic happens when `fsRoot` contains invalid path according to `fs.ValidPath` rules.
//
Expand Down

0 comments on commit 8b9b584

Please sign in to comment.