retrieval: Make the ls command work

This commit is contained in:
Łukasz Magiera 2021-11-15 17:27:20 +01:00
parent a1d5b2a293
commit 9c119bfdad
2 changed files with 35 additions and 2 deletions

View File

@ -5,6 +5,8 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@ -274,6 +276,17 @@ func ClientExportStream(apiAddr string, apiAuth http.Header, eref lapi.ExportRef
return nil, xerrors.Errorf("marshaling export ref: %w", err) return nil, xerrors.Errorf("marshaling export ref: %w", err)
} }
ma, err := multiaddr.NewMultiaddr(apiAddr)
if err == nil {
_, addr, err := manet.DialArgs(ma)
if err != nil {
return nil, err
}
// todo: make cliutil helpers for this
apiAddr = "http://" + addr
}
aa, err := url.Parse(apiAddr) aa, err := url.Parse(apiAddr)
if err != nil { if err != nil {
return nil, xerrors.Errorf("parsing api address: %w", err) return nil, xerrors.Errorf("parsing api address: %w", err)
@ -298,6 +311,11 @@ func ClientExportStream(apiAddr string, apiAuth http.Header, eref lapi.ExportRef
return nil, err return nil, err
} }
if resp.StatusCode != http.StatusOK {
resp.Body.Close() // nolint
return nil, xerrors.Errorf("getting root car: http %d", resp.StatusCode)
}
return resp.Body, nil return resp.Body, nil
} }
@ -376,7 +394,7 @@ var clientRetrieveLsCmd = &cli.Command{
eref.DAGs = append(eref.DAGs, lapi.DagSpec{ eref.DAGs = append(eref.DAGs, lapi.DagSpec{
RootSelector: &rootSelector, RootSelector: &rootSelector,
DataSelector: &dataSelector, DataSelector: &rootSelector,
}) })
rc, err := ClientExportStream(ainfo.Addr, ainfo.AuthHeader(), *eref, true) rc, err := ClientExportStream(ainfo.Addr, ainfo.AuthHeader(), *eref, true)
@ -391,7 +409,7 @@ var clientRetrieveLsCmd = &cli.Command{
return err return err
} }
cbs, err := blockstore.NewReadOnly(bytes.NewReader(memcar.Bytes()), nil, cbs, err := blockstore.NewReadOnly(&bytesReaderAt{bytes.NewReader(memcar.Bytes())}, nil,
carv2.ZeroLengthSectionAsEOF(true), carv2.ZeroLengthSectionAsEOF(true),
blockstore.UseWholeCIDs(true)) blockstore.UseWholeCIDs(true))
if err != nil { if err != nil {
@ -421,3 +439,13 @@ var clientRetrieveLsCmd = &cli.Command{
return err return err
}, },
} }
type bytesReaderAt struct {
btr *bytes.Reader
}
func (b bytesReaderAt) ReadAt(p []byte, off int64) (n int, err error) {
return b.btr.ReadAt(p, off)
}
var _ io.ReaderAt = &bytesReaderAt{}

View File

@ -1055,6 +1055,11 @@ func parseDagSpec(ctx context.Context, root cid.Cid, dsp []api.DagSpec, ds forma
} }
if p.LastBlock.Link == nil { if p.LastBlock.Link == nil {
// this is likely the root node that we've matched here
// todo: is this a correct assumption
// todo: is the n ipld.Node above the node we want as the (sub)root?
// todo: how to go from ipld.Node to a cid?
out[i].root = root
return nil return nil
} }