summaryrefslogtreecommitdiffstats
path: root/merge.cc
diff options
context:
space:
mode:
Diffstat (limited to 'merge.cc')
-rw-r--r--merge.cc166
1 files changed, 89 insertions, 77 deletions
diff --git a/merge.cc b/merge.cc
index 75faf07..2f1afdc 100644
--- a/merge.cc
+++ b/merge.cc
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2015 Antonio Diaz Diaz.
+ Copyright (C) 2009-2016 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -17,6 +17,7 @@
#define _FILE_OFFSET_BITS 64
+#include <algorithm>
#include <cerrno>
#include <climits>
#include <cstdio>
@@ -36,6 +37,28 @@
namespace {
+bool file_crc( uint32_t & crc, const int infd )
+ {
+ const int buffer_size = 65536;
+ crc = 0xFFFFFFFFU;
+ uint8_t * const buffer = new uint8_t[buffer_size];
+ bool error = false;
+
+ while( true )
+ {
+ const int rd = readblock( infd, buffer, buffer_size );
+ if( rd != buffer_size && errno )
+ { show_error( "Error reading input file", errno ); error = true; break; }
+ if( rd > 0 )
+ crc32.update_buf( crc, buffer, rd );
+ if( rd < buffer_size ) break; // EOF
+ }
+ delete[] buffer;
+ crc ^= 0xFFFFFFFFU;
+ return !error;
+ }
+
+
// Add 'bv' to 'block_vector' splitting blocks as needed to keep all the
// edges (pos and end of every block).
// 'block_vector' contains the result. 'bv' is destroyed.
@@ -171,37 +194,43 @@ long ipow( const unsigned base, const unsigned exponent )
int open_input_files( const std::vector< std::string > & filenames,
std::vector< int > & infd_vector,
- File_index & file_index, const int verbosity )
+ File_index & file_index, struct stat * const in_statsp,
+ const int verbosity )
{
const int files = filenames.size();
- bool identical = false;
- for( int i = 1; i < files; ++i )
- if( filenames[0] == filenames[i] )
- { identical = true; break; }
- if( !identical )
- for( int i = 0; i < files; ++i )
- {
- struct stat in_stats;
- ino_t st_ino0 = 0;
- dev_t st_dev0 = 0;
- infd_vector[i] = open_instream( filenames[i].c_str(), &in_stats, true, true );
- if( infd_vector[i] < 0 ) return 1;
- if( i == 0 ) { st_ino0 = in_stats.st_ino; st_dev0 = in_stats.st_dev; }
- else if( st_ino0 == in_stats.st_ino && st_dev0 == in_stats.st_dev )
- { identical = true; break; }
- }
- if( identical ) { show_error( "Two input files are the same." ); return 2; }
+ for( int i = 0; i + 1 < files; ++i )
+ for( int j = i + 1; j < files; ++j )
+ if( filenames[i] == filenames[j] )
+ { show_error2( "Input file", filenames[i].c_str(), "given twice." );
+ return 2; }
+ {
+ std::vector< uint32_t > crc_vector( files );
+ for( int i = 0; i < files; ++i )
+ {
+ struct stat in_stats; // not used
+ infd_vector[i] = open_instream( filenames[i].c_str(),
+ ( i == 0 ) ? in_statsp : &in_stats, true, true );
+ if( infd_vector[i] < 0 ) return 1;
+ if( !file_crc( crc_vector[i], infd_vector[i] ) ) return 1;
+ for( int j = 0; j < i; ++j )
+ if( crc_vector[i] == crc_vector[j] )
+ { show_error4( "Input files", filenames[j].c_str(),
+ filenames[i].c_str(), "are identical." ); return 2; }
+ }
+ }
long long isize = 0;
+ int good_fi = -1;
for( int i = 0; i < files; ++i )
{
long long tmp;
const File_index fi( infd_vector[i] );
if( fi.retval() == 0 ) // file format is intact
{
- if( file_index.retval() != 0 ) file_index = fi;
+ if( good_fi < 0 ) { good_fi = i; file_index = fi; }
else if( file_index != fi )
- { show_error( "Input files are different." ); return 2; }
+ { show_error4( "Input files", filenames[good_fi].c_str(),
+ filenames[i].c_str(), "are different." ); return 2; }
tmp = file_index.file_size();
}
else // file format is damaged
@@ -213,15 +242,13 @@ int open_input_files( const std::vector< std::string > & filenames,
return 1;
}
}
- if( i == 0 )
- {
- isize = tmp;
- if( isize < min_member_size )
- { show_error2( "Input file", filenames[i].c_str(), "is too short." );
- return 2; }
- }
+ if( tmp < min_member_size )
+ { show_error2( "Input file", filenames[i].c_str(), "is too short." );
+ return 2; }
+ if( i == 0 ) isize = tmp;
else if( isize != tmp )
- { show_error( "Sizes of input files are different." ); return 2; }
+ { show_error4( "Sizes of input files", filenames[0].c_str(),
+ filenames[i].c_str(), "are different." ); return 2; }
}
if( file_index.retval() != 0 )
@@ -242,7 +269,7 @@ int open_input_files( const std::vector< std::string > & filenames,
const long long mpos = file_index.mblock( j ).pos();
const long long msize = file_index.mblock( j ).size();
if( !safe_seek( infd, mpos ) ) return 1;
- if( !try_decompress_member( infd, msize ) ) { error = true; break; }
+ if( !test_member_from_file( infd, msize ) ) { error = true; break; }
}
if( !error )
{
@@ -261,8 +288,7 @@ bool try_merge_member( const long long mpos, const long long msize,
const std::vector< Block > & block_vector,
const std::vector< int > & color_vector,
const std::vector< int > & infd_vector,
- const std::string & output_filename,
- const int outfd, const int verbosity )
+ const int verbosity )
{
const int blocks = block_vector.size();
const int files = infd_vector.size();
@@ -273,7 +299,7 @@ bool try_merge_member( const long long mpos, const long long msize,
show_error( "Too many damaged blocks. Try merging fewer files." );
else
show_error( "Too many damaged blocks. Merging is not possible." );
- cleanup_and_fail( output_filename, outfd, 2 );
+ cleanup_and_fail( 2 );
}
int bi = 0; // block index
std::vector< int > file_idx( blocks, 0 ); // file to read each block from
@@ -294,13 +320,13 @@ bool try_merge_member( const long long mpos, const long long msize,
if( !safe_seek( infd, block_vector[bi].pos() ) ||
!safe_seek( outfd, block_vector[bi].pos() ) ||
!copy_file( infd, outfd, block_vector[bi].size() ) )
- cleanup_and_fail( output_filename, outfd, 1 );
+ cleanup_and_fail( 1 );
++bi;
}
if( !safe_seek( outfd, mpos ) )
- cleanup_and_fail( output_filename, outfd, 1 );
+ cleanup_and_fail( 1 );
long long failure_pos = 0;
- if( try_decompress_member( outfd, msize, &failure_pos ) ) return true;
+ if( test_member_from_file( outfd, msize, &failure_pos ) ) return true;
while( bi > 0 && mpos + failure_pos < block_vector[bi-1].pos() ) --bi;
while( --bi >= 0 )
{
@@ -325,8 +351,7 @@ bool try_merge_member1( const long long mpos, const long long msize,
const std::vector< Block > & block_vector,
const std::vector< int > & color_vector,
const std::vector< int > & infd_vector,
- const std::string & output_filename,
- const int outfd, const int verbosity )
+ const int verbosity )
{
if( block_vector.size() != 1 || block_vector[0].size() <= 1 ) return false;
const long long pos = block_vector[0].pos();
@@ -344,7 +369,7 @@ bool try_merge_member1( const long long mpos, const long long msize,
!safe_seek( infd_vector[i2], pos ) ||
!safe_seek( outfd, pos ) ||
!copy_file( infd_vector[i2], outfd, size ) )
- cleanup_and_fail( output_filename, outfd, 1 );
+ cleanup_and_fail( 1 );
const int var = ( i1 * ( files - 1 ) ) + i2 - ( i2 > i1 ) + 1;
for( long long i = 0; i < size; ++i )
{
@@ -358,9 +383,9 @@ bool try_merge_member1( const long long mpos, const long long msize,
readblock( infd, &byte, 1 ) != 1 ||
writeblock( outfd, &byte, 1 ) != 1 ||
!safe_seek( outfd, mpos ) )
- cleanup_and_fail( output_filename, outfd, 1 );
+ cleanup_and_fail( 1 );
long long failure_pos = 0;
- if( try_decompress_member( outfd, msize, &failure_pos ) ) return true;
+ if( test_member_from_file( outfd, msize, &failure_pos ) ) return true;
if( mpos + failure_pos <= pos + i ) break;
}
}
@@ -370,16 +395,6 @@ bool try_merge_member1( const long long mpos, const long long msize,
} // end namespace
-void cleanup_and_fail( const std::string & output_filename,
- const int outfd, const int retval )
- {
- if( outfd >= 0 ) close( outfd );
- if( std::remove( output_filename.c_str() ) != 0 && errno != ENOENT )
- show_error( "WARNING: deletion of output file (apparently) failed." );
- std::exit( retval );
- }
-
-
// max_size < 0 means no size limit.
bool copy_file( const int infd, const int outfd, const long long max_size )
{
@@ -410,44 +425,45 @@ bool copy_file( const int infd, const int outfd, const long long max_size )
}
-bool try_decompress_member( const int fd, const unsigned long long msize,
- long long * failure_posp )
+bool test_member_from_file( const int infd, const unsigned long long msize,
+ long long * const failure_posp )
{
- Range_decoder rdec( fd );
+ Range_decoder rdec( infd );
File_header header;
rdec.read_data( header.data, File_header::size );
- if( !rdec.finished() && // End Of File
- header.verify_magic() && header.verify_version() &&
- header.dictionary_size() >= min_dictionary_size &&
- header.dictionary_size() <= max_dictionary_size )
+ const unsigned dictionary_size = header.dictionary_size();
+ if( !rdec.finished() && header.verify_magic() &&
+ header.verify_version() && isvalid_ds( dictionary_size ) )
{
- LZ_decoder decoder( header, rdec, -1 );
+ LZ_decoder decoder( rdec, dictionary_size, -1 );
Pretty_print dummy( "", -1 );
if( decoder.decode_member( dummy ) == 0 &&
rdec.member_position() == msize ) return true;
- if( failure_posp ) *failure_posp = rdec.member_position();
}
+ if( failure_posp ) *failure_posp = rdec.member_position();
return false;
}
int merge_files( const std::vector< std::string > & filenames,
- const std::string & output_filename, const int verbosity,
- const bool force )
+ const std::string & default_output_filename,
+ const int verbosity, const bool force )
{
const int files = filenames.size();
std::vector< int > infd_vector( files );
File_index file_index;
+ struct stat in_stats;
const int retval =
- open_input_files( filenames, infd_vector, file_index, verbosity );
+ open_input_files( filenames, infd_vector, file_index, &in_stats, verbosity );
if( retval >= 0 ) return retval;
if( !safe_seek( infd_vector[0], 0 ) ) return 1;
- const int outfd = open_outstream_rw( output_filename, force );
- if( outfd < 0 ) return 1;
+ output_filename = default_output_filename.empty() ?
+ insert_fixed( filenames[0] ) : default_output_filename;
+ if( !open_outstream( force, false, true, false ) ) return 1;
if( !copy_file( infd_vector[0], outfd ) ) // copy whole file
- cleanup_and_fail( output_filename, outfd, 1 );
+ cleanup_and_fail( 1 );
for( long j = 0; j < file_index.members(); ++j )
{
@@ -458,14 +474,14 @@ int merge_files( const std::vector< std::string > & filenames,
std::vector< int > color_vector( files, 0 );
if( !diff_member( mpos, msize, infd_vector, block_vector, color_vector ) ||
!safe_seek( outfd, mpos ) )
- cleanup_and_fail( output_filename, outfd, 1 );
+ cleanup_and_fail( 1 );
if( block_vector.empty() )
{
- if( file_index.members() > 1 && try_decompress_member( outfd, msize ) )
+ if( file_index.members() > 1 && test_member_from_file( outfd, msize ) )
continue;
show_error( "Input files are (partially) identical. Merging is not possible." );
- cleanup_and_fail( output_filename, outfd, 2 );
+ cleanup_and_fail( 2 );
}
if( verbosity >= 1 && file_index.members() > 1 )
@@ -479,12 +495,12 @@ int merge_files( const std::vector< std::string > & filenames,
if( file_index.members() > 1 || block_vector.size() > 1 )
{
done = try_merge_member( mpos, msize, block_vector, color_vector,
- infd_vector, output_filename, outfd, verbosity );
+ infd_vector, verbosity );
if( !done && verbosity >= 1 ) std::fputc( '\n', stdout );
}
if( !done )
done = try_merge_member1( mpos, msize, block_vector, color_vector,
- infd_vector, output_filename, outfd, verbosity );
+ infd_vector, verbosity );
if( verbosity >= 1 ) std::fputc( '\n', stdout );
if( !done )
{
@@ -493,15 +509,11 @@ int merge_files( const std::vector< std::string > & filenames,
std::fprintf( stderr, "area %2d from position %6lld to %6lld\n", i + 1,
block_vector[i].pos(), block_vector[i].end() - 1 );
show_error( "Some error areas overlap. Can't recover input file." );
- cleanup_and_fail( output_filename, outfd, 2 );
+ cleanup_and_fail( 2 );
}
}
- if( close( outfd ) != 0 )
- {
- show_error( "Error closing output file", errno );
- cleanup_and_fail( output_filename, -1, 1 );
- }
+ if( close_outstream( &in_stats ) != 0 ) return 1;
if( verbosity >= 1 )
std::fputs( "Input files merged successfully.\n", stdout );
return 0;