Skip to content

Commit

Permalink
Rename system.FS.API to system.FS.Unix.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Aug 28, 2024
1 parent 2a8a5a8 commit 9f8cc40
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
30 changes: 9 additions & 21 deletions system/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package system

import (
"context"
"fmt"
"io"
"io/fs"
"log/slog"
"path/filepath"
"runtime"
"time"

Expand All @@ -28,8 +26,8 @@ var _ fs.FS = (*FS)(nil)
// correctness.
type FS struct {
Ctx context.Context
API iface.UnixfsAPI
Root path.Path
Unix iface.UnixfsAPI
}

// Open opens the named file.
Expand All @@ -42,20 +40,12 @@ 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 pathInvalid(name) {
return nil, &fs.PathError{
Op: "open",
Path: name,
Err: errors.New("invalid path"),
}
}

path, node, err := f.Resolve(f.Ctx, name)
if err != nil {
return nil, &fs.PathError{
Op: "open",
Path: name,
Err: fmt.Errorf("resolve: %w", err),
Err: err,
}
}

Expand All @@ -66,25 +56,23 @@ func (f FS) Open(name string) (fs.File, error) {
}

func (f FS) Resolve(ctx context.Context, name string) (path.Path, files.Node, error) {
joined, err := path.Join(f.Root, clean(name))
if pathInvalid(name) {
return nil, nil, fs.ErrInvalid
}

p, err := path.Join(f.Root, name)
if err != nil {
return nil, nil, err
}

node, err := f.API.Get(ctx, joined)
return joined, node, err
node, err := f.Unix.Get(ctx, p)
return p, node, err
}

func pathInvalid(name string) bool {
return !fs.ValidPath(name)
}

func clean(name string) string {
name = filepath.Clean(name)
// name = strings.Trim(name, "./")
return name
}

var (
_ fs.FileInfo = (*ipfsNode)(nil)
_ fs.ReadDirFile = (*ipfsNode)(nil)
Expand Down
2 changes: 1 addition & 1 deletion system/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestFS(t *testing.T) {
ipfs, err := rpc.NewLocalApi()
require.NoError(t, err)

fs := system.FS{Ctx: context.Background(), API: ipfs.Unixfs(), Root: root}
fs := system.FS{Ctx: context.Background(), Unix: ipfs.Unixfs(), Root: root}
err = fstest.TestFS(fs,
"main.go",
"main.wasm",
Expand Down

0 comments on commit 9f8cc40

Please sign in to comment.