-
Notifications
You must be signed in to change notification settings - Fork 17
/
cmd-car-split.go
744 lines (625 loc) · 19.9 KB
/
cmd-car-split.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
package main
import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/binary"
"encoding/csv"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"sort"
"strconv"
"github.com/anjor/carlet"
commcid "github.com/filecoin-project/go-fil-commcid"
commp "github.com/filecoin-project/go-fil-commp-hashhash"
"github.com/filecoin-project/go-leb128"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car"
carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/fluent/qp"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/schema"
"github.com/multiformats/go-multicodec"
"github.com/rpcpool/yellowstone-faithful/accum"
"github.com/rpcpool/yellowstone-faithful/carreader"
"github.com/rpcpool/yellowstone-faithful/ipld/ipldbindcode"
"github.com/rpcpool/yellowstone-faithful/iplddecoders"
splitcarfetcher "github.com/rpcpool/yellowstone-faithful/split-car-fetcher"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
"k8s.io/klog/v2"
)
var (
CBOR_SHA256_DUMMY_CID = cid.MustParse("bafyreics5uul5lbtxslcigtoa5fkba7qgwu7cyb7ih7z6fzsh4lgfgraau")
hdr = &car.CarHeader{
Roots: []cid.Cid{CBOR_SHA256_DUMMY_CID}, // placeholder
Version: 1,
}
hdrSize, _ = car.HeaderSize(hdr)
maxSectionSize = 2 << 20 // 2 MiB
)
const (
maxLinks = 432000 / 18 // 18 subsets
bufSize = ((16 << 20) / 128 * 127)
)
type subsetInfo struct {
fileName string
firstSlot int
lastSlot int
blockLinks []datamodel.Link
}
type carFile struct {
name string
commP cid.Cid
payloadCid cid.Cid
paddedSize uint64
fileSize uint64
}
func newCmd_SplitCar() *cli.Command {
return &cli.Command{
Name: "split-car",
Description: "Splits an epoch car file into smaller chunks. Each chunk corresponds to a subset.",
ArgsUsage: "<epoch-car-path>",
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "size",
Aliases: []string{"s"},
Value: 4 * 1024 * 1024 * 1024, // 4 GiB
Usage: "Target size in bytes to chunk CARs to.",
Required: false,
},
&cli.IntFlag{
Name: "epoch",
Aliases: []string{"e"},
Usage: "Epoch number",
Required: true,
},
&cli.StringFlag{
Name: "metadata",
Aliases: []string{"m"},
Value: "metadata.csv",
Required: false,
Usage: "Filename for metadata. Defaults to metadata.csv",
},
&cli.StringFlag{
Name: "output-dir",
Aliases: []string{"o"},
Usage: "Output directory",
Required: false,
Value: ".",
},
},
Action: func(c *cli.Context) error {
carPath := c.Args().First()
var file fs.File
var err error
if carPath == "-" {
file = os.Stdin
} else {
file, err = os.Open(carPath)
if err != nil {
return fmt.Errorf("failed to open CAR: %w", err)
}
defer file.Close()
}
var (
currentFileSize uint64
currentFileNum int
currentFile *os.File
bufferedWriter *bufio.Writer
currentSubsetInfo subsetInfo
subsetLinks []datamodel.Link
carFiles []carFile
metadata *splitcarfetcher.Metadata
)
metadata = &splitcarfetcher.Metadata{}
headerBuf := new(bytes.Buffer)
teeReader := io.TeeReader(file, headerBuf)
streamBuf := bufio.NewReaderSize(teeReader, 1<<20)
actualHeader, headerSize, err := readHeader(streamBuf)
if err != nil {
return fmt.Errorf("failed to read header: %w", err)
}
encodedHeader := base64.StdEncoding.EncodeToString(actualHeader)
metadata.CarPieces = &carlet.CarPiecesAndMetadata{OriginalCarHeader: encodedHeader, OriginalCarHeaderSize: uint64(headerSize)}
combinedReader := io.MultiReader(headerBuf, file)
rd, err := carreader.New(io.NopCloser(combinedReader))
if err != nil {
return fmt.Errorf("failed to open CAR: %w", err)
}
{
// print roots:
roots := rd.Header.Roots
klog.Infof("Roots: %d", len(roots))
for i, root := range roots {
if i == 0 && len(roots) == 1 {
klog.Infof("- %s (Epoch CID)", root.String())
} else {
klog.Infof("- %s", root.String())
}
}
}
epoch := c.Int("epoch")
maxFileSize := uint64(c.Int64("size"))
outputDir := c.String("output-dir")
meta := c.String("metadata")
if outputDir == "" {
outputDir = "."
}
createNewFile := func() error {
if currentFile != nil {
sl, err := writeSubsetNode(currentSubsetInfo, bufferedWriter)
if err != nil {
return fmt.Errorf("failed to write subset node: %w", err)
}
subsetLinks = append(subsetLinks, sl)
cf := carFile{
name: fmt.Sprintf("epoch-%d-%d.car", epoch, currentFileNum),
payloadCid: sl.(cidlink.Link).Cid,
fileSize: currentFileSize,
}
carFiles = append(carFiles, cf)
metadata.CarPieces.CarPieces = append(
metadata.CarPieces.CarPieces,
carlet.CarFile{
Name: currentSubsetInfo.fileName,
ContentSize: currentFileSize - hdrSize,
HeaderSize: hdrSize,
})
err = closeFile(bufferedWriter, currentFile)
if err != nil {
return fmt.Errorf("failed to close file: %w", err)
}
err = carv2.ReplaceRootsInFile(filepath.Join(outputDir, cf.name), []cid.Cid{cf.payloadCid})
if err != nil {
return fmt.Errorf("failed to replace root: %w", err)
}
}
currentFileNum++
filename := filepath.Join(outputDir, fmt.Sprintf("epoch-%d-%d.car", epoch, currentFileNum))
currentFile, err = os.Create(filename)
if err != nil {
return fmt.Errorf("failed to create file %s: %w", filename, err)
}
bufferedWriter = bufio.NewWriter(currentFile)
if err := car.WriteHeader(hdr, bufferedWriter); err != nil {
return fmt.Errorf("failed to write header: %w", err)
}
// Set the currentFileSize to the size of the header
currentFileSize = hdrSize
currentSubsetInfo = subsetInfo{fileName: filename, firstSlot: -1, lastSlot: -1}
return nil
}
writeObject := func(data []byte) error {
_, err := bufferedWriter.Write(data)
if err != nil {
return fmt.Errorf("failed to write object to car file: %s, error: %w", currentFile.Name(), err)
}
currentFileSize += uint64(len(data))
return nil
}
writeBlockDag := func(blockDag []accum.ObjectWithMetadata) error {
for _, owm := range blockDag {
rs, err := owm.RawSection()
if err != nil {
return fmt.Errorf("failed to get raw section: %w", err)
}
err = writeObject(rs)
if err != nil {
return fmt.Errorf("failed to write object: %w", err)
}
}
return nil
}
accum := accum.NewObjectAccumulator(
rd,
iplddecoders.KindBlock,
func(parent *accum.ObjectWithMetadata, children []accum.ObjectWithMetadata) error {
if parent == nil {
return nil
}
family := append(children, *parent)
dagSize := 0
for _, member := range family {
dagSize += member.RawSectionSize()
}
if currentFile == nil || currentFileSize+uint64(dagSize) > maxFileSize || len(currentSubsetInfo.blockLinks) > maxLinks {
klog.Infof("Creating new file, currentFileSize: %d, dagSize: %d, maxFileSize: %d, maxLinks: %d, currentSubsetInfo.blockLinks: %d", currentFileSize, dagSize, maxFileSize, maxLinks, len(currentSubsetInfo.blockLinks))
err := createNewFile()
if err != nil {
return fmt.Errorf("failed to create a new file: %w", err)
}
}
// owm1 is necessarily a Block
block, err := iplddecoders.DecodeBlock(parent.ObjectData)
if err != nil {
return fmt.Errorf("failed to decode block: %w", err)
}
if currentSubsetInfo.firstSlot == -1 || block.Slot < currentSubsetInfo.firstSlot {
currentSubsetInfo.firstSlot = block.Slot
}
if block.Slot > currentSubsetInfo.lastSlot {
currentSubsetInfo.lastSlot = block.Slot
}
currentSubsetInfo.blockLinks = append(currentSubsetInfo.blockLinks, cidlink.Link{Cid: parent.Cid})
err = writeBlockDag(family)
if err != nil {
return fmt.Errorf("failed to write block dag to file: %w", err)
}
return nil
},
iplddecoders.KindEpoch,
iplddecoders.KindSubset,
)
if err := accum.Run((context.Background())); err != nil {
return fmt.Errorf("failed to run accumulator while accumulating objects: %w", err)
}
sl, err := writeSubsetNode(currentSubsetInfo, bufferedWriter)
if err != nil {
return fmt.Errorf("failed to write subset node: %w", err)
}
subsetLinks = append(subsetLinks, sl)
epochNode, err := qp.BuildMap(ipldbindcode.Prototypes.Epoch, -1, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "kind", qp.Int(int64(iplddecoders.KindEpoch)))
qp.MapEntry(ma, "epoch", qp.Int(int64(epoch)))
qp.MapEntry(ma, "subsets",
qp.List(-1, func(la datamodel.ListAssembler) {
for _, sl := range subsetLinks {
qp.ListEntry(la, qp.Link(sl))
}
}),
)
})
if err != nil {
return fmt.Errorf("failed to construct epochNode: %w", err)
}
_, err = writeNode(epochNode, bufferedWriter)
if err != nil {
return fmt.Errorf("failed to write epochNode: %w", err)
}
cf := carFile{
name: fmt.Sprintf("epoch-%d-%d.car", epoch, currentFileNum),
payloadCid: sl.(cidlink.Link).Cid,
fileSize: currentFileSize,
}
carFiles = append(carFiles, cf)
metadata.CarPieces.CarPieces = append(
metadata.CarPieces.CarPieces,
carlet.CarFile{
Name: currentSubsetInfo.fileName,
ContentSize: currentFileSize - hdrSize,
HeaderSize: hdrSize,
})
err = closeFile(bufferedWriter, currentFile)
if err != nil {
return fmt.Errorf("failed to close file: %w", err)
}
err = carv2.ReplaceRootsInFile(filepath.Join(outputDir, cf.name), []cid.Cid{cf.payloadCid})
if err != nil {
return fmt.Errorf("failed to replace root: %w", err)
}
f, err := os.Create(meta)
defer f.Close()
if err != nil {
return err
}
w := csv.NewWriter(f)
err = w.Write([]string{"car file", "piece cid", "payload cid", "padded piece size", "file size"})
if err != nil {
return err
}
defer w.Flush()
for idx, c := range carFiles {
commP, paddedPieceSize, err := calcCommP(filepath.Join(outputDir, c.name))
if err != nil {
return fmt.Errorf("failed to calculate commP: %w", err)
}
err = w.Write([]string{
c.name,
commP.String(),
c.payloadCid.String(),
strconv.FormatUint(paddedPieceSize, 10),
strconv.FormatUint(c.fileSize, 10),
})
if err != nil {
return fmt.Errorf("failed to write metatadata csv: %w", err)
}
// it is assumed that metadata and car files are in the same order
metadata.CarPieces.CarPieces[idx].CommP = commP
metadata.CarPieces.CarPieces[idx].PaddedSize = paddedPieceSize
}
err = writeMetadata(metadata, epoch)
if err != nil {
return fmt.Errorf("failed to write metatadata yaml: %w", err)
}
return nil
},
}
}
func writeSubsetNode(currentSubsetInfo subsetInfo, writer io.Writer) (datamodel.Link, error) {
subsetNode, err := qp.BuildMap(ipldbindcode.Prototypes.Subset, -1, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "kind", qp.Int(int64(iplddecoders.KindSubset)))
qp.MapEntry(ma, "first", qp.Int(int64(currentSubsetInfo.firstSlot)))
qp.MapEntry(ma, "last", qp.Int(int64(currentSubsetInfo.lastSlot)))
qp.MapEntry(ma, "blocks",
qp.List(-1, func(la datamodel.ListAssembler) {
for _, bl := range currentSubsetInfo.blockLinks {
qp.ListEntry(la, qp.Link(bl))
}
}))
})
if err != nil {
return nil, fmt.Errorf("failed to write a subsetNode: %w", err)
}
cid, err := writeNode(subsetNode, writer)
if err != nil {
return nil, fmt.Errorf("failed to write a subsetNode: %w", err)
}
return cidlink.Link{Cid: cid}, nil
}
func closeFile(bufferedWriter *bufio.Writer, currentFile *os.File) error {
err := bufferedWriter.Flush()
if err != nil {
return fmt.Errorf("failed to flush buffer: %w", err)
}
err = currentFile.Close()
if err != nil {
return fmt.Errorf("failed to close file: %w", err)
}
return nil
}
func writeNode(node datamodel.Node, w io.Writer) (cid.Cid, error) {
node = node.(schema.TypedNode).Representation()
var buf bytes.Buffer
err := dagcbor.Encode(node, &buf)
if err != nil {
return cid.Cid{}, err
}
data := buf.Bytes()
bd := cid.V1Builder{MhLength: -1, MhType: uint64(multicodec.Sha2_256), Codec: uint64(multicodec.DagCbor)}
cd, err := bd.Sum(data)
if err != nil {
return cid.Cid{}, err
}
c := cd.Bytes()
sizeVi := leb128.FromUInt64(uint64(len(c)) + uint64(len(data)))
if _, err := w.Write(sizeVi); err == nil {
if _, err := w.Write(c); err == nil {
if _, err := w.Write(data); err != nil {
return cid.Cid{}, err
}
}
}
return cd, nil
}
func writeMetadata(metadata *splitcarfetcher.Metadata, epoch int) error {
metadataFileName := fmt.Sprintf("epoch-%d-metadata.yaml", epoch)
// Open file in append mode
metadataFile, err := os.OpenFile(metadataFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("failed to open metadata file: %w", err)
}
defer metadataFile.Close()
encoder := yaml.NewEncoder(metadataFile)
err = encoder.Encode(metadata)
if err != nil {
return fmt.Errorf("failed to encode metadata: %w", err)
}
return nil
}
func readHeader(streamBuf *bufio.Reader) ([]byte, int64, error) {
maybeHeaderLen, err := streamBuf.Peek(varintSize)
if err != nil {
return nil, 0, fmt.Errorf("failed to read header: %s", err)
}
hdrLen, viLen := binary.Uvarint(maybeHeaderLen)
if hdrLen <= 0 || viLen < 0 {
return nil, 0, fmt.Errorf("unexpected header len = %d, varint len = %d", hdrLen, viLen)
}
actualViLen, err := io.CopyN(io.Discard, streamBuf, int64(viLen))
if err != nil {
return nil, 0, fmt.Errorf("failed to discard header varint: %s", err)
}
streamLen := actualViLen
headerBuf := new(bytes.Buffer)
actualHdrLen, err := io.CopyN(headerBuf, streamBuf, int64(hdrLen))
if err != nil {
return nil, 0, fmt.Errorf("failed to read header: %s", err)
}
streamLen += actualHdrLen
return headerBuf.Bytes(), streamLen, nil
}
type carFileInfo struct {
name string
firstSlot int64
size int64
}
func SortCarFiles(carFiles []string) ([]carFileInfo, error) {
var fileInfos []carFileInfo
for _, path := range carFiles {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open CAR file %s: %w", path, err)
}
defer file.Close()
// Create a new CarReader
cr, err := carreader.New(file)
if err != nil {
return nil, fmt.Errorf("failed to create CarReader for %s: %w", path, err)
}
// Get the root CID
if len(cr.Header.Roots) != 1 {
return nil, fmt.Errorf("expected 1 root CID, got %d in file %s", len(cr.Header.Roots), path)
}
rootCid := cr.Header.Roots[0]
// Read nodes until we find the one matching the root CID
var subset *ipldbindcode.Subset
for {
c, _, blockData, err := cr.NextNodeBytes()
if err != nil {
if err == io.EOF {
return nil, fmt.Errorf("reached end of file without finding root node in %s", path)
}
return nil, fmt.Errorf("failed to read node in file %s: %w", path, err)
}
if c == rootCid {
// Parse the block as a Subset object
subset, err = iplddecoders.DecodeSubset(blockData)
if err != nil {
return nil, fmt.Errorf("failed to decode Subset from block in file %s: %w", path, err)
}
break
}
}
if subset == nil {
return nil, fmt.Errorf("failed to find root node in file %s", path)
}
fi, err := file.Stat()
if err != nil {
return nil, fmt.Errorf("failed to get file info for %s: %w", path, err)
}
fileInfos = append(fileInfos, carFileInfo{
name: path,
firstSlot: int64(subset.First),
size: fi.Size(),
})
}
// Sort the file infos based on the firstSlot
sort.Slice(fileInfos, func(i, j int) bool {
return fileInfos[i].firstSlot < fileInfos[j].firstSlot
})
return fileInfos, nil
}
func SortCarURLs(carURLs []string) ([]carFileInfo, error) {
var urlInfos []carFileInfo
for _, url := range carURLs {
firstSlot, size, err := getSlotAndSizeFromURL(url)
if err != nil {
return nil, fmt.Errorf("failed to get first slot from URL %s: %w", url, err)
}
urlInfos = append(urlInfos, carFileInfo{
name: url,
firstSlot: firstSlot,
size: size,
})
}
// Sort the URL infos based on the firstSlot
sort.Slice(urlInfos, func(i, j int) bool {
return urlInfos[i].firstSlot < urlInfos[j].firstSlot
})
return urlInfos, nil
}
func getSlotAndSizeFromURL(url string) (int64, int64, error) {
fileSize, err := splitcarfetcher.GetContentSizeWithHeadOrZeroRange(url)
if err != nil {
return 0, 0, fmt.Errorf("failed to get file size: %w", err)
}
rootCID, err := getRootCid(url)
if err != nil {
return 0, 0, fmt.Errorf("failed to get root CID: %w", err)
}
endOffset := getEndOffset(fileSize)
partialContent, err := fetchFromOffset(url, endOffset)
if err != nil {
return 0, 0, fmt.Errorf("failed to fetch partial content: %w", err)
}
cidBytes := rootCID.Bytes()
index := bytes.LastIndex(partialContent, cidBytes)
if index == -1 {
return 0, 0, fmt.Errorf("CID block not found in the last 2MiB of the file")
}
blockData := partialContent[index-2:]
r := bufio.NewReader(bytes.NewBuffer(blockData))
cid, _, data, err := carreader.ReadNodeInfoWithData(r)
if err != nil {
return 0, 0, fmt.Errorf("failed to read node info: %w", err)
}
if cid != rootCID {
return 0, 0, fmt.Errorf("expected CID %s, got %s", rootCID, cid)
}
// Decode the Subset
subset, err := iplddecoders.DecodeSubset(data)
if err != nil {
return 0, 0, fmt.Errorf("failed to decode Subset from block: %w", err)
}
return int64(subset.First), fileSize, nil
}
func getRootCid(url string) (cid.Cid, error) {
// Make a GET request for the beginning of the file
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return cid.Undef, fmt.Errorf("failed to create request: %w", err)
}
// Request only the first hdrSize bytes
req.Header.Set("Range", fmt.Sprintf("bytes=0-%d", hdrSize))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return cid.Undef, fmt.Errorf("failed to fetch CAR file header: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusPartialContent {
return cid.Undef, fmt.Errorf("server does not support range requests")
}
// Read the header content
headerContent, err := io.ReadAll(resp.Body)
if err != nil {
return cid.Undef, fmt.Errorf("failed to read header content: %w", err)
}
// Parse the CAR header
rc := io.NopCloser(bytes.NewReader(headerContent))
cr, err := carreader.New(rc)
if err != nil {
return cid.Undef, fmt.Errorf("failed to create CarReader: %w", err)
}
roots := cr.Header.Roots
if len(roots) != 1 {
return cid.Undef, fmt.Errorf("expected 1 root CID, got %d", len(roots))
}
rootCID := roots[0]
return rootCID, nil
}
func getEndOffset(fileSize int64) int64 {
eo := fileSize - int64(maxSectionSize)
return max(eo, 0)
}
func fetchFromOffset(url string, offset int64) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", offset))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch CAR file: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusPartialContent {
return nil, fmt.Errorf("server does not support range requests")
}
return io.ReadAll(resp.Body)
}
func calcCommP(fileName string) (cid.Cid, uint64, error) {
r, err := os.Open(fileName)
if err != nil {
return cid.Undef, 0, fmt.Errorf("failed to open file: %w", err)
}
cp := new(commp.Calc)
streamBuf := bufio.NewReaderSize(io.TeeReader(r, cp), bufSize)
_, err = io.Copy(io.Discard, streamBuf)
if err != nil {
return cid.Undef, 0, fmt.Errorf("failed to copy stream: %w", err)
}
rawCommP, paddedSize, err := cp.Digest()
if err != nil {
return cid.Undef, 0, fmt.Errorf("failed to calculate CommP: %w", err)
}
commCid, err := commcid.DataCommitmentV1ToCID(rawCommP)
return commCid, paddedSize, err
}