Skip to content

Commit

Permalink
Clean up guest package.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Aug 3, 2024
1 parent d4394ac commit c0dddc3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
65 changes: 44 additions & 21 deletions guest/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@ package guest

import (
"context"
"errors"
"io/fs"

"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
iface "github.com/ipfs/kubo/core/coreiface"
"github.com/pkg/errors"
)

var _ fs.FS = (*FS)(nil)

// An FS provides access to a hierarchical file system.
//
// The FS interface is the minimum implementation required of the file system.
// A file system may implement additional interfaces,
// such as [ReadFileFS], to provide additional or optimized functionality.
//
// [testing/fstest.TestFS] may be used to test implementations of an FS for
// correctness.
type FS struct {
IPFS iface.CoreAPI
UNIX iface.UnixfsAPI
Root path.Path
}

// Open opens the named file.
Expand All @@ -23,34 +32,48 @@ type FS struct {
// and the Err field describing the problem.
//
// Open should reject attempts to open names that do not satisfy
// ValidPath(name), returning a *PathError with Err set to
// ErrInvalid or ErrNotExist.
func (fs FS) Open(name string) (fs.File, error) {
p, err := path.NewPath(name)
// 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) {
return nil, &fs.PathError{
Op: "open",
Path: name,
Err: errors.New("invalid path"),
}
}

root, err := f.UNIX.Get(context.TODO(), f.Root)
if err != nil {
return nil, err
}

n, err := fs.IPFS.Unixfs().Get(context.TODO(), p)
return fsNode{Node: n}, err
}
switch node := root.(type) {
case files.File:
return fileNode{File: node}, nil

// fsNode provides access to a single file. The fs.File interface is the minimum
// implementation required of the file. Directory files should also implement [ReadDirFile].
// A file may implement io.ReaderAt or io.Seeker as optimizations.
case files.Directory:
defer node.Close()

type fsNode struct {
files.Node
}
return nil, &fs.PathError{
Op: "open",
Path: name,
Err: errors.New("is a directory"),
}

func (n fsNode) Stat() (fs.FileInfo, error) {
return nil, errors.New("fsNode.Stat::NOT IMPLEMENTED")
default:
panic(node) // unhandled type
}
}

func (n fsNode) Read([]byte) (int, error) {
return 0, errors.New("fsNode.Read::NOT IMPLEMENTED")
// fileNode provides access to a single file. The fs.File interface is the minimum
// implementation required of the file. Directory files should also implement [ReadDirFile].
// A file may implement io.ReaderAt or io.Seeker as optimizations.

type fileNode struct {
files.File
}

func (n fsNode) Close() error {
return n.Node.Close()
func (n fileNode) Stat() (fs.FileInfo, error) {
return nil, errors.New("fileNode.Stat::NOT IMPLEMENTED")
}
5 changes: 3 additions & 2 deletions guest/guest_test.go → guest/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package guest_test

import (
"testing"
"testing/fstest"

"github.com/stretchr/testify/require"
"github.com/wetware/go/guest"
Expand All @@ -10,6 +11,6 @@ import (
func TestFS(t *testing.T) {
t.Parallel()

_, err := guest.FS{}.Open("")
require.EqualError(t, err, "FS.Open::NOT IMPLEMENTED")
err := fstest.TestFS(guest.FS{})
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion guest/guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c Config) Instanatiate(ctx context.Context, r wazero.Runtime) (api.Module,
// WithArgs().
// WithEnv().
WithRandSource(rand.Reader).
WithFS(FS{ /* TODO: pass IPFS here and mount a UnixFS */ }).
WithFS(FS{UNIX: c.IPFS.Unixfs(), Root: c.Root}).
// WithFSConfig().
// WithStartFunctions(). // remove _start so that we can call it later
WithStdin(c.Sys.Stdin()).
Expand Down

0 comments on commit c0dddc3

Please sign in to comment.