Skip to content

Commit 4b18d58

Browse files
committed
refactor: return remainder from ResolvePath
1 parent 4d2eb03 commit 4b18d58

File tree

23 files changed

+84
-64
lines changed

23 files changed

+84
-64
lines changed

client/rpc/apifile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const forwardSeekLimit = 1 << 14 // 16k
1717
func (api *UnixfsAPI) Get(ctx context.Context, p path.Path) (files.Node, error) {
1818
if p.Namespace().Mutable() { // use resolved path in case we are dealing with IPNS / MFS
1919
var err error
20-
p, err = api.core().ResolvePath(ctx, p)
20+
p, _, err = api.core().ResolvePath(ctx, p)
2121
if err != nil {
2222
return nil, err
2323
}

client/rpc/dht.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopt
4242
return nil, err
4343
}
4444

45-
rp, err := api.core().ResolvePath(ctx, p)
45+
rp, _, err := api.core().ResolvePath(ctx, p)
4646
if err != nil {
4747
return nil, err
4848
}
@@ -98,7 +98,7 @@ func (api *DhtAPI) Provide(ctx context.Context, p path.Path, opts ...caopts.DhtP
9898
return err
9999
}
100100

101-
rp, err := api.core().ResolvePath(ctx, p)
101+
rp, _, err := api.core().ResolvePath(ctx, p)
102102
if err != nil {
103103
return err
104104
}

client/rpc/path.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,38 @@ import (
88
ipld "github.com/ipfs/go-ipld-format"
99
)
1010

11-
func (api *HttpApi) ResolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
11+
func (api *HttpApi) ResolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, []string, error) {
1212
var out struct {
1313
Cid cid.Cid
1414
RemPath string
1515
}
1616

17-
// TODO: this is hacky, fixing https://github.com/ipfs/go-ipfs/issues/5703 would help
18-
1917
var err error
2018
if p.Namespace() == path.IPNSNamespace {
2119
if p, err = api.Name().Resolve(ctx, p.String()); err != nil {
22-
return nil, err
20+
return nil, nil, err
2321
}
2422
}
2523

2624
if err := api.Request("dag/resolve", p.String()).Exec(ctx, &out); err != nil {
27-
return nil, err
25+
return nil, nil, err
2826
}
2927

3028
p, err = path.NewPathFromSegments(p.Namespace().String(), out.Cid.String(), out.RemPath)
3129
if err != nil {
32-
return nil, err
30+
return nil, nil, err
31+
}
32+
33+
imPath, err := path.NewImmutablePath(p)
34+
if err != nil {
35+
return nil, nil, err
3336
}
3437

35-
return path.NewImmutablePath(p)
38+
return imPath, path.StringToSegments(out.RemPath), nil
3639
}
3740

3841
func (api *HttpApi) ResolveNode(ctx context.Context, p path.Path) (ipld.Node, error) {
39-
rp, err := api.ResolvePath(ctx, p)
42+
rp, _, err := api.ResolvePath(ctx, p)
4043
if err != nil {
4144
return nil, err
4245
}

core/commands/block.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ It takes a list of CIDs to remove from the local datastore..
270270
return err
271271
}
272272

273-
rp, err := api.ResolvePath(req.Context, p)
273+
rp, _, err := api.ResolvePath(req.Context, p)
274274
if err != nil {
275275
return err
276276
}

core/commands/dag/get.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66

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

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

5152
finalNode := universal.(ipld.Node)
5253

