Skip to content

Commit

Permalink
refactor: return remainder from ResolvePath
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Sep 18, 2023
1 parent d14a172 commit 77ea675
Show file tree
Hide file tree
Showing 23 changed files with 84 additions and 64 deletions.
2 changes: 1 addition & 1 deletion client/rpc/apifile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const forwardSeekLimit = 1 << 14 // 16k
func (api *UnixfsAPI) Get(ctx context.Context, p path.Path) (files.Node, error) {
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)
p, _, err = api.core().ResolvePath(ctx, p)

Check warning on line 20 in client/rpc/apifile.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/apifile.go#L20

Added line #L20 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions client/rpc/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopt
return nil, err
}

rp, err := api.core().ResolvePath(ctx, p)
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func (api *DhtAPI) Provide(ctx context.Context, p path.Path, opts ...caopts.DhtP
return err
}

rp, err := api.core().ResolvePath(ctx, p)
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return err
}
Expand Down
19 changes: 11 additions & 8 deletions client/rpc/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,38 @@ import (
ipld "github.com/ipfs/go-ipld-format"
)

func (api *HttpApi) ResolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
func (api *HttpApi) ResolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, []string, error) {
var out struct {
Cid cid.Cid
RemPath string
}

// TODO: this is hacky, fixing https://github.com/ipfs/go-ipfs/issues/5703 would help

var err error
if p.Namespace() == path.IPNSNamespace {
if p, err = api.Name().Resolve(ctx, p.String()); err != nil {
return nil, err
return nil, nil, err

Check warning on line 20 in client/rpc/path.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/path.go#L20

Added line #L20 was not covered by tests
}
}

if err := api.Request("dag/resolve", p.String()).Exec(ctx, &out); err != nil {
return nil, err
return nil, nil, err
}

p, err = path.NewPathFromSegments(p.Namespace().String(), out.Cid.String(), out.RemPath)
if err != nil {
return nil, err
return nil, nil, err

Check warning on line 30 in client/rpc/path.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/path.go#L30

Added line #L30 was not covered by tests
}

imPath, err := path.NewImmutablePath(p)
if err != nil {
return nil, nil, err

Check warning on line 35 in client/rpc/path.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/path.go#L35

Added line #L35 was not covered by tests
}

return path.NewImmutablePath(p)
return imPath, path.StringToSegments(out.RemPath), nil
}

func (api *HttpApi) ResolveNode(ctx context.Context, p path.Path) (ipld.Node, error) {
rp, err := api.ResolvePath(ctx, p)
rp, _, err := api.ResolvePath(ctx, p)

Check warning on line 42 in client/rpc/path.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/path.go#L42

Added line #L42 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ It takes a list of CIDs to remove from the local datastore..
return err
}

Check warning on line 271 in core/commands/block.go

View check run for this annotation

Codecov / codecov/patch

core/commands/block.go#L268-L271

Added lines #L268 - L271 were not covered by tests

rp, err := api.ResolvePath(req.Context, p)
rp, _, err := api.ResolvePath(req.Context, p)

Check warning on line 273 in core/commands/block.go

View check run for this annotation

Codecov / codecov/patch

core/commands/block.go#L273

Added line #L273 was not covered by tests
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions core/commands/dag/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"

"github.com/ipfs/boxo/path"
ipldlegacy "github.com/ipfs/go-ipld-legacy"
"github.com/ipfs/kubo/core/commands/cmdenv"
"github.com/ipfs/kubo/core/commands/cmdutils"
Expand Down Expand Up @@ -33,7 +34,7 @@ func dagGet(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) e
return err
}

rp, err := api.ResolvePath(req.Context, p)
rp, remainder, err := api.ResolvePath(req.Context, p)
if err != nil {
return err
}
Expand All @@ -50,8 +51,8 @@ func dagGet(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) e

finalNode := universal.(ipld.Node)

