2021-11-15 12:53:28 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-11-17 18:52:31 +00:00
|
|
|
"io/ioutil"
|
2021-11-15 12:53:28 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"sort"
|
2021-11-15 16:40:05 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-11-15 12:53:28 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-blockservice"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
offline "github.com/ipfs/go-ipfs-exchange-offline"
|
|
|
|
"github.com/ipfs/go-merkledag"
|
|
|
|
carv2 "github.com/ipld/go-car/v2"
|
|
|
|
"github.com/ipld/go-car/v2/blockstore"
|
2021-11-17 18:52:31 +00:00
|
|
|
"github.com/ipld/go-ipld-prime"
|
|
|
|
"github.com/ipld/go-ipld-prime/codec/dagjson"
|
|
|
|
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
|
|
|
"github.com/ipld/go-ipld-prime/traversal"
|
|
|
|
"github.com/ipld/go-ipld-prime/traversal/selector"
|
|
|
|
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
|
|
|
|
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
|
|
|
|
textselector "github.com/ipld/go-ipld-selector-text-lite"
|
2021-11-16 11:04:03 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2021-11-15 12:53:28 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2021-11-16 11:04:03 +00:00
|
|
|
|
2021-11-15 12:53:28 +00:00
|
|
|
lapi "github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2021-11-17 18:52:31 +00:00
|
|
|
"github.com/filecoin-project/lotus/markets/utils"
|
2021-11-16 11:04:03 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2021-11-15 12:53:28 +00:00
|
|
|
)
|
|
|
|
|
2021-11-16 14:06:05 +00:00
|
|
|
const DefaultMaxRetrievePrice = "0"
|
2021-11-15 12:53:28 +00:00
|
|
|
|
|
|
|
func retrieve(ctx context.Context, cctx *cli.Context, fapi lapi.FullNode, sel *lapi.Selector, printf func(string, ...interface{})) (*lapi.ExportRef, error) {
|
|
|
|
var payer address.Address
|
|
|
|
var err error
|
|
|
|
if cctx.String("from") != "" {
|
|
|
|
payer, err = address.NewFromString(cctx.String("from"))
|
|
|
|
} else {
|
|
|
|
payer, err = fapi.WalletDefaultAddress(ctx)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := cid.Parse(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pieceCid *cid.Cid
|
|
|
|
if cctx.String("pieceCid") != "" {
|
|
|
|
parsed, err := cid.Parse(cctx.String("pieceCid"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pieceCid = &parsed
|
|
|
|
}
|
|
|
|
|
|
|
|
var eref *lapi.ExportRef
|
|
|
|
if cctx.Bool("allow-local") {
|
|
|
|
imports, err := fapi.ClientListImports(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range imports {
|
|
|
|
if i.Root != nil && i.Root.Equals(file) {
|
|
|
|
eref = &lapi.ExportRef{
|
|
|
|
Root: file,
|
|
|
|
FromLocalCAR: i.CARPath,
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no local found, so make a retrieval
|
|
|
|
if eref == nil {
|
|
|
|
var offer lapi.QueryOffer
|
2021-11-23 21:23:13 +00:00
|
|
|
minerStrAddr := cctx.String("provider")
|
2021-11-15 12:53:28 +00:00
|
|
|
if minerStrAddr == "" { // Local discovery
|
|
|
|
offers, err := fapi.ClientFindData(ctx, file, pieceCid)
|
|
|
|
|
|
|
|
var cleaned []lapi.QueryOffer
|
|
|
|
// filter out offers that errored
|
|
|
|
for _, o := range offers {
|
|
|
|
if o.Err == "" {
|
|
|
|
cleaned = append(cleaned, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offers = cleaned
|
|
|
|
|
|
|
|
// sort by price low to high
|
|
|
|
sort.Slice(offers, func(i, j int) bool {
|
|
|
|
return offers[i].MinPrice.LessThan(offers[j].MinPrice)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: parse offer strings from `client find`, make this smarter
|
|
|
|
if len(offers) < 1 {
|
|
|
|
fmt.Println("Failed to find file")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
offer = offers[0]
|
|
|
|
} else { // Directed retrieval
|
|
|
|
minerAddr, err := address.NewFromString(minerStrAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
offer, err = fapi.ClientMinerQueryOffer(ctx, minerAddr, file, pieceCid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if offer.Err != "" {
|
|
|
|
return nil, fmt.Errorf("offer error: %s", offer.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxPrice := types.MustParseFIL(DefaultMaxRetrievePrice)
|
|
|
|
|
|
|
|
if cctx.String("maxPrice") != "" {
|
|
|
|
maxPrice, err = types.ParseFIL(cctx.String("maxPrice"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("parsing maxPrice: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if offer.MinPrice.GreaterThan(big.Int(maxPrice)) {
|
2022-06-07 16:10:20 +00:00
|
|
|
return nil, xerrors.Errorf("failed to find offer satisfying maxPrice: %s. Try increasing maxPrice", maxPrice)
|
2021-11-15 12:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o := offer.Order(payer)
|
|
|
|
o.DataSelector = sel
|
|
|
|
|
|
|
|
subscribeEvents, err := fapi.ClientGetRetrievalUpdates(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("error setting up retrieval updates: %w", err)
|
|
|
|
}
|
|
|
|
retrievalRes, err := fapi.ClientRetrieve(ctx, o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("error setting up retrieval: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-15 16:40:05 +00:00
|
|
|
start := time.Now()
|
2021-11-15 12:53:28 +00:00
|
|
|
readEvents:
|
|
|
|
for {
|
|
|
|
var evt lapi.RetrievalInfo
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, xerrors.New("Retrieval Timed Out")
|
|
|
|
case evt = <-subscribeEvents:
|
|
|
|
if evt.ID != retrievalRes.DealID {
|
|
|
|
// we can't check the deal ID ahead of time because:
|
|
|
|
// 1. We need to subscribe before retrieving.
|
|
|
|
// 2. We won't know the deal ID until after retrieving.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event := "New"
|
|
|
|
if evt.Event != nil {
|
|
|
|
event = retrievalmarket.ClientEvents[*evt.Event]
|
|
|
|
}
|
|
|
|
|
2021-11-15 16:40:05 +00:00
|
|
|
printf("Recv %s, Paid %s, %s (%s), %s\n",
|
2021-11-15 12:53:28 +00:00
|
|
|
types.SizeStr(types.NewInt(evt.BytesReceived)),
|
|
|
|
types.FIL(evt.TotalPaid),
|
2021-11-15 16:40:05 +00:00
|
|
|
strings.TrimPrefix(event, "ClientEvent"),
|
|
|
|
strings.TrimPrefix(retrievalmarket.DealStatuses[evt.Status], "DealStatus"),
|
|
|
|
time.Now().Sub(start).Truncate(time.Millisecond),
|
2021-11-15 12:53:28 +00:00
|
|
|
)
|
2021-11-15 16:40:05 +00:00
|
|
|
|
2021-11-15 12:53:28 +00:00
|
|
|
switch evt.Status {
|
|
|
|
case retrievalmarket.DealStatusCompleted:
|
|
|
|
break readEvents
|
|
|
|
case retrievalmarket.DealStatusRejected:
|
|
|
|
return nil, xerrors.Errorf("Retrieval Proposal Rejected: %s", evt.Message)
|
2022-06-23 20:59:44 +00:00
|
|
|
case retrievalmarket.DealStatusCancelled:
|
|
|
|
return nil, xerrors.Errorf("Retrieval Proposal Cancelled: %s", evt.Message)
|
2021-11-15 12:53:28 +00:00
|
|
|
case
|
|
|
|
retrievalmarket.DealStatusDealNotFound,
|
|
|
|
retrievalmarket.DealStatusErrored:
|
|
|
|
return nil, xerrors.Errorf("Retrieval Error: %s", evt.Message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eref = &lapi.ExportRef{
|
|
|
|
Root: file,
|
|
|
|
DealID: retrievalRes.DealID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return eref, nil
|
|
|
|
}
|
|
|
|
|
2021-11-16 11:40:25 +00:00
|
|
|
var retrFlagsCommon = []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "from",
|
|
|
|
Usage: "address to send transactions from",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2021-11-23 21:23:13 +00:00
|
|
|
Name: "provider",
|
|
|
|
Usage: "provider to use for retrieval, if not present it'll use local discovery",
|
|
|
|
Aliases: []string{"miner"},
|
2021-11-16 11:40:25 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "maxPrice",
|
|
|
|
Usage: fmt.Sprintf("maximum price the client is willing to consider (default: %s FIL)", DefaultMaxRetrievePrice),
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "pieceCid",
|
|
|
|
Usage: "require data to be retrieved from a specific Piece CID",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "allow-local",
|
|
|
|
// todo: default to true?
|
2021-11-15 12:53:28 +00:00
|
|
|
},
|
2021-11-16 11:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var clientRetrieveCmd = &cli.Command{
|
|
|
|
Name: "retrieve",
|
2021-11-15 12:53:28 +00:00
|
|
|
Usage: "Retrieve data from network",
|
|
|
|
ArgsUsage: "[dataCid outputPath]",
|
2021-11-23 21:23:13 +00:00
|
|
|
Description: `Retrieve data from the Filecoin network.
|
|
|
|
|
|
|
|
The retrieve command will attempt to find a provider make a retrieval deal with
|
|
|
|
them. In case a provider can't be found, it can be specified with the --provider
|
|
|
|
flag.
|
|
|
|
|
|
|
|
By default the data will be interpreted as DAG-PB UnixFSv1 File. Alternatively
|
|
|
|
a CAR file containing the raw IPLD graph can be exported by setting the --car
|
|
|
|
flag.
|
|
|
|
|
|
|
|
Partial Retrieval:
|
|
|
|
|
|
|
|
The --data-selector flag can be used to specify a sub-graph to fetch. The
|
|
|
|
selector can be specified as either IPLD datamodel text-path selector, or IPLD
|
|
|
|
json selector.
|
|
|
|
|
|
|
|
In case of unixfs retrieval, the selector must point at a single root node, and
|
|
|
|
match the entire graph under that node.
|
|
|
|
|
|
|
|
In case of CAR retrieval, the selector must have one common "sub-root" node.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
- Retrieve a file by CID
|
|
|
|
$ lotus client retrieve Qm... my-file.txt
|
|
|
|
|
|
|
|
- Retrieve a file by CID from f0123
|
|
|
|
$ lotus client retrieve --provider f0123 Qm... my-file.txt
|
|
|
|
|
|
|
|
- Retrieve a first file from a specified directory
|
|
|
|
$ lotus client retrieve --data-selector /Links/0/Hash Qm... my-file.txt
|
|
|
|
`,
|
2021-11-16 11:40:25 +00:00
|
|
|
Flags: append([]cli.Flag{
|
2021-11-15 12:53:28 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "car",
|
2021-11-29 20:37:28 +00:00
|
|
|
Usage: "Export to a car file instead of a regular file",
|
2021-11-15 12:53:28 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2021-11-23 21:23:13 +00:00
|
|
|
Name: "data-selector",
|
2021-11-29 20:37:28 +00:00
|
|
|
Aliases: []string{"datamodel-path-selector"},
|
2021-11-23 21:23:13 +00:00
|
|
|
Usage: "IPLD datamodel text-path selector, or IPLD json selector",
|
2021-11-15 12:53:28 +00:00
|
|
|
},
|
2021-11-29 20:37:28 +00:00
|
|
|
&cli.BoolFlag{
|
2021-11-29 20:39:27 +00:00
|
|
|
Name: "car-export-merkle-proof",
|
2021-11-29 20:37:28 +00:00
|
|
|
Usage: "(requires --data-selector and --car) Export data-selector merkle proof",
|
|
|
|
},
|
2021-11-16 11:40:25 +00:00
|
|
|
}, retrFlagsCommon...),
|
2021-11-15 12:53:28 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.NArg() != 2 {
|
|
|
|
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
|
|
|
|
}
|
|
|
|
|
2021-11-29 20:39:18 +00:00
|
|
|
if cctx.Bool("car-export-merkle-proof") {
|
2021-11-29 20:41:38 +00:00
|
|
|
if !cctx.Bool("car") || !cctx.IsSet("data-selector") {
|
2021-11-29 20:39:18 +00:00
|
|
|
return ShowHelp(cctx, fmt.Errorf("--car-export-merkle-proof requires --car and --data-selector"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 12:53:28 +00:00
|
|
|
fapi, closer, err := GetFullNodeAPIV1(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
afmt := NewAppFmt(cctx.App)
|
|
|
|
|
|
|
|
var s *lapi.Selector
|
2021-11-23 21:23:13 +00:00
|
|
|
if sel := lapi.Selector(cctx.String("data-selector")); sel != "" {
|
2021-11-15 12:53:28 +00:00
|
|
|
s = &sel
|
|
|
|
}
|
|
|
|
|
|
|
|
eref, err := retrieve(ctx, cctx, fapi, s, afmt.Printf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s != nil {
|
2021-11-29 20:37:28 +00:00
|
|
|
eref.DAGs = append(eref.DAGs, lapi.DagSpec{DataSelector: s, ExportMerkleProof: cctx.Bool("car-export-merkle-proof")})
|
2021-11-15 12:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = fapi.ClientExport(ctx, *eref, lapi.FileRef{
|
|
|
|
Path: cctx.Args().Get(1),
|
|
|
|
IsCAR: cctx.Bool("car"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
afmt.Println("Success")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClientExportStream(apiAddr string, apiAuth http.Header, eref lapi.ExportRef, car bool) (io.ReadCloser, error) {
|
|
|
|
rj, err := json.Marshal(eref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("marshaling export ref: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-15 16:27:20 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-15 12:53:28 +00:00
|
|
|
aa, err := url.Parse(apiAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("parsing api address: %w", err)
|
|
|
|
}
|
|
|
|
switch aa.Scheme {
|
|
|
|
case "ws":
|
|
|
|
aa.Scheme = "http"
|
|
|
|
case "wss":
|
|
|
|
aa.Scheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
aa.Path = path.Join(aa.Path, "rest/v0/export")
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s?car=%t&export=%s", aa, car, url.QueryEscape(string(rj))), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header = apiAuth
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-15 16:27:20 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2021-11-17 18:52:31 +00:00
|
|
|
em, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("reading error body: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-15 16:27:20 +00:00
|
|
|
resp.Body.Close() // nolint
|
2021-11-17 18:52:31 +00:00
|
|
|
return nil, xerrors.Errorf("getting root car: http %d: %s", resp.StatusCode, string(em))
|
2021-11-15 16:27:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 12:53:28 +00:00
|
|
|
return resp.Body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var clientRetrieveCatCmd = &cli.Command{
|
|
|
|
Name: "cat",
|
|
|
|
Usage: "Show data from network",
|
|
|
|
ArgsUsage: "[dataCid]",
|
2021-11-17 19:24:12 +00:00
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "ipld",
|
|
|
|
Usage: "list IPLD datamodel links",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2021-11-23 21:23:13 +00:00
|
|
|
Name: "data-selector",
|
|
|
|
Usage: "IPLD datamodel text-path selector, or IPLD json selector",
|
2021-11-17 19:24:12 +00:00
|
|
|
},
|
|
|
|
}, retrFlagsCommon...),
|
2021-11-15 12:53:28 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.NArg() != 1 {
|
|
|
|
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ainfo, err := GetAPIInfo(cctx, repo.FullNode)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("could not get API info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fapi, closer, err := GetFullNodeAPIV1(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
afmt := NewAppFmt(cctx.App)
|
|
|
|
|
2021-11-23 21:23:13 +00:00
|
|
|
sel := lapi.Selector(cctx.String("data-selector"))
|
2021-11-17 19:24:12 +00:00
|
|
|
selp := &sel
|
|
|
|
if sel == "" {
|
|
|
|
selp = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
eref, err := retrieve(ctx, cctx, fapi, selp, afmt.Printf)
|
2021-11-15 12:53:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-17 19:24:12 +00:00
|
|
|
fmt.Println() // separate retrieval events from results
|
|
|
|
|
|
|
|
if sel != "" {
|
2021-11-15 12:53:28 +00:00
|
|
|
eref.DAGs = append(eref.DAGs, lapi.DagSpec{DataSelector: &sel})
|
|
|
|
}
|
|
|
|
|
|
|
|
rc, err := ClientExportStream(ainfo.Addr, ainfo.AuthHeader(), *eref, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rc.Close() // nolint
|
|
|
|
|
|
|
|
_, err = io.Copy(os.Stdout, rc)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-29 20:08:53 +00:00
|
|
|
func pathToSel(psel string, matchTraversal bool, sub builder.SelectorSpec) (lapi.Selector, error) {
|
|
|
|
rs, err := textselector.SelectorSpecFromPath(textselector.Expression(psel), matchTraversal, sub)
|
2021-11-17 18:52:31 +00:00
|
|
|
if err != nil {
|
2021-11-22 11:52:11 +00:00
|
|
|
return "", xerrors.Errorf("failed to parse path-selector: %w", err)
|
2021-11-17 18:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := dagjson.Encode(rs.Node(), &b); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lapi.Selector(b.String()), nil
|
|
|
|
}
|
|
|
|
|
2021-11-15 12:53:28 +00:00
|
|
|
var clientRetrieveLsCmd = &cli.Command{
|
|
|
|
Name: "ls",
|
2021-11-23 21:23:13 +00:00
|
|
|
Usage: "List object links",
|
2021-11-15 12:53:28 +00:00
|
|
|
ArgsUsage: "[dataCid]",
|
2021-11-17 16:39:27 +00:00
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "ipld",
|
2021-11-17 18:17:59 +00:00
|
|
|
Usage: "list IPLD datamodel links",
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "depth",
|
|
|
|
Usage: "list links recursively up to the specified depth",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2021-11-23 21:23:13 +00:00
|
|
|
Name: "data-selector",
|
|
|
|
Usage: "IPLD datamodel text-path selector, or IPLD json selector",
|
2021-11-17 16:39:27 +00:00
|
|
|
},
|
|
|
|
}, retrFlagsCommon...),
|
2021-11-15 12:53:28 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.NArg() != 1 {
|
|
|
|
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ainfo, err := GetAPIInfo(cctx, repo.FullNode)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("could not get API info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fapi, closer, err := GetFullNodeAPIV1(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
afmt := NewAppFmt(cctx.App)
|
|
|
|
|
2021-11-23 16:42:43 +00:00
|
|
|
dataSelector := lapi.Selector(fmt.Sprintf(`{"R":{"l":{"depth":%d},":>":{"a":{">":{"|":[{"@":{}},{".":{}}]}}}}}`, cctx.Int("depth")))
|
2021-11-17 18:52:31 +00:00
|
|
|
|
2021-11-23 21:23:13 +00:00
|
|
|
if cctx.IsSet("data-selector") {
|
2021-11-17 18:52:31 +00:00
|
|
|
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
|
2021-11-29 20:08:53 +00:00
|
|
|
dataSelector, err = pathToSel(cctx.String("data-selector"), cctx.Bool("ipld"),
|
2021-11-23 16:42:43 +00:00
|
|
|
ssb.ExploreUnion(
|
|
|
|
ssb.Matcher(),
|
|
|
|
ssb.ExploreAll(
|
|
|
|
ssb.ExploreRecursive(selector.RecursionLimitDepth(int64(cctx.Int("depth"))), ssb.ExploreAll(ssb.ExploreUnion(ssb.Matcher(), ssb.ExploreRecursiveEdge()))),
|
|
|
|
)))
|
2021-11-17 18:52:31 +00:00
|
|
|
if err != nil {
|
2021-11-23 16:42:43 +00:00
|
|
|
return xerrors.Errorf("parsing datamodel path: %w", err)
|
2021-11-17 18:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-15 12:53:28 +00:00
|
|
|
|
|
|
|
eref, err := retrieve(ctx, cctx, fapi, &dataSelector, afmt.Printf)
|
|
|
|
if err != nil {
|
2021-11-23 16:42:43 +00:00
|
|
|
return xerrors.Errorf("retrieve: %w", err)
|
2021-11-15 12:53:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 18:52:31 +00:00
|
|
|
fmt.Println() // separate retrieval events from results
|
|
|
|
|
2021-11-15 12:53:28 +00:00
|
|
|
eref.DAGs = append(eref.DAGs, lapi.DagSpec{
|
2021-11-17 18:17:59 +00:00
|
|
|
DataSelector: &dataSelector,
|
2021-11-15 12:53:28 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
rc, err := ClientExportStream(ainfo.Addr, ainfo.AuthHeader(), *eref, true)
|
|
|
|
if err != nil {
|
2021-11-23 16:42:43 +00:00
|
|
|
return xerrors.Errorf("export: %w", err)
|
2021-11-15 12:53:28 +00:00
|
|
|
}
|
|
|
|
defer rc.Close() // nolint
|
|
|
|
|
|
|
|
var memcar bytes.Buffer
|
|
|
|
_, err = io.Copy(&memcar, rc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-15 16:27:20 +00:00
|
|
|
cbs, err := blockstore.NewReadOnly(&bytesReaderAt{bytes.NewReader(memcar.Bytes())}, nil,
|
2021-11-15 12:53:28 +00:00
|
|
|
carv2.ZeroLengthSectionAsEOF(true),
|
|
|
|
blockstore.UseWholeCIDs(true))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening car blockstore: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
roots, err := cbs.Roots()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting roots: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(roots) != 1 {
|
2021-11-16 11:04:03 +00:00
|
|
|
return xerrors.Errorf("expected 1 car root, got %d", len(roots))
|
2021-11-15 12:53:28 +00:00
|
|
|
}
|
|
|
|
dserv := merkledag.NewDAGService(blockservice.New(cbs, offline.Exchange(cbs)))
|
|
|
|
|
2021-11-17 16:39:27 +00:00
|
|
|
if !cctx.Bool("ipld") {
|
|
|
|
links, err := dserv.GetLinks(ctx, roots[0])
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting links: %w", err)
|
|
|
|
}
|
2021-11-15 12:53:28 +00:00
|
|
|
|
2021-11-17 16:39:27 +00:00
|
|
|
for _, link := range links {
|
|
|
|
fmt.Printf("%s %s\t%d\n", link.Cid, link.Name, link.Size)
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-17 18:52:31 +00:00
|
|
|
jsel := lapi.Selector(fmt.Sprintf(`{"R":{"l":{"depth":%d},":>":{"a":{">":{"|":[{"@":{}},{".":{}}]}}}}}`, cctx.Int("depth")))
|
|
|
|
|
2021-11-23 21:23:13 +00:00
|
|
|
if cctx.IsSet("data-selector") {
|
2021-11-17 18:52:31 +00:00
|
|
|
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
|
2021-11-29 20:08:53 +00:00
|
|
|
jsel, err = pathToSel(cctx.String("data-selector"), false,
|
2021-11-17 18:52:31 +00:00
|
|
|
ssb.ExploreRecursive(selector.RecursionLimitDepth(int64(cctx.Int("depth"))), ssb.ExploreAll(ssb.ExploreUnion(ssb.Matcher(), ssb.ExploreRecursiveEdge()))),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
sel, _ := selectorparse.ParseJSONSelector(string(jsel))
|
2021-11-17 16:39:27 +00:00
|
|
|
|
|
|
|
if err := utils.TraverseDag(
|
|
|
|
ctx,
|
|
|
|
dserv,
|
|
|
|
roots[0],
|
|
|
|
sel,
|
|
|
|
func(p traversal.Progress, n ipld.Node, r traversal.VisitReason) error {
|
|
|
|
if r == traversal.VisitReason_SelectionMatch {
|
|
|
|
fmt.Println(p.Path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-15 12:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
2021-11-15 16:27:20 +00:00
|
|
|
|
|
|
|
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{}
|