/
decoder.go
738 lines (613 loc) · 21.7 KB
/
decoder.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
package par2
import (
"bytes"
"crypto/md5"
"encoding/binary"
"errors"
"hash/crc32"
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"github.com/akalin/gopar/rsec16"
)
type fileIO interface {
ReadFile(path string) ([]byte, error)
FindWithPrefixAndSuffix(prefix, suffix string) ([]string, error)
WriteFile(path string, data []byte) error
}
type defaultFileIO struct{}
func (io defaultFileIO) ReadFile(path string) ([]byte, error) {
return ioutil.ReadFile(path)
}
func (io defaultFileIO) FindWithPrefixAndSuffix(prefix, suffix string) ([]string, error) {
return filepath.Glob(prefix + "*" + suffix)
}
func (io defaultFileIO) WriteFile(path string, data []byte) error {
return ioutil.WriteFile(path, data, 0600)
}
type decoderInputFileInfo struct {
fileID fileID
filename string
byteCount int
sixteenKHash [md5.Size]byte
hash [md5.Size]byte
checksumPairs []checksumPair
}
func decoderInputFileInfoIDs(infos []decoderInputFileInfo) []fileID {
fileIDs := make([]fileID, len(infos))
for i, info := range infos {
fileIDs[i] = info.fileID
}
return fileIDs
}
func makeDecoderInputFileInfos(fileIDs []fileID, fileDescriptionPackets map[fileID]fileDescriptionPacket, ifscPackets map[fileID]ifscPacket) ([]decoderInputFileInfo, error) {
var decoderInputFileInfos []decoderInputFileInfo
for _, fileID := range fileIDs {
descriptionPacket, ok := fileDescriptionPackets[fileID]
if !ok {
return nil, errors.New("file description packet not found")
}
ifscPacket, ok := ifscPackets[fileID]
if !ok {
return nil, errors.New("input file slice checksum packet not found")
}
decoderInputFileInfos = append(decoderInputFileInfos, decoderInputFileInfo{
fileID,
descriptionPacket.filename,
descriptionPacket.byteCount,
descriptionPacket.sixteenKHash,
descriptionPacket.hash,
ifscPacket.checksumPairs,
})
}
return decoderInputFileInfos, nil
}
type shardLocation struct {
fileID fileID
start int
}
type shardLocationSet map[shardLocation]bool
type checksumShardLocationMap map[uint32]map[[md5.Size]byte]shardLocationSet
func (m checksumShardLocationMap) put(crc32 uint32, md5Hash [md5.Size]byte, location shardLocation) {
byCRC, ok := m[crc32]
if !ok {
m[crc32] = make(map[[md5.Size]byte]shardLocationSet)
byCRC = m[crc32]
}
byMD5, ok := byCRC[md5Hash]
if !ok {
byCRC[md5Hash] = make(shardLocationSet)
byMD5 = byCRC[md5Hash]
}
byMD5[location] = true
}
func (m checksumShardLocationMap) get(crc32 uint32, data []byte) shardLocationSet {
byCRC := m[crc32]
if len(byCRC) == 0 {
return nil
}
return byCRC[md5.Sum(data)]
}
func makeChecksumShardLocationMap(sliceByteCount int, infos []decoderInputFileInfo) checksumShardLocationMap {
m := make(checksumShardLocationMap)
for _, info := range infos {
for i, checksumPair := range info.checksumPairs {
// TODO: Handle overflow.
start := i * sliceByteCount
m.put(binary.LittleEndian.Uint32(checksumPair.CRC32[:]), checksumPair.MD5, shardLocation{info.fileID, start})
}
}
return m
}
type shardIntegrityInfo struct {
data []byte
locations shardLocationSet
}
func (info shardIntegrityInfo) ok(location shardLocation) bool {
return len(info.data) != 0 && info.locations[location]
}
type fileIntegrityInfo struct {
fileID fileID
missing bool
hashMismatch bool
hasWrongByteCount bool
shardInfos []shardIntegrityInfo
}
func (info fileIntegrityInfo) allShardsOK(sliceByteCount int) bool {
for i, shardInfo := range info.shardInfos {
// TODO: Handle overflow.
start := i * sliceByteCount
if !shardInfo.ok(shardLocation{info.fileID, start}) {
return false
}
}
return true
}
func (info fileIntegrityInfo) ok(sliceByteCount int) bool {
return !info.missing && !info.hashMismatch && !info.hasWrongByteCount && info.allShardsOK(sliceByteCount)
}
// A Decoder keeps track of all information needed to check the
// integrity of a set of data files, and possibly repair any
// missing/corrupt data files from the parity files (that usually end
// in .par2).
type Decoder struct {
fileIO fileIO
delegate DecoderDelegate
indexPath string
setID recoverySetID
clientID string
sliceByteCount int
recoverySet []decoderInputFileInfo
nonRecoverySet []decoderInputFileInfo
numGoroutines int
checksumToLocation checksumShardLocationMap
// Indexed the same as recoverySet.
fileIntegrityInfos []fileIntegrityInfo
parityShards [][]byte
}
// DecoderDelegate holds methods that are called during the decode
// process.
type DecoderDelegate interface {
OnCreatorPacketLoad(clientID string)
OnMainPacketLoad(sliceByteCount, recoverySetCount, nonRecoverySetCount int)
OnFileDescriptionPacketLoad(fileID [16]byte, filename string, byteCount int)
OnIFSCPacketLoad(fileID [16]byte)
OnRecoveryPacketLoad(exponent uint16, byteCount int)
OnUnknownPacketLoad(packetType [16]byte, byteCount int)
OnOtherPacketSkip(setID [16]byte, packetType [16]byte, byteCount int)
OnDataFileLoad(i, n int, path string, byteCount, hits, misses int, err error)
OnParityFileLoad(i int, path string, err error)
OnDetectCorruptDataChunk(fileID [16]byte, path string, startByteOffset, endByteOffset int)
OnDetectDataFileHashMismatch(fileID [16]byte, path string)
OnDetectDataFileWrongByteCount(fileID [16]byte, path string)
OnDataFileWrite(i, n int, path string, byteCount int, err error)
}
// DoNothingDecoderDelegate is an implementation of DecoderDelegate
// that does nothing for all methods.
type DoNothingDecoderDelegate struct{}
// OnCreatorPacketLoad implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnCreatorPacketLoad(clientID string) {}
// OnMainPacketLoad implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnMainPacketLoad(sliceByteCount, recoverySetCount, nonRecoverySetCount int) {
}
// OnFileDescriptionPacketLoad implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnFileDescriptionPacketLoad(fileID [16]byte, filename string, byteCount int) {
}
// OnIFSCPacketLoad implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnIFSCPacketLoad(fileID [16]byte) {}
// OnRecoveryPacketLoad implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnRecoveryPacketLoad(exponent uint16, byteCount int) {}
// OnUnknownPacketLoad implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnUnknownPacketLoad(packetType [16]byte, byteCount int) {}
// OnOtherPacketSkip implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnOtherPacketSkip(setID [16]byte, packetType [16]byte, byteCount int) {
}
// OnDataFileLoad implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnDataFileLoad(i, n int, path string, byteCount, hits, misses int, err error) {
}
// OnParityFileLoad implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnParityFileLoad(i int, path string, err error) {}
// OnDetectCorruptDataChunk implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnDetectCorruptDataChunk(fileID [16]byte, path string, startByteOffset, endByteOffset int) {
}
// OnDetectDataFileHashMismatch implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnDetectDataFileHashMismatch(fileID [16]byte, path string) {}
// OnDetectDataFileWrongByteCount implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnDetectDataFileWrongByteCount(fileID [16]byte, path string) {}
// OnDataFileWrite implements the DecoderDelegate interface.
func (DoNothingDecoderDelegate) OnDataFileWrite(i, n int, path string, byteCount int, err error) {}
func newDecoder(fileIO fileIO, delegate DecoderDelegate, indexPath string, numGoroutines int) (*Decoder, error) {
indexBytes, err := fileIO.ReadFile(indexPath)
if err != nil {
return nil, err
}
setID, indexFile, err := readFile(delegate, nil, indexBytes)
if err != nil {
return nil, err
}
if indexFile.mainPacket == nil {
// TODO: Relax this check.
return nil, errors.New("no main packet found")
}
if len(indexFile.recoveryPackets) > 0 {
// TODO: Relax this check.
return nil, errors.New("recovery packets found in index file")
}
recoverySet, err := makeDecoderInputFileInfos(indexFile.mainPacket.recoverySet, indexFile.fileDescriptionPackets, indexFile.ifscPackets)
if err != nil {
return nil, err
}
nonRecoverySet, err := makeDecoderInputFileInfos(indexFile.mainPacket.nonRecoverySet, indexFile.fileDescriptionPackets, indexFile.ifscPackets)
if err != nil {
return nil, err
}
return &Decoder{
fileIO, delegate,
indexPath,
setID,
indexFile.clientID, indexFile.mainPacket.sliceByteCount,
recoverySet, nonRecoverySet,
numGoroutines,
nil,
nil,
nil,
}, nil
}
func sixteenKHash(data []byte) [md5.Size]byte {
if len(data) < 16*1024 {
return md5.Sum(data)
}
return md5.Sum(data[:16*1024])
}
func sliceAndPadByteArray(bs []byte, start, end int) []byte {
padLength := 0
if end > len(bs) {
padLength = end - len(bs)
end = len(bs)
}
slice := bs[start:end]
if padLength > 0 {
slice = append(slice, make([]byte, padLength)...)
}
return slice
}
func fillShardInfos(sliceByteCount int, data []byte, checksumToLocation checksumShardLocationMap, fileID fileID, fileIntegrityInfos []fileIntegrityInfo, fileIDIndices map[fileID]int) (int, int) {
hits := 0
misses := 0
justMissed := false
window := newCRC32Window(sliceByteCount)
var crcSlice uint32
for j := 0; j < len(data); {
slice := sliceAndPadByteArray(data, j, j+sliceByteCount)
if justMissed {
crcSlice = window.update(crcSlice, data[j-1], slice[len(slice)-1])
} else {
crcSlice = crc32.ChecksumIEEE(slice)
}
foundLocations := checksumToLocation.get(crcSlice, slice)
if len(foundLocations) == 0 {
j++
misses++
justMissed = true
continue
}
location := shardLocation{fileID, j}
for foundLocation := range foundLocations {
integrityInfo := fileIntegrityInfos[fileIDIndices[foundLocation.fileID]]
shardInfo := &integrityInfo.shardInfos[foundLocation.start/sliceByteCount]
if shardInfo.data == nil {
*shardInfo = shardIntegrityInfo{
slice,
shardLocationSet{},
}
}
shardInfo.locations[location] = true
}
justMissed = false
j += sliceByteCount
hits++
}
return hits, misses
}
func (d *Decoder) getFilePath(info decoderInputFileInfo) string {
// TODO: Make this configurable.
basePath := filepath.Dir(d.indexPath)
return filepath.Join(basePath, info.filename)
}
func (d *Decoder) fillFileIntegrityInfos(checksumToLocation checksumShardLocationMap, fileIntegrityInfos []fileIntegrityInfo, fileIDIndices map[fileID]int, i int, info decoderInputFileInfo) (int, int, int, error) {
path := d.getFilePath(info)
data, err := d.fileIO.ReadFile(path)
if os.IsNotExist(err) {
fileIntegrityInfos[i].missing = true
return 0, 0, 0, nil
} else if err != nil {
return len(data), 0, 0, err
}
hits, misses := fillShardInfos(d.sliceByteCount, data, checksumToLocation, info.fileID, fileIntegrityInfos, fileIDIndices)
hashMismatch := sixteenKHash(data) != info.sixteenKHash || md5.Sum(data) != info.hash
fileIntegrityInfos[i].hashMismatch = hashMismatch
if hashMismatch {
d.delegate.OnDetectDataFileHashMismatch(info.fileID, path)
}
hasWrongByteCount := len(data) != info.byteCount
fileIntegrityInfos[i].hasWrongByteCount = hasWrongByteCount
if hasWrongByteCount {
d.delegate.OnDetectDataFileWrongByteCount(info.fileID, path)
}
return len(data), hits, misses, nil
}
// LoadFileData loads existing file data into memory.
func (d *Decoder) LoadFileData() error {
checksumToLocation := makeChecksumShardLocationMap(d.sliceByteCount, d.recoverySet)
fileIntegrityInfos := make([]fileIntegrityInfo, len(d.recoverySet))
fileIDIndices := make(map[fileID]int)
for i, info := range d.recoverySet {
fileIntegrityInfos[i] = fileIntegrityInfo{
fileID: info.fileID,
shardInfos: make([]shardIntegrityInfo, len(info.checksumPairs)),
}
fileIDIndices[info.fileID] = i
}
for i, info := range d.recoverySet {
path := d.getFilePath(info)
byteCount, hits, misses, err := d.fillFileIntegrityInfos(checksumToLocation, fileIntegrityInfos, fileIDIndices, i, info)
d.delegate.OnDataFileLoad(i+1, len(d.recoverySet), path, byteCount, hits, misses, err)
if err != nil {
return err
}
if byteCount != info.byteCount {
var startByteOffset, endByteOffset int
if byteCount < info.byteCount {
startByteOffset = byteCount
endByteOffset = info.byteCount
} else {
startByteOffset = info.byteCount
endByteOffset = byteCount
}
d.delegate.OnDetectCorruptDataChunk(info.fileID, path, startByteOffset, endByteOffset)
}
}
for i, info := range d.recoverySet {
integrityInfo := fileIntegrityInfos[i]
corruptStartByteOffset := -1
corruptEndByteOffset := -1
for j, shardInfo := range integrityInfo.shardInfos {
startByteOffset := j * d.sliceByteCount
endByteOffset := startByteOffset + d.sliceByteCount
if endByteOffset > info.byteCount {
endByteOffset = info.byteCount
}
if shardInfo.ok(shardLocation{info.fileID, startByteOffset}) {
if corruptStartByteOffset != -1 {
d.delegate.OnDetectCorruptDataChunk(info.fileID, d.getFilePath(info), corruptStartByteOffset, corruptEndByteOffset)
corruptStartByteOffset = -1
corruptEndByteOffset = -1
}
} else {
if corruptStartByteOffset == -1 {
corruptStartByteOffset = startByteOffset
}
corruptEndByteOffset = endByteOffset
}
}
if corruptStartByteOffset != -1 {
d.delegate.OnDetectCorruptDataChunk(info.fileID, d.getFilePath(info), corruptStartByteOffset, info.byteCount)
}
}
d.checksumToLocation = checksumToLocation
d.fileIntegrityInfos = fileIntegrityInfos
return nil
}
type recoveryDelegate struct {
d DecoderDelegate
}
func (recoveryDelegate) OnCreatorPacketLoad(clientID string) {}
func (recoveryDelegate) OnMainPacketLoad(sliceByteCount, recoverySetCount, nonRecoverySetCount int) {}
func (recoveryDelegate) OnFileDescriptionPacketLoad(fileID [16]byte, filename string, byteCount int) {
}
func (recoveryDelegate) OnIFSCPacketLoad(fileID [16]byte) {}
func (r recoveryDelegate) OnRecoveryPacketLoad(exponent uint16, byteCount int) {
r.d.OnRecoveryPacketLoad(exponent, byteCount)
}
func (r recoveryDelegate) OnUnknownPacketLoad(packetType [16]byte, byteCount int) {
r.d.OnUnknownPacketLoad(packetType, byteCount)
}
func (recoveryDelegate) OnOtherPacketSkip(setID [16]byte, packetType [16]byte, byteCount int) {}
func (recoveryDelegate) OnDataFileLoad(i, n int, path string, byteCount, hits, misses int, err error) {
}
func (recoveryDelegate) OnParityFileLoad(i int, path string, err error) {}
func (recoveryDelegate) OnDetectCorruptDataChunk(fileID [16]byte, path string, startByteOffset, endByteOffset int) {
}
func (recoveryDelegate) OnDetectDataFileHashMismatch(fileID [16]byte, path string) {}
func (recoveryDelegate) OnDetectDataFileWrongByteCount(fileID [16]byte, path string) {}
func (recoveryDelegate) OnDataFileWrite(i, n int, path string, byteCount int, err error) {}
// LoadParityData searches for parity volumes and loads them into
// memory.
func (d *Decoder) LoadParityData() error {
ext := path.Ext(d.indexPath)
base := d.indexPath[:len(d.indexPath)-len(ext)]
matches, err := d.fileIO.FindWithPrefixAndSuffix(base+".", ext)
if err != nil {
return err
}
var parityFiles []file
for i, match := range matches {
parityFile, err := func() (*file, error) {
volumeBytes, err := d.fileIO.ReadFile(match)
if err != nil {
return nil, err
}
// Ignore all the other packet types other
// than recovery packets.
_, parityFile, err := readFile(recoveryDelegate{d.delegate}, &d.setID, volumeBytes)
if _, ok := err.(noPacketsFoundError); ok {
return nil, nil
} else if err != nil {
// TODO: Relax this check.
return nil, err
}
if d.sliceByteCount != parityFile.mainPacket.sliceByteCount {
return nil, errors.New("slice byte count mismatch")
}
if !reflect.DeepEqual(decoderInputFileInfoIDs(d.recoverySet), parityFile.mainPacket.recoverySet) {
return nil, errors.New("recovery set mismatch")
}
if !reflect.DeepEqual(decoderInputFileInfoIDs(d.nonRecoverySet), parityFile.mainPacket.nonRecoverySet) {
return nil, errors.New("non-recovery set mismatch")
}
return &parityFile, nil
}()
d.delegate.OnParityFileLoad(i+1, match, err)
if err != nil {
return err
}
if parityFile == nil {
continue
}
parityFiles = append(parityFiles, *parityFile)
}
var parityShards [][]byte
for _, file := range parityFiles {
for exponent, packet := range file.recoveryPackets {
if int(exponent) >= len(parityShards) {
parityShards = append(parityShards, make([][]byte, int(exponent+1)-len(parityShards))...)
}
parityShards[exponent] = packet.data
}
}
d.parityShards = parityShards
return nil
}
func (d *Decoder) newCoderAndShards() (rsec16.Coder, [][]byte, error) {
if len(d.fileIntegrityInfos) == 0 {
return rsec16.Coder{}, nil, errors.New("no file integrity info")
}
if len(d.parityShards) == 0 {
return rsec16.Coder{}, nil, errors.New("no parity shards")
}
var dataShards [][]byte
for _, info := range d.fileIntegrityInfos {
for _, shardInfo := range info.shardInfos {
dataShards = append(dataShards, shardInfo.data)
}
}
coder, err := rsec16.NewCoderPAR2Vandermonde(len(dataShards), len(d.parityShards), d.numGoroutines)
if err != nil {
return rsec16.Coder{}, nil, err
}
return coder, dataShards, err
}
// ShardCounts contains shard counts which can be used to deduce
// whether repair is necessary and/or possible.
type ShardCounts struct {
// UsableDataShardCount is the number of data shards that are
// usable, i.e. not missing and not corrupt.
UsableDataShardCount int
// UnusableDataShardCount is the number of data shards that
// are unusable, i.e. missing or corrupt.
UnusableDataShardCount int
// UsableParityShardCount is the number of parity shards that
// exist, i.e. not missing and not corrupt.
UsableParityShardCount int
// UnusableDataShardCount is the number of parity shards that
// are unusable, i.e. missing or corrupt.
UnusableParityShardCount int
}
// RepairNeeded returns whether repair is needed, i.e. whether
// UnusableDataShardCount is non-zero.
func (fc ShardCounts) RepairNeeded() bool {
return fc.UnusableDataShardCount > 0
}
// RepairPossible returns whether repair is possible i.e. whether
// UsableParityShardCount >= UnusableDataShardCount.
func (fc ShardCounts) RepairPossible() bool {
return fc.UsableParityShardCount >= fc.UnusableDataShardCount
}
// ShardCounts returns a ShardCounts object for the current shard set.
func (d *Decoder) ShardCounts() ShardCounts {
usableDataShardCount := 0
unusableDataShardCount := 0
for _, info := range d.fileIntegrityInfos {
for _, shardInfo := range info.shardInfos {
if shardInfo.data == nil {
unusableDataShardCount++
} else {
usableDataShardCount++
}
}
}
usableParityShardCount := 0
unusableParityShardCount := 0
for _, shard := range d.parityShards {
if shard == nil {
unusableParityShardCount++
} else {
usableParityShardCount++
}
}
return ShardCounts{
UsableDataShardCount: usableDataShardCount,
UnusableDataShardCount: unusableDataShardCount,
UsableParityShardCount: usableParityShardCount,
UnusableParityShardCount: unusableParityShardCount,
}
}
// Repair tries to repair any missing or corrupt data, using the
// parity volumes. Returns a list of paths to files that were
// successfully repaired (relative to the indexFile passed to
// NewDecoder) in no particular order, which is present even if an
// error is returned. If checkParity is true, extra checking is done
// of the reconstructed parity data.
func (d *Decoder) Repair(checkParity bool) ([]string, error) {
coder, dataShards, err := d.newCoderAndShards()
if err != nil {
return nil, err
}
err = coder.ReconstructData(dataShards, d.parityShards)
if err != nil {
return nil, err
}
if checkParity {
computedParityShards := coder.GenerateParity(dataShards)
for i, shard := range d.parityShards {
if len(shard) == 0 {
continue
}
eq := reflect.DeepEqual(computedParityShards[i], shard)
if !eq {
return nil, errors.New("repair failed")
}
}
}
wasOK := make([]bool, len(d.fileIntegrityInfos))
k := 0
for i, info := range d.fileIntegrityInfos {
wasOK[i] = info.ok(d.sliceByteCount)
shardCount := len(info.shardInfos)
for j, shard := range dataShards[k : k+shardCount] {
info.shardInfos[j] = shardIntegrityInfo{
data: shard,
locations: d.checksumToLocation.get(crc32.ChecksumIEEE(shard), shard),
}
}
k += shardCount
d.fileIntegrityInfos[i] = info
}
var repairedPaths []string
for i, decoderInputFileInfo := range d.recoverySet {
fileIntegrityInfo := d.fileIntegrityInfos[i]
if wasOK[i] {
continue
}
buf := bytes.NewBuffer(nil)
for _, shardInfo := range fileIntegrityInfo.shardInfos {
err := binary.Write(buf, binary.LittleEndian, shardInfo.data)
if err != nil {
return repairedPaths, err
}
}
data := buf.Bytes()[:decoderInputFileInfo.byteCount]
if sixteenKHash(data) != decoderInputFileInfo.sixteenKHash {
return repairedPaths, errors.New("hash mismatch (16k) in reconstructed data")
} else if md5.Sum(data) != decoderInputFileInfo.hash {
return repairedPaths, errors.New("hash mismatch in reconstructed data")
}
path := d.getFilePath(decoderInputFileInfo)
err = d.fileIO.WriteFile(path, data)
d.delegate.OnDataFileWrite(i+1, len(d.recoverySet), path, len(data), err)
if err != nil {
return repairedPaths, err
}
repairedPaths = append(repairedPaths, path)
}
// TODO: Repair missing parity volumes, too, and then make
// sure d.Verify() passes.
return repairedPaths, nil
}
// NewDecoder reads the given index file, which usually has a .par2
// extension.
func NewDecoder(delegate DecoderDelegate, indexFile string, numGoroutines int) (*Decoder, error) {
return newDecoder(defaultFileIO{}, delegate, indexFile, numGoroutines)
}