2021-01-28 02:35:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-blockservice"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
offline "github.com/ipfs/go-ipfs-exchange-offline"
|
|
|
|
format "github.com/ipfs/go-ipld-format"
|
|
|
|
"github.com/ipfs/go-merkledag"
|
|
|
|
"github.com/ipld/go-car"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2022-11-04 11:28:10 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2021-01-28 02:35:28 +00:00
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func carWalkFunc(nd format.Node) (out []*format.Link, err error) {
|
|
|
|
for _, link := range nd.Links() {
|
|
|
|
if link.Cid.Prefix().Codec == cid.FilCommitmentSealed || link.Cid.Prefix().Codec == cid.FilCommitmentUnsealed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out = append(out, link)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var exportCarCmd = &cli.Command{
|
|
|
|
Name: "export-car",
|
2022-11-04 11:28:10 +00:00
|
|
|
Description: "Export a car from repo",
|
2021-01-28 02:35:28 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
2023-01-27 16:20:01 +00:00
|
|
|
Name: "repo",
|
|
|
|
Value: "~/.lotus",
|
|
|
|
EnvVars: []string{"LOTUS_PATH"},
|
2021-01-28 02:35:28 +00:00
|
|
|
},
|
|
|
|
},
|
2022-11-04 11:28:10 +00:00
|
|
|
ArgsUsage: "[outfile] [root cid]",
|
2021-01-28 02:35:28 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 2 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return lcli.IncorrectNumArgs(cctx)
|
2021-01-28 02:35:28 +00:00
|
|
|
}
|
|
|
|
outfile := cctx.Args().First()
|
|
|
|
var roots []cid.Cid
|
|
|
|
for _, arg := range cctx.Args().Tail() {
|
|
|
|
c, err := cid.Decode(arg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
roots = append(roots, c)
|
|
|
|
}
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
r, err := repo.NewFS(cctx.String("repo"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening fs repo: %w", err)
|
|
|
|
}
|
|
|
|
exists, err := r.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return xerrors.Errorf("lotus repo doesn't exist")
|
|
|
|
}
|
|
|
|
|
2022-11-04 11:28:10 +00:00
|
|
|
var bs blockstore.Blockstore
|
|
|
|
|
2021-01-28 02:35:28 +00:00
|
|
|
lr, err := r.Lock(repo.FullNode)
|
2022-11-04 11:28:10 +00:00
|
|
|
if err == nil {
|
|
|
|
bs, err = lr.Blockstore(ctx, repo.UniversalBlockstore)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open blockstore: %w", err)
|
|
|
|
}
|
|
|
|
defer lr.Close() //nolint:errcheck
|
|
|
|
} else {
|
|
|
|
api, closer, err := lcli.GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
bs = blockstore.NewAPIBlockstore(api)
|
2021-01-28 02:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Create(outfile)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening the output file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fi.Close() //nolint:errcheck
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if c, ok := bs.(io.Closer); ok {
|
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
log.Warnf("failed to close blockstore: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
dag := merkledag.NewDAGService(blockservice.New(bs, offline.Exchange(bs)))
|
|
|
|
err = car.WriteCarWithWalker(ctx, dag, roots, fi, carWalkFunc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-04 11:28:10 +00:00
|
|
|
|
|
|
|
sz, err := fi.Seek(0, io.SeekEnd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("done %s\n", types.SizeStr(types.NewInt(uint64(sz))))
|
|
|
|
|
2021-01-28 02:35:28 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|