Skip to content

Commit

Permalink
allow setting basepath and fix some tests
Browse files Browse the repository at this point in the history
added a new command flag -B to set the basepath if desired.
We could go further and try to 'detect' a basepath, but for the sake of
sanity just pass it along if you need it.

closes #61

Signed-off-by: BlackEagle <ike.devolder@gmail.com>
  • Loading branch information
BlackIkeEagle committed Jun 26, 2016
1 parent 287469f commit 19dd496
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 36 deletions.
9 changes: 5 additions & 4 deletions README
Expand Up @@ -6,7 +6,7 @@ https://github.com/BlackIkeEagle/par2cmdline
The original development was done on Sourceforge but stalled.
For more information from the original authors see
http://parchive.sourceforge.net
Also for details of the PAR 2.0 specification and discussion of all
Also for details of the PAR 2.0 specification and discussion of all
things PAR.

WHAT EXACTLY IS PAR2CMDLINE?
Expand Down Expand Up @@ -42,7 +42,7 @@ WHY IS PAR 2.0 better than PAR 1.0?
* Damaged or incomplete recovery files can also be used during the
recovery process in the same way that damaged data files can.

* PAR 2.0 requires less recovery data to provide the same level of
* PAR 2.0 requires less recovery data to provide the same level of
protection from damage compared with PAR 1.0.

DOES PAR 2.0 HAVE ANY DISADVANTAGES?
Expand Down Expand Up @@ -121,6 +121,7 @@ The command line parameters for par2cmdline are as follow:
-R : Recurse into subdirectories (only useful on create)
-N : No data skipping (find badly misspositioned data blocks)
-S<n> : Skip leaway (distance +/- from expected block position)
-B<path> : Set the basepath to use as reference for the datafiles
-- : Treat all remaining CommandLine as filenames

If you wish to create par2 files for a single source file, you may leave
Expand Down Expand Up @@ -190,8 +191,8 @@ The "-u" and "-n" options can be used to control exactly how many recovery
files are created and how the recovery blocks are distributed among them.
They do not affect the total quantity of recovery data created.

The "-f" option is used when you create additional recovery data e.g. If
you have already created 10% and want another 5% then you migh use the
The "-f" option is used when you create additional recovery data e.g. If
you have already created 10% and want another 5% then you migh use the
following command:

par2 create -s307200 -r5 -f300 test.mpg.par2 test.mpg
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -99,6 +99,7 @@ The command line parameters for par2cmdline are as follow:
-R : Recurse into subdirectories (only useful on create)
-N : No data skipping (find badly misspositioned data blocks)
-S<n> : Skip leaway (distance +/- from expected block position)
-B<path> : Set the basepath to use as reference for the datafiles
-- : Treat all remaining CommandLine as filenames

If you wish to create par2 files for a single source file, you may leave out the name of the par2 file from the command line. par2cmdline will then assume that you wish to base the filenames for the par2 files on the name of the source file.
Expand Down
26 changes: 26 additions & 0 deletions commandline.cpp
Expand Up @@ -136,6 +136,7 @@ void CommandLine::usage(void)
" -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"
" -B<path> : Set the basepath to use as reference for the datafiles\n"
" -- : Treat all remaining CommandLine as filenames\n"
"\n";
}
Expand Down Expand Up @@ -705,6 +706,31 @@ bool CommandLine::Parse(int argc, char *argv[])
}
break;

case 'B': // Set the basepath manually
{
string str = argv[0];
if (str == "-B")
{
basepath = DiskFile::GetCanonicalPathname(argv[1]);
argc--;
argv++;
}
else
{
basepath = DiskFile::GetCanonicalPathname(str.substr(2));
}
string lastchar = basepath.substr(basepath.length() -1);
if ("/" != lastchar && "\\" != lastchar)
{
#ifdef WIN32
basepath = basepath + "\\";
#else
basepath = basepath + "/";
#endif
}
}
break;

case '-':
{
argc--;
Expand Down
14 changes: 7 additions & 7 deletions diskfile.cpp
Expand Up @@ -66,14 +66,14 @@ bool DiskFile::CreateParentDirectory(string _pathname)
string::npos != (where = _pathname.find_last_of('\\')))
{
string path = filename.substr(0, where);

struct stat st;
if (stat(path.c_str(), &st) == 0)
return true; // let the caller deal with non-directories

if (!DiskFile::CreateParentDirectory(path))
return false;

if (!CreateDirectory(path.c_str(), NULL))
{
DWORD error = ::GetLastError();
Expand Down Expand Up @@ -453,14 +453,14 @@ bool DiskFile::CreateParentDirectory(string _pathname)
string::npos != (where = _pathname.find_last_of('\\')))
{
string path = filename.substr(0, where);

struct stat st;
if (stat(path.c_str(), &st) == 0)
return true; // let the caller deal with non-directories

if (!DiskFile::CreateParentDirectory(path))
return false;

if (mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
{
cerr << "Could not create the " << path << " directory: " << strerror(errno) << endl;
Expand Down Expand Up @@ -841,7 +841,7 @@ list<string>* DiskFile::FindFiles(string path, string wildcard, bool recursive)
}
}
}

return matches;
}

