dirfs: corrects path joining bug when mounting root (#1228)

Before, we created invalid directory paths when the host directory was
"/", and now we don't!

This brings tests left failing on GOOS=js to 7
```bash
$ wazero run -mount=/:/ --experimental-workdir=$(go env GOROOT)/src/os os.wasm| grep '^--- FAIL'|wc -l
       7
```

See #1222

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
Co-authored-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
This commit is contained in:
Crypt Keeper
2023-03-14 07:30:12 +08:00
committed by GitHub
parent 7e953d7483
commit f3c83dbd8d
2 changed files with 18 additions and 1 deletions

View File

@@ -16,7 +16,7 @@ func NewDirFS(dir string) FS {
}
func ensureTrailingPathSeparator(dir string) string {
if dir[len(dir)-1] != os.PathSeparator {
if !os.IsPathSeparator(dir[len(dir)-1]) {
return dir + string(os.PathSeparator)
}
return dir
@@ -139,6 +139,9 @@ func (d *dirFS) Truncate(path string, size int64) error {
func (d *dirFS) join(path string) string {
switch path {
case "", ".", "/":
if d.cleanedDir == "/" {
return "/"
}
// cleanedDir includes an unnecessary delimiter for the root path.
return d.cleanedDir[:len(d.cleanedDir)-1]
}

View File

@@ -39,6 +39,20 @@ func TestNewDirFS(t *testing.T) {
})
}
func TestDirFS_join(t *testing.T) {
testFS := NewDirFS("/").(*dirFS)
require.Equal(t, "/", testFS.join(""))
require.Equal(t, "/", testFS.join("."))
require.Equal(t, "/", testFS.join("/"))
require.Equal(t, "/tmp", testFS.join("tmp"))
testFS = NewDirFS(".").(*dirFS)
require.Equal(t, ".", testFS.join(""))
require.Equal(t, ".", testFS.join("."))
require.Equal(t, ".", testFS.join("/"))
require.Equal(t, "."+string(os.PathSeparator)+"tmp", testFS.join("tmp"))
}
func TestDirFS_String(t *testing.T) {
testFS := NewDirFS(".")