Skip to content

Commit 4276346

Browse files
committed
refactor!: remove .Remainder, coreiface.ResolvePath returns remainder
1 parent 54844c4 commit 4276346

File tree

10 files changed

+150
-91
lines changed

10 files changed

+150
-91
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ The following emojis are used to highlight certain changes:
3131
condensed the different path-related packages under a single one. Therefore, there
3232
are many breaking changes. Please consult the [documentation](https://pkg.go.dev/github.com/ipfs/boxo/path)
3333
for more details on how to use the new package.
34+
* 🛠 The signature of `CoreAPI.ResolvePath` in `coreiface` has changed to now return
35+
the remainder segments as a second return value, matching the signature of `resolver.ResolveToLastNode`.
3436

3537
### Removed
3638

coreiface/coreapi.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ type CoreAPI interface {
4646
// Routing returns an implementation of Routing API
4747
Routing() RoutingAPI
4848

49-
// ResolvePath resolves the path using Unixfs resolver
50-
ResolvePath(context.Context, path.Path) (path.ImmutablePath, error)
49+
// ResolvePath resolves the path using UnixFS resolver, and returns the resolved
50+
// immutable path, and the remainder of the path segments that cannot be resolved
51+
// within UnixFS.
52+
ResolvePath(context.Context, path.Path) (path.ImmutablePath, []string, error)
5153

5254
// ResolveNode resolves the path (if not resolved already) using Unixfs
5355
// resolver, gets and returns the resolved Node

coreiface/tests/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (tp *TestSuite) TestBlockGet(t *testing.T) {
220220

221221
p := path.NewIPFSPath(res.Path().Cid())
222222

223-
rp, err := api.ResolvePath(ctx, p)
223+
rp, _, err := api.ResolvePath(ctx, p)
224224
if err != nil {
225225
t.Fatal(err)
226226
}

coreiface/tests/dag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (tp *TestSuite) TestDagPath(t *testing.T) {
116116
t.Fatal(err)
117117
}
118118

119-
rp, err := api.ResolvePath(ctx, p)
119+
rp, _, err := api.ResolvePath(ctx, p)
120120
if err != nil {
121121
t.Fatal(err)
122122
}

coreiface/tests/path.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ func (tp *TestSuite) TestPathRemainder(t *testing.T) {
5555
p, err := path.Join(path.NewIPFSPath(nd.Cid()), "foo", "bar")
5656
require.NoError(t, err)
5757

58-
rp1, err := api.ResolvePath(ctx, p)
58+
_, remainder, err := api.ResolvePath(ctx, p)
5959
require.NoError(t, err)
60-
require.Equal(t, "/foo/bar", rp1.Remainder())
60+
require.Equal(t, "/foo/bar", path.SegmentsToString(remainder...))
6161
}
6262

6363
func (tp *TestSuite) TestEmptyPathRemainder(t *testing.T) {
@@ -74,9 +74,9 @@ func (tp *TestSuite) TestEmptyPathRemainder(t *testing.T) {
7474
err = api.Dag().Add(ctx, nd)
7575
require.NoError(t, err)
7676

77-
rp1, err := api.ResolvePath(ctx, path.NewIPFSPath(nd.Cid()))
77+
_, remainder, err := api.ResolvePath(ctx, path.NewIPFSPath(nd.Cid()))
7878
require.NoError(t, err)
79-
require.Empty(t, rp1.Remainder())
79+
require.Empty(t, remainder)
8080
}
8181

8282
func (tp *TestSuite) TestInvalidPathRemainder(t *testing.T) {
@@ -96,7 +96,7 @@ func (tp *TestSuite) TestInvalidPathRemainder(t *testing.T) {
9696
p, err := path.Join(path.NewIPLDPath(nd.Cid()), "/bar/baz")
9797
require.NoError(t, err)
9898

99-
_, err = api.ResolvePath(ctx, p)
99+
_, _, err = api.ResolvePath(ctx, p)
100100
require.NotNil(t, err)
101101
require.ErrorContains(t, err, `no link named "bar"`)
102102
}
@@ -122,7 +122,7 @@ func (tp *TestSuite) TestPathRoot(t *testing.T) {
122122
p, err := path.Join(path.NewIPLDPath(nd.Cid()), "/foo")
123123
require.NoError(t, err)
124124

125-
rp, err := api.ResolvePath(ctx, p)
125+
rp, _, err := api.ResolvePath(ctx, p)
126126
require.NoError(t, err)
127127
require.Equal(t, rp.Cid().String(), blk.Path().Cid().String())
128128
}

gateway/blocks_backend.go

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,15 @@ func walkGatewaySimpleSelector(ctx context.Context, p path.ImmutablePath, params
485485
}
486486

487487
func (bb *BlocksBackend) getNode(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, format.Node, error) {
488-
roots, lastSeg, err := bb.getPathRoots(ctx, path)
488+
roots, lastSeg, remainder, err := bb.getPathRoots(ctx, path)
489489
if err != nil {
490490
return ContentPathMetadata{}, nil, err
491491
}
492492

493493
md := ContentPathMetadata{
494-
PathSegmentRoots: roots,
495-
LastSegment: lastSeg,
494+
PathSegmentRoots: roots,
495+
LastSegment: lastSeg,
496+
LastSegmentRemainder: remainder,
496497
}
497498

498499
lastRoot := lastSeg.Cid()
@@ -505,7 +506,7 @@ func (bb *BlocksBackend) getNode(ctx context.Context, path path.ImmutablePath) (
505506
return md, nd, err
506507
}
507508

508-
func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.ImmutablePath) ([]cid.Cid, path.ImmutablePath, error) {
509+
func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.ImmutablePath) ([]cid.Cid, path.ImmutablePath, []string, error) {
509510
/*
510511
These are logical roots where each CID represent one path segment
511512
and resolves to either a directory or the root block of a file.
@@ -529,7 +530,10 @@ func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.Immu
529530
contentPathStr := contentPath.String()
530531
pathSegments := strings.Split(contentPathStr[6:], "/")
531532
sp.WriteString(contentPathStr[:5]) // /ipfs or /ipns
532-
var lastPath path.ImmutablePath
533+
var (
534+
lastPath path.ImmutablePath
535+
remainder []string
536+
)
533537
for _, root := range pathSegments {
534538
if root == "" {
535539
continue
@@ -538,23 +542,24 @@ func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.Immu
538542
sp.WriteString(root)
539543
p, err := path.NewPath(sp.String())
540544
if err != nil {
541-
return nil, nil, err
545+
return nil, nil, nil, err
542546
}
543-
resolvedSubPath, err := bb.resolvePath(ctx, p)
547+
resolvedSubPath, remainderSubPath, err := bb.resolvePath(ctx, p)
544548
if err != nil {
545549
// TODO: should we be more explicit here and is this part of the IPFSBackend contract?
546550
// The issue here was that we returned datamodel.ErrWrongKind instead of this resolver error
547551
if isErrNotFound(err) {
548-
return nil, nil, &resolver.ErrNoLink{Name: root, Node: lastPath.Cid()}
552+
return nil, nil, nil, &resolver.ErrNoLink{Name: root, Node: lastPath.Cid()}
549553
}
550-
return nil, nil, err
554+
return nil, nil, nil, err
551555
}
552556
lastPath = resolvedSubPath
557+
remainder = remainderSubPath
553558
pathRoots = append(pathRoots, lastPath.Cid())
554559
}
555560

556561
pathRoots = pathRoots[:len(pathRoots)-1]
557-
return pathRoots, lastPath, nil
562+
return pathRoots, lastPath, remainder, nil
558563
}
559564

560565
func (bb *BlocksBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
@@ -605,7 +610,7 @@ func (bb *BlocksBackend) GetDNSLinkRecord(ctx context.Context, hostname string)
605610
}
606611

607612
func (bb *BlocksBackend) IsCached(ctx context.Context, p path.Path) bool {
608-
rp, err := bb.resolvePath(ctx, p)
613+
rp, _, err := bb.resolvePath(ctx, p)
609614
if err != nil {
610615
return false
611616
}
@@ -615,46 +620,52 @@ func (bb *BlocksBackend) IsCached(ctx context.Context, p path.Path) bool {
615620
}
616621

617622
func (bb *BlocksBackend) ResolvePath(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, error) {
618-
roots, lastSeg, err := bb.getPathRoots(ctx, path)
623+
roots, lastSeg, remainder, err := bb.getPathRoots(ctx, path)
619624
if err != nil {
620625
return ContentPathMetadata{}, err
621626
}
622627
md := ContentPathMetadata{
623-
PathSegmentRoots: roots,
624-
LastSegment: lastSeg,
628+
PathSegmentRoots: roots,
629+
LastSegment: lastSeg,
630+
LastSegmentRemainder: remainder,
625631
}
626632
return md, nil
627633
}
628634

629-
func (bb *BlocksBackend) resolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
635+
func (bb *BlocksBackend) resolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, []string, error) {
630636
var err error
631637
if p.Namespace() == path.IPNSNamespace {
632638
p, err = resolve.ResolveIPNS(ctx, bb.namesys, p)
633639
if err != nil {
634-
return nil, err
640+
return nil, nil, err
635641
}
636642
}
637643

638644
if p.Namespace() != path.IPFSNamespace {
639-
return nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace())
645+
return nil, nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace())
640646
}
641647

642648
imPath, err := path.NewImmutablePath(p)
643649
if err != nil {
644-
return nil, err
650+
return nil, nil, err
645651
}
646652

647-
node, rest, err := bb.resolver.ResolveToLastNode(ctx, imPath)
653+
node, remainder, err := bb.resolver.ResolveToLastNode(ctx, imPath)
648654
if err != nil {
649-
return nil, err
655+
return nil, nil, err
650656
}
651657

652-
p, err = path.Join(path.NewIPFSPath(node), rest...)
658+
p, err = path.Join(path.NewIPFSPath(node), remainder...)
653659
if err != nil {
654-
return nil, err
660+
return nil, nil, err
661+
}
662+
663+
imPath, err = path.NewImmutablePath(p)
664+
if err != nil {
665+
return nil, nil, err
655666
}
656667

657-
return path.NewImmutablePath(p)
668+
return imPath, remainder, nil
658669
}
659670

660671
type nodeGetterToCarExporer struct {

gateway/gateway.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,10 @@ func (d DuplicateBlocksPolicy) String() string {
204204
}
205205

206206
type ContentPathMetadata struct {
207-
PathSegmentRoots []cid.Cid
208-
LastSegment path.ImmutablePath
209-
ContentType string // Only used for UnixFS requests
207+
PathSegmentRoots []cid.Cid
208+
LastSegment path.ImmutablePath
209+
LastSegmentRemainder []string
210+
ContentType string // Only used for UnixFS requests
210211
}
211212

212213
// ByteRange describes a range request within a UnixFS file. "From" and "To" mostly

gateway/handler_codec.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ func (i *handler) renderCodec(ctx context.Context, w http.ResponseWriter, r *htt
8484
// If the resolved path still has some remainder, return error for now.
8585
// TODO: handle this when we have IPLD Patch (https://ipld.io/specs/patch/) via HTTP PUT
8686
// TODO: (depends on https://github.com/ipfs/kubo/issues/4801 and https://github.com/ipfs/kubo/issues/4782)
87-
if resolvedPath.Remainder() != "" {
88-
path := strings.TrimSuffix(resolvedPath.String(), resolvedPath.Remainder())
89-
err := fmt.Errorf("%q of %q could not be returned: reading IPLD Kinds other than Links (CBOR Tag 42) is not implemented: try reading %q instead", resolvedPath.Remainder(), resolvedPath.String(), path)
87+
if len(rq.pathMetadata.LastSegmentRemainder) != 0 {
88+
remainderStr := path.SegmentsToString(rq.pathMetadata.LastSegmentRemainder...)
89+
path := strings.TrimSuffix(resolvedPath.String(), remainderStr)
90+
err := fmt.Errorf("%q of %q could not be returned: reading IPLD Kinds other than Links (CBOR Tag 42) is not implemented: try reading %q instead", remainderStr, resolvedPath.String(), path)
9091
i.webError(w, r, err, http.StatusNotImplemented)
9192
return false
9293
}

0 commit comments

Comments
 (0)