if len(rp.Remainder()) > 0 {
remainderPath := ipld.ParsePath(rp.Remainder())
if len(remainder) > 0 {
remainderPath := ipld.ParsePath(path.SegmentsToString(remainder...))

finalNode, err = traversal.Get(finalNode, remainderPath)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions core/commands/dag/resolve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dagcmd

import (
"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/core/commands/cmdenv"
"github.com/ipfs/kubo/core/commands/cmdutils"

Expand All @@ -18,13 +19,13 @@ func dagResolve(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environmen
return err
}

rp, err := api.ResolvePath(req.Context, p)
rp, remainder, err := api.ResolvePath(req.Context, p)
if err != nil {
return err
}

return cmds.EmitOnce(res, &ResolveOutput{
Cid: rp.Cid(),
RemPath: rp.Remainder(),
RemPath: path.SegmentsToString(remainder...),
})
}
4 changes: 2 additions & 2 deletions core/commands/dag/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func dagStat(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment)
if err != nil {
return err
}
rp, err := api.ResolvePath(req.Context, p)
rp, remainder, err := api.ResolvePath(req.Context, p)
if err != nil {
return err
}
if len(rp.Remainder()) > 0 {
if len(remainder) > 0 {
return fmt.Errorf("cannot return size for anything other than a DAG with a root CID")
}

Expand Down
2 changes: 1 addition & 1 deletion core/commands/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ multihash. Provided for legacy reasons. Use 'ipfs dag get' instead.
return err
}

rp, err := api.ResolvePath(req.Context, path)
rp, _, err := api.ResolvePath(req.Context, path)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions core/commands/pin/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder,
return nil, err
}

Check warning on line 190 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L187-L190

Added lines #L187 - L190 were not covered by tests

rp, err := api.ResolvePath(ctx, p)
rp, _, err := api.ResolvePath(ctx, p)

Check warning on line 192 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L192

Added line #L192 was not covered by tests
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -252,7 +252,7 @@ ipfs pin ls -t indirect <cid>
return err
}

Check warning on line 253 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L250-L253

Added lines #L250 - L253 were not covered by tests

rp, err := api.ResolvePath(req.Context, p)
rp, _, err := api.ResolvePath(req.Context, p)

Check warning on line 255 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L255

Added line #L255 was not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -468,7 +468,7 @@ func pinLsKeys(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fu
return err
}

Check warning on line 469 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L466-L469

Added lines #L466 - L469 were not covered by tests

rp, err := api.ResolvePath(req.Context, p)
rp, _, err := api.ResolvePath(req.Context, p)

Check warning on line 471 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L471

Added line #L471 was not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -594,11 +594,11 @@ pin.
}

Check warning on line 594 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L591-L594

Added lines #L591 - L594 were not covered by tests

// Resolve the paths ahead of time so we can return the actual CIDs
from, err := api.ResolvePath(req.Context, fromPath)
from, _, err := api.ResolvePath(req.Context, fromPath)

Check warning on line 597 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L597

Added line #L597 was not covered by tests
if err != nil {
return err
}
to, err := api.ResolvePath(req.Context, toPath)
to, _, err := api.ResolvePath(req.Context, toPath)

Check warning on line 601 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L601

Added line #L601 was not covered by tests
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/pin/remotepin.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ NOTE: a comma-separated notation is supported in CLI for convenience:
return err
}

Check warning on line 163 in core/commands/pin/remotepin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/remotepin.go#L160-L163

Added lines #L160 - L163 were not covered by tests

rp, err := api.ResolvePath(ctx, p)
rp, _, err := api.ResolvePath(ctx, p)

Check warning on line 165 in core/commands/pin/remotepin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/remotepin.go#L165

Added line #L165 was not covered by tests
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func objectsForPaths(ctx context.Context, n iface.CoreAPI, paths []string) ([]ci
if err != nil {
return nil, err
}
o, err := n.ResolvePath(ctx, p)
o, _, err := n.ResolvePath(ctx, p)

Check warning on line 178 in core/commands/refs.go

View check run for this annotation

Codecov / codecov/patch

core/commands/refs.go#L174-L178

Added lines #L174 - L178 were not covered by tests
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions core/commands/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ Resolve the value of an IPFS DAG path:
}

Check warning on line 134 in core/commands/resolve.go

View check run for this annotation

Codecov / codecov/patch

core/commands/resolve.go#L131-L134

Added lines #L131 - L134 were not covered by tests

// else, ipfs path or ipns with recursive flag
rp, err := api.ResolvePath(req.Context, p)
rp, remainder, err := api.ResolvePath(req.Context, p)

Check warning on line 137 in core/commands/resolve.go

View check run for this annotation

Codecov / codecov/patch

core/commands/resolve.go#L137

Added line #L137 was not covered by tests
if err != nil {
return err
}

// Trick to encode path with correct encoding.
encodedPath := "/" + rp.Namespace().String() + "/" + enc.Encode(rp.Cid())
if remainder := rp.Remainder(); remainder != "" {
encodedPath += "/" + remainder
if len(remainder) != 0 {
encodedPath += path.SegmentsToString(remainder...)
}

Check warning on line 146 in core/commands/resolve.go

View check run for this annotation

Codecov / codecov/patch

core/commands/resolve.go#L143-L146

Added lines #L143 - L146 were not covered by tests

// Ensure valid and sanitized.
Expand Down
6 changes: 3 additions & 3 deletions core/coreapi/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
func (api *BlockAPI) Get(ctx context.Context, p path.Path) (io.Reader, error) {
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Get", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()
rp, err := api.core().ResolvePath(ctx, p)
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}
Expand All @@ -91,7 +91,7 @@ func (api *BlockAPI) Rm(ctx context.Context, p path.Path, opts ...caopts.BlockRm
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Rm", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()

rp, err := api.core().ResolvePath(ctx, p)
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return err
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func (api *BlockAPI) Stat(ctx context.Context, p path.Path) (coreiface.BlockStat
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Stat", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()

rp, err := api.core().ResolvePath(ctx, p)
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions core/coreapi/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopt
return nil, err
}

rp, err := api.core().ResolvePath(ctx, p)
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func (api *DhtAPI) Provide(ctx context.Context, path path.Path, opts ...caopts.D
return err
}

rp, err := api.core().ResolvePath(ctx, path)
rp, _, err := api.core().ResolvePath(ctx, path)
if err != nil {
return err
}
Expand Down
39 changes: 24 additions & 15 deletions core/coreapi/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (api *CoreAPI) ResolveNode(ctx context.Context, p path.Path) (ipld.Node, er
ctx, span := tracing.Span(ctx, "CoreAPI", "ResolveNode", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()

rp, err := api.ResolvePath(ctx, p)
rp, _, err := api.ResolvePath(ctx, p)
if err != nil {
return nil, err
}
Expand All @@ -36,40 +36,49 @@ func (api *CoreAPI) ResolveNode(ctx context.Context, p path.Path) (ipld.Node, er

// ResolvePath resolves the path `p` using Unixfs resolver, returns the
// resolved path.
func (api *CoreAPI) ResolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
func (api *CoreAPI) ResolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, []string, error) {
ctx, span := tracing.Span(ctx, "CoreAPI", "ResolvePath", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()

p, err := resolve.ResolveIPNS(ctx, api.namesys, p)
if err == resolve.ErrNoNamesys {
return nil, coreiface.ErrOffline
return nil, nil, coreiface.ErrOffline
} else if err != nil {
return nil, err
}

if p.Namespace() != path.IPFSNamespace && p.Namespace() != path.IPLDNamespace {
return nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace().String())
return nil, nil, err
}

var resolver ipfspathresolver.Resolver
if p.Namespace() == path.IPLDNamespace {
switch p.Namespace() {
case path.IPLDNamespace:
resolver = api.ipldPathResolver
} else {
case path.IPFSNamespace:
resolver = api.unixFSPathResolver
default:
return nil, nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace().String())
}

node, rest, err := resolver.ResolveToLastNode(ctx, p)
imPath, err := path.NewImmutablePath(p)
if err != nil {
return nil, err
return nil, nil, err
}

node, remainder, err := resolver.ResolveToLastNode(ctx, imPath)
if err != nil {
return nil, nil, err
}

segments := []string{p.Namespace().String(), node.String()}
segments = append(segments, rest...)
segments = append(segments, remainder...)

p, err = path.NewPathFromSegments(segments...)
if err != nil {
return nil, err
return nil, nil, err
}

imPath, err = path.NewImmutablePath(p)
if err != nil {
return nil, nil, err
}

return path.NewImmutablePath(p)
return imPath, remainder, nil
}
8 changes: 4 additions & 4 deletions core/coreapi/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (api *PinAPI) IsPinned(ctx context.Context, p path.Path, opts ...caopts.Pin
ctx, span := tracing.Span(ctx, "CoreAPI.PinAPI", "IsPinned", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()

resolved, err := api.core().ResolvePath(ctx, p)
resolved, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return "", false, fmt.Errorf("error resolving path: %s", err)
}
Expand All @@ -99,7 +99,7 @@ func (api *PinAPI) Rm(ctx context.Context, p path.Path, opts ...caopts.PinRmOpti
ctx, span := tracing.Span(ctx, "CoreAPI.PinAPI", "Rm", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()

rp, err := api.core().ResolvePath(ctx, p)
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return err
}
Expand Down Expand Up @@ -136,12 +136,12 @@ func (api *PinAPI) Update(ctx context.Context, from path.Path, to path.Path, opt

span.SetAttributes(attribute.Bool("unpin", settings.Unpin))

fp, err := api.core().ResolvePath(ctx, from)
fp, _, err := api.core().ResolvePath(ctx, from)
if err != nil {
return err
}

tp, err := api.core().ResolvePath(ctx, to)
tp, _, err := api.core().ResolvePath(ctx, to)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go 1.20
replace github.com/ipfs/kubo => ./../../..

require (
github.com/ipfs/boxo v0.12.1-0.20230906083417-32140848b581
github.com/ipfs/boxo v0.12.1-0.20230907091818-4276346cc566
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.30.0
github.com/multiformats/go-multiaddr v0.11.0
Expand Down
Loading

0 comments on commit 77ea675

Please sign in to comment.