diff --git a/guest/guest.go b/guest/guest.go deleted file mode 100644 index bccf463..0000000 --- a/guest/guest.go +++ /dev/null @@ -1,65 +0,0 @@ -package guest - -import ( - "context" - "crypto/rand" - "io" - "os" - - "github.com/ipfs/boxo/files" - "github.com/ipfs/boxo/path" - iface "github.com/ipfs/kubo/core/coreiface" - "github.com/tetratelabs/wazero" - "github.com/tetratelabs/wazero/api" -) - -type Config struct { - IPFS iface.CoreAPI - Root path.Path - Sys interface { - Stdin() io.Reader - } -} - -// Compile and instantiate the guest module from the namespace path. -// Note that CompiledModule is produced in an intermediate step, and -// that it is not closed until r is closed. -func (c Config) Instanatiate(ctx context.Context, r wazero.Runtime) (api.Module, error) { - cm, err := c.CompileModule(ctx, r) - if err != nil { - return nil, err - } - - return r.InstantiateModule(ctx, cm, wazero.NewModuleConfig(). - // WithName(). - // WithArgs(). - // WithEnv(). - WithRandSource(rand.Reader). - WithFS(FS{IPFS: c.IPFS}). - // WithFSConfig(). - // WithStartFunctions(). // remove _start so that we can call it later - WithStdin(c.Sys.Stdin()). - WithStdout(os.Stdout). // FIXME - WithStderr(os.Stderr). // FIXME - WithSysNanotime()) -} - -func (c Config) CompileModule(ctx context.Context, r wazero.Runtime) (wazero.CompiledModule, error) { - bytecode, err := c.LoadByteCode(ctx) - if err != nil { - return nil, err - } - - return r.CompileModule(ctx, bytecode) -} - -func (c Config) LoadByteCode(ctx context.Context) ([]byte, error) { - n, err := c.IPFS.Unixfs().Get(ctx, c.Root) - if err != nil { - return nil, err - } - defer n.Close() - - // FIXME: address the obvious DoS vector - return io.ReadAll(n.(files.File)) -} diff --git a/guest/fs.go b/system/fs.go similarity index 85% rename from guest/fs.go rename to system/fs.go index 94a0598..6226b06 100644 --- a/guest/fs.go +++ b/system/fs.go @@ -1,10 +1,11 @@ -package guest +package system import ( "context" "io/fs" "github.com/ipfs/boxo/files" + "github.com/ipfs/boxo/path" iface "github.com/ipfs/kubo/core/coreiface" "github.com/pkg/errors" ) @@ -20,7 +21,8 @@ var _ fs.FS = (*FS)(nil) // [testing/fstest.TestFS] may be used to test implementations of an FS for // correctness. type FS struct { - IPFS iface.CoreAPI + API iface.UnixfsAPI + Path path.Path } // Open opens the named file. @@ -33,20 +35,24 @@ type FS struct { // fs.ValidPath(name), returning a *fs.PathError with Err set to // fs.ErrInvalid or fs.ErrNotExist. func (f FS) Open(name string) (fs.File, error) { - if !fs.ValidPath(name) { + p, err := path.Join(f.Path, name) + if err != nil { return nil, &fs.PathError{ - Op: "FS.Open", + Op: "path.Join", Path: name, - Err: errors.New("invalid path"), + Err: err, } } - path, err := f.IPFS.Name().Resolve(context.TODO(), name) - if err != nil { - return nil, err + if !fs.ValidPath(p.String()) { + return nil, &fs.PathError{ + Op: "fs.ValidPath", + Path: name, + Err: errors.New("invalid path"), + } } - node, err := f.IPFS.Unixfs().Get(context.TODO(), path) + node, err := f.API.Get(context.TODO(), p) if err != nil { return nil, err } diff --git a/guest/fs_test.go b/system/fs_test.go similarity index 63% rename from guest/fs_test.go rename to system/fs_test.go index 915eb27..da78a41 100644 --- a/guest/fs_test.go +++ b/system/fs_test.go @@ -1,16 +1,16 @@ -package guest_test +package system_test import ( "testing" "testing/fstest" "github.com/stretchr/testify/require" - "github.com/wetware/go/guest" + "github.com/wetware/go/system" ) func TestFS(t *testing.T) { t.Parallel() - err := fstest.TestFS(guest.FS{}) + err := fstest.TestFS(system.FS{}) require.NoError(t, err) } diff --git a/ww.go b/ww.go index 651be34..34397a0 100644 --- a/ww.go +++ b/ww.go @@ -17,7 +17,7 @@ import ( "github.com/tetratelabs/wazero" "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" "github.com/thejerf/suture/v4" - "github.com/wetware/go/guest" + "github.com/wetware/go/system" ) const Proto = "/ww/0.0.0" @@ -103,7 +103,7 @@ func (c Cluster) Serve(ctx context.Context) error { // WithSysWalltime(). // WithWalltime(). WithStartFunctions(). - WithFS(guest.FS{IPFS: c.IPFS}). + WithFS(system.FS{API: c.IPFS.Unixfs()}). WithRandSource(rand.Reader). WithStdin(os.Stdin). WithStderr(os.Stderr).