diff options
author | Russ Cox <rsc@golang.org> | 2021-02-09 13:46:53 -0500 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2021-02-11 01:10:28 +0000 |
commit | e5b08e6d5cecb646066c0cadddf6300e2a10ffb2 (patch) | |
tree | 74c7b56d1c89924544a37c36eb93ddd2fd40b539 /src/os/os_test.go | |
parent | ed8079096fe2e78d6dcb8002758774dca6d24eee (diff) | |
download | go-git-e5b08e6d5cecb646066c0cadddf6300e2a10ffb2.tar.gz |
io/fs: allow backslash in ValidPath, reject in os.DirFS.Open
Rejecting backslash introduces problems with presenting
underlying OS file systems that contain names with backslash.
Rejecting backslash also does not Windows-proof the syntax,
because colon can also be a path separator. And we are not
going to reject colon from all names. So don't reject backslash
either.
There is a similar problem on Windows with names containing
slashes, but those are more difficult (though not impossible)
to create.
Also document and enforce that paths must be UTF-8.
Fixes #44166.
Change-Id: Iac7a9a268025c1fd31010dbaf3f51e1660c7ae2a
Reviewed-on: https://go-review.googlesource.com/c/go/+/290709
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/os/os_test.go')
-rw-r--r-- | src/os/os_test.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/os/os_test.go b/src/os/os_test.go index ee54b4aba1..a32e5fc11e 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -2719,6 +2719,40 @@ func TestDirFS(t *testing.T) { if err := fstest.TestFS(DirFS("./testdata/dirfs"), "a", "b", "dir/x"); err != nil { t.Fatal(err) } + + // Test that Open does not accept backslash as separator. + d := DirFS(".") + _, err := d.Open(`testdata\dirfs`) + if err == nil { + t.Fatalf(`Open testdata\dirfs succeeded`) + } +} + +func TestDirFSPathsValid(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skipf("skipping on Windows") + } + + d := t.TempDir() + if err := os.WriteFile(filepath.Join(d, "control.txt"), []byte(string("Hello, world!")), 0644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(d, `e:xperi\ment.txt`), []byte(string("Hello, colon and backslash!")), 0644); err != nil { + t.Fatal(err) + } + + fsys := os.DirFS(d) + err := fs.WalkDir(fsys, ".", func(path string, e fs.DirEntry, err error) error { + if fs.ValidPath(e.Name()) { + t.Logf("%q ok", e.Name()) + } else { + t.Errorf("%q INVALID", e.Name()) + } + return nil + }) + if err != nil { + t.Fatal(err) + } } func TestReadFileProc(t *testing.T) { |