Skip to content

Commit

Permalink
fix(gateway): return CAR proving the requested path does not exist in…
Browse files Browse the repository at this point in the history
…stead of just erroring
  • Loading branch information
aschmahmann committed Aug 17, 2023
1 parent 5f3ef39 commit aaf8be9
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,45 @@ func (bb *BlocksBackend) Head(ctx context.Context, path ImmutablePath) (ContentP
return md, fileNode, nil
}

// emptyRoot is a CAR root with the empty identity CID. CAR files are recommended
// to always include a CID in their root, even if it's just the empty CID.
// https://ipld.io/specs/transport/car/carv1/#number-of-roots
var emptyRoot = []cid.Cid{cid.MustParse("bafkqaaa")}

func (bb *BlocksBackend) GetCAR(ctx context.Context, p ImmutablePath, params CarParams) (ContentPathMetadata, io.ReadCloser, error) {
pathMetadata, err := bb.ResolvePath(ctx, p)
if err != nil {
rootCid, err := cid.Decode(strings.Split(p.String(), "/")[2])
if err != nil {
return ContentPathMetadata{}, nil, err
}

var buf bytes.Buffer
cw, err := storage.NewWritable(&buf, emptyRoot, car.WriteAsCarV1(true))
if err != nil {
return ContentPathMetadata{}, nil, err
}

blockGetter := merkledag.NewDAGService(bb.blockService).Session(ctx)

blockGetter = &nodeGetterToCarExporer{
ng: blockGetter,
cw: cw,
}

// Setup the UnixFS resolver.
f := newNodeGetterFetcherSingleUseFactory(ctx, blockGetter)
pathResolver := resolver.NewBasicResolver(f)
ip := ipfspath.FromString(p.String())
_, _, err = pathResolver.ResolveToLastNode(ctx, ip)

if isErrNotFound(err) {
return ContentPathMetadata{
PathSegmentRoots: nil,
LastSegment: ifacepath.NewResolvedPath(ip, rootCid, rootCid, ""),
ContentType: "",
}, io.NopCloser(&buf), nil
}
return ContentPathMetadata{}, nil, err
}

Expand Down

0 comments on commit aaf8be9

Please sign in to comment.