Skip to content

Commit

Permalink
wip: path consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Aug 14, 2023
1 parent 54ac812 commit bdc0648
Show file tree
Hide file tree
Showing 63 changed files with 432 additions and 269 deletions.
4 changes: 2 additions & 2 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/ipfs/kubo/core/coreapi"

options "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
cid "github.com/ipfs/go-cid"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) {
return cid.Cid{}, err
}

basePath := path.IpfsPath(dirb.Cid())
basePath := path.NewIPFSPath(dirb.Cid())

for _, p := range l {
d, err := Asset.ReadFile(p)
Expand Down
14 changes: 11 additions & 3 deletions client/rpc/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"time"

iface "github.com/ipfs/boxo/coreiface"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/coreiface/tests"
"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/test/cli/harness"
ma "github.com/multiformats/go-multiaddr"
"go.uber.org/multierr"
Expand Down Expand Up @@ -70,7 +70,11 @@ func (np NodeProvider) MakeAPISwarm(t *testing.T, ctx context.Context, fullIdent
apis[i] = api

// empty node is pinned even with --empty-repo, we don't want that
emptyNode := path.New("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn")
emptyNode, err := path.NewPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn")
if err != nil {
return err
}

if err := api.Pin().Rm(ctx, emptyNode); err != nil {
return err
}
Expand Down Expand Up @@ -126,7 +130,11 @@ func Test_NewURLApiWithClient_With_Headers(t *testing.T) {
t.Fatal(err)
}
api.Headers.Set(headerToTest, expectedHeaderValue)
if err := api.Pin().Rm(context.Background(), path.New("/ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")); err != nil {
p, err := path.NewPath("/ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")
if err != nil {
t.Fatal(err)
}
if err := api.Pin().Rm(context.Background(), p); err != nil {
t.Fatal(err)
}
}
Expand Down
8 changes: 4 additions & 4 deletions client/rpc/apifile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"fmt"
"io"

"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/files"
unixfs "github.com/ipfs/boxo/ipld/unixfs"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
)

const forwardSeekLimit = 1 << 14 //16k

func (api *UnixfsAPI) Get(ctx context.Context, p path.Path) (files.Node, error) {
if p.Mutable() { // use resolved path in case we are dealing with IPNS / MFS
if p.Namespace().Mutable() { // use resolved path in case we are dealing with IPNS / MFS
var err error
p, err = api.core().ResolvePath(ctx, p)
if err != nil {
Expand Down Expand Up @@ -195,13 +195,13 @@ func (it *apiIter) Next() bool {

switch it.cur.Type {
case unixfs.THAMTShard, unixfs.TMetadata, unixfs.TDirectory:
it.curFile, err = it.core.getDir(it.ctx, path.IpfsPath(c), int64(it.cur.Size))
it.curFile, err = it.core.getDir(it.ctx, path.NewIPFSPath(c), int64(it.cur.Size))
if err != nil {
it.err = err
return false
}
case unixfs.TFile:
it.curFile, err = it.core.getFile(it.ctx, path.IpfsPath(c), int64(it.cur.Size))
it.curFile, err = it.core.getFile(it.ctx, path.NewIPFSPath(c), int64(it.cur.Size))
if err != nil {
it.err = err
return false
Expand Down
6 changes: 3 additions & 3 deletions client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

iface "github.com/ipfs/boxo/coreiface"
caopts "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
mc "github.com/multiformats/go-multicodec"
mh "github.com/multiformats/go-multihash"
Expand All @@ -27,8 +27,8 @@ func (s *blockStat) Size() int {
return s.BSize
}

func (s *blockStat) Path() path.Resolved {
return path.IpldPath(s.cid)
func (s *blockStat) Path() path.ImmutablePath {
return path.NewIPLDPath(s.cid)
}

func (api *BlockAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.BlockPutOption) (iface.BlockStat, error) {
Expand Down
8 changes: 4 additions & 4 deletions client/rpc/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"io"

"github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/go-block-format"
"github.com/ipfs/boxo/path"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
multicodec "github.com/multiformats/go-multicodec"
Expand All @@ -19,7 +19,7 @@ type HttpDagServ httpNodeAdder
type pinningHttpNodeAdder httpNodeAdder

func (api *HttpDagServ) Get(ctx context.Context, c cid.Cid) (format.Node, error) {
r, err := api.core().Block().Get(ctx, path.IpldPath(c))
r, err := api.core().Block().Get(ctx, path.NewIPLDPath(c))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (api *HttpDagServ) Pinning() format.NodeAdder {
}

func (api *HttpDagServ) Remove(ctx context.Context, c cid.Cid) error {
return api.core().Block().Rm(ctx, path.IpldPath(c)) //TODO: should we force rm?
return api.core().Block().Rm(ctx, path.NewIPLDPath(c)) //TODO: should we force rm?
}

func (api *HttpDagServ) RemoveMany(ctx context.Context, cids []cid.Cid) error {
Expand Down
2 changes: 1 addition & 1 deletion client/rpc/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"

caopts "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/path"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
)
Expand Down
20 changes: 16 additions & 4 deletions client/rpc/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (

iface "github.com/ipfs/boxo/coreiface"
caopts "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/path"
"github.com/libp2p/go-libp2p/core/peer"
)

Expand All @@ -16,15 +17,16 @@ type keyOutput struct {
JName string `json:"Name"`
Id string

pid peer.ID
pid peer.ID
path path.Path
}

func (k *keyOutput) Name() string {
return k.JName
}

func (k *keyOutput) Path() path.Path {
return path.New("/ipns/" + k.Id)
return k.path
}

func (k *keyOutput) ID() peer.ID {
Expand All @@ -45,8 +47,18 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
if err != nil {
return nil, err
}

out.pid, err = peer.Decode(out.Id)
return &out, err
if err != nil {
return nil, err
}

out.path, err = path.NewPath("/ipns/" + ipns.NameFromPeer(out.pid).String())
if err != nil {
return nil, err
}

return &out, nil
}

func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (iface.Key, bool, error) {
Expand Down
10 changes: 7 additions & 3 deletions client/rpc/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
iface "github.com/ipfs/boxo/coreiface"
caopts "github.com/ipfs/boxo/coreiface/options"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/path"
)

type NameAPI HttpApi
Expand Down Expand Up @@ -84,7 +84,11 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
}
var ires iface.IpnsResult
if err == nil {
ires.Path = path.New(out.Path)
p, err := path.NewPath(out.Path)
if err != nil {
return
}
ires.Path = p
}

select {
Expand Down Expand Up @@ -122,7 +126,7 @@ func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.Nam
return nil, err
}

return path.New(out.Path), nil
return path.NewPath(out.Path)
}

func (api *NameAPI) core() *HttpApi {
Expand Down
26 changes: 13 additions & 13 deletions client/rpc/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

iface "github.com/ipfs/boxo/coreiface"
caopts "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/ipld/merkledag"
ft "github.com/ipfs/boxo/ipld/unixfs"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
)
Expand Down Expand Up @@ -40,7 +40,7 @@ func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (
return n, nil
}

func (api *ObjectAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.ObjectPutOption) (path.Resolved, error) {
func (api *ObjectAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.ObjectPutOption) (path.ImmutablePath, error) {
options, err := caopts.ObjectPutOptions(opts...)
if err != nil {
return nil, err
Expand All @@ -62,7 +62,7 @@ func (api *ObjectAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.Objec
return nil, err
}

return path.IpfsPath(c), nil
return path.NewIPFSPath(c), nil
}

func (api *ObjectAPI) Get(ctx context.Context, p path.Path) (ipld.Node, error) {
Expand Down Expand Up @@ -153,7 +153,7 @@ func (api *ObjectAPI) Stat(ctx context.Context, p path.Path) (*iface.ObjectStat,
}, nil
}

func (api *ObjectAPI) AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...caopts.ObjectAddLinkOption) (path.Resolved, error) {
func (api *ObjectAPI) AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...caopts.ObjectAddLinkOption) (path.ImmutablePath, error) {
options, err := caopts.ObjectAddLinkOptions(opts...)
if err != nil {
return nil, err
Expand All @@ -172,10 +172,10 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base path.Path, name string,
return nil, err
}

return path.IpfsPath(c), nil
return path.NewIPFSPath(c), nil
}

func (api *ObjectAPI) RmLink(ctx context.Context, base path.Path, link string) (path.Resolved, error) {
func (api *ObjectAPI) RmLink(ctx context.Context, base path.Path, link string) (path.ImmutablePath, error) {
var out objectOut
err := api.core().Request("object/patch/rm-link", base.String(), link).
Exec(ctx, &out)
Expand All @@ -188,10 +188,10 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base path.Path, link string) (
return nil, err
}

return path.IpfsPath(c), nil
return path.NewIPFSPath(c), nil
}

func (api *ObjectAPI) AppendData(ctx context.Context, p path.Path, r io.Reader) (path.Resolved, error) {
func (api *ObjectAPI) AppendData(ctx context.Context, p path.Path, r io.Reader) (path.ImmutablePath, error) {
var out objectOut
err := api.core().Request("object/patch/append-data", p.String()).
FileBody(r).
Expand All @@ -205,10 +205,10 @@ func (api *ObjectAPI) AppendData(ctx context.Context, p path.Path, r io.Reader)
return nil, err
}

return path.IpfsPath(c), nil
return path.NewIPFSPath(c), nil
}

func (api *ObjectAPI) SetData(ctx context.Context, p path.Path, r io.Reader) (path.Resolved, error) {
func (api *ObjectAPI) SetData(ctx context.Context, p path.Path, r io.Reader) (path.ImmutablePath, error) {
var out objectOut
err := api.core().Request("object/patch/set-data", p.String()).
FileBody(r).
Expand All @@ -222,7 +222,7 @@ func (api *ObjectAPI) SetData(ctx context.Context, p path.Path, r io.Reader) (pa
return nil, err
}

return path.IpfsPath(c), nil
return path.NewIPFSPath(c), nil
}

type change struct {
Expand All @@ -246,10 +246,10 @@ func (api *ObjectAPI) Diff(ctx context.Context, a path.Path, b path.Path) ([]ifa
Path: ch.Path,
}
if ch.Before != cid.Undef {
res[i].Before = path.IpfsPath(ch.Before)
res[i].Before = path.NewIPFSPath(ch.Before)
}
if ch.After != cid.Undef {
res[i].After = path.IpfsPath(ch.After)
res[i].After = path.NewIPFSPath(ch.After)
}
}
return res, nil
Expand Down
17 changes: 5 additions & 12 deletions client/rpc/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package rpc
import (
"context"

"github.com/ipfs/boxo/coreiface/path"
ipfspath "github.com/ipfs/boxo/path"
"github.com/ipfs/boxo/path"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
)

func (api *HttpApi) ResolvePath(ctx context.Context, p path.Path) (path.Resolved, error) {
func (api *HttpApi) ResolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
var out struct {
Cid cid.Cid
RemPath string
Expand All @@ -18,7 +17,7 @@ func (api *HttpApi) ResolvePath(ctx context.Context, p path.Path) (path.Resolved
//TODO: this is hacky, fixing https://github.com/ipfs/go-ipfs/issues/5703 would help

var err error
if p.Namespace() == "ipns" {
if p.Namespace() == path.IPNSNamespace {
if p, err = api.Name().Resolve(ctx, p.String()); err != nil {
return nil, err
}
Expand All @@ -28,18 +27,12 @@ func (api *HttpApi) ResolvePath(ctx context.Context, p path.Path) (path.Resolved
return nil, err
}

// TODO:
ipath, err := ipfspath.FromSegments("/"+p.Namespace()+"/", out.Cid.String(), out.RemPath)
p, err = path.NewPathFromSegments(p.Namespace().String(), out.Cid.String(), out.RemPath)
if err != nil {
return nil, err
}

root, err := cid.Parse(ipfspath.Path(p.String()).Segments()[1])
if err != nil {
return nil, err
}

return path.NewResolvedPath(ipath, out.Cid, root, out.RemPath), nil
return path.NewImmutablePath(p)
}

func (api *HttpApi) ResolveNode(ctx context.Context, p path.Path) (ipld.Node, error) {
Expand Down
Loading

0 comments on commit bdc0648

Please sign in to comment.