Skip to content

Commit

Permalink
Add fs.File implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Aug 3, 2024
1 parent 99118cc commit d4394ac
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions guest/fs.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package guest

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

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

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

type FS struct{}
type FS struct {
IPFS iface.CoreAPI
}

// Open opens the named file.
//
Expand All @@ -18,6 +25,32 @@ type FS struct{}
// 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) Open(name string) (fs.File, error) {
return nil, errors.New("FS.Open::NOT IMPLEMENTED")
func (fs FS) Open(name string) (fs.File, error) {
p, err := path.NewPath(name)
if err != nil {
return nil, err
}

n, err := fs.IPFS.Unixfs().Get(context.TODO(), p)
return fsNode{Node: n}, err
}

// 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.

type fsNode struct {
files.Node
}

func (n fsNode) Stat() (fs.FileInfo, error) {
return nil, errors.New("fsNode.Stat::NOT IMPLEMENTED")
}

func (n fsNode) Read([]byte) (int, error) {
return 0, errors.New("fsNode.Read::NOT IMPLEMENTED")
}

func (n fsNode) Close() error {
return n.Node.Close()
}

0 comments on commit d4394ac

Please sign in to comment.