context fs

This commit is contained in:
royalcat 2024-03-21 00:47:51 +03:00
parent fd3beea874
commit 7b1863109c
25 changed files with 593 additions and 349 deletions
src/host/vfs

View file

@ -1,6 +1,7 @@
package vfs
import (
"context"
"io/fs"
"os"
"path"
@ -26,15 +27,15 @@ func (d *Dummy) IsDir() bool {
return false
}
func (d *Dummy) Close() error {
func (d *Dummy) Close(ctx context.Context) error {
return nil
}
func (d *Dummy) Read(p []byte) (n int, err error) {
func (d *Dummy) Read(ctx context.Context, p []byte) (n int, err error) {
return 0, nil
}
func (d *Dummy) ReadAt(p []byte, off int64) (n int, err error) {
func (d *Dummy) ReadAt(ctx context.Context, p []byte, off int64) (n int, err error) {
return 0, nil
}
@ -45,19 +46,19 @@ type DummyFs struct {
}
// Stat implements Filesystem.
func (*DummyFs) Stat(filename string) (fs.FileInfo, error) {
func (*DummyFs) Stat(ctx context.Context, filename string) (fs.FileInfo, error) {
return newFileInfo(path.Base(filename), 0), nil // TODO
}
func (d *DummyFs) Open(filename string) (File, error) {
func (d *DummyFs) Open(ctx context.Context, filename string) (File, error) {
return &Dummy{}, nil
}
func (d *DummyFs) Unlink(filename string) error {
func (d *DummyFs) Unlink(ctx context.Context, filename string) error {
return ErrNotImplemented
}
func (d *DummyFs) ReadDir(path string) ([]fs.DirEntry, error) {
func (d *DummyFs) ReadDir(ctx context.Context, path string) ([]fs.DirEntry, error) {
if path == "/dir/here" {
return []fs.DirEntry{
newFileInfo("file1.txt", 0),
@ -93,11 +94,13 @@ var _ Filesystem = &DummyFs{}
func TestResolver(t *testing.T) {
t.Parallel()
resolver := newResolver(ArchiveFactories)
ctx := context.Background()
t.Run("nested fs", func(t *testing.T) {
t.Parallel()
require := require.New(t)
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("/f1.rar/f2.rar", func(path string) (File, error) {
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath(ctx, "/f1.rar/f2.rar", func(_ context.Context, path string) (File, error) {
require.Equal("/f1.rar", path)
return &Dummy{}, nil
})
@ -110,7 +113,7 @@ func TestResolver(t *testing.T) {
t.Parallel()
require := require.New(t)
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("/", func(path string) (File, error) {
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath(ctx, "/", func(_ context.Context, path string) (File, error) {
require.Equal("/", path)
return &Dummy{}, nil
})
@ -124,7 +127,7 @@ func TestResolver(t *testing.T) {
t.Parallel()
require := require.New(t)
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("//.//", func(path string) (File, error) {
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath(ctx, "//.//", func(_ context.Context, path string) (File, error) {
require.Equal("/", path)
return &Dummy{}, nil
})
@ -137,7 +140,7 @@ func TestResolver(t *testing.T) {
t.Parallel()
require := require.New(t)
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("//.//f1.rar", func(path string) (File, error) {
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath(ctx, "//.//f1.rar", func(_ context.Context, path string) (File, error) {
require.Equal("/f1.rar", path)
return &Dummy{}, nil
})
@ -150,7 +153,7 @@ func TestResolver(t *testing.T) {
t.Parallel()
require := require.New(t)
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("//test1/f1.rar", func(path string) (File, error) {
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath(ctx, "//test1/f1.rar", func(_ context.Context, path string) (File, error) {
require.Equal("/test1/f1.rar", path)
return &Dummy{}, nil
})
@ -164,21 +167,23 @@ func TestResolver(t *testing.T) {
func TestArchiveFactories(t *testing.T) {
t.Parallel()
ctx := context.Background()
require := require.New(t)
require.Contains(ArchiveFactories, ".zip")
require.Contains(ArchiveFactories, ".rar")
require.Contains(ArchiveFactories, ".7z")
fs, err := ArchiveFactories[".zip"](&Dummy{})
fs, err := ArchiveFactories[".zip"](ctx, &Dummy{})
require.NoError(err)
require.NotNil(fs)
fs, err = ArchiveFactories[".rar"](&Dummy{})
fs, err = ArchiveFactories[".rar"](ctx, &Dummy{})
require.NoError(err)
require.NotNil(fs)
fs, err = ArchiveFactories[".7z"](&Dummy{})
fs, err = ArchiveFactories[".7z"](ctx, &Dummy{})
require.NoError(err)
require.NotNil(fs)
}