53-
if len(rp.Remainder()) > 0 {
54-
remainderPath := ipld.ParsePath(rp.Remainder())
54+
if len(remainder) > 0 {
55+
remainderPath := ipld.ParsePath(path.SegmentsToString(remainder...))
5556

5657
finalNode, err = traversal.Get(finalNode, remainderPath)
5758
if err != nil {

core/commands/dag/resolve.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dagcmd
22

33
import (
4+
"github.com/ipfs/boxo/path"
45
"github.com/ipfs/kubo/core/commands/cmdenv"
56
"github.com/ipfs/kubo/core/commands/cmdutils"
67

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

21-
rp, err := api.ResolvePath(req.Context, p)
22+
rp, remainder, err := api.ResolvePath(req.Context, p)
2223
if err != nil {
2324
return err
2425
}
2526

2627
return cmds.EmitOnce(res, &ResolveOutput{
2728
Cid: rp.Cid(),
28-
RemPath: rp.Remainder(),
29+
RemPath: path.SegmentsToString(remainder...),
2930
})
3031
}

core/commands/dag/stat.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ func dagStat(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment)
3333
if err != nil {
3434
return err
3535
}
36-
rp, err := api.ResolvePath(req.Context, p)
36+
rp, remainder, err := api.ResolvePath(req.Context, p)
3737
if err != nil {
3838
return err
3939
}
40-
if len(rp.Remainder()) > 0 {
40+
if len(remainder) > 0 {
4141
return fmt.Errorf("cannot return size for anything other than a DAG with a root CID")
4242
}
4343

core/commands/object/object.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ multihash. Provided for legacy reasons. Use 'ipfs dag get' instead.
143143
return err
144144
}
145145

146-
rp, err := api.ResolvePath(req.Context, path)
146+
rp, _, err := api.ResolvePath(req.Context, path)
147147
if err != nil {
148148
return err
149149
}

core/commands/pin/pin.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder,
189189
return nil, err
190190
}
191191

192-
rp, err := api.ResolvePath(ctx, p)
192+
rp, _, err := api.ResolvePath(ctx, p)
193193
if err != nil {
194194
return nil, err
195195
}
@@ -252,7 +252,7 @@ ipfs pin ls -t indirect <cid>
252252
return err
253253
}
254254

255-
rp, err := api.ResolvePath(req.Context, p)
255+
rp, _, err := api.ResolvePath(req.Context, p)
256256
if err != nil {
257257
return err
258258
}
@@ -468,7 +468,7 @@ func pinLsKeys(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fu
468468
return err
469469
}
470470

471-
rp, err := api.ResolvePath(req.Context, p)
471+
rp, _, err := api.ResolvePath(req.Context, p)
472472
if err != nil {
473473
return err
474474
}
@@ -594,11 +594,11 @@ pin.
594594
}
595595

596596
// Resolve the paths ahead of time so we can return the actual CIDs
597-
from, err := api.ResolvePath(req.Context, fromPath)
597+
from, _, err := api.ResolvePath(req.Context, fromPath)
598598
if err != nil {
599599
return err
600600
}
601-
to, err := api.ResolvePath(req.Context, toPath)
601+
to, _, err := api.ResolvePath(req.Context, toPath)
602602
if err != nil {
603603
return err
604604
}

core/commands/pin/remotepin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ NOTE: a comma-separated notation is supported in CLI for convenience:
162162
return err
163163
}
164164

165-
rp, err := api.ResolvePath(ctx, p)
165+
rp, _, err := api.ResolvePath(ctx, p)
166166
if err != nil {
167167
return err
168168
}

core/commands/refs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func objectsForPaths(ctx context.Context, n iface.CoreAPI, paths []string) ([]ci
175175
if err != nil {
176176
return nil, err
177177
}
178-
o, err := n.ResolvePath(ctx, p)
178+
o, _, err := n.ResolvePath(ctx, p)
179179
if err != nil {
180180
return nil, err
181181
}

core/commands/resolve.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ Resolve the value of an IPFS DAG path:
134134
}
135135

136136
// else, ipfs path or ipns with recursive flag
137-
rp, err := api.ResolvePath(req.Context, p)
137+
rp, remainder, err := api.ResolvePath(req.Context, p)
138138
if err != nil {
139139
return err
140140
}
141141

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

148148
// Ensure valid and sanitized.

