2020-04-02 03:27:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-12-30 10:04:00 +00:00
|
|
|
"context"
|
2020-08-31 21:24:23 +00:00
|
|
|
"encoding/hex"
|
2020-04-02 03:27:06 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2020-08-31 21:24:23 +00:00
|
|
|
block "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
2020-05-05 01:31:56 +00:00
|
|
|
"github.com/ipld/go-car"
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-06-05 22:59:01 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-04-02 03:27:06 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var importCarCmd = &cli.Command{
|
|
|
|
Name: "import-car",
|
|
|
|
Description: "Import a car file into node chain blockstore",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
r, err := repo.NewFS(cctx.String("repo"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening fs repo: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-12-30 10:04:00 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
2020-04-02 03:27:06 +00:00
|
|
|
exists, err := r.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return xerrors.Errorf("lotus repo doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
lr, err := r.Lock(repo.FullNode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-27 20:53:20 +00:00
|
|
|
defer lr.Close() //nolint:errcheck
|
2020-04-02 03:27:06 +00:00
|
|
|
|
|
|
|
cf := cctx.Args().Get(0)
|
|
|
|
f, err := os.OpenFile(cf, os.O_RDONLY, 0664)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening the car file: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
bs, err := lr.Blockstore(ctx, repo.UniversalBlockstore)
|
2020-04-02 03:27:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-01 13:03:21 +00:00
|
|
|
defer func() {
|
|
|
|
if c, ok := bs.(io.Closer); ok {
|
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
log.Warnf("failed to close blockstore: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-04-02 03:27:06 +00:00
|
|
|
|
|
|
|
cr, err := car.NewCarReader(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
blk, err := cr.Next()
|
|
|
|
switch err {
|
|
|
|
case io.EOF:
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
2020-11-01 13:03:21 +00:00
|
|
|
return nil
|
2020-04-02 03:27:06 +00:00
|
|
|
default:
|
2020-07-23 10:21:13 +00:00
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-02 03:27:06 +00:00
|
|
|
fmt.Println()
|
|
|
|
return err
|
|
|
|
case nil:
|
|
|
|
fmt.Printf("\r%s", blk.Cid())
|
|
|
|
if err := bs.Put(blk); err != nil {
|
2020-07-23 10:21:13 +00:00
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-02 03:27:06 +00:00
|
|
|
return xerrors.Errorf("put %s: %w", blk.Cid(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-08-31 21:24:23 +00:00
|
|
|
|
|
|
|
var importObjectCmd = &cli.Command{
|
|
|
|
Name: "import-obj",
|
|
|
|
Usage: "import a raw ipld object into your datastore",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
r, err := repo.NewFS(cctx.String("repo"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening fs repo: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-12-30 10:04:00 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
2020-08-31 21:24:23 +00:00
|
|
|
exists, err := r.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return xerrors.Errorf("lotus repo doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
lr, err := r.Lock(repo.FullNode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lr.Close() //nolint:errcheck
|
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
bs, err := lr.Blockstore(ctx, repo.UniversalBlockstore)
|
2020-08-31 21:24:23 +00:00
|
|
|
if err != nil {
|
2020-11-01 13:03:21 +00:00
|
|
|
return fmt.Errorf("failed to open blockstore: %w", err)
|
2020-08-31 21:24:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 13:03:21 +00:00
|
|
|
defer func() {
|
|
|
|
if c, ok := bs.(io.Closer); ok {
|
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
log.Warnf("failed to close blockstore: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-08-31 21:24:23 +00:00
|
|
|
|
|
|
|
c, err := cid.Decode(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := hex.DecodeString(cctx.Args().Get(1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
blk, err := block.NewBlockWithCid(data, c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bs.Put(blk); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|