-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
import-car.go
83 lines (72 loc) · 1.55 KB
/
import-car.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"io"
"os"
"github.com/ipld/go-car"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/lib/blockstore"
"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)
}
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
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)
}
ds, err := lr.Datastore("/chain")
if err != nil {
return err
}
bs := blockstore.NewBlockstore(ds)
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()
return ds.Close()
default:
if err := f.Close(); err != nil {
return err
}
fmt.Println()
return err
case nil:
fmt.Printf("\r%s", blk.Cid())
if err := bs.Put(blk); err != nil {
if err := f.Close(); err != nil {
return err
}
return xerrors.Errorf("put %s: %w", blk.Cid(), err)
}
}
}
},
}