-
Notifications
You must be signed in to change notification settings - Fork 86
/
create.go
142 lines (126 loc) · 3.4 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
142
package car
import (
"bytes"
"context"
"fmt"
"io"
"os"
"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"
)
// copied from https://github.com/ipld/go-car/blob/master/cmd/car/create.go
func CreateCar(
ctx context.Context,
inputDirectory string,
outputFile string,
// this can be 1 or 2
version int,
) (string, error) {
// 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)
var options []car.Option
switch version {
case 1:
options = []car.Option{blockstore.WriteAsCarV1(true)}
case 2:
// already the default
default:
return "", fmt.Errorf("invalid CAR version %d", version)
}
cdest, err := blockstore.OpenReadWrite(outputFile, []cid.Cid{proxyRoot}, options...)
if err != nil {
return "", err
}
// Write the unixfs blocks into the store.
files, err := os.ReadDir(inputDirectory)
if err != nil {
return "", err
}
var carFilePaths []string
for _, file := range files {
carFilePaths = append(carFilePaths, fmt.Sprintf("%s/%s", inputDirectory, file.Name()))
}
root, err := writeFiles(ctx, cdest, carFilePaths...)
if err != nil {
return "", err
}
if err := cdest.Finalize(); err != nil {
return "", err
}
// re-open/finalize with the final root.
if err := car.ReplaceRootsInFile(outputFile, []cid.Cid{root}); err != nil {
return "", err
}
return root.String(), nil
}
func writeFiles(ctx context.Context, 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) //nolint:errcheck
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
}
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
}