Skip to content

Commit 968b014

Browse files
committed
refactor!: remove .Remainder, coreiface.ResolvePath returns remainder
1 parent b2abe18 commit 968b014

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
@@ -28,6 +28,8 @@ The following emojis are used to highlight certain changes:
2828
condensed the different path-related packages under a single one. Therefore, there
2929
are many breaking changes. Please consult the [documentation](https://pkg.go.dev/github.com/ipfs/boxo/path)
3030
for more details on how to use the new package.
31+
* 🛠 The signature of `CoreAPI.ResolvePath` in `coreiface` has changed to now return
32+
the remainder segments as a second return value, matching the signature of `resolver.ResolveToLastNode`.
3133

3234
### Removed
3335

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
@@ -538,14 +538,15 @@ func walkGatewaySimpleSelector(ctx context.Context, p path.ImmutablePath, params
538538
}
539539

540540
func (bb *BlocksBackend) getNode(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, format.Node, error) {
541-
roots, lastSeg, err := bb.getPathRoots(ctx, path)
541+
roots, lastSeg, remainder, err := bb.getPathRoots(ctx, path)
542542
if err != nil {
543543
return ContentPathMetadata{}, nil, err
544544
}
545545

546546
md := ContentPathMetadata{
547-
PathSegmentRoots: roots,
548-
LastSegment: lastSeg,
547+
PathSegmentRoots: roots,
548+
LastSegment: lastSeg,
549+
LastSegmentRemainder: remainder,
549550
}
550551

551552
lastRoot := lastSeg.Cid()
@@ -558,7 +559,7 @@ func (bb *BlocksBackend) getNode(ctx context.Context, path path.ImmutablePath) (
558559
return md, nd, err
559560
}
560561

561-
func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.ImmutablePath) ([]cid.Cid, path.ImmutablePath, error) {
562+
func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.ImmutablePath) ([]cid.Cid, path.ImmutablePath, []string, error) {
562563
/*
563564
These are logical roots where each CID represent one path segment
564565
and resolves to either a directory or the root block of a file.
@@ -582,7 +583,10 @@ func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.Immu
582583
contentPathStr := contentPath.String()
583584
pathSegments := strings.Split(contentPathStr[6:], "/")
584585
sp.WriteString(contentPathStr[:5]) // /ipfs or /ipns
585-
var lastPath path.ImmutablePath
586+
var (
587+
lastPath path.ImmutablePath
588+
remainder []string
589+
)
586590
for _, root := range pathSegments {
587591
if root == "" {
588592
continue
@@ -591,23 +595,24 @@ func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.Immu
591595
sp.WriteString(root)
592596
p, err := path.NewPath(sp.String())
593597
if err != nil {
594-
return nil, nil, err
598+
return nil, nil, nil, err
595599
}
596-
resolvedSubPath, err := bb.resolvePath(ctx, p)
600+
resolvedSubPath, remainderSubPath, err := bb.resolvePath(ctx, p)
597601
if err != nil {
598602
// TODO: should we be more explicit here and is this part of the IPFSBackend contract?
599603
// The issue here was that we returned datamodel.ErrWrongKind instead of this resolver error
600604
if isErrNotFound(err) {
601-
return nil, nil, &resolver.ErrNoLink{Name: root, Node: lastPath.Cid()}
605+
return nil, nil, nil, &resolver.ErrNoLink{Name: root, Node: lastPath.Cid()}
602606
}
603-
return nil, nil, err
607+
return nil, nil, nil, err
604608
}
605609
lastPath = resolvedSubPath
610+
remainder = remainderSubPath
606611
pathRoots = append(pathRoots, lastPath.Cid())
607612
}
608613

609614
pathRoots = pathRoots[:len(pathRoots)-1]
610-
return pathRoots, lastPath, nil
615+
return pathRoots, lastPath, remainder, nil
611616
}
612617

613618
func (bb *BlocksBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
@@ -658,7 +663,7 @@ func (bb *BlocksBackend) GetDNSLinkRecord(ctx context.Context, hostname string)
658663
}
659664

660665
func (bb *BlocksBackend) IsCached(ctx context.Context, p path.Path) bool {
661-
rp, err := bb.resolvePath(ctx, p)
666+
rp, _, err := bb.resolvePath(ctx, p)
662667
if err != nil {
663668
return false
664669
}
@@ -668,46 +673,52 @@ func (bb *BlocksBackend) IsCached(ctx context.Context, p path.Path) bool {
668673
}
669674

670675
func (bb *BlocksBackend) ResolvePath(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, error) {
671-
roots, lastSeg, err := bb.getPathRoots(ctx, path)
676+
roots, lastSeg, remainder, err := bb.getPathRoots(ctx, path)
672677
if err != nil {
673678
return ContentPathMetadata{}, err
674679
}
675680
md := ContentPathMetadata{
676-
PathSegmentRoots: roots,
677-
LastSegment: lastSeg,
681+
PathSegmentRoots: roots,
682+
LastSegment: lastSeg,
683+
LastSegmentRemainder: remainder,
678684
}
679685
return md, nil
680686
}
681687

682-
func (bb *BlocksBackend) resolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
688+
func (bb *BlocksBackend) resolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, []string, error) {
683689
var err error
684690
if p.Namespace() == path.IPNSNamespace {
685691
p, err = resolve.ResolveIPNS(ctx, bb.namesys, p)
686692
if err != nil {
687-
return nil, err
693+
return nil, nil, err
688694
}
689695
}
690696

691697
if p.Namespace() != path.IPFSNamespace {
692-
return nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace())
698+
return nil, nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace())
693699
}
694700

695701
imPath, err := path.NewImmutablePath(p)
696702
if err != nil {
697-
return nil, err
703+
return nil, nil, err
698704
}
699705

700-
node, rest, err := bb.resolver.ResolveToLastNode(ctx, imPath)
706+
node, remainder, err := bb.resolver.ResolveToLastNode(ctx, imPath)
701707
if err != nil {
702-
return nil, err
708+
return nil, nil, err
703709
}
704710

705-
p, err = path.Join(path.NewIPFSPath(node), rest...)
711+
p, err = path.Join(path.NewIPFSPath(node), remainder...)
706712
if err != nil {
707-
return nil, err
713+
return nil, nil, err
714+
}
715+
716+
imPath, err = path.NewImmutablePath(p)
717+
if err != nil {
718+
return nil, nil, err
708719
}
709720

710-
return path.NewImmutablePath(p)
721+
return imPath, remainder, nil
711722
}
712723

713724
type nodeGetterToCarExporer struct {

gateway/gateway.go

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

211211
type ContentPathMetadata struct {
212-
PathSegmentRoots []cid.Cid
213-
LastSegment path.ImmutablePath
214-
ContentType string // Only used for UnixFS requests
212+
PathSegmentRoots []cid.Cid
213+
LastSegment path.ImmutablePath
214+
LastSegmentRemainder []string
215+
ContentType string // Only used for UnixFS requests
215216
}
216217

217218
// 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
@@ -90,9 +90,10 @@ func (i *handler) renderCodec(ctx context.Context, w http.ResponseWriter, r *htt
9090
// If the resolved path still has some remainder, return error for now.
9191
// TODO: handle this when we have IPLD Patch (https://ipld.io/specs/patch/) via HTTP PUT
9292
// TODO: (depends on https://github.com/ipfs/kubo/issues/4801 and https://github.com/ipfs/kubo/issues/4782)
93-
if resolvedPath.Remainder() != "" {
94-
path := strings.TrimSuffix(resolvedPath.String(), resolvedPath.Remainder())
95-
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)
93+
if len(rq.pathMetadata.LastSegmentRemainder) != 0 {
94+
remainderStr := path.SegmentsToString(rq.pathMetadata.LastSegmentRemainder...)
95+
path := strings.TrimSuffix(resolvedPath.String(), remainderStr)
96+
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)
9697
i.webError(w, r, err, http.StatusNotImplemented)
9798
return false
9899
}

0 commit comments

Comments
 (0)