Expand Down
3 changes: 3 additions & 0 deletions par2.1
Expand Up @@ -93,6 +93,9 @@ No data skipping (find badly mispositioned data blocks)
.B \-S<n>
Skip leaway (distance +/\- from expected block position)
.TP
.B \-B<path>
Set the basepath to use as reference for the datafiles
.TP
.B \-\-
Treat all remaining command line as filenames
.SH AUTHORS
Expand Down
32 changes: 16 additions & 16 deletions par2creator.cpp
Expand Up @@ -429,7 +429,7 @@ bool Par2Creator::ComputeRecoveryFileCount(void)
recoveryfilecount = 0;
return true;
}

switch (recoveryfilescheme)
{
case CommandLine::scUnknown:
Expand All @@ -453,7 +453,7 @@ bool Par2Creator::ComputeRecoveryFileCount(void)
recoveryfilecount++;
}
}

if (recoveryfilecount > recoveryblockcount)
{
// You cannot have move recovery files that there are recovery blocks
Expand Down Expand Up @@ -556,7 +556,7 @@ bool Par2Creator::CreateSourceBlocks(void)
sourceblocks.resize(sourceblockcount);

vector<DataBlock>::iterator sourceblock = sourceblocks.begin();

for (vector<Par2CreatorSourceFile*>::iterator sourcefile = sourcefiles.begin();
sourcefile!= sourcefiles.end();
sourcefile++)
Expand All @@ -573,7 +573,7 @@ bool Par2Creator::CreateSourceBlocks(void)
class FileAllocation
{
public:
FileAllocation(void)
FileAllocation(void)
{
filename = "";
exponent = 0;
Expand Down Expand Up @@ -658,7 +658,7 @@ bool Par2Creator::InitialiseOutputFiles(string par2filename)
u32 largest = (u32)((largestfilesize + blocksize-1) / blocksize);
u32 filenumber = recoveryfilecount;
u32 blocks = recoveryblockcount;

exponent = firstrecoveryblock + recoveryblockcount;

// Allocate uniformly at the top
Expand Down Expand Up @@ -692,7 +692,7 @@ bool Par2Creator::InitialiseOutputFiles(string par2filename)
break;
}
}

// There will be an extra file with no recovery blocks.
fileallocations[recoveryfilecount].exponent = exponent;
fileallocations[recoveryfilecount].count = 0;
Expand All @@ -719,7 +719,7 @@ bool Par2Creator::InitialiseOutputFiles(string par2filename)
{
digitsLow++;
}

u32 digitsCount = 1;
for (u32 t=limitCount; t>=10; t/=10)
{
Expand Down Expand Up @@ -767,8 +767,8 @@ bool Par2Creator::InitialiseOutputFiles(string par2filename)

while (nextCriticalPacket != criticalpackets.end())
{
criticalpacketentries.push_back(CriticalPacketEntry(&*recoveryfile,
offset,
criticalpacketentries.push_back(CriticalPacketEntry(&*recoveryfile,
offset,
*nextCriticalPacket));
offset += (*nextCriticalPacket)->PacketLength();

Expand Down Expand Up @@ -807,7 +807,7 @@ bool Par2Creator::InitialiseOutputFiles(string par2filename)
while (packetCount >= count)
{
if (nextCriticalPacket == criticalpackets.end()) nextCriticalPacket = criticalpackets.begin();
criticalpacketentries.push_back(CriticalPacketEntry(&*recoveryfile,
criticalpacketentries.push_back(CriticalPacketEntry(&*recoveryfile,
offset,
*nextCriticalPacket));
offset += (*nextCriticalPacket)->PacketLength();
Expand All @@ -819,8 +819,8 @@ bool Par2Creator::InitialiseOutputFiles(string par2filename)
}

// Add one copy of the creator packet
criticalpacketentries.push_back(CriticalPacketEntry(&*recoveryfile,
offset,
criticalpacketentries.push_back(CriticalPacketEntry(&*recoveryfile,
offset,
creatorpacket));
offset += creatorpacket->PacketLength();

Expand Down Expand Up @@ -860,8 +860,8 @@ bool Par2Creator::ComputeRSMatrix(void)
return false;

// Set the number of output blocks to be created
if (!rs.SetOutput(false,
(u16)firstrecoveryblock,
if (!rs.SetOutput(false,
(u16)firstrecoveryblock,
(u16)firstrecoveryblock + (u16)(recoveryblockcount-1)))
return false;

Expand Down Expand Up @@ -1021,8 +1021,8 @@ bool Par2Creator::FinishCriticalPackets(void)
// Get the setid from the main packet
const MD5Hash &setid = mainpacket->SetId();

for (list<CriticalPacket*>::iterator criticalpacket=criticalpackets.begin();
criticalpacket!=criticalpackets.end();
for (list<CriticalPacket*>::iterator criticalpacket=criticalpackets.begin();
criticalpacket!=criticalpackets.end();
criticalpacket++)
{
// Store the setid in each of the critical packets
Expand Down
2 changes: 1 addition & 1 deletion par2creatorsourcefile.cpp
Expand Up @@ -77,7 +77,7 @@ bool Par2CreatorSourceFile::Open(CommandLine::NoiseLevel noiselevel, const Comma
if (!diskfile->Open(diskfilename, filesize))
return false;

// Do we want to defer the computation of the full file hash, and
// Do we want to defer the computation of the full file hash, and
// the block crc and hashes. This is only permitted if there
// is sufficient memory available to create all recovery blocks
// in one pass of the source files (i.e. chunksize == blocksize)
Expand Down
2 changes: 1 addition & 1 deletion par2repairer.cpp
Expand Up @@ -107,7 +107,7 @@ Result Par2Repairer::Process(const CommandLine &commandline, bool dorepair)
par2list.push_back(par2filename);

// Load packets from the main PAR2 file
if (!LoadPacketsFromFile(basepath + name))
if (!LoadPacketsFromFile(searchpath + name))
return eLogicError;

// Load packets from other PAR2 files with names based on the original PAR2 file
Expand Down
2 changes: 1 addition & 1 deletion tests/test16
Expand Up @@ -30,7 +30,7 @@ cd subdir1
# check if there were ignored files
grep 'Ignoring' $tests/$testname.log || { echo "ERROR: there were no files ignored"; exit 1; } >&2

cd ..
cd ../..
rm -rf run$testname

exit 0;
Expand Down
4 changes: 2 additions & 2 deletions tests/test22
Expand Up @@ -25,9 +25,9 @@ echo $dashes

cd rundir

../../../par2 c -r2 "$faraway"/test.par2 "$faraway"/*.data > ../$testname.log || { echo "ERROR: create of PAR 2.0 files failed" ; exit 1; } >&2
../../../par2 c -r2 -B"$faraway" "$faraway"/test.par2 "$faraway"/*.data > ../$testname.log || { echo "ERROR: create of PAR 2.0 files failed" ; exit 1; } >&2

cd ..
cd ../..
rm -rf run$testname

exit 0;
Expand Down
4 changes: 2 additions & 2 deletions tests/test23
Expand Up @@ -26,9 +26,9 @@ echo $dashes

cd rundir

../../../par2 v "$faraway"/testdata.par2 > ../$testname.log || { echo "ERROR: verify of PAR 2.0 files failed" ; exit 1; } >&2
../../../par2 v -B "$faraway" "$faraway"/testdata.par2 > ../$testname.log || { echo "ERROR: verify of PAR 2.0 files failed" ; exit 1; } >&2

cd ..
cd ../..
rm -rf run$testname

exit 0;
Expand Down
4 changes: 2 additions & 2 deletions tests/test24
Expand Up @@ -38,10 +38,10 @@ echo $dashes
cd rundir

rm -f "$faraway"/test-1.data "$faraway"/test-3.data
../../../par2 r "$faraway"/testdata.par2 > ../$testname.log || { echo "ERROR: repair of PAR 2.0 files failed" ; exit 1; } >&2
../../../par2 r -B"$faraway" "$faraway"/testdata.par2 > ../$testname.log || { echo "ERROR: repair of PAR 2.0 files failed" ; exit 1; } >&2
cmp -s "$faraway"/test-1.data "$faraway"/test-1.data.orig && cmp -s "$faraway"/test-3.data "$faraway"/test-3.data.orig || { echo "ERROR: Repaired files do not match originals" ; exit 1; } >&2

cd ..
cd ../..
rm -rf run$testname

exit 0;
Expand Down

0 comments on commit 19dd496

Please sign in to comment.