Skip to content

Commit

Permalink
Issue #2321.
Browse files Browse the repository at this point in the history
Signed-off-by: Nuno Cruces <[email protected]>
  • Loading branch information
ncruces committed Sep 24, 2024
1 parent dc058c0 commit b36a8c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
25 changes: 14 additions & 11 deletions internal/sysfs/dirfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package sysfs
import (
"io/fs"
"os"
"path"
"strings"

experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/internal/platform"
Expand All @@ -12,7 +14,7 @@ import (
func DirFS(dir string) experimentalsys.FS {
return &dirFS{
dir: dir,
cleanedDir: ensureTrailingPathSeparator(dir),
cleanedDir: strings.TrimSuffix(dir, "/"),
}
}

Expand Down Expand Up @@ -84,16 +86,17 @@ func (d *dirFS) Utimens(path string, atim, mtim int64) experimentalsys.Errno {
return utimens(d.join(path), atim, mtim)
}

func (d *dirFS) join(path string) string {
switch path {
case "", ".", "/":
if d.cleanedDir == "/" {
return "/"
func (d *dirFS) join(name string) string {
last := strings.HasSuffix(name, "/")
name = path.Clean("/" + name)
if name == "/" {
if d.cleanedDir != "" {
return d.cleanedDir
}
// cleanedDir includes an unnecessary delimiter for the root path.
return d.cleanedDir[:len(d.cleanedDir)-1]
return "/"
}
// TODO: Enforce similar to safefilepath.FromFS(path), but be careful as
// relative path inputs are allowed. e.g. dir or path == ../
return d.cleanedDir + path
if last {
return d.cleanedDir + name + "/"
}
return d.cleanedDir + name
}
9 changes: 6 additions & 3 deletions internal/sysfs/dirfs_supported.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package sysfs
import (
"io/fs"
"os"
"path"
"strings"

experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
)
Expand Down Expand Up @@ -34,9 +36,10 @@ func (d *dirFS) Chmod(path string, perm fs.FileMode) experimentalsys.Errno {

// Symlink implements the same method as documented on sys.FS
func (d *dirFS) Symlink(oldName, link string) experimentalsys.Errno {
// Note: do not resolve `oldName` relative to this dirFS. The link result is always resolved
// when dereference the `link` on its usage (e.g. readlink, read, etc).
// https://github.com/bytecodealliance/cap-std/blob/v1.0.4/cap-std/src/fs/dir.rs#L404-L409
oldName = path.Clean(oldName)
if strings.HasPrefix(oldName, "../") {
return experimentalsys.EFAULT
}
err := os.Symlink(oldName, d.join(link))
return experimentalsys.UnwrapOSError(err)
}
2 changes: 1 addition & 1 deletion internal/sysfs/dirfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestDirFS_join(t *testing.T) {
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"))
require.Equal(t, "./tmp", testFS.join("tmp"))
}

func TestDirFS_String(t *testing.T) {
Expand Down

0 comments on commit b36a8c8

Please sign in to comment.