summaryrefslogtreecommitdiffstats
path: root/repair.cc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--repair.cc115
1 files changed, 64 insertions, 51 deletions
diff --git a/repair.cc b/repair.cc
index e9cef61..712002b 100644
--- a/repair.cc
+++ b/repair.cc
@@ -28,6 +28,7 @@
#include <sys/stat.h>
#include "lzip.h"
+#include "file_index.h"
int seek_read( const int fd, uint8_t * const buf, const int size,
@@ -55,72 +56,84 @@ int repair_file( const std::string & input_filename,
struct stat in_stats;
const int infd = open_instream( input_filename, &in_stats, true, true );
if( infd < 0 ) return 1;
- const long long isize = lseek( infd, 0, SEEK_END );
- if( isize < 0 )
- { show_error( "Input file is not seekable", errno ); return 1; }
- if( isize < min_member_size )
- { show_error( "Input file is too short." ); return 2; }
- if( !verify_single_member( infd, isize, verbosity ) ) return 2;
- if( lseek( infd, 0, SEEK_SET ) < 0 )
- { show_error( "Seek error in input file", errno ); return 1; }
-
- long long failure_pos = 0;
- if( try_decompress( infd, isize, &failure_pos ) )
- {
- if( verbosity >= 1 )
- std::printf( "Input file has no errors. Recovery is not needed.\n" );
- return 0;
- }
- if( failure_pos >= isize - 8 ) failure_pos = isize - 8 - 1;
- if( failure_pos < File_header::size )
- { show_error( "Can't repair error in input file." ); return 2; }
-
- if( lseek( infd, 0, SEEK_SET ) < 0 )
- { show_error( "Seek error in input file", errno ); return 1; }
-
- const int outfd = open_outstream_rw( output_filename, force );
- if( outfd < 0 ) { close( infd ); return 1; }
- if( !copy_file( infd, outfd ) )
- cleanup_and_fail( output_filename, outfd, 1 );
-
- const long long min_pos =
- std::max( (long long)File_header::size, failure_pos - 1000 );
- bool done = false;
- for( long long pos = failure_pos; pos >= min_pos && !done ; --pos )
+
+ const File_index file_index( infd );
+ if( file_index.retval() != 0 )
+ { show_error( file_index.error().c_str() ); return file_index.retval(); }
+
+ int outfd = -1;
+ for( int i = 0; i < file_index.members(); ++i )
{
+ const long long mpos = file_index.mblock( i ).pos();
+ const long long msize = file_index.mblock( i ).size();
+ if( !safe_seek( infd, mpos ) )
+ cleanup_and_fail( output_filename, outfd, 1 );
+ long long failure_pos = 0;
+ if( try_decompress_member( infd, msize, &failure_pos ) ) continue;
+ if( failure_pos >= msize - 8 ) failure_pos = msize - 8 - 1;
+ if( failure_pos < File_header::size )
+ { show_error( "Can't repair error in input file." );
+ cleanup_and_fail( output_filename, outfd, 2 ); }
+
+ if( outfd < 0 ) // first damaged member found
+ {
+ if( !safe_seek( infd, 0 ) ) return 1;
+ outfd = open_outstream_rw( output_filename, force );
+ if( outfd < 0 ) { close( infd ); return 1; }
+ if( !copy_file( infd, outfd ) ) // copy whole file
+ cleanup_and_fail( output_filename, outfd, 1 );
+ }
+
if( verbosity >= 1 )
{
- std::printf( "Trying position %llu \r", pos );
+ std::printf( "Repairing member %d\n", i + 1 );
std::fflush( stdout );
}
- uint8_t byte;
- if( seek_read( outfd, &byte, 1, pos ) != 1 )
- { show_error( "Error reading output file", errno );
- cleanup_and_fail( output_filename, outfd, 1 ); }
- for( int i = 0; i < 256; ++i )
+ const long long min_pos =
+ std::max( (long long)File_header::size, failure_pos - 1000 );
+ bool done = false;
+ for( long long pos = failure_pos; pos >= min_pos && !done ; --pos )
{
- ++byte;
- if( seek_write( outfd, &byte, 1, pos ) != 1 ||
- lseek( outfd, 0, SEEK_SET ) < 0 )
- { show_error( "Error writing output file", errno );
+ if( verbosity >= 1 )
+ {
+ std::printf( "Trying position %llu \r", mpos + pos );
+ std::fflush( stdout );
+ }
+ uint8_t byte;
+ if( seek_read( outfd, &byte, 1, mpos + pos ) != 1 )
+ { show_error( "Error reading output file", errno );
cleanup_and_fail( output_filename, outfd, 1 ); }
- if( i == 255 ) break;
- if( try_decompress( outfd, isize ) )
- { done = true; break; }
+ for( int i = 0; i < 256; ++i )
+ {
+ ++byte;
+ if( seek_write( outfd, &byte, 1, mpos + pos ) != 1 ||
+ lseek( outfd, mpos, SEEK_SET ) < 0 )
+ { show_error( "Error writing output file", errno );
+ cleanup_and_fail( output_filename, outfd, 1 ); }
+ if( i == 255 ) break;
+ if( try_decompress_member( outfd, msize ) )
+ { done = true; break; }
+ }
+ }
+ if( verbosity >= 1 ) std::printf( "\n" );
+ if( !done )
+ {
+ show_error( "Error is larger than 1 byte. Can't repair input file." );
+ cleanup_and_fail( output_filename, outfd, 2 );
}
}
- if( verbosity >= 1 ) std::printf( "\n" );
+ if( outfd < 0 )
+ {
+ if( verbosity >= 1 )
+ std::printf( "Input file has no errors. Recovery is not needed.\n" );
+ return 0;
+ }
if( close( outfd ) != 0 )
{
show_error( "Error closing output file", errno );
cleanup_and_fail( output_filename, -1, 1 );
}
- if( !done )
- {
- show_error( "Error is larger than 1 byte. Can't repair input file." );
- cleanup_and_fail( output_filename, -1, 2 );
- }
if( verbosity >= 1 )
std::printf( "Copy of input file repaired successfully.\n" );
return 0;