Merge pull request #9590 from filecoin-project/feat/online-exportcar

feat: shed: Online export-car
This commit is contained in:
Łukasz Magiera 2022-11-06 10:53:39 +00:00 committed by GitHub
commit 2d664c49b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,6 +14,8 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/node/repo" "github.com/filecoin-project/lotus/node/repo"
) )
@ -30,18 +32,18 @@ func carWalkFunc(nd format.Node) (out []*format.Link, err error) {
var exportCarCmd = &cli.Command{ var exportCarCmd = &cli.Command{
Name: "export-car", Name: "export-car",
Description: "Export a car from repo (requires node to be offline)", Description: "Export a car from repo",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "repo", Name: "repo",
Value: "~/.lotus", Value: "~/.lotus",
}, },
}, },
ArgsUsage: "[outfile] [root cid]",
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
if cctx.NArg() != 2 { if cctx.NArg() != 2 {
return lcli.IncorrectNumArgs(cctx) return lcli.IncorrectNumArgs(cctx)
} }
outfile := cctx.Args().First() outfile := cctx.Args().First()
var roots []cid.Cid var roots []cid.Cid
for _, arg := range cctx.Args().Tail() { for _, arg := range cctx.Args().Tail() {
@ -51,14 +53,11 @@ var exportCarCmd = &cli.Command{
} }
roots = append(roots, c) roots = append(roots, c)
} }
ctx := lcli.ReqContext(cctx) ctx := lcli.ReqContext(cctx)
r, err := repo.NewFS(cctx.String("repo")) r, err := repo.NewFS(cctx.String("repo"))
if err != nil { if err != nil {
return xerrors.Errorf("opening fs repo: %w", err) return xerrors.Errorf("opening fs repo: %w", err)
} }
exists, err := r.Exists() exists, err := r.Exists()
if err != nil { if err != nil {
return err return err
@ -67,11 +66,25 @@ var exportCarCmd = &cli.Command{
return xerrors.Errorf("lotus repo doesn't exist") return xerrors.Errorf("lotus repo doesn't exist")
} }
var bs blockstore.Blockstore
lr, err := r.Lock(repo.FullNode) lr, err := r.Lock(repo.FullNode)
if err != nil { if err == nil {
return err 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)
} }
defer lr.Close() //nolint:errcheck
fi, err := os.Create(outfile) fi, err := os.Create(outfile)
if err != nil { if err != nil {
@ -80,11 +93,6 @@ var exportCarCmd = &cli.Command{
defer fi.Close() //nolint:errcheck defer fi.Close() //nolint:errcheck
bs, err := lr.Blockstore(ctx, repo.UniversalBlockstore)
if err != nil {
return fmt.Errorf("failed to open blockstore: %w", err)
}
defer func() { defer func() {
if c, ok := bs.(io.Closer); ok { if c, ok := bs.(io.Closer); ok {
if err := c.Close(); err != nil { if err := c.Close(); err != nil {
@ -98,6 +106,14 @@ var exportCarCmd = &cli.Command{
if err != nil { if err != nil {
return err return err
} }
sz, err := fi.Seek(0, io.SeekEnd)
if err != nil {
return err
}
fmt.Printf("done %s\n", types.SizeStr(types.NewInt(uint64(sz))))
return nil return nil
}, },
} }