-
Notifications
You must be signed in to change notification settings - Fork 44
/
create.go
141 lines (126 loc) · 3.5 KB
/
create.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
package main
import (
"bytes"
"context"
"fmt"
"io"
"path"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-unixfsnode/data/builder"
"github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/blockstore"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-multihash"
"github.com/urfave/cli/v2"
)
// CreateCar creates a car
func CreateCar(c *cli.Context) error {
var err error
if c.Args().Len() == 0 {
return fmt.Errorf("a source location to build the car from must be specified")
}
if !c.IsSet("file") {
return fmt.Errorf("a file destination must be specified")
}
if c.Bool("no-wrap") && c.Args().Len() > 1 {
return fmt.Errorf("no-wrap cannot be set with multiple source locations")
}
// make a cid with the right length that we eventually will patch with the root.
hasher, err := multihash.GetHasher(multihash.SHA2_256)
if err != nil {
return err
}
digest := hasher.Sum([]byte{})
hash, err := multihash.Encode(digest, multihash.SHA2_256)
if err != nil {
return err
}
proxyRoot := cid.NewCidV1(uint64(multicodec.DagPb), hash)
options := []car.Option{}
switch c.Int("version") {
case 1:
options = []car.Option{blockstore.WriteAsCarV1(true)}
case 2:
// already the default
default:
return fmt.Errorf("invalid CAR version %d", c.Int("version"))
}
cdest, err := blockstore.OpenReadWrite(c.String("file"), []cid.Cid{proxyRoot}, options...)
if err != nil {
return err
}
// Write the unixfs blocks into the store.
root, err := writeFiles(c.Context, c.Bool("no-wrap"), cdest, c.Args().Slice()...)
if err != nil {
return err
}
if err := cdest.Finalize(); err != nil {
return err
}
// re-open/finalize with the final root.
return car.ReplaceRootsInFile(c.String("file"), []cid.Cid{root})
}
func writeFiles(ctx context.Context, noWrap bool, bs *blockstore.ReadWrite, paths ...string) (cid.Cid, error) {
ls := cidlink.DefaultLinkSystem()
ls.TrustedStorage = true
ls.StorageReadOpener = func(_ ipld.LinkContext, l ipld.Link) (io.Reader, error) {
cl, ok := l.(cidlink.Link)
if !ok {
return nil, fmt.Errorf("not a cidlink")
}
blk, err := bs.Get(ctx, cl.Cid)
if err != nil {
return nil, err
}
return bytes.NewBuffer(blk.RawData()), nil
}
ls.StorageWriteOpener = func(_ ipld.LinkContext) (io.Writer, ipld.BlockWriteCommitter, error) {
buf := bytes.NewBuffer(nil)
return buf, func(l ipld.Link) error {
cl, ok := l.(cidlink.Link)
if !ok {
return fmt.Errorf("not a cidlink")
}
blk, err := blocks.NewBlockWithCid(buf.Bytes(), cl.Cid)
if err != nil {
return err
}
bs.Put(ctx, blk)
return nil
}, nil
}
topLevel := make([]dagpb.PBLink, 0, len(paths))
for _, p := range paths {
l, size, err := builder.BuildUnixFSRecursive(p, &ls)
if err != nil {
return cid.Undef, err
}
if noWrap {
rcl, ok := l.(cidlink.Link)
if !ok {
return cid.Undef, fmt.Errorf("could not interpret %s", l)
}
return rcl.Cid, nil
}
name := path.Base(p)
entry, err := builder.BuildUnixFSDirectoryEntry(name, int64(size), l)
if err != nil {
return cid.Undef, err
}
topLevel = append(topLevel, entry)
}
// make a directory for the file(s).
root, _, err := builder.BuildUnixFSDirectory(topLevel, &ls)
if err != nil {
return cid.Undef, nil
}
rcl, ok := root.(cidlink.Link)
if !ok {
return cid.Undef, fmt.Errorf("could not interpret %s", root)
}
return rcl.Cid, nil
}