This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
forked from Parchive/par2cmdline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandline.cpp
1014 lines (931 loc) · 28 KB
/
commandline.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"
#ifdef _MSC_VER
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif
CommandLine::ExtraFile::ExtraFile(void)
: filename()
, filesize(0)
{
}
CommandLine::ExtraFile::ExtraFile(const CommandLine::ExtraFile &other)
: filename(other.filename)
, filesize(other.filesize)
{
}
CommandLine::ExtraFile& CommandLine::ExtraFile::operator=(const CommandLine::ExtraFile &other)
{
filename = other.filename;
filesize = other.filesize;
return *this;
}
CommandLine::ExtraFile::ExtraFile(const string &name, u64 size)
: filename(name)
, filesize(size)
{
}
CommandLine::CommandLine(void)
: operation(opNone)
, version(verUnknown)
, noiselevel(nlUnknown)
, blockcount(0)
, blocksize(0)
, firstblock(0)
, recoveryfilescheme(scUnknown)
, recoveryfilecount(0)
, recoveryblockcount(0)
, recoveryblockcountset(false)
, redundancy(0)
, redundancysize(0)
, redundancyset(false)
, parfilename()
, extrafiles()
, totalsourcesize(0)
, largestsourcesize(0)
, memorylimit(0)
, purgefiles(false)
, recursive(false)
, skipdata(true)
, skipleaway(0)
{
}
void CommandLine::showversion(void)
{
string version = PACKAGE " version " VERSION;
cout << version << endl;
}
void CommandLine::banner(void)
{
cout << "Copyright (C) 2003-2015 Peter Brian Clements." << endl
<< "Copyright (C) 2011-2012 Marcel Partap." << endl
<< "Copyright (C) 2012-2015 Ike Devolder." << endl
<< endl
<< "par2cmdline comes with ABSOLUTELY NO WARRANTY." << endl
<< endl
<< "This is free software, and you are welcome to redistribute it and/or modify" << endl
<< "it under the terms of the GNU General Public License as published by the" << endl
<< "Free Software Foundation; either version 2 of the License, or (at your" << endl
<< "option) any later version. See COPYING for details." << endl
<< endl;
}
void CommandLine::usage(void)
{
cout <<
"Usage:\n"
" par2 -h : show this help\n"
" par2 -V : show version\n"
" par2 -VV : show version and copyright\n"
"\n"
" par2 c(reate) [options] <par2 file> [files] : Create PAR2 files\n"
" par2 v(erify) [options] <par2 file> [files] : Verify files using PAR2 file\n"
" par2 r(epair) [options] <par2 file> [files] : Repair files using PAR2 files\n"
"\n"
"You may also leave out the \"c\", \"v\", and \"r\" commands by using \"par2create\",\n"
"\"par2verify\", or \"par2repair\" instead.\n"
"\n"
"Options:\n"
"\n"
" -a<file> : Set the main par2 archive name\n"
" -b<n> : Set the Block-Count\n"
" -s<n> : Set the Block-Size (Don't use both -b and -s)\n"
" -r<n> : Level of Redundancy (%%)\n"
" -r<c><n> : Redundancy target size, <c>=g(iga),m(ega),k(ilo) bytes\n"
" -c<n> : Recovery block count (Don't use both -r and -c)\n"
" -f<n> : First Recovery-Block-Number\n"
" -u : Uniform recovery file sizes\n"
" -l : Limit size of recovery files (Don't use both -u and -l)\n"
" -n<n> : Number of recovery files (Don't use both -n and -l)\n"
" -m<n> : Memory (in MB) to use\n"
" -v [-v] : Be more verbose\n"
" -q [-q] : Be more quiet (-q -q gives silence)\n"
" -p : Purge backup files and par files on successful recovery or\n"
" when no recovery is needed\n"
" -R : Recurse into subdirectories (only useful on create)\n"
" -N : No data skipping (find badly mispositioned data blocks)\n"
" -S<n> : Skip leaway (distance +/- from expected block position)\n"
" -- : Treat all remaining CommandLine as filenames\n"
"\n";
}
bool CommandLine::Parse(int argc, char *argv[])
{
if (argc<1)
{
return false;
}
// Split the program name into path and filename
string path, name;
DiskFile::SplitFilename(argv[0], path, name);
argc--;
argv++;
if (argc>0)
{
if (argv[0][0] != 0 &&
argv[0][0] == '-')
{
if (argv[0][1] != 0)
{
switch (argv[0][1])
{
case 'h':
usage();
return true;
case 'V':
showversion();
if (argv[0][2] != 0 &&
argv[0][2] == 'V')
{
cout << endl;
banner();
}
return true;
case '-':
string str = argv[0];
if (str == "--help")
{
usage();
return true;
}
}
}
}
}
// Strip ".exe" from the end
if (name.size() > 4 && 0 == stricmp(".exe", name.substr(name.length()-4).c_str()))
{
name = name.substr(0, name.length()-4);
}
// Check the resulting program name
if (0 == stricmp("par2create", name.c_str()))
{
operation = opCreate;
}
else if (0 == stricmp("par2verify", name.c_str()))
{
operation = opVerify;
}
else if (0 == stricmp("par2repair", name.c_str()))
{
operation = opRepair;
}
// Have we determined what operation we want?
if (operation == opNone)
{
if (argc<2)
{
cerr << "Not enough command line arguments." << endl;
return false;
}
switch (tolower(argv[0][0]))
{
case 'c':
if (argv[0][1] == 0 || 0 == stricmp(argv[0], "create"))
operation = opCreate;
break;
case 'v':
if (argv[0][1] == 0 || 0 == stricmp(argv[0], "verify"))
operation = opVerify;
break;
case 'r':
if (argv[0][1] == 0 || 0 == stricmp(argv[0], "repair"))
operation = opRepair;
break;
}
if (operation == opNone)
{
cerr << "Invalid operation specified: " << argv[0] << endl;
return false;
}
argc--;
argv++;
}
bool options = true;
list<string> a_filenames;
while (argc>0)
{
if (argv[0][0])
{
if (options && argv[0][0] != '-')
options = false;
if (options)
{
switch (argv[0][1])
{
case 'a':
{
if (operation == opCreate)
{
string str = argv[0];
if (str == "-a")
{
SetParFilename(argv[1]);
argc--;
argv++;
}
else
{
SetParFilename(str.substr(2));
}
}
}
break;
case 'b': // Set the block count
{
if (operation != opCreate)
{
cerr << "Cannot specify block count unless creating." << endl;
return false;
}
if (blockcount > 0)
{
cerr << "Cannot specify block count twice." << endl;
return false;
}
else if (blocksize > 0)
{
cerr << "Cannot specify both block count and block size." << endl;
return false;
}
char *p = &argv[0][2];
while (blockcount <= 3276 && *p && isdigit(*p))
{
blockcount = blockcount * 10 + (*p - '0');
p++;
}
if (0 == blockcount || blockcount > 32768 || *p)
{
cerr << "Invalid block count option: " << argv[0] << endl;
return false;
}
}
break;
case 's': // Set the block size
{
if (operation != opCreate)
{
cerr << "Cannot specify block size unless creating." << endl;
return false;
}
if (blocksize > 0)
{
cerr << "Cannot specify block size twice." << endl;
return false;
}
else if (blockcount > 0)
{
cerr << "Cannot specify both block count and block size." << endl;
return false;
}
char *p = &argv[0][2];
while (blocksize <= 429496729 && *p && isdigit(*p))
{
blocksize = blocksize * 10 + (*p - '0');
p++;
}
if (*p || blocksize == 0)
{
cerr << "Invalid block size option: " << argv[0] << endl;
return false;
}
if (blocksize & 3)
{
cerr << "Block size must be a multiple of 4." << endl;
return false;
}
}
break;
case 'r': // Set the amount of redundancy required
{
if (operation != opCreate)
{
cerr << "Cannot specify redundancy unless creating." << endl;
return false;
}
if (redundancyset)
{
cerr << "Cannot specify redundancy twice." << endl;
return false;
}
else if (recoveryblockcountset)
{
cerr << "Cannot specify both redundancy and recovery block count." << endl;
return false;
}
if (argv[0][2] == 'k'
|| argv[0][2] == 'm'
|| argv[0][2] == 'g'
)
{
char *p = &argv[0][3];
while (*p && isdigit(*p))
{
redundancysize = redundancysize * 10 + (*p - '0');
p++;
}
switch (argv[0][2])
{
case 'g':
redundancysize = redundancysize * 1024;
case 'm':
redundancysize = redundancysize * 1024;
case 'k':
redundancysize = redundancysize * 1024;
break;
}
}
else
{
char *p = &argv[0][2];
while (redundancy <= 10 && *p && isdigit(*p))
{
redundancy = redundancy * 10 + (*p - '0');
p++;
}
if (redundancy > 100 || *p)
{
cerr << "Invalid redundancy option: " << argv[0] << endl;
return false;
}
if (redundancy == 0 && recoveryfilecount > 0)
{
cerr << "Cannot set redundancy to 0 and file count > 0" << endl;
return false;
}
}
redundancyset = true;
}
break;
case 'c': // Set the number of recovery blocks to create
{
if (operation != opCreate)
{
cerr << "Cannot specify recovery block count unless creating." << endl;
return false;
}
if (recoveryblockcountset)
{
cerr << "Cannot specify recovery block count twice." << endl;
return false;
}
else if (redundancyset)
{
cerr << "Cannot specify both recovery block count and redundancy." << endl;
return false;
}
char *p = &argv[0][2];
while (recoveryblockcount <= 32768 && *p && isdigit(*p))
{
recoveryblockcount = recoveryblockcount * 10 + (*p - '0');
p++;
}
if (recoveryblockcount > 32768 || *p)
{
cerr << "Invalid recoveryblockcount option: " << argv[0] << endl;
return false;
}
if (recoveryblockcount == 0 && recoveryfilecount > 0)
{
cerr << "Cannot set recoveryblockcount to 0 and file count > 0" << endl;
return false;
}
recoveryblockcountset = true;
}
break;
case 'f': // Specify the First block recovery number
{
if (operation != opCreate)
{
cerr << "Cannot specify first block number unless creating." << endl;
return false;
}
if (firstblock > 0)
{
cerr << "Cannot specify first block twice." << endl;
return false;
}
char *p = &argv[0][2];
while (firstblock <= 3276 && *p && isdigit(*p))
{
firstblock = firstblock * 10 + (*p - '0');
p++;
}
if (firstblock > 32768 || *p)
{
cerr << "Invalid first block option: " << argv[0] << endl;
return false;
}
}
break;
case 'u': // Specify uniformly sized recovery files
{
if (operation != opCreate)
{
cerr << "Cannot specify uniform files unless creating." << endl;
return false;
}
if (argv[0][2])
{
cerr << "Invalid option: " << argv[0] << endl;
return false;
}
if (recoveryfilescheme != scUnknown)
{
cerr << "Cannot specify two recovery file size schemes." << endl;
return false;
}
recoveryfilescheme = scUniform;
}
break;
case 'l': // Limit the size of the recovery files
{
if (operation != opCreate)
{
cerr << "Cannot specify limit files unless creating." << endl;
return false;
}
if (argv[0][2])
{
cerr << "Invalid option: " << argv[0] << endl;
return false;
}
if (recoveryfilescheme != scUnknown)
{
cerr << "Cannot specify two recovery file size schemes." << endl;
return false;
}
if (recoveryfilecount > 0)
{
cerr << "Cannot specify limited size and number of files at the same time." << endl;
return false;
}
recoveryfilescheme = scLimited;
}
break;
case 'n': // Specify the number of recovery files
{
if (operation != opCreate)
{
cerr << "Cannot specify recovery file count unless creating." << endl;
return false;
}
if (recoveryfilecount > 0)
{
cerr << "Cannot specify recovery file count twice." << endl;
return false;
}
if (redundancyset && redundancy == 0)
{
cerr << "Cannot set file count when redundancy is set to 0." << endl;
return false;
}
if (recoveryblockcountset && recoveryblockcount == 0)
{
cerr << "Cannot set file count when recovery block count is set to 0." << endl;
return false;
}
if (recoveryfilescheme == scLimited)
{
cerr << "Cannot specify limited size and number of files at the same time." << endl;
return false;
}
char *p = &argv[0][2];
while (*p && isdigit(*p))
{
recoveryfilecount = recoveryfilecount * 10 + (*p - '0');
p++;
}
if (recoveryfilecount == 0 || *p)
{
cerr << "Invalid recovery file count option: " << argv[0] << endl;
return false;
}
}
break;
case 'm': // Specify how much memory to use for output buffers
{
if (memorylimit > 0)
{
cerr << "Cannot specify memory limit twice." << endl;
return false;
}
char *p = &argv[0][2];
while (*p && isdigit(*p))
{
memorylimit = memorylimit * 10 + (*p - '0');
p++;
}
if (memorylimit == 0 || *p)
{
cerr << "Invalid memory limit option: " << argv[0] << endl;
return false;
}
}
break;
case 'v':
{
switch (noiselevel)
{
case nlUnknown:
{
if (argv[0][2] == 'v')
noiselevel = nlDebug;
else
noiselevel = nlNoisy;
}
break;
case nlNoisy:
case nlDebug:
noiselevel = nlDebug;
break;
default:
cerr << "Cannot use both -v and -q." << endl;
return false;
break;
}
}
break;
case 'q':
{
switch (noiselevel)
{
case nlUnknown:
{
if (argv[0][2] == 'q')
noiselevel = nlSilent;
else
noiselevel = nlQuiet;
}
break;
case nlQuiet:
case nlSilent:
noiselevel = nlSilent;
break;
default:
cerr << "Cannot use both -v and -q." << endl;
return false;
break;
}
}
break;
case 'p':
{
if (operation != opRepair && operation != opVerify)
{
cerr << "Cannot specify purge unless repairing or verifying." << endl;
return false;
}
purgefiles = true;
}
break;
case 'h':
{
usage();
return false;
break;
}
case 'R':
{
if (operation == opCreate)
{
recursive = true;
}
else
{
cerr << "Recursive has no impact except on creating." << endl;
}
}
break;
case 'N':
{
if (operation == opCreate)
{
cerr << "Cannot specify No Data Skipping unless reparing or verifying." << endl;
return false;
}
if (skipleaway > 0)
{
cerr << "Cannot specify no skipping and skip leaway." << endl;
return false;
}
skipdata = false;
}
break;
case 'S': // Set the skip leaway
{
if (operation == opCreate)
{
cerr << "Cannot specify skip leaway when creating." << endl;
return false;
}
if (!skipdata)
{
cerr << "Cannot specify skip leaway and no skipping." << endl;
return false;
}
char *p = &argv[0][2];
while (skipleaway <= 429496729 && *p && isdigit(*p))
{
skipleaway = skipleaway * 10 + (*p - '0');
p++;
}
if (*p || skipleaway == 0)
{
cerr << "Invalid skipleaway option: " << argv[0] << endl;
return false;
}
}
break;
case '-':
{
argc--;
argv++;
options = false;
continue;
}
break;
default:
{
cerr << "Invalid option specified: " << argv[0] << endl;
return false;
}
}
}
else if (parfilename.length() == 0)
{
string filename = argv[0];
string::size_type where;
if ((where = filename.find_first_of('*')) != string::npos ||
(where = filename.find_first_of('?')) != string::npos)
{
cerr << "par2 file must not have a wildcard in it." << endl;
return false;
}
SetParFilename(filename);
}
else
{
list<string> *filenames;
string path;
string name;
DiskFile::SplitFilename(argv[0], path, name);
filenames = DiskFile::FindFiles(path, name, recursive);
string canonicalBasepath = DiskFile::GetCanonicalPathname(basepath);
list<string>::iterator fn = filenames->begin();
while (fn != filenames->end())
{
// Convert filename from command line into a full path + filename
string filename = DiskFile::GetCanonicalPathname(*fn);
// Originally, all specified files were supposed to exist, or the program
// would stop with an error message. This was not practical, for example in
// a directory with files appearing and disappearing (an active download directory).
// So the new rule is: when a specified file doesn't exist, it is silently skipped.
if (!DiskFile::FileExists(filename))
{
cout << "Ignoring non-existent source file: " << filename << endl;
}
// skip files outside basepath
else if (filename.find(canonicalBasepath) == string::npos)
{
cout << "Ignoring out of basepath source file: " << filename << endl;
}
else
{
u64 filesize = DiskFile::GetFileSize(filename);
// Ignore all 0 byte files
if (filesize == 0)
{
cout << "Skipping 0 byte file: " << filename << endl;
}
else if (a_filenames.end() != find(a_filenames.begin(), a_filenames.end(), filename))
{
cout << "Skipping duplicate filename: " << filename << endl;
}
else
{
a_filenames.push_back(filename);
extrafiles.push_back(ExtraFile(filename, filesize));
// track the total size of the source files and how
// big the largest one is.
totalsourcesize += filesize;
if (largestsourcesize < filesize)
largestsourcesize = filesize;
}
} //end file exists
++fn;
}
delete filenames;
}
}
argc--;
argv++;
}
if (parfilename.length() == 0)
{
cerr << "You must specify a Recovery file." << endl;
return false;
}
// Default noise level
if (noiselevel == nlUnknown)
{
noiselevel = nlNormal;
}
// Default skip leaway
if (operation != opCreate
&& skipdata
&& skipleaway == 0)
{
// Expect to find blocks within +/- 64 bytes of the expected
// position relative to the last block that was found.
skipleaway = 64;
}
// If we a creating, check the other parameters
if (operation == opCreate)
{
// If no recovery file size scheme is specified then use Variable
if (recoveryfilescheme == scUnknown)
{
recoveryfilescheme = scVariable;
}
// If neither block count not block size is specified
if (blockcount == 0 && blocksize == 0)
{
// Use a block count of 2000
blockcount = 2000;
}
// If we are creating, the source files must be given.
if (extrafiles.size() == 0)
{
// Does the par filename include the ".par2" on the end?
if (parfilename.length() > 5 && 0 == stricmp(parfilename.substr(parfilename.length()-5, 5).c_str(), ".par2"))
{
// Yes it does.
cerr << "You must specify a list of files when creating." << endl;
return false;
}
else
{
// No it does not.
// In that case check to see if the file exists, and if it does
// assume that you wish to create par2 files for it.
u64 filesize = 0;
if (DiskFile::FileExists(parfilename) &&
(filesize = DiskFile::GetFileSize(parfilename)) > 0)
{
extrafiles.push_back(ExtraFile(parfilename, filesize));
// track the total size of the source files and how
// big the largest one is.
totalsourcesize += filesize;
if (largestsourcesize < filesize)
largestsourcesize = filesize;
}
else
{
// The file does not exist or it is empty.
cerr << "You must specify a list of files when creating." << endl;
return false;
}
}
}
// Strip the ".par2" from the end of the filename of the main PAR2 file.
if (parfilename.length() > 5 && 0 == stricmp(parfilename.substr(parfilename.length()-5, 5).c_str(), ".par2"))
{
parfilename = parfilename.substr(0, parfilename.length()-5);
}
// Assume a redundancy of 5% if neither redundancy or recoveryblockcount were set.
if (!redundancyset && !recoveryblockcountset)
{
redundancy = 5;
}
}
// Assume a memory limit of 16MB if not specified.
if (memorylimit == 0)
{
#ifdef WIN32
u64 TotalPhysicalMemory = 0;
HMODULE hLib = ::LoadLibraryA("kernel32.dll");
if (NULL != hLib)
{
BOOL (WINAPI *pfn)(LPMEMORYSTATUSEX) = (BOOL (WINAPI*)(LPMEMORYSTATUSEX))::GetProcAddress(hLib, "GlobalMemoryStatusEx");
if (NULL != pfn)
{
MEMORYSTATUSEX mse;
mse.dwLength = sizeof(mse);
if (pfn(&mse))
{
TotalPhysicalMemory = mse.ullTotalPhys;
}
}
::FreeLibrary(hLib);
}
if (TotalPhysicalMemory == 0)
{
MEMORYSTATUS ms;
::ZeroMemory(&ms, sizeof(ms));
::GlobalMemoryStatus(&ms);
TotalPhysicalMemory = ms.dwTotalPhys;
}
if (TotalPhysicalMemory == 0)
{
// Assume 128MB
TotalPhysicalMemory = 128 * 1048576;
}
// Half of total physical memory
memorylimit = (size_t)(TotalPhysicalMemory / 1048576 / 2);
#else
memorylimit = 16;
#endif
}
memorylimit *= 1048576;
return true;
}
void CommandLine::SetParFilename(string filename)
{
parfilename = DiskFile::GetCanonicalPathname(filename);
// If we are verifying or repairing, the PAR2 file must
// already exist
if (operation != opCreate)
{
// Find the last '.' in the filename
string::size_type where = filename.find_last_of('.');
if (where != string::npos)
{
// Get what follows the last '.'
string tail = filename.substr(where+1);
if (0 == stricmp(tail.c_str(), "par2"))
{
parfilename = filename;
version = verPar2;
}
else if (0 == stricmp(tail.c_str(), "par") ||
(tail.size() == 3 &&
tolower(tail[0]) == 'p' &&
isdigit(tail[1]) &&
isdigit(tail[2])))
{
parfilename = filename;
version = verPar1;
}
}
// If we haven't figured out which version of PAR file we
// are using from the file extension, then presumable the
// files filename was actually the name of a data file.
if (version == verUnknown)
{
// Check for the existence of a PAR2 of PAR file.
if (DiskFile::FileExists(filename + ".par2"))
{
version = verPar2;
parfilename = filename + ".par2";
}
else if (DiskFile::FileExists(filename + ".PAR2"))
{
version = verPar2;
parfilename = filename + ".PAR2";
}
else if (DiskFile::FileExists(filename + ".par"))
{
version = verPar1;
parfilename = filename + ".par";
}
else if (DiskFile::FileExists(filename + ".PAR"))
{
version = verPar1;
parfilename = filename + ".PAR";
}
}
else
{
// Does the specified PAR or PAR2 file exist