core/coreapi/block.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
7474
func (api *BlockAPI) Get(ctx context.Context, p path.Path) (io.Reader, error) {
7575
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Get", trace.WithAttributes(attribute.String("path", p.String())))
7676
defer span.End()
77-
rp, err := api.core().ResolvePath(ctx, p)
77+
rp, _, err := api.core().ResolvePath(ctx, p)
7878
if err != nil {
7979
return nil, err
8080
}
@@ -91,7 +91,7 @@ func (api *BlockAPI) Rm(ctx context.Context, p path.Path, opts ...caopts.BlockRm
9191
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Rm", trace.WithAttributes(attribute.String("path", p.String())))
9292
defer span.End()
9393

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

135-
rp, err := api.core().ResolvePath(ctx, p)
135+
rp, _, err := api.core().ResolvePath(ctx, p)
136136
if err != nil {
137137
return nil, err
138138
}

core/coreapi/dht.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopt
5353
return nil, err
5454
}
5555

56-
rp, err := api.core().ResolvePath(ctx, p)
56+
rp, _, err := api.core().ResolvePath(ctx, p)
5757
if err != nil {
5858
return nil, err
5959
}
@@ -82,7 +82,7 @@ func (api *DhtAPI) Provide(ctx context.Context, path path.Path, opts ...caopts.D
8282
return err
8383
}
8484

85-
rp, err := api.core().ResolvePath(ctx, path)
85+
rp, _, err := api.core().ResolvePath(ctx, path)
8686
if err != nil {
8787
return err
8888
}

core/coreapi/path.go

+24-15
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (api *CoreAPI) ResolveNode(ctx context.Context, p path.Path) (ipld.Node, er
2222
ctx, span := tracing.Span(ctx, "CoreAPI", "ResolveNode", trace.WithAttributes(attribute.String("path", p.String())))
2323
defer span.End()
2424

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

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

4343
p, err := resolve.ResolveIPNS(ctx, api.namesys, p)
4444
if err == resolve.ErrNoNamesys {
45-
return nil, coreiface.ErrOffline
45+
return nil, nil, coreiface.ErrOffline
4646
} else if err != nil {
47-
return nil, err
48-
}
49-
50-
if p.Namespace() != path.IPFSNamespace && p.Namespace() != path.IPLDNamespace {
51-
return nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace().String())
47+
return nil, nil, err
5248
}
5349

5450
var resolver ipfspathresolver.Resolver
55-
if p.Namespace() == path.IPLDNamespace {
51+
switch p.Namespace() {
52+
case path.IPLDNamespace:
5653
resolver = api.ipldPathResolver
57-
} else {
54+
case path.IPFSNamespace:
5855
resolver = api.unixFSPathResolver
56+
default:
57+
return nil, nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace().String())
5958
}
6059

61-
node, rest, err := resolver.ResolveToLastNode(ctx, p)
60+
imPath, err := path.NewImmutablePath(p)
6261
if err != nil {
63-
return nil, err
62+
return nil, nil, err
63+
}
64+
65+
node, remainder, err := resolver.ResolveToLastNode(ctx, imPath)
66+
if err != nil {
67+
return nil, nil, err
6468
}
6569

6670
segments := []string{p.Namespace().String(), node.String()}
67-
segments = append(segments, rest...)
71+
segments = append(segments, remainder...)
6872

6973
p, err = path.NewPathFromSegments(segments...)
7074
if err != nil {
71-
return nil, err
75+
return nil, nil, err
76+
}
77+
78+
imPath, err = path.NewImmutablePath(p)
79+
if err != nil {
80+
return nil, nil, err
7281
}
7382

74-
return path.NewImmutablePath(p)
83+
return imPath, remainder, nil
7584
}

core/coreapi/pin.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (api *PinAPI) IsPinned(ctx context.Context, p path.Path, opts ...caopts.Pin
7474
ctx, span := tracing.Span(ctx, "CoreAPI.PinAPI", "IsPinned", trace.WithAttributes(attribute.String("path", p.String())))
7575
defer span.End()
7676

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

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

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

139-
fp, err := api.core().ResolvePath(ctx, from)
139+
fp, _, err := api.core().ResolvePath(ctx, from)
140140
if err != nil {
141141
return err
142142
}
143143

144-
tp, err := api.core().ResolvePath(ctx, to)
144+
tp, _, err := api.core().ResolvePath(ctx, to)
145145
if err != nil {
146146
return err
147147
}

docs/examples/kubo-as-a-library/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ go 1.20
77
replace github.com/ipfs/kubo => ./../../..
88

99
require (
10-
github.com/ipfs/boxo v0.12.1-0.20230906083417-32140848b581
10+
github.com/ipfs/boxo v0.12.1-0.20230907091818-4276346cc566
1111
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
1212
github.com/libp2p/go-libp2p v0.30.0
1313
github.com/multiformats/go-multiaddr v0.11.0

0 commit comments

Comments
 (0)