-
Notifications
You must be signed in to change notification settings - Fork 1
/
par2creator.cpp
1211 lines (1019 loc) · 35.9 KB
/
par2creator.cpp
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of par2cmdline (a PAR 2.0 compatible file verification and
// repair tool). See http://parchive.sourceforge.net for details of PAR 2.0.
//
// Copyright (c) 2003 Peter Brian Clements
//
// par2cmdline is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// par2cmdline is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "par2cmdline.h"
#include <sys/types.h>
#include <sys/sysctl.h>
#include <unistd.h>
#ifdef _MSC_VER
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif
// Multiple thread support
struct CreateThreadParams
{
Par2Creator *This;
size_t blocklength;
u32 inputindex;
u32 aStartBlockNo;
u32 aEndBlockNo;
};
void *Par2Creator::CreateParityBlockRangeFunc (void *aParams)
{
assert (aParams);
CreateThreadParams *lParams = (CreateThreadParams *) aParams;
lParams->This->CreateParityBlockRange (lParams->blocklength, lParams->inputindex,
lParams->aStartBlockNo, lParams->aEndBlockNo);
free (aParams); // Done with it
return NULL;
}
Par2Creator::Par2Creator(void)
: noiselevel(CommandLine::nlUnknown)
, blocksize(0)
, chunksize(0)
, inputbuffer(0)
, outputbuffer(0)
, sourcefilecount(0)
, sourceblockcount(0)
, largestfilesize(0)
, recoveryfilescheme(CommandLine::scUnknown)
, recoveryfilecount(0)
, recoveryblockcount(0)
, firstrecoveryblock(0)
, mainpacket(0)
, creatorpacket(0)
, previouslyReportedFraction (0)
, deferhashcomputation(false)
{
// Some stuff for the multi-thread optimization
pthread_mutex_init (&progressMutex, NULL);
// Go and find number of CPU's in this machine
#ifdef CTL_HW
int lName [2] = { CTL_HW, HW_NCPU };
size_t lLen = sizeof (numCPUs);
if (sysctl(lName, 2, &numCPUs, &lLen, NULL, 0) != 0)
{
assert (false);
numCPUs = 1; // Default value if we have an error in sysctl
}
#elif defined(_SC_NPROCESSORS_ONLN)
numCPUs = sysconf( _SC_NPROCESSORS_ONLN );
#else
numCPUs = 1;
#endif
}
Par2Creator::~Par2Creator(void)
{
delete mainpacket;
delete creatorpacket;
delete [] (u8*)inputbuffer;
delete [] (u8*)outputbuffer;
vector<Par2CreatorSourceFile*>::iterator sourcefile = sourcefiles.begin();
while (sourcefile != sourcefiles.end())
{
delete *sourcefile;
++sourcefile;
}
pthread_mutex_destroy (&progressMutex);
}
Result Par2Creator::Process(const CommandLine &commandline)
{
// Get information from commandline
noiselevel = commandline.GetNoiseLevel();
blocksize = commandline.GetBlockSize();
sourceblockcount = commandline.GetBlockCount();
const list<CommandLine::ExtraFile> extrafiles = commandline.GetExtraFiles();
sourcefilecount = (u32)extrafiles.size();
u32 redundancy = commandline.GetRedundancy();
recoveryblockcount = commandline.GetRecoveryBlockCount();
recoveryfilecount = commandline.GetRecoveryFileCount();
firstrecoveryblock = commandline.GetFirstRecoveryBlock();
recoveryfilescheme = commandline.GetRecoveryFileScheme();
string par2filename = commandline.GetParFilename();
size_t memorylimit = commandline.GetMemoryLimit();
largestfilesize = commandline.GetLargestSourceSize();
// Compute block size from block count or vice versa depending on which was
// specified on the command line
if (!ComputeBlockSizeAndBlockCount(extrafiles))
return eInvalidCommandLineArguments;
// Determine how many recovery blocks to create based on the source block
// count and the requested level of redundancy.
if (redundancy > 0 && !ComputeRecoveryBlockCount(redundancy))
return eInvalidCommandLineArguments;
// Determine how much recovery data can be computed on one pass
if (!CalculateProcessBlockSize(memorylimit))
return eLogicError;
// Determine how many recovery files to create.
if (!ComputeRecoveryFileCount())
return eInvalidCommandLineArguments;
if (noiselevel > CommandLine::nlQuiet)
{
// Display information.
cout << "Block size: " << blocksize << endl;
cout << "Source file count: " << sourcefilecount << endl;
cout << "Source block count: " << sourceblockcount << endl;
if (redundancy>0 || recoveryblockcount==0)
cout << "Redundancy: " << redundancy << '%' << endl;
cout << "Recovery block count: " << recoveryblockcount << endl;
cout << "Recovery file count: " << recoveryfilecount << endl;
cout << endl;
}
// Open all of the source files, compute the Hashes and CRC values, and store
// the results in the file verification and file description packets.
if (!OpenSourceFiles(extrafiles))
return eFileIOError;
// Create the main packet and determine the setid to use with all packets
if (!CreateMainPacket())
return eLogicError;
// Create the creator packet.
if (!CreateCreatorPacket())
return eLogicError;
// Initialise all of the source blocks ready to start reading data from the source files.
if (!CreateSourceBlocks())
return eLogicError;
// Create all of the output files and allocate all packets to appropriate file offets.
if (!InitialiseOutputFiles(par2filename))
return eFileIOError;
if (recoveryblockcount > 0)
{
// Allocate memory buffers for reading and writing data to disk.
if (!AllocateBuffers())
return eMemoryError;
// Compute the Reed Solomon matrix
if (!ComputeRSMatrix())
return eLogicError;
// Set the total amount of data to be processed.
progress = 0;
totaldata = blocksize * sourceblockcount * recoveryblockcount;
previouslyReportedFraction = -10000000; // Big negative
// Start at an offset of 0 within a block.
u64 blockoffset = 0;
while (blockoffset < blocksize) // Continue until the end of the block.
{
// Work out how much data to process this time.
size_t blocklength = (size_t)min((u64)chunksize, blocksize-blockoffset);
// Read source data, process it through the RS matrix and write it to disk.
if (!ProcessData(blockoffset, blocklength))
return eFileIOError;
blockoffset += blocklength;
}
if (noiselevel > CommandLine::nlQuiet)
cout << "Writing recovery packets" << endl;
// Finish computation of the recovery packets and write the headers to disk.
if (!WriteRecoveryPacketHeaders())
return eFileIOError;
// Finish computing the full file hash values of the source files
if (!FinishFileHashComputation())
return eLogicError;
}
// Fill in all remaining details in the critical packets.
if (!FinishCriticalPackets())
return eLogicError;
if (noiselevel > CommandLine::nlQuiet)
cout << "Writing verification packets" << endl;
// Write all other critical packets to disk.
if (!WriteCriticalPackets())
return eFileIOError;
// Close all files.
if (!CloseFiles())
return eFileIOError;
if (noiselevel > CommandLine::nlSilent)
cout << "Done" << endl;
return eSuccess;
}
// Compute block size from block count or vice versa depending on which was
// specified on the command line
bool Par2Creator::ComputeBlockSizeAndBlockCount(const list<CommandLine::ExtraFile> &extrafiles)
{
// Determine blocksize from sourceblockcount or vice-versa
if (blocksize > 0)
{
u64 count = 0;
for (ExtraFileIterator i=extrafiles.begin(); i!=extrafiles.end(); i++)
{
count += (i->FileSize() + blocksize-1) / blocksize;
}
if (count > 32768)
{
cerr << "Block size is too small. It would require " << count << "blocks." << endl;
return false;
}
sourceblockcount = (u32)count;
}
else if (sourceblockcount > 0)
{
if (sourceblockcount < extrafiles.size())
{
// The block count cannot be less that the number of files.
cerr << "Block count is too small." << endl;
return false;
}
else if (sourceblockcount == extrafiles.size())
{
// If the block count is the same as the number of files, then the block
// size is the size of the largest file (rounded up to a multiple of 4).
u64 largestsourcesize = 0;
for (ExtraFileIterator i=extrafiles.begin(); i!=extrafiles.end(); i++)
{
if (largestsourcesize < i->FileSize())
{
largestsourcesize = i->FileSize();
}
}
blocksize = (largestsourcesize + 3) & ~3;
}
else
{
u64 totalsize = 0;
for (ExtraFileIterator i=extrafiles.begin(); i!=extrafiles.end(); i++)
{
totalsize += (i->FileSize() + 3) / 4;
}
if (sourceblockcount > totalsize)
{
sourceblockcount = (u32)totalsize;
blocksize = 4;
}
else
{
// Absolute lower bound and upper bound on the source block size that will
// result in the requested source block count.
u64 lowerBound = totalsize / sourceblockcount;
u64 upperBound = (totalsize + sourceblockcount - extrafiles.size() - 1) / (sourceblockcount - extrafiles.size());
u64 bestsize = lowerBound;
u64 bestdistance = 1000000;
u64 bestcount = 0;
u64 count;
u64 size;
// Work out how many blocks you get for the lower bound block size
{
size = lowerBound;
count = 0;
for (ExtraFileIterator i=extrafiles.begin(); i!=extrafiles.end(); i++)
{
count += ((i->FileSize()+3)/4 + size-1) / size;
}
if (bestdistance > (count>sourceblockcount ? count-sourceblockcount : sourceblockcount-count))
{
bestdistance = (count>sourceblockcount ? count-sourceblockcount : sourceblockcount-count);
bestcount = count;
bestsize = size;
}
}
// Work out how many blocks you get for the upper bound block size
{
size = upperBound;
count = 0;
for (ExtraFileIterator i=extrafiles.begin(); i!=extrafiles.end(); i++)
{
count += ((i->FileSize()+3)/4 + size-1) / size;
}
if (bestdistance > (count>sourceblockcount ? count-sourceblockcount : sourceblockcount-count))
{
bestdistance = (count>sourceblockcount ? count-sourceblockcount : sourceblockcount-count);
bestcount = count;
bestsize = size;
}
}
// Use binary search to find best block size
while (lowerBound+1 < upperBound)
{
size = (lowerBound + upperBound)/2;
count = 0;
for (ExtraFileIterator i=extrafiles.begin(); i!=extrafiles.end(); i++)
{
count += ((i->FileSize()+3)/4 + size-1) / size;
}
if (bestdistance > (count>sourceblockcount ? count-sourceblockcount : sourceblockcount-count))
{
bestdistance = (count>sourceblockcount ? count-sourceblockcount : sourceblockcount-count);
bestcount = count;
bestsize = size;
}
if (count < sourceblockcount)
{
upperBound = size;
}
else if (count > sourceblockcount)
{
lowerBound = size;
}
else
{
upperBound = size;
}
}
size = bestsize;
count = bestcount;
if (count > 32768)
{
cerr << "Error calculating block size." << endl;
return false;
}
sourceblockcount = (u32)count;
blocksize = size*4;
}
}
}
return true;
}
// Determine how many recovery blocks to create based on the source block
// count and the requested level of redundancy.
bool Par2Creator::ComputeRecoveryBlockCount(u32 redundancy)
{
// Determine recoveryblockcount
recoveryblockcount = (sourceblockcount * redundancy + 50) / 100;
// Force valid values if necessary
if (recoveryblockcount == 0 && redundancy > 0)
recoveryblockcount = 1;
if (recoveryblockcount > 65536)
{
cerr << "Too many recovery blocks requested." << endl;
return false;
}
// Check that the last recovery block number would not be too large
if (firstrecoveryblock + recoveryblockcount >= 65536)
{
cerr << "First recovery block number is too high." << endl;
return false;
}
return true;
}
// Determine how much recovery data can be computed on one pass
bool Par2Creator::CalculateProcessBlockSize(size_t memorylimit)
{
// Are we computing any recovery blocks
if (recoveryblockcount == 0)
{
deferhashcomputation = false;
}
else
{
// Would single pass processing use too much memory
if (blocksize * recoveryblockcount > memorylimit)
{
// Pick a size that is small enough
chunksize = ~3 & (memorylimit / recoveryblockcount);
deferhashcomputation = false;
}
else
{
chunksize = (size_t)blocksize;
deferhashcomputation = true;
}
}
return true;
}
// Determine how many recovery files to create.
bool Par2Creator::ComputeRecoveryFileCount(void)
{
// Are we computing any recovery blocks
if (recoveryblockcount == 0)
{
recoveryfilecount = 0;
return true;
}
switch (recoveryfilescheme)
{
case CommandLine::scUnknown:
{
assert(false);
return false;
}
break;
case CommandLine::scVariable:
case CommandLine::scUniform:
{
if (recoveryfilecount == 0)
{
// If none specified then then filecount is roughly log2(blockcount)
// This prevents you getting excessively large numbers of files
// when the block count is high and also allows the files to have
// sizes which vary exponentially.
for (u32 blocks=recoveryblockcount; blocks>0; blocks>>=1)
{
recoveryfilecount++;
}
}
if (recoveryfilecount > recoveryblockcount)
{
// You cannot have move recovery files that there are recovery blocks
// to put in them.
cerr << "Too many recovery files specified." << endl;
return false;
}
}
break;
case CommandLine::scLimited:
{
// No recovery file will contain more recovery blocks than would
// be required to reconstruct the largest source file if it
// were missing. Other recovery files will have recovery blocks
// distributed in an exponential scheme.
u32 largest = (u32)((largestfilesize + blocksize-1) / blocksize);
u32 whole = recoveryblockcount / largest;
whole = (whole >= 1) ? whole-1 : 0;
u32 extra = recoveryblockcount - whole * largest;
recoveryfilecount = whole;
for (u32 blocks=extra; blocks>0; blocks>>=1)
{
recoveryfilecount++;
}
}
break;
}
return true;
}
// Open all of the source files, compute the Hashes and CRC values, and store
// the results in the file verification and file description packets.
bool Par2Creator::OpenSourceFiles(const list<CommandLine::ExtraFile> &extrafiles)
{
ExtraFileIterator extrafile = extrafiles.begin();
while (extrafile != extrafiles.end())
{
Par2CreatorSourceFile *sourcefile = new Par2CreatorSourceFile;
string name = extrafile->FileName();
if (noiselevel > CommandLine::nlSilent)
cout << "Opening: " << name << endl;
// Open the source file and compute its Hashes and CRCs.
if (!sourcefile->Open(noiselevel, *extrafile, blocksize, deferhashcomputation))
{
delete sourcefile;
return false;
}
// Record the file verification and file description packets
// in the critical packet list.
sourcefile->RecordCriticalPackets(criticalpackets);
// Add the source file to the sourcefiles array.
sourcefiles.push_back(sourcefile);
// Close the source file until its needed
sourcefile->Close();
++extrafile;
}
return true;
}
// Create the main packet and determine the setid to use with all packets
bool Par2Creator::CreateMainPacket(void)
{
// Construct the main packet from the list of source files and the block size.
mainpacket = new MainPacket;
// Add the main packet to the list of critical packets.
criticalpackets.push_back(mainpacket);
// Create the packet (sourcefiles will get sorted into FileId order).
return mainpacket->Create(sourcefiles, blocksize);
}
// Create the creator packet.
bool Par2Creator::CreateCreatorPacket(void)
{
// Construct the creator packet
creatorpacket = new CreatorPacket;
// Create the packet
return creatorpacket->Create(mainpacket->SetId());
}
// Initialise all of the source blocks ready to start reading data from the source files.
bool Par2Creator::CreateSourceBlocks(void)
{
// Allocate the array of source blocks
sourceblocks.resize(sourceblockcount);
vector<DataBlock>::iterator sourceblock = sourceblocks.begin();
for (vector<Par2CreatorSourceFile*>::iterator sourcefile = sourcefiles.begin();
sourcefile!= sourcefiles.end();
sourcefile++)
{
// Allocate the appopriate number of source blocks to each source file.
// sourceblock will be advanced.
(*sourcefile)->InitialiseSourceBlocks(sourceblock, blocksize);
}
return true;
}
class FileAllocation
{
public:
FileAllocation(void)
{
filename = "";
exponent = 0;
count = 0;
}
string filename;
u32 exponent;
u32 count;
};
// Create all of the output files and allocate all packets to appropriate file offets.
bool Par2Creator::InitialiseOutputFiles(string par2filename)
{
// Allocate the recovery packets
recoverypackets.resize(recoveryblockcount);
// Choose filenames and decide which recovery blocks to place in each file
vector<FileAllocation> fileallocations;
fileallocations.resize(recoveryfilecount+1); // One extra file with no recovery blocks
{
// Decide how many recovery blocks to place in each file
u32 exponent = firstrecoveryblock;
if (recoveryfilecount > 0)
{
switch (recoveryfilescheme)
{
case CommandLine::scUnknown:
{
assert(false);
return false;
}
break;
case CommandLine::scUniform:
{
// Files will have roughly the same number of recovery blocks each.
u32 base = recoveryblockcount / recoveryfilecount;
u32 remainder = recoveryblockcount % recoveryfilecount;
for (u32 filenumber=0; filenumber<recoveryfilecount; filenumber++)
{
fileallocations[filenumber].exponent = exponent;
fileallocations[filenumber].count = (filenumber<remainder) ? base+1 : base;
exponent += fileallocations[filenumber].count;
}
}
break;
case CommandLine::scVariable:
{
// Files will have recovery blocks allocated in an exponential fashion.
// Work out how many blocks to place in the smallest file
u32 lowblockcount = 1;
u32 maxrecoveryblocks = (1 << recoveryfilecount) - 1;
while (maxrecoveryblocks < recoveryblockcount)
{
lowblockcount <<= 1;
maxrecoveryblocks <<= 1;
}
// Allocate the blocks.
u32 blocks = recoveryblockcount;
for (u32 filenumber=0; filenumber<recoveryfilecount; filenumber++)
{
u32 number = min(lowblockcount, blocks);
fileallocations[filenumber].exponent = exponent;
fileallocations[filenumber].count = number;
exponent += number;
blocks -= number;
lowblockcount <<= 1;
}
}
break;
case CommandLine::scLimited:
{
// Files will be allocated in an exponential fashion but the
// Maximum file size will be limited.
u32 largest = (u32)((largestfilesize + blocksize-1) / blocksize);
u32 filenumber = recoveryfilecount;
u32 blocks = recoveryblockcount;
exponent = firstrecoveryblock + recoveryblockcount;
// Allocate uniformly at the top
while (blocks >= 2*largest && filenumber > 0)
{
filenumber--;
exponent -= largest;
blocks -= largest;
fileallocations[filenumber].exponent = exponent;
fileallocations[filenumber].count = largest;
}
assert(blocks > 0 && filenumber > 0);
exponent = firstrecoveryblock;
u32 count = 1;
u32 files = filenumber;
// Allocate exponentially at the bottom
for (filenumber=0; filenumber<files; filenumber++)
{
u32 number = min(count, blocks);
fileallocations[filenumber].exponent = exponent;
fileallocations[filenumber].count = number;
exponent += number;
blocks -= number;
count <<= 1;
}
}
break;
}
}
// There will be an extra file with no recovery blocks.
fileallocations[recoveryfilecount].exponent = exponent;
fileallocations[recoveryfilecount].count = 0;
// Determine the format to use for filenames of recovery files
char filenameformat[300];
{
u32 limitLow = 0;
u32 limitCount = 0;
for (u32 filenumber=0; filenumber<=recoveryfilecount; filenumber++)
{
if (limitLow < fileallocations[filenumber].exponent)
{
limitLow = fileallocations[filenumber].exponent;
}
if (limitCount < fileallocations[filenumber].count)
{
limitCount = fileallocations[filenumber].count;
}
}
u32 digitsLow = 1;
for (u32 t=limitLow; t>=10; t/=10)
{
digitsLow++;
}
u32 digitsCount = 1;
for (u32 t=limitCount; t>=10; t/=10)
{
digitsCount++;
}
sprintf(filenameformat, "%%s.vol%%0%dd+%%0%dd.par2", digitsLow, digitsCount);
}
// Set the filenames
for (u32 filenumber=0; filenumber<recoveryfilecount; filenumber++)
{
char filename[300];
snprintf(filename, sizeof(filename), filenameformat, par2filename.c_str(), fileallocations[filenumber].exponent, fileallocations[filenumber].count);
fileallocations[filenumber].filename = filename;
}
fileallocations[recoveryfilecount].filename = par2filename + ".par2";
}
// Allocate the recovery files
{
recoveryfiles.resize(recoveryfilecount+1);
// Allocate packets to the output files
{
const MD5Hash &setid = mainpacket->SetId();
vector<RecoveryPacket>::iterator recoverypacket = recoverypackets.begin();
vector<DiskFile>::iterator recoveryfile = recoveryfiles.begin();
vector<FileAllocation>::iterator fileallocation = fileallocations.begin();
// For each recovery file:
while (recoveryfile != recoveryfiles.end())
{
// How many recovery blocks in this file
u32 count = fileallocation->count;
// start at the beginning of the recovery file
u64 offset = 0;
if (count == 0)
{
// Write one set of critical packets
list<CriticalPacket*>::const_iterator nextCriticalPacket = criticalpackets.begin();
while (nextCriticalPacket != criticalpackets.end())
{
criticalpacketentries.push_back(CriticalPacketEntry(&*recoveryfile,
offset,
*nextCriticalPacket));
offset += (*nextCriticalPacket)->PacketLength();
++nextCriticalPacket;
}
}
else
{
// How many copies of each critical packet
u32 copies = 0;
for (u32 t=count; t>0; t>>=1)
{
copies++;
}
// Get ready to iterate through the critical packets
u32 packetCount = 0;
list<CriticalPacket*>::const_iterator nextCriticalPacket = criticalpackets.end();
// What is the first exponent
u32 exponent = fileallocation->exponent;
// Start allocating the recovery packets
u32 limit = exponent + count;
while (exponent < limit)
{
// Add the next recovery packet
recoverypacket->Create(&*recoveryfile, offset, blocksize, exponent, setid);
offset += recoverypacket->PacketLength();
++recoverypacket;
++exponent;
// Add some critical packets
packetCount += copies * criticalpackets.size();
while (packetCount >= count)
{
if (nextCriticalPacket == criticalpackets.end()) nextCriticalPacket = criticalpackets.begin();
criticalpacketentries.push_back(CriticalPacketEntry(&*recoveryfile,
offset,
*nextCriticalPacket));
offset += (*nextCriticalPacket)->PacketLength();
++nextCriticalPacket;
packetCount -= count;
}
}
}
// Add one copy of the creator packet
criticalpacketentries.push_back(CriticalPacketEntry(&*recoveryfile,
offset,
creatorpacket));
offset += creatorpacket->PacketLength();
// Create the file on disk and make it the required size
if (!recoveryfile->Create(fileallocation->filename, offset))
return false;
++recoveryfile;
++fileallocation;
}
}
}
return true;
}
// Allocate memory buffers for reading and writing data to disk.
bool Par2Creator::AllocateBuffers(void)
{
inputbuffer = new u8[chunksize];
outputbuffer = new u8[chunksize * recoveryblockcount];
if (inputbuffer == NULL || outputbuffer == NULL)
{
cerr << "Could not allocate buffer memory." << endl;
return false;
}
return true;
}
// Compute the Reed Solomon matrix
bool Par2Creator::ComputeRSMatrix(void)
{
// Set the number of input blocks
if (!rs.SetInput(sourceblockcount))
return false;
// Set the number of output blocks to be created
if (!rs.SetOutput(false,
(u16)firstrecoveryblock,
(u16)firstrecoveryblock + (u16)(recoveryblockcount-1)))
return false;
// Compute the RS matrix
if (!rs.Compute(noiselevel))
return false;
return true;
}
// Read source data, process it through the RS matrix and write it to disk.
bool Par2Creator::ProcessData(u64 blockoffset, size_t blocklength)
{
// Clear the output buffer
memset(outputbuffer, 0, chunksize * recoveryblockcount);
// If we have defered computation of the file hash and block crc and hashes
// sourcefile and sourceindex will be used to update them during
// the main recovery block computation
vector<Par2CreatorSourceFile*>::iterator sourcefile = sourcefiles.begin();
u32 sourceindex = 0;
vector<DataBlock>::iterator sourceblock;
u32 inputblock;
DiskFile *lastopenfile = NULL;
// For each input block
for ((sourceblock=sourceblocks.begin()),(inputblock=0);
sourceblock != sourceblocks.end();
++sourceblock, ++inputblock)
{
// Are we reading from a new file?
if (lastopenfile != (*sourceblock).GetDiskFile())
{
// Close the last file
if (lastopenfile != NULL)
{
lastopenfile->Close();
}
// Open the new file
lastopenfile = (*sourceblock).GetDiskFile();
if (!lastopenfile->Open())
{
return false;
}
}
// Read data from the current input block
if (!sourceblock->ReadData(blockoffset, blocklength, inputbuffer))
return false;
if (deferhashcomputation)
{
assert(blockoffset == 0 && blocklength == blocksize);
assert(sourcefile != sourcefiles.end());
(*sourcefile)->UpdateHashes(sourceindex, inputbuffer, blocklength);
}
// Function that does the subtask in multiple threads if appropriate.
if (!this->CreateParityBlocks (blocklength, inputblock))
return false;
// Work out which source file the next block belongs to
if (++sourceindex >= (*sourcefile)->BlockCount())
{
sourceindex = 0;
++sourcefile;
}
}
// Close the last file
if (lastopenfile != NULL)
{
lastopenfile->Close();
}
if (noiselevel > CommandLine::nlQuiet)
cout << "Writing recovery packets\r";
// For each output block
for (u32 outputblock=0; outputblock<recoveryblockcount;outputblock++)
{
// Select the appropriate part of the output buffer
char *outbuf = &((char*)outputbuffer)[chunksize * outputblock];
// Write the data to the recovery packet
if (!recoverypackets[outputblock].WriteData(blockoffset, blocklength, outbuf))
return false;
}
if (noiselevel > CommandLine::nlQuiet)
cout << "Wrote " << recoveryblockcount * blocklength << " bytes to disk" << endl;
return true;
}
//-----------------------------------------------------------------------------
bool Par2Creator::CreateParityBlocks (size_t blocklength, u32 inputindex)