Skip to content

Commit

Permalink
feat: shed: Online export-car
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Nov 4, 2022
1 parent f5ec47a commit 2c5087e
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions cmd/lotus-shed/export-car.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"fmt"
"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/types"
"io"
"os"

Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
},
}

0 comments on commit 2c5087e

Please sign in to comment.