diff options
author | Daniel Baumann <mail@daniel-baumann.ch> | 2016-05-20 06:54:39 +0000 |
---|---|---|
committer | Daniel Baumann <mail@daniel-baumann.ch> | 2016-05-20 06:55:18 +0000 |
commit | 07c2a71a11edd637f0ec9b42b5c5621980c96562 (patch) | |
tree | ffe015da89db655e2f4edbabd6924e41b3971f02 /main.cc | |
parent | Adding debian version 1.18~pre2-1. (diff) | |
download | lziprecover-07c2a71a11edd637f0ec9b42b5c5621980c96562.tar.xz lziprecover-07c2a71a11edd637f0ec9b42b5c5621980c96562.zip |
Merging upstream version 1.18.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'main.cc')
-rw-r--r-- | main.cc | 282 |
1 files changed, 170 insertions, 112 deletions
@@ -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 @@ -66,12 +66,14 @@ #error "Environments where CHAR_BIT != 8 are not supported." #endif +std::string output_filename; // global vars for output file +int outfd = -1; namespace { const char * const Program_name = "Lziprecover"; const char * const program_name = "lziprecover"; -const char * const program_year = "2015"; +const char * const program_year = "2016"; const char * invocation_name = 0; struct { const char * from; const char * to; } const known_extensions[] = { @@ -79,15 +81,11 @@ struct { const char * from; const char * to; } const known_extensions[] = { { ".tlz", ".tar" }, { 0, 0 } }; -enum Mode { m_none, m_debug_delay, m_debug_repair, m_decompress, m_list, - m_merge, m_range_dec, m_repair, m_show_packets, m_split, m_test }; +enum Mode { m_none, m_alone_to_lz, m_debug_decompress, m_debug_delay, + m_debug_repair, m_decompress, m_list, m_merge, m_range_dec, + m_repair, m_show_packets, m_split, m_test }; -std::string output_filename; -int outfd = -1; int verbosity = 0; -const mode_t usr_rw = S_IRUSR | S_IWUSR; -const mode_t all_rw = usr_rw | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; -mode_t outfd_mode = usr_rw; bool delete_output_on_interrupt = false; @@ -98,15 +96,18 @@ void show_help() "single-byte error per member), without the need of any extra redundance\n" "at all. Losing an entire archive just because of a corrupt byte near the\n" "beginning is a thing of the past.\n" - "Lziprecover can also produce a correct file by merging the good parts of\n" + "\nLziprecover can also produce a correct file by merging the good parts of\n" "two or more damaged copies, extract data from damaged files, decompress\n" "files and test integrity of files.\n" + "\nLziprecover is not a replacement for regular backups, but a last line of\n" + "defense for the case where the backups are also damaged.\n" "\nUsage: %s [options] [files]\n", invocation_name ); std::printf( "\nOptions:\n" " -h, --help display this help and exit\n" " -V, --version output version information and exit\n" " -a, --trailing-error exit with error status if trailing data\n" - " -c, --stdout send decompressed output to standard output\n" + " -A, --alone-to-lz convert lzma-alone files to lzip format\n" + " -c, --stdout write to standard output, keep input files\n" " -d, --decompress decompress\n" " -D, --range-decompress=<range> decompress a range of bytes (N-M) to stdout\n" " -f, --force overwrite existing output files\n" @@ -117,16 +118,19 @@ void show_help() " -o, --output=<file> place the output into <file>\n" " -q, --quiet suppress all messages\n" " -R, --repair try to repair a small error in file\n" - " -s, --split split multi-member file in single-member files\n" + " -s, --split split multimember file in single-member files\n" " -t, --test test compressed file integrity\n" " -v, --verbose be verbose (a 2nd -v gives more)\n" ); if( verbosity >= 1 ) { - std::printf( " -x, --show-packets[=<pos>,<val>] show in stdout the decoded LZMA packets\n" - " -y, --debug-delay=<range> find max error detection delay in <range>\n" - " -z, --debug-repair=<pos>,<val> test repair one-byte error at <pos>\n" ); + std::printf( " -W, --debug-decompress=<pos>,<val> set pos to val and decompress to stdout\n" + " -X, --show-packets[=<pos>,<val>] show in stdout the decoded LZMA packets\n" + " -Y, --debug-delay=<range> find max error detection delay in <range>\n" + " -Z, --debug-repair=<pos>,<val> test repair one-byte error at <pos>\n" ); } - std::printf( "Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n" + std::printf( "If no file names are given, or if a file is '-', lziprecover decompresses\n" + "from standard input to standard output.\n" + "Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n" "Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n" "\nExit status: 0 for a normal exit, 1 for environmental problems (file\n" "not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or\n" @@ -211,14 +215,15 @@ int parse_long_long( const char * const ptr, long long & value ) } -// Recognized formats: <begin> <begin>-<end> <begin>,<size> +// Recognized formats: <begin> <begin>-<end> <begin>,<size> ,<size> // void parse_range( const char * const ptr, Block & range ) { long long value = 0; - int c = parse_long_long( ptr, value ); // pos - if( c && value >= 0 && value < INT64_MAX && - ( ptr[c] == 0 || ptr[c] == ',' || ptr[c] == '-' ) ) + const bool size_only = ( ptr[0] == ',' ); + int c = size_only ? 0 : parse_long_long( ptr, value ); // pos + if( size_only || ( c && value >= 0 && value < INT64_MAX && + ( ptr[c] == 0 || ptr[c] == ',' || ptr[c] == '-' ) ) ) { range.pos( value ); if( ptr[c] == 0 ) { range.size( INT64_MAX - value ); return; } @@ -321,6 +326,18 @@ int open_instream( const char * const name, struct stat * const in_statsp, namespace { +void set_a_outname( const std::string & name ) + { + output_filename = name; + if( name.size() > 5 && name.compare( name.size() - 5, 5, ".lzma" ) == 0 ) + output_filename.erase( name.size() - 2 ); + else if( name.size() > 4 && name.compare( name.size() - 4, 4, ".tlz" ) == 0 ) + output_filename.insert( name.size() - 2, "ar." ); + else if( name.size() <= 3 || name.compare( name.size() - 3, 3, ".lz" ) != 0 ) + output_filename += known_extensions[0].from; + } + + void set_d_outname( const std::string & name, const int i ) { if( i >= 0 ) @@ -339,18 +356,25 @@ void set_d_outname( const std::string & name, const int i ) program_name, name.c_str(), output_filename.c_str() ); } +} // end namespace -bool open_outstream( const bool force ) +bool open_outstream( const bool force, const bool from_stdin, + const bool rw, const bool skipping ) { - int flags = O_CREAT | O_WRONLY | O_BINARY; + const mode_t usr_rw = S_IRUSR | S_IWUSR; + const mode_t all_rw = usr_rw | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; + const mode_t outfd_mode = from_stdin ? all_rw : usr_rw; + int flags = O_CREAT | ( rw ? O_RDWR : O_WRONLY ) | O_BINARY; if( force ) flags |= O_TRUNC; else flags |= O_EXCL; outfd = open( output_filename.c_str(), flags, outfd_mode ); - if( outfd < 0 && verbosity >= 0 ) + if( outfd >= 0 ) delete_output_on_interrupt = true; + else if( verbosity >= 0 ) { if( errno == EEXIST ) - std::fprintf( stderr, "%s: Output file '%s' already exists, skipping.\n", - program_name, output_filename.c_str() ); + std::fprintf( stderr, "%s: Output file '%s' already exists%s.\n", + program_name, output_filename.c_str(), skipping ? + ", skipping" : ". Use '--force' to overwrite it" ); else std::fprintf( stderr, "%s: Can't create output file '%s': %s\n", program_name, output_filename.c_str(), std::strerror( errno ) ); @@ -359,6 +383,37 @@ bool open_outstream( const bool force ) } +bool file_exists( const std::string & filename ) + { + struct stat st; + if( stat( filename.c_str(), &st ) == 0 ) + { + if( verbosity >= 0 ) + std::fprintf( stderr, "%s: Output file '%s' already exists." + " Use '--force' to overwrite it.\n", + program_name, filename.c_str() ); + return true; + } + return false; + } + + +bool check_tty( const int infd, const Mode program_mode ) + { + if( program_mode == m_alone_to_lz && isatty( outfd ) ) + { + show_error( "I won't write compressed data to a terminal.", 0, true ); + return false; + } + if( isatty( infd ) ) // all modes read compressed data + { + show_error( "I won't read compressed data from a terminal.", 0, true ); + return false; + } + return true; + } + + void cleanup_and_fail( const int retval ) { if( delete_output_on_interrupt ) @@ -374,6 +429,7 @@ void cleanup_and_fail( const int retval ) std::exit( retval ); } +namespace { // Set permissions, owner and times. void close_and_set_permissions( const struct stat * const in_statsp ) @@ -390,7 +446,11 @@ void close_and_set_permissions( const struct stat * const in_statsp ) fchmod( outfd, mode & ~( S_ISUID | S_ISGID | S_ISVTX ) ) != 0 ) warning = true; } - if( close( outfd ) != 0 ) cleanup_and_fail( 1 ); + if( close( outfd ) != 0 ) + { + show_error( "Error closing output file", errno ); + cleanup_and_fail( 1 ); + } outfd = -1; delete_output_on_interrupt = false; if( in_statsp ) @@ -405,19 +465,6 @@ void close_and_set_permissions( const struct stat * const in_statsp ) } -std::string insert_fixed( std::string name ) - { - if( name.size() > 7 && name.compare( name.size() - 7, 7, ".tar.lz" ) == 0 ) - name.insert( name.size() - 7, "_fixed" ); - else if( name.size() > 3 && name.compare( name.size() - 3, 3, ".lz" ) == 0 ) - name.insert( name.size() - 3, "_fixed" ); - else if( name.size() > 4 && name.compare( name.size() - 4, 4, ".tlz" ) == 0 ) - name.insert( name.size() - 4, "_fixed" ); - else name += "_fixed.lz"; - return name; - } - - unsigned char xdigit( const int value ) { if( value >= 0 && value <= 9 ) return '0' + value; @@ -475,7 +522,7 @@ int decompress( const int infd, const Pretty_print & pp, const int size = rdec.read_data( header.data, File_header::size ); if( rdec.finished() ) // End Of File { - if( first_member ) + if( first_member || header.verify_prefix( size ) ) { pp( "File ends unexpectedly at member header." ); retval = 2; } else if( size > 0 && !show_trailing_data( header.data, size, pp, true, ignore_trailing ) ) @@ -499,14 +546,13 @@ int decompress( const int infd, const Pretty_print & pp, retval = 2; break; } const unsigned dictionary_size = header.dictionary_size(); - if( dictionary_size < min_dictionary_size || - dictionary_size > max_dictionary_size ) + if( !isvalid_ds( dictionary_size ) ) { pp( "Invalid dictionary size in member header." ); retval = 2; break; } if( verbosity >= 2 || ( verbosity == 1 && first_member ) ) { pp(); show_header( dictionary_size ); } - LZ_decoder decoder( header, rdec, outfd ); + LZ_decoder decoder( rdec, dictionary_size, outfd ); const int result = decoder.decode_member( pp ); partial_file_pos += rdec.member_position(); if( result != 0 ) @@ -549,56 +595,42 @@ void set_signals() } // end namespace -bool file_exists( const std::string & filename ) +int close_outstream( const struct stat * const in_statsp ) { - struct stat st; - if( stat( filename.c_str(), &st ) == 0 ) - { - if( verbosity >= 0 ) - std::fprintf( stderr, "%s: Output file '%s' already exists." - " Use '--force' to overwrite it.\n", - program_name, filename.c_str() ); - return true; - } - return false; + if( delete_output_on_interrupt ) + close_and_set_permissions( in_statsp ); + if( outfd >= 0 && close( outfd ) != 0 ) + { show_error( "Can't close stdout", errno ); return 1; } + outfd = -1; + return 0; } -int open_outstream_rw( const std::string & output_filename, const bool force ) +std::string insert_fixed( std::string name ) { - int flags = O_CREAT | O_RDWR | O_BINARY; - if( force ) flags |= O_TRUNC; else flags |= O_EXCL; - - int outfd = open( output_filename.c_str(), flags, all_rw ); - if( outfd < 0 && verbosity >= 0 ) - { - if( errno == EEXIST ) - std::fprintf( stderr, "%s: Output file '%s' already exists." - " Use '--force' to overwrite it.\n", - program_name, output_filename.c_str() ); - else - std::fprintf( stderr, "%s: Can't create output file '%s': %s\n", - program_name, output_filename.c_str(), std::strerror( errno ) ); - } - return outfd; + if( name.size() > 7 && name.compare( name.size() - 7, 7, ".tar.lz" ) == 0 ) + name.insert( name.size() - 7, "_fixed" ); + else if( name.size() > 3 && name.compare( name.size() - 3, 3, ".lz" ) == 0 ) + name.insert( name.size() - 3, "_fixed" ); + else if( name.size() > 4 && name.compare( name.size() - 4, 4, ".tlz" ) == 0 ) + name.insert( name.size() - 4, "_fixed" ); + else name += "_fixed.lz"; + return name; } void show_error( const char * const msg, const int errcode, const bool help ) { - if( verbosity >= 0 ) + if( verbosity < 0 ) return; + if( msg && msg[0] ) { - if( msg && msg[0] ) - { - std::fprintf( stderr, "%s: %s", program_name, msg ); - if( errcode > 0 ) - std::fprintf( stderr, ": %s", std::strerror( errcode ) ); - std::fputc( '\n', stderr ); - } - if( help ) - std::fprintf( stderr, "Try '%s --help' for more information.\n", - invocation_name ); + std::fprintf( stderr, "%s: %s", program_name, msg ); + if( errcode > 0 ) std::fprintf( stderr, ": %s", std::strerror( errcode ) ); + std::fputc( '\n', stderr ); } + if( help ) + std::fprintf( stderr, "Try '%s --help' for more information.\n", + invocation_name ); } @@ -618,6 +650,15 @@ void show_error2( const char * const msg1, const char * const name, } +void show_error4( const char * const msg1, const char * const name1, + const char * const name2, const char * const msg2 ) + { + if( verbosity >= 0 ) + std::fprintf( stderr, "%s: %s '%s' and '%s' %s\n", + program_name, msg1, name1, name2, msg2 ); + } + + int main( const int argc, const char * const argv[] ) { Block range( 0, 0 ); @@ -638,6 +679,7 @@ int main( const int argc, const char * const argv[] ) const Arg_parser::Option options[] = { { 'a', "trailing-error", Arg_parser::no }, + { 'A', "alone-to-lz", Arg_parser::no }, { 'c', "stdout", Arg_parser::no }, { 'd', "decompress", Arg_parser::no }, { 'D', "range-decompress", Arg_parser::yes }, @@ -655,10 +697,11 @@ int main( const int argc, const char * const argv[] ) { 't', "test", Arg_parser::no }, { 'v', "verbose", Arg_parser::no }, { 'V', "version", Arg_parser::no }, - { 'x', "show-packets", Arg_parser::maybe }, - { 'y', "debug-delay", Arg_parser::yes }, - { 'z', "debug-repair", Arg_parser::yes }, - { 0 , 0, Arg_parser::no } }; + { 'W', "debug-decompress", Arg_parser::yes }, + { 'X', "show-packets", Arg_parser::maybe }, + { 'Y', "debug-delay", Arg_parser::yes }, + { 'Z', "debug-repair", Arg_parser::yes }, + { 0 , 0, Arg_parser::no } }; const Arg_parser parser( argc, argv, options ); if( parser.error().size() ) // bad option @@ -670,13 +713,15 @@ int main( const int argc, const char * const argv[] ) const int code = parser.code( argind ); if( !code ) break; // no more options const std::string & arg = parser.argument( argind ); + const char * const ptr = arg.c_str(); switch( code ) { case 'a': ignore_trailing = false; break; + case 'A': set_mode( program_mode, m_alone_to_lz ); break; case 'c': to_stdout = true; break; case 'd': set_mode( program_mode, m_decompress ); break; case 'D': set_mode( program_mode, m_range_dec ); - parse_range( arg.c_str(), range ); break; + parse_range( ptr, range ); break; case 'f': force = true; break; case 'h': show_help(); return 0; case 'i': ignore_errors = true; break; @@ -691,13 +736,14 @@ int main( const int argc, const char * const argv[] ) case 't': set_mode( program_mode, m_test ); break; case 'v': if( verbosity < 4 ) ++verbosity; break; case 'V': show_version(); return 0; - case 'x': set_mode( program_mode, m_show_packets ); - if( arg.size() ) - parse_pos_value( arg.c_str(), bad_pos, bad_value ); break; - case 'y': set_mode( program_mode, m_debug_delay ); - parse_range( arg.c_str(), range ); break; - case 'z': set_mode( program_mode, m_debug_repair ); - parse_pos_value( arg.c_str(), bad_pos, bad_value ); break; + case 'W': set_mode( program_mode, m_debug_decompress ); + parse_pos_value( ptr, bad_pos, bad_value ); break; + case 'X': set_mode( program_mode, m_show_packets ); + if( ptr[0] ) parse_pos_value( ptr, bad_pos, bad_value ); break; + case 'Y': set_mode( program_mode, m_debug_delay ); + parse_range( ptr, range ); break; + case 'Z': set_mode( program_mode, m_debug_repair ); + parse_pos_value( ptr, bad_pos, bad_value ); break; default : internal_error( "uncaught option." ); } } // end process options @@ -724,6 +770,10 @@ int main( const int argc, const char * const argv[] ) switch( program_mode ) { case m_none: internal_error( "invalid operation." ); break; + case m_alone_to_lz: break; + case m_debug_decompress: + one_file( filenames.size() ); + return debug_decompress( filenames[0], bad_pos, verbosity, bad_value, false ); case m_debug_delay: one_file( filenames.size() ); return debug_delay( filenames[0], range, verbosity ); @@ -738,34 +788,35 @@ int main( const int argc, const char * const argv[] ) case m_merge: if( filenames.size() < 2 ) { show_error( "You must specify at least 2 files.", 0, true ); return 1; } - if( default_output_filename.empty() ) - default_output_filename = insert_fixed( filenames[0] ); + set_signals(); return merge_files( filenames, default_output_filename, verbosity, force ); case m_range_dec: one_file( filenames.size() ); + set_signals(); return range_decompress( filenames[0], default_output_filename, range, verbosity, force, ignore_errors, to_stdout ); case m_repair: one_file( filenames.size() ); - if( default_output_filename.empty() ) - default_output_filename = insert_fixed( filenames[0] ); + set_signals(); return repair_file( filenames[0], default_output_filename, verbosity, force ); case m_show_packets: one_file( filenames.size() ); - return debug_show_packets( filenames[0], bad_pos, verbosity, bad_value ); + return debug_decompress( filenames[0], bad_pos, verbosity, bad_value, true ); case m_split: one_file( filenames.size() ); + set_signals(); return split_file( filenames[0], default_output_filename, verbosity, force ); case m_test: break; } } - catch( std::bad_alloc ) { show_error( "Not enough memory." ); return 1; } - catch( Error e ) { show_error( e.msg, errno ); return 1; } + catch( std::bad_alloc ) + { show_error( "Not enough memory." ); cleanup_and_fail( 1 ); } + catch( Error e ) { show_error( e.msg, errno ); cleanup_and_fail( 1 ); } if( program_mode == m_test ) outfd = -1; - else if( program_mode != m_decompress ) + else if( program_mode != m_alone_to_lz && program_mode != m_decompress ) internal_error( "invalid decompressor operation." ); if( filenames.empty() ) filenames.push_back("-"); @@ -776,6 +827,7 @@ int main( const int argc, const char * const argv[] ) Pretty_print pp( filenames, verbosity ); int retval = 0; + bool stdin_used = false; for( unsigned i = 0; i < filenames.size(); ++i ) { struct stat in_stats; @@ -783,6 +835,7 @@ int main( const int argc, const char * const argv[] ) if( filenames[i].empty() || filenames[i] == "-" ) { + if( stdin_used ) continue; else stdin_used = true; input_filename.clear(); infd = STDIN_FILENO; if( program_mode != m_test ) @@ -792,8 +845,10 @@ int main( const int argc, const char * const argv[] ) else { output_filename = default_output_filename; - outfd_mode = all_rw; - if( !open_outstream( force ) ) + if( program_mode == m_alone_to_lz && + extension_index( default_output_filename ) < 0 ) + output_filename += known_extensions[0].from; + if( !open_outstream( force, true ) ) { if( retval < 1 ) retval = 1; close( infd ); infd = -1; @@ -813,9 +868,10 @@ int main( const int argc, const char * const argv[] ) if( to_stdout ) outfd = STDOUT_FILENO; else { - set_d_outname( input_filename, extension_index( input_filename ) ); - outfd_mode = usr_rw; - if( !open_outstream( force ) ) + if( program_mode == m_alone_to_lz ) + set_a_outname( input_filename ); + else set_d_outname( input_filename, extension_index( input_filename ) ); + if( !open_outstream( force, false ) ) { if( retval < 1 ) retval = 1; close( infd ); infd = -1; @@ -825,17 +881,19 @@ int main( const int argc, const char * const argv[] ) } } - if( isatty( infd ) ) + if( !check_tty( infd, program_mode ) ) { - show_error( "I won't read compressed data from a terminal.", 0, true ); - return 1; + if( retval < 1 ) retval = 1; + cleanup_and_fail( retval ); } - if( output_filename.size() && !to_stdout && program_mode != m_test ) - delete_output_on_interrupt = true; const struct stat * const in_statsp = input_filename.size() ? &in_stats : 0; pp.set_name( input_filename ); - const int tmp = decompress( infd, pp, ignore_trailing, program_mode == m_test ); + int tmp; + if( program_mode == m_alone_to_lz ) + tmp = alone_to_lz( infd, pp ); + else + tmp = decompress( infd, pp, ignore_trailing, program_mode == m_test ); if( tmp > retval ) retval = tmp; if( tmp && program_mode != m_test ) cleanup_and_fail( retval ); |