-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathget.go
215 lines (187 loc) · 5.53 KB
/
get.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"bytes"
"context"
"fmt"
"io"
"os"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
_ "github.com/ipld/go-ipld-prime/codec/cbor"
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
_ "github.com/ipld/go-ipld-prime/codec/dagjson"
_ "github.com/ipld/go-ipld-prime/codec/json"
_ "github.com/ipld/go-ipld-prime/codec/raw"
"github.com/ipfs/go-cid"
ipldfmt "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-unixfsnode"
"github.com/ipld/go-car"
"github.com/ipld/go-car/v2/blockstore"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/traversal"
"github.com/ipld/go-ipld-prime/traversal/selector"
selectorParser "github.com/ipld/go-ipld-prime/traversal/selector/parse"
"github.com/urfave/cli/v2"
)
// GetCarBlock is a command to get a block out of a car
func GetCarBlock(c *cli.Context) error {
if c.Args().Len() < 2 {
return fmt.Errorf("usage: car get-block <file.car> <block cid> [output file]")
}
bs, err := blockstore.OpenReadOnly(c.Args().Get(0))
if err != nil {
return err
}
// string to CID
blkCid, err := cid.Parse(c.Args().Get(1))
if err != nil {
return err
}
blk, err := bs.Get(c.Context, blkCid)
if err != nil {
return err
}
outStream := os.Stdout
if c.Args().Len() >= 3 {
outStream, err = os.Create(c.Args().Get(2))
if err != nil {
return err
}
defer outStream.Close()
}
_, err = outStream.Write(blk.RawData())
return err
}
// GetCarDag is a command to get a dag out of a car
func GetCarDag(c *cli.Context) error {
if c.Args().Len() < 2 {
return fmt.Errorf("usage: car get-dag [-s selector] <file.car> [root cid] <output file>")
}
// if root cid is emitted we'll read it from the root of file.car.
output := c.Args().Get(1)
var rootCid cid.Cid
bs, err := blockstore.OpenReadOnly(c.Args().Get(0))
if err != nil {
return err
}
if c.Args().Len() == 2 {
roots, err := bs.Roots()
if err != nil {
return err
}
if len(roots) != 1 {
return fmt.Errorf("car file has does not have exactly one root, dag root must be specified explicitly")
}
rootCid = roots[0]
} else {
rootCid, err = cid.Parse(output)
if err != nil {
return err
}
output = c.Args().Get(2)
}
strict := c.Bool("strict")
// selector traversal, default to ExploreAllRecursively which only explores the DAG blocks
// because we only care about the blocks loaded during the walk, not the nodes matched
sel := selectorParser.CommonSelector_MatchAllRecursively
if c.IsSet("selector") {
sel, err = selectorParser.ParseJSONSelector(c.String("selector"))
if err != nil {
return err
}
}
linkVisitOnlyOnce := !c.IsSet("selector") // if using a custom selector, this isn't as safe
switch c.Int("version") {
case 2:
return writeCarV2(c.Context, rootCid, output, bs, strict, sel, linkVisitOnlyOnce)
case 1:
return writeCarV1(rootCid, output, bs, strict, sel, linkVisitOnlyOnce)
default:
return fmt.Errorf("invalid CAR version %d", c.Int("version"))
}
}
func writeCarV2(ctx context.Context, rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict bool, sel datamodel.Node, linkVisitOnlyOnce bool) error {
_ = os.Remove(output)
outStore, err := blockstore.OpenReadWrite(output, []cid.Cid{rootCid}, blockstore.AllowDuplicatePuts(false))
if err != nil {
return err
}
ls := cidlink.DefaultLinkSystem()
ls.KnownReifiers = map[string]linking.NodeReifier{"unixfs": unixfsnode.Reify}
ls.TrustedStorage = true
ls.StorageReadOpener = func(_ linking.LinkContext, l datamodel.Link) (io.Reader, error) {
if cl, ok := l.(cidlink.Link); ok {
blk, err := bs.Get(ctx, cl.Cid)
if err != nil {
if ipldfmt.IsNotFound(err) {
if strict {
return nil, err
}
return nil, traversal.SkipMe{}
}
return nil, err
}
if err := outStore.Put(ctx, blk); err != nil {
return nil, err
}
return bytes.NewBuffer(blk.RawData()), nil
}
return nil, fmt.Errorf("unknown link type: %T", l)
}
nsc := func(lnk datamodel.Link, lctx ipld.LinkContext) (datamodel.NodePrototype, error) {
if lnk, ok := lnk.(cidlink.Link); ok && lnk.Cid.Prefix().Codec == 0x70 {
return dagpb.Type.PBNode, nil
}
return basicnode.Prototype.Any, nil
}
rootLink := cidlink.Link{Cid: rootCid}
ns, _ := nsc(rootLink, ipld.LinkContext{})
rootNode, err := ls.Load(ipld.LinkContext{}, rootLink, ns)
if err != nil {
return err
}
traversalProgress := traversal.Progress{
Cfg: &traversal.Config{
LinkSystem: ls,
LinkTargetNodePrototypeChooser: nsc,
LinkVisitOnlyOnce: linkVisitOnlyOnce,
},
}
s, err := selector.CompileSelector(sel)
if err != nil {
return err
}
err = traversalProgress.WalkMatching(rootNode, s, func(p traversal.Progress, n datamodel.Node) error {
lb, ok := n.(datamodel.LargeBytesNode)
if ok {
rs, err := lb.AsLargeBytes()
if err == nil {
_, err := io.Copy(io.Discard, rs)
if err != nil {
return err
}
}
}
return nil
})
if err != nil {
return err
}
return outStore.Finalize()
}
func writeCarV1(rootCid cid.Cid, output string, bs *blockstore.ReadOnly, _ bool, sel datamodel.Node, linkVisitOnlyOnce bool) error {
opts := make([]car.Option, 0)
if linkVisitOnlyOnce {
opts = append(opts, car.TraverseLinksOnlyOnce())
}
sc := car.NewSelectiveCar(context.Background(), bs, []car.Dag{{Root: rootCid, Selector: sel}}, opts...)
f, err := os.Create(output)
if err != nil {
return err
}
defer f.Close()
return sc.Write(f)
}