From a0e3111b644f0285e5462931d2e311af9daea220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 4 Nov 2022 11:28:10 +0000 Subject: [PATCH] feat: shed: Online export-car --- cmd/lotus-shed/export-car.go | 42 +++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/cmd/lotus-shed/export-car.go b/cmd/lotus-shed/export-car.go index 214b1d5df..5cb4737ea 100644 --- a/cmd/lotus-shed/export-car.go +++ b/cmd/lotus-shed/export-car.go @@ -14,6 +14,8 @@ import ( "github.com/urfave/cli/v2" "golang.org/x/xerrors" + "github.com/filecoin-project/lotus/blockstore" + "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" "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{ Name: "export-car", - Description: "Export a car from repo (requires node to be offline)", + Description: "Export a car from repo", Flags: []cli.Flag{ &cli.StringFlag{ Name: "repo", Value: "~/.lotus", }, }, + ArgsUsage: "[outfile] [root cid]", Action: func(cctx *cli.Context) error { if cctx.NArg() != 2 { return lcli.IncorrectNumArgs(cctx) } - outfile := cctx.Args().First() var roots []cid.Cid for _, arg := range cctx.Args().Tail() { @@ -51,14 +53,11 @@ var exportCarCmd = &cli.Command{ } 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 @@ -67,11 +66,25 @@ var exportCarCmd = &cli.Command{ return xerrors.Errorf("lotus repo doesn't exist") } + var bs blockstore.Blockstore + lr, err := r.Lock(repo.FullNode) - if err != nil { - return err + 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) } - defer lr.Close() //nolint:errcheck fi, err := os.Create(outfile) if err != nil { @@ -80,11 +93,6 @@ var exportCarCmd = &cli.Command{ 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() { if c, ok := bs.(io.Closer); ok { if err := c.Close(); err != nil { @@ -98,6 +106,14 @@ var exportCarCmd = &cli.Command{ if err != nil { 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 }, }