wasi: supports rewinding a directory and fixes seek error (#1084)

This allows rewinding a directory by specifying the cookie 0, after
already scrolling a directory. This allows zig and clang functionality
to work which expect this.

This also fixes the case where a directory was allowed to be seeked.
This is prohibited by the current version of wasi-testsuite.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
Crypt Keeper
2023-01-31 10:22:48 +02:00
committed by GitHub
parent 9bb46ebeed
commit 67125fcaa4
16 changed files with 242 additions and 38 deletions

View File

@@ -7,6 +7,7 @@ import (
"io"
"io/fs"
"os"
"syscall"
"testing"
"testing/fstest"
@@ -181,3 +182,49 @@ func TestContext_Close_Error(t *testing.T) {
// Paths should clear even under error
require.Zero(t, fsc.openedFiles.Len(), "expected no opened files")
}
func TestFSContext_ReOpenDir(t *testing.T) {
tmpDir := t.TempDir()
dirFs := sysfs.NewDirFS(tmpDir)
const dirName = "dir"
err := dirFs.Mkdir(dirName, 0o700)
require.NoError(t, err)
fsc, err := NewFSContext(nil, nil, nil, dirFs)
require.NoError(t, err)
defer func() {
require.NoError(t, fsc.Close(context.Background()))
}()
t.Run("ok", func(t *testing.T) {
dirFd, err := fsc.OpenFile(dirFs, dirName, os.O_RDONLY, 0o600)
require.NoError(t, err)
ent, ok := fsc.LookupFile(dirFd)
require.True(t, ok)
// Set arbitrary state.
ent.ReadDir = &ReadDir{Entries: make([]fs.DirEntry, 10), CountRead: 12345}
// Then reopen the same file descriptor.
ent, err = fsc.ReOpenDir(dirFd)
require.NoError(t, err)
// Verify the read dir state has been reset.
require.Equal(t, &ReadDir{}, ent.ReadDir)
})
t.Run("non existing ", func(t *testing.T) {
_, err = fsc.ReOpenDir(12345)
require.ErrorIs(t, err, syscall.EBADF)
})
t.Run("not dir", func(t *testing.T) {
const fileName = "dog"
fd, err := fsc.OpenFile(dirFs, fileName, os.O_CREATE, 0o600)
require.NoError(t, err)
_, err = fsc.ReOpenDir(fd)
require.ErrorIs(t, err, syscall.EISDIR)
})
}