fs: empty guest path should bind to / (#1565)

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
This commit is contained in:
Edoardo Vacchi
2023-07-07 01:10:01 +02:00
committed by GitHub
parent 2c21f3aa8f
commit affca16f15
2 changed files with 34 additions and 26 deletions

View File

@@ -413,6 +413,8 @@ func (c *Context) InitFSContext(
guestPath := guestPaths[i] guestPath := guestPaths[i]
if StripPrefixesAndTrailingSlash(guestPath) == "" { if StripPrefixesAndTrailingSlash(guestPath) == "" {
// Default to bind to '/' when guestPath is effectively empty.
guestPath = "/"
c.fsc.rootFS = fs c.fsc.rootFS = fs
} }
c.fsc.openedFiles.Insert(&FileEntry{ c.fsc.openedFiles.Insert(&FileEntry{

View File

@@ -3,6 +3,7 @@ package sys
import ( import (
"embed" "embed"
"errors" "errors"
"fmt"
"io/fs" "io/fs"
"os" "os"
"path" "path"
@@ -55,36 +56,41 @@ func TestNewFSContext(t *testing.T) {
tc := tt tc := tt
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
c := Context{} for _, root := range []string{"/", ""} {
err := c.InitFSContext(nil, nil, nil, []fsapi.FS{tc.fs}, []string{"/"}, nil) t.Run(fmt.Sprintf("root = '%s'", root), func(t *testing.T) {
require.NoError(t, err) c := Context{}
fsc := c.fsc err := c.InitFSContext(nil, nil, nil, []fsapi.FS{tc.fs}, []string{root}, nil)
defer fsc.Close() require.NoError(t, err)
fsc := c.fsc
defer fsc.Close()
preopenedDir, _ := fsc.openedFiles.Lookup(FdPreopen) preopenedDir, _ := fsc.openedFiles.Lookup(FdPreopen)
require.Equal(t, tc.fs, fsc.rootFS) require.Equal(t, tc.fs, fsc.rootFS)
require.NotNil(t, preopenedDir) require.NotNil(t, preopenedDir)
require.Equal(t, "/", preopenedDir.Name) require.Equal(t, "/", preopenedDir.Name)
// Verify that each call to OpenFile returns a different file // Verify that each call to OpenFile returns a different file
// descriptor. // descriptor.
f1, errno := fsc.OpenFile(preopenedDir.FS, preopenedDir.Name, 0, 0) f1, errno := fsc.OpenFile(preopenedDir.FS, preopenedDir.Name, 0, 0)
require.EqualErrno(t, 0, errno) require.EqualErrno(t, 0, errno)
require.NotEqual(t, FdPreopen, f1) require.NotEqual(t, FdPreopen, f1)
// Verify that file descriptors are reused. // Verify that file descriptors are reused.
// //
// Note that this specific behavior is not required by WASI which // Note that this specific behavior is not required by WASI which
// only documents that file descriptor numbers will be selected // only documents that file descriptor numbers will be selected
// randomly and applications should not rely on them. We added this // randomly and applications should not rely on them. We added this
// test to ensure that our implementation properly reuses descriptor // test to ensure that our implementation properly reuses descriptor
// numbers but if we were to change the reuse strategy, this test // numbers but if we were to change the reuse strategy, this test
// would likely break and need to be updated. // would likely break and need to be updated.
require.EqualErrno(t, 0, fsc.CloseFile(f1)) require.EqualErrno(t, 0, fsc.CloseFile(f1))
f2, errno := fsc.OpenFile(preopenedDir.FS, preopenedDir.Name, 0, 0) f2, errno := fsc.OpenFile(preopenedDir.FS, preopenedDir.Name, 0, 0)
require.EqualErrno(t, 0, errno) require.EqualErrno(t, 0, errno)
require.Equal(t, f1, f2) require.Equal(t, f1, f2)
})
}
}) })
} }
} }