diff options
Diffstat (limited to 'main.cc')
-rw-r--r-- | main.cc | 321 |
1 files changed, 166 insertions, 155 deletions
@@ -1,5 +1,5 @@ /* Lziprecover - Data recovery tool for the lzip format - Copyright (C) 2009-2022 Antonio Diaz Diaz. + Copyright (C) 2009-2023 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 @@ -16,9 +16,9 @@ */ /* Exit status: 0 for a normal exit, 1 for environmental problems - (file not found, invalid flags, I/O errors, etc), 2 to indicate a - corrupt or invalid input file, 3 for an internal consistency error - (e.g., bug) which caused lziprecover to panic. + (file not found, invalid command line options, I/O errors, etc), 2 to + indicate a corrupt or invalid input file, 3 for an internal consistency + error (e.g., bug) which caused lziprecover to panic. */ #define _FILE_OFFSET_BITS 64 @@ -26,7 +26,7 @@ #include <algorithm> #include <cctype> #include <cerrno> -#include <climits> +#include <climits> // SSIZE_MAX #include <csignal> #include <cstdio> #include <cstdlib> @@ -35,7 +35,7 @@ #include <string> #include <vector> #include <fcntl.h> -#include <stdint.h> +#include <stdint.h> // SIZE_MAX #include <unistd.h> #include <utime.h> #include <sys/stat.h> @@ -71,11 +71,15 @@ #error "Environments where CHAR_BIT != 8 are not supported." #endif -#if ( defined SIZE_MAX && SIZE_MAX < UINT_MAX ) || \ - ( defined SSIZE_MAX && SSIZE_MAX < INT_MAX ) -#error "Environments where 'size_t' is narrower than 'int' are not supported." +#if ( defined SIZE_MAX && SIZE_MAX < ULONG_MAX ) || \ + ( defined SSIZE_MAX && SSIZE_MAX < LONG_MAX ) +#error "Environments where 'size_t' is narrower than 'long' are not supported." #endif +bool fits_in_size_t( const unsigned long long size ) + { return ( sizeof (long) <= sizeof (size_t) && size <= LONG_MAX ) || + ( sizeof (int) <= sizeof (size_t) && size <= INT_MAX ); } + int verbosity = 0; const char * const program_name = "lziprecover"; @@ -91,11 +95,11 @@ const struct { const char * from; const char * to; } known_extensions[] = { { ".tlz", ".tar" }, { 0, 0 } }; -enum Mode { m_none, m_alone_to_lz, m_debug_decompress, m_debug_delay, - m_debug_repair, m_decompress, m_dump, m_list, m_md5sum, m_merge, - m_nrep_stats, m_range_dec, m_remove, m_repair, m_reproduce, - m_show_packets, m_split, m_strip, m_test, m_unzcrash_bit, - m_unzcrash_block }; +enum Mode { m_none, m_alone_to_lz, m_byte_repair, m_clear_marking, + m_debug_byte_repair, m_debug_decompress, m_debug_delay, + m_decompress, m_dump, m_list, m_md5sum, m_merge, m_nrep_stats, + m_range_dec, m_remove, m_reproduce, m_show_packets, m_split, + m_strip, m_test, m_unzcrash_bit, m_unzcrash_block }; /* Variable used in signal handler context. It is not declared volatile because the handler never returns. */ @@ -127,7 +131,7 @@ void show_help() " -a, --trailing-error exit with error status if trailing data\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, --decompress decompress, test compressed file integrity\n" " -D, --range-decompress=<n-m> decompress a range of bytes to stdout\n" " -e, --reproduce try to reproduce a zeroed sector in file\n" " --lzip-level=N|a|m[N] reproduce one level, all, or match length\n" @@ -140,14 +144,17 @@ void show_help() " -m, --merge correct errors in file using several copies\n" " -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" + " -R, --byte-repair try to repair a corrupt byte in file\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" + " --dump=<list>:d:e:t dump members, damaged/empty, tdata to stdout\n" + " --remove=<list>:d:e:t remove members, tdata from files in place\n" + " --strip=<list>:d:e:t copy files to stdout stripping members given\n" + " --empty-error exit with error status if empty member in file\n" + " --marking-error exit with error status if 1st LZMA byte not 0\n" " --loose-trailing allow trailing data seeming corrupt header\n" - " --dump=<list>:d:t dump members listed/damaged, tdata to stdout\n" - " --remove=<list>:d:t remove members, tdata from files in place\n" - " --strip=<list>:d:t copy files to stdout stripping members given\n" ); + " --clear-marking reset the first LZMA byte of each member\n" ); if( verbosity >= 1 ) { std::printf( "\nDebug options for experts:\n" @@ -158,7 +165,7 @@ void show_help() " -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" ); + " -Z, --debug-byte-repair=<pos>,<val> test repair one-byte error at <pos>\n" ); } std::printf( "\nIf no file names are given, or if a file is '-', lziprecover decompresses\n" "from standard input to standard output.\n" @@ -166,10 +173,10 @@ void show_help() "Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n" "\nTo extract all the files from archive 'foo.tar.lz', use the commands\n" "'tar -xf foo.tar.lz' or 'lziprecover -cd foo.tar.lz | tar -xf -'.\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" - "invalid input file, 3 for an internal consistency error (e.g., bug) which\n" - "caused lziprecover to panic.\n" + "\nExit status: 0 for a normal exit, 1 for environmental problems\n" + "(file not found, invalid command line options, I/O errors, etc), 2 to\n" + "indicate a corrupt or invalid input file, 3 for an internal consistency\n" + "error (e.g., bug) which caused lziprecover to panic.\n" "\nReport bugs to lzip-bug@nongnu.org\n" "Lziprecover home page: http://www.nongnu.org/lzip/lziprecover.html\n" ); } @@ -202,14 +209,13 @@ const char * format_ds( const unsigned dictionary_size ) { enum { bufsize = 16, factor = 1024 }; static char buf[bufsize]; - const char * const prefix[8] = - { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" }; + const char * const prefix[3] = { "Ki", "Mi", "Gi" }; const char * p = ""; const char * np = " "; unsigned num = dictionary_size; bool exact = ( num % factor == 0 ); - for( int i = 0; i < 8 && ( num > 9999 || ( exact && num >= factor ) ); ++i ) + for( int i = 0; i < 3 && ( num > 9999 || ( exact && num >= factor ) ); ++i ) { num /= factor; if( num % factor != 0 ) exact = false; p = prefix[i]; np = ""; } snprintf( buf, bufsize, "%s%4u %sB", np, num, p ); @@ -226,42 +232,47 @@ void show_header( const unsigned dictionary_size ) #include "main_common.cc" -// Colon-separated list of "damaged", "tdata", [r][^]<list> (1 1,3-5,8) -void Member_list::parse_ml( const char * arg, const char * const option_name ) +// Colon-separated list of "damaged", "empty", "tdata", [r][^]<list> (1 1,3-5) +void Member_list::parse_ml( const char * const arg, + const char * const option_name, + Cl_options & cl_opts ) { + const char * p = arg; // points to current char while( true ) { - const char * tp = arg; // points to terminator (':' or '\0') + const char * tp = p; // points to terminator (':' or '\0') while( *tp && *tp != ':' ) ++tp; - const unsigned len = tp - arg; - if( std::islower( *(const unsigned char *)arg ) ) + const unsigned len = tp - p; + if( std::islower( *(const unsigned char *)p ) ) { - if( len <= 7 && std::strncmp( "damaged", arg, len ) == 0 ) - { damaged = true; goto next; } - if( len <= 5 && std::strncmp( "tdata", arg, len ) == 0 ) - { tdata = true; goto next; } + if( len <= 7 && std::strncmp( "damaged", p, len ) == 0 ) + { damaged = true; cl_opts.ignore_errors = true; goto next; } + if( len <= 5 && std::strncmp( "empty", p, len ) == 0 ) + { empty = true; cl_opts.ignore_empty = true; goto next; } + if( len <= 5 && std::strncmp( "tdata", p, len ) == 0 ) + { tdata = true; cl_opts.ignore_trailing = true; goto next; } } { - const bool reverse = ( *arg == 'r' ); - if( reverse ) ++arg; - if( *arg == '^' ) { ++arg; if( reverse ) rin = false; else in = false; } + const bool reverse = ( *p == 'r' ); + if( reverse ) ++p; + if( *p == '^' ) { ++p; if( reverse ) rin = false; else in = false; } std::vector< Block > * rvp = reverse ? &rrange_vector : &range_vector; - while( std::isdigit( *(const unsigned char *)arg ) ) + while( std::isdigit( *(const unsigned char *)p ) ) { const char * tail; - const int pos = getnum( arg, option_name, 0, 1, INT_MAX, &tail ) - 1; + const long pos = getnum( p, option_name, 0, 1, LONG_MAX, &tail ) - 1; if( rvp->size() && pos < rvp->back().end() ) break; - const int size = (*tail == '-') ? - getnum( tail + 1, option_name, 0, pos + 1, INT_MAX, &tail ) - pos : 1; + const long size = (*tail == '-') ? + getnum( tail + 1, option_name, 0, pos + 1, LONG_MAX, &tail ) - pos : 1; rvp->push_back( Block( pos, size ) ); if( tail == tp ) goto next; - if( *tail == ',' ) arg = tail + 1; else break; + if( *tail == ',' ) p = tail + 1; else break; } } - show_error( "Invalid list of members." ); + show_option_error( arg, "Invalid list of members in", option_name ); std::exit( 1 ); next: - if( *(arg = tp) != 0 ) ++arg; else return; + if( *(p = tp) != 0 ) ++p; else return; } } @@ -274,12 +285,8 @@ int parse_lzip_level( const char * const arg, const char * const option_name ) { if( *arg == 'a' || std::isdigit( *(const unsigned char *)arg ) ) return *arg; if( *arg != 'm' ) - { - if( verbosity >= 0 ) - std::fprintf( stderr, "%s: Bad argument in option '%s'.\n", - program_name, option_name ); - std::exit( 1 ); - } + { show_option_error( arg, "Invalid argument in", option_name ); + std::exit( 1 ); } if( arg[1] == 0 ) return -1; return -getnum( arg + 1, option_name, 0, min_match_len_limit, max_match_len ); } @@ -287,39 +294,34 @@ int parse_lzip_level( const char * const arg, const char * const option_name ) /* Recognized format: <range>[,<sector_size>] range formats: <begin> <begin>-<end> <begin>,<size> ,<size> + Return a pointer to the byte following the bytes parsed. */ -void parse_range( const char * const arg, const char * const pn, - Block & range, int * const sector_sizep = 0 ) +const char * parse_range( const char * const arg, const char * const pn, + Block & range, int * const sector_sizep = 0 ) { const char * tail = arg; long long value = ( arg[0] == ',' ) ? 0 : getnum( arg, pn, 0, 0, INT64_MAX - 1, &tail ); - if( tail[0] == 0 || tail[0] == ',' || tail[0] == '-' ) + if( tail[0] == 0 || tail[0] == ',' || tail[0] == '-' || tail[0] == ':' ) { range.pos( value ); - if( tail[0] == 0 ) { range.size( INT64_MAX - value ); return; } + if( tail[0] == 0 || tail[0] == ':' ) + { range.size( INT64_MAX - value ); return tail; } const bool is_size = ( tail[0] == ',' ); if( sector_sizep && tail[1] == ',' ) { value = INT64_MAX - value; ++tail; } else value = getnum( tail + 1, pn, 0, 1, INT64_MAX, &tail ); // size if( !is_size && value <= range.pos() ) - { - if( verbosity >= 0 ) - std::fprintf( stderr, "%s: Begin must be < end in range argument " - "of option '%s'.\n", program_name, pn ); - std::exit( 1 ); - } - if( !is_size ) value -= range.pos(); + { show_option_error( arg, "Begin must be < end in", pn ); std::exit( 1 ); } + if( !is_size ) value -= range.pos(); // size = end - pos if( INT64_MAX - value >= range.pos() ) { range.size( value ); if( sector_sizep && tail[0] == ',' ) - *sector_sizep = getnum( tail + 1, pn, 0, 8, INT_MAX ); - return; + *sector_sizep = getnum( tail + 1, pn, 0, 8, INT_MAX, &tail ); + return tail; } } - if( verbosity >= 0 ) - std::fprintf( stderr, "%s: Bad decompression range in option '%s'.\n", - program_name, pn ); + show_option_error( arg, "Invalid decompression range in", pn ); std::exit( 1 ); } @@ -333,6 +335,15 @@ void one_file( const int files ) } } +void at_least_one_file( const int files ) + { + if( files < 1 ) + { + show_error( "You must specify at least 1 file.", 0, true ); + std::exit( 1 ); + } + } + void set_mode( Mode & program_mode, const Mode new_mode ) { @@ -353,12 +364,8 @@ void parse_u( const char * const arg, const char * const option_name, { set_mode( program_mode, m_unzcrash_block ); sector_size = getnum( arg + 1, option_name, 0, 1, INT_MAX ); } else - { - if( verbosity >= 0 ) - std::fprintf( stderr, "%s: Bad argument for option '%s'.\n", - program_name, option_name ); - std::exit( 1 ); - } + { show_option_error( arg, "Invalid argument in", option_name ); + std::exit( 1 ); } } @@ -476,15 +483,15 @@ bool open_outstream( const bool force, const bool protect, } -bool file_exists( const std::string & filename ) +bool output_file_exists() { struct stat st; - if( stat( filename.c_str(), &st ) == 0 ) + if( stat( output_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() ); + program_name, output_filename.c_str() ); return true; } return false; @@ -585,7 +592,7 @@ void close_and_set_permissions( const struct stat * const in_statsp ) } -unsigned char xdigit( const unsigned value ) +unsigned char xdigit( const unsigned value ) // hex digit for 'value' { if( value <= 9 ) return '0' + value; if( value <= 15 ) return 'A' + value - 10; @@ -620,8 +627,7 @@ bool show_trailing_data( const uint8_t * const data, const int size, int decompress( const unsigned long long cfile_size, const int infd, - const Pretty_print & pp, const bool ignore_errors, - const bool ignore_trailing, const bool loose_trailing, + const Cl_options & cl_opts, const Pretty_print & pp, const bool testing ) { unsigned long long partial_file_pos = 0; @@ -632,50 +638,49 @@ int decompress( const unsigned long long cfile_size, const int infd, { Lzip_header header; rdec.reset_member_position(); - const int size = rdec.read_header_carefully( header, ignore_errors ); + const int size = rdec.read_header_carefully( header, cl_opts.ignore_errors ); if( rdec.finished() || // End Of File ( size < Lzip_header::size && !rdec.find_header( header ) ) ) { if( first_member ) { show_file_error( pp.name(), "File ends unexpectedly at member header." ); retval = 2; } - else if( header.verify_prefix( size ) ) + else if( header.check_prefix( size ) ) { pp( "Truncated header in multimember file." ); - show_trailing_data( header.data, size, pp, true, -1 ); - retval = 2; } - else if( size > 0 && !show_trailing_data( header.data, size, pp, - true, ignore_trailing ) ) - retval = 2; + show_trailing_data( header.data, size, pp, true, -1 ); retval = 2; } + else if( size > 0 && !show_trailing_data( header.data, size, pp, true, + cl_opts.ignore_trailing ) ) retval = 2; break; } - if( !header.verify_magic() ) + if( !header.check_magic() ) { if( first_member ) { show_file_error( pp.name(), bad_magic_msg ); retval = 2; } - else if( !loose_trailing && header.verify_corrupt() ) + else if( !cl_opts.loose_trailing && header.check_corrupt() ) { pp( corrupt_mm_msg ); - show_trailing_data( header.data, size, pp, false, -1 ); - retval = 2; } - else if( !show_trailing_data( header.data, size, pp, false, ignore_trailing ) ) - retval = 2; - if( ignore_errors ) { pp.reset(); continue; } else break; + show_trailing_data( header.data, size, pp, false, -1 ); retval = 2; } + else if( !show_trailing_data( header.data, size, pp, false, + cl_opts.ignore_trailing ) ) retval = 2; + if( cl_opts.ignore_errors ) { pp.reset(); continue; } else break; } - if( !header.verify_version() ) + if( !header.check_version() ) { pp( bad_version( header.version() ) ); retval = 2; - if( ignore_errors ) { pp.reset(); continue; } else break; } + if( cl_opts.ignore_errors ) { pp.reset(); continue; } else break; } const unsigned dictionary_size = header.dictionary_size(); if( !isvalid_ds( dictionary_size ) ) { pp( bad_dict_msg ); retval = 2; - if( ignore_errors ) { pp.reset(); continue; } else break; } + if( cl_opts.ignore_errors ) { pp.reset(); continue; } else break; } if( verbosity >= 2 || ( verbosity == 1 && first_member ) ) pp(); LZ_decoder decoder( rdec, dictionary_size, outfd ); show_dprogress( cfile_size, partial_file_pos, &rdec, &pp ); // init - const int result = decoder.decode_member( pp ); + const int result = + decoder.decode_member( pp, cl_opts.ignore_empty, cl_opts.ignore_marking ); partial_file_pos += rdec.member_position(); if( result != 0 ) { + retval = 2; if( verbosity >= 0 && result <= 2 ) { pp(); @@ -683,14 +688,16 @@ int decompress( const unsigned long long cfile_size, const int infd, "File ends unexpectedly" : "Decoder error", partial_file_pos ); } - retval = 2; if( ignore_errors ) { pp.reset(); continue; } else break; + else if( result == 5 ) { pp( empty_msg ); break; } + else if( result == 6 ) { pp( marking_msg ); break; } + if( cl_opts.ignore_errors ) { pp.reset(); continue; } else break; } if( verbosity >= 2 ) { std::fputs( testing ? "ok\n" : "done\n", stderr ); pp.reset(); } } if( verbosity == 1 && retval == 0 ) std::fputs( testing ? "ok\n" : "done\n", stderr ); - if( retval == 2 && ignore_errors ) retval = 0; + if( retval == 2 && cl_opts.ignore_errors ) retval = 0; return retval; } @@ -776,15 +783,14 @@ int main( const int argc, const char * const argv[] ) // '0'..'9' = level, 'a' = all levels // -5..-273 = match length, -1 = all lengths int repeated_byte = -1; // 0 to 255, or -1 for all values + Cl_options cl_opts; // command line options bool force = false; - bool ignore_errors = false; - bool ignore_trailing = true; bool keep_input_files = false; - bool loose_trailing = false; bool to_stdout = false; if( argc > 0 ) invocation_name = argv[0]; - enum { opt_du = 256, opt_lt, opt_lzl, opt_lzn, opt_ref, opt_re, opt_st }; + enum { opt_cm = 256, opt_du, opt_eer, opt_lt, opt_lzl, opt_lzn, opt_mer, + opt_ref, opt_rem, opt_st }; const Arg_parser::Option options[] = { { 'a', "trailing-error", Arg_parser::no }, @@ -804,6 +810,7 @@ int main( const int argc, const char * const argv[] ) { 'n', "threads", Arg_parser::yes }, { 'o', "output", Arg_parser::yes }, { 'q', "quiet", Arg_parser::no }, + { 'R', "byte-repair", Arg_parser::no }, { 'R', "repair", Arg_parser::no }, { 's', "split", Arg_parser::no }, { 'S', "nrep-stats", Arg_parser::maybe }, @@ -814,13 +821,16 @@ int main( const int argc, const char * const argv[] ) { 'W', "debug-decompress", Arg_parser::yes }, { 'X', "show-packets", Arg_parser::maybe }, { 'Y', "debug-delay", Arg_parser::yes }, - { 'Z', "debug-repair", Arg_parser::yes }, + { 'Z', "debug-byte-repair", Arg_parser::yes }, + { opt_cm, "clear-marking", Arg_parser::no }, { opt_du, "dump", Arg_parser::yes }, + { opt_eer, "empty-error", Arg_parser::no }, + { opt_mer, "marking-error", Arg_parser::no }, { opt_lt, "loose-trailing", Arg_parser::no }, { opt_lzl, "lzip-level", Arg_parser::yes }, { opt_lzn, "lzip-name", Arg_parser::yes }, { opt_ref, "reference-file", Arg_parser::yes }, - { opt_re, "remove", Arg_parser::yes }, + { opt_rem, "remove", Arg_parser::yes }, { opt_st, "strip", Arg_parser::yes }, { 0 , 0, Arg_parser::no } }; @@ -838,7 +848,7 @@ int main( const int argc, const char * const argv[] ) const char * const arg = sarg.c_str(); switch( code ) { - case 'a': ignore_trailing = false; break; + case 'a': cl_opts.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; @@ -849,7 +859,7 @@ int main( const int argc, const char * const argv[] ) parse_range( arg, pn, range, §or_size ); break; case 'f': force = true; break; case 'h': show_help(); return 0; - case 'i': ignore_errors = true; break; + case 'i': cl_opts.ignore_errors = true; break; case 'k': keep_input_files = true; break; case 'l': set_mode( program_mode, m_list ); break; case 'm': set_mode( program_mode, m_merge ); break; @@ -858,7 +868,7 @@ int main( const int argc, const char * const argv[] ) case 'o': if( sarg == "-" ) to_stdout = true; else { default_output_filename = sarg; } break; case 'q': verbosity = -1; break; - case 'R': set_mode( program_mode, m_repair ); break; + case 'R': set_mode( program_mode, m_byte_repair ); break; case 's': set_mode( program_mode, m_split ); break; case 'S': if( arg[0] ) repeated_byte = getnum( arg, pn, 0, 0, 255 ); set_mode( program_mode, m_nrep_stats ); break; @@ -872,18 +882,22 @@ int main( const int argc, const char * const argv[] ) if( arg[0] ) { bad_byte.parse_bb( arg, pn ); } break; case 'Y': set_mode( program_mode, m_debug_delay ); parse_range( arg, pn, range ); break; - case 'Z': set_mode( program_mode, m_debug_repair ); + case 'Z': set_mode( program_mode, m_debug_byte_repair ); bad_byte.parse_bb( arg, pn ); break; + case opt_cm: set_mode( program_mode, m_clear_marking ); + cl_opts.ignore_marking = true; break; case opt_du: set_mode( program_mode, m_dump ); - member_list.parse_ml( arg, pn ); break; - case opt_lt: loose_trailing = true; break; + member_list.parse_ml( arg, pn, cl_opts ); break; + case opt_eer: cl_opts.ignore_empty = false; break; + case opt_lt: cl_opts.loose_trailing = true; break; case opt_lzl: lzip_level = parse_lzip_level( arg, pn ); break; case opt_lzn: lzip_name = arg; break; + case opt_mer: cl_opts.ignore_marking = false; break; case opt_ref: reference_filename = arg; break; - case opt_re: set_mode( program_mode, m_remove ); - member_list.parse_ml( arg, pn ); break; + case opt_rem: set_mode( program_mode, m_remove ); + member_list.parse_ml( arg, pn, cl_opts ); break; case opt_st: set_mode( program_mode, m_strip ); - member_list.parse_ml( arg, pn ); break; + member_list.parse_ml( arg, pn, cl_opts ); break; default : internal_error( "uncaught option." ); } } // end process options @@ -913,67 +927,67 @@ int main( const int argc, const char * const argv[] ) { case m_none: internal_error( "invalid operation." ); break; case m_alone_to_lz: break; + case m_byte_repair: + one_file( filenames.size() ); + return byte_repair( filenames[0], default_output_filename, cl_opts, + terminator, force ); + case m_clear_marking: + at_least_one_file( filenames.size() ); + return clear_marking( filenames, cl_opts ); + case m_debug_byte_repair: + one_file( filenames.size() ); + return debug_byte_repair( filenames[0].c_str(), cl_opts, bad_byte, terminator ); case m_debug_decompress: one_file( filenames.size() ); - return debug_decompress( filenames[0], bad_byte, false ); + return debug_decompress( filenames[0].c_str(), cl_opts, bad_byte, false ); case m_debug_delay: one_file( filenames.size() ); - return debug_delay( filenames[0], range, terminator ); - case m_debug_repair: - one_file( filenames.size() ); - return debug_repair( filenames[0], bad_byte, terminator ); + return debug_delay( filenames[0].c_str(), cl_opts, range, terminator ); case m_decompress: break; case m_dump: case m_strip: - if( filenames.size() < 1 ) - { show_error( "You must specify at least 1 file.", 0, true ); return 1; } - return dump_members( filenames, default_output_filename, member_list, - force, ignore_errors, ignore_trailing, - loose_trailing, program_mode == m_strip, to_stdout ); + at_least_one_file( filenames.size() ); + return dump_members( filenames, default_output_filename, cl_opts, + member_list, force, program_mode == m_strip, to_stdout ); case m_list: break; case m_md5sum: break; case m_merge: if( filenames.size() < 2 ) { show_error( "You must specify at least 2 files.", 0, true ); return 1; } - return merge_files( filenames, default_output_filename, terminator, force ); - case m_nrep_stats: return print_nrep_stats( filenames, repeated_byte, - ignore_errors, ignore_trailing, loose_trailing ); + return merge_files( filenames, default_output_filename, cl_opts, + terminator, force ); + case m_nrep_stats: + return print_nrep_stats( filenames, cl_opts, repeated_byte ); case m_range_dec: one_file( filenames.size() ); - return range_decompress( filenames[0], default_output_filename, range, - force, ignore_errors, ignore_trailing, - loose_trailing, to_stdout ); + return range_decompress( filenames[0], default_output_filename, + cl_opts, range, force, to_stdout ); case m_remove: - if( filenames.size() < 1 ) - { show_error( "You must specify at least 1 file.", 0, true ); return 1; } - return remove_members( filenames, member_list, ignore_errors, - ignore_trailing, loose_trailing ); - case m_repair: - one_file( filenames.size() ); - return repair_file( filenames[0], default_output_filename, terminator, force ); + at_least_one_file( filenames.size() ); + return remove_members( filenames, cl_opts, member_list ); case m_reproduce: one_file( filenames.size() ); if( !reference_filename || !reference_filename[0] ) { show_error( "You must specify a reference file.", 0, true ); return 1; } if( range.size() > 0 ) - return debug_reproduce_file( filenames[0], lzip_name, - reference_filename, range, sector_size, lzip_level ); + return debug_reproduce_file( filenames[0].c_str(), lzip_name, + reference_filename, cl_opts, range, sector_size, lzip_level ); else - return reproduce_file( filenames[0], default_output_filename, - lzip_name, reference_filename, lzip_level, terminator, force ); + return reproduce_file( filenames[0], default_output_filename, lzip_name, + reference_filename, cl_opts, lzip_level, terminator, force ); case m_show_packets: one_file( filenames.size() ); - return debug_decompress( filenames[0], bad_byte, true ); + return debug_decompress( filenames[0].c_str(), cl_opts, bad_byte, true ); case m_split: one_file( filenames.size() ); - return split_file( filenames[0], default_output_filename, force ); + return split_file( filenames[0], default_output_filename, cl_opts, force ); case m_test: break; case m_unzcrash_bit: one_file( filenames.size() ); - return lunzcrash_bit( filenames[0].c_str() ); + return lunzcrash_bit( filenames[0].c_str(), cl_opts ); case m_unzcrash_block: one_file( filenames.size() ); - return lunzcrash_block( filenames[0].c_str(), sector_size ); + return lunzcrash_block( filenames[0].c_str(), cl_opts, sector_size ); } } catch( std::bad_alloc & ) { show_error( mem_msg ); cleanup_and_fail( 1 ); } @@ -981,10 +995,8 @@ int main( const int argc, const char * const argv[] ) if( filenames.empty() ) filenames.push_back("-"); - if( program_mode == m_list ) - return list_files( filenames, ignore_errors, ignore_trailing, loose_trailing ); - if( program_mode == m_md5sum ) - return md5sum_files( filenames ); + if( program_mode == m_list ) return list_files( filenames, cl_opts ); + if( program_mode == m_md5sum ) return md5sum_files( filenames ); if( program_mode != m_alone_to_lz && program_mode != m_decompress && program_mode != m_test ) @@ -1028,7 +1040,7 @@ int main( const int argc, const char * const argv[] ) infd = open_instream( input_filename.c_str(), &in_stats, one_to_one ); if( infd < 0 ) { set_retval( retval, 1 ); continue; } if( !check_tty_in( pp.name(), infd, program_mode, retval ) ) continue; - if( one_to_one ) // open outfd after verifying infd + if( one_to_one ) // open outfd after checking infd { if( program_mode == m_alone_to_lz ) set_a_outname( input_filename ); else set_d_outname( input_filename, extension_index( input_filename ) ); @@ -1040,7 +1052,7 @@ int main( const int argc, const char * const argv[] ) if( one_to_one && !check_tty_out( program_mode ) ) { set_retval( retval, 1 ); return retval; } // don't delete a tty - if( to_file && outfd < 0 ) // open outfd after verifying infd + if( to_file && outfd < 0 ) // open outfd after checking infd { output_filename = default_output_filename; if( !open_outstream( force, false ) || !check_tty_out( program_mode ) ) @@ -1057,8 +1069,7 @@ int main( const int argc, const char * const argv[] ) if( program_mode == m_alone_to_lz ) tmp = alone_to_lz( infd, pp ); else - tmp = decompress( cfile_size, infd, pp, ignore_errors, ignore_trailing, - loose_trailing, program_mode == m_test ); + tmp = decompress( cfile_size, infd, cl_opts, pp, program_mode == m_test ); } catch( std::bad_alloc & ) { pp( mem_msg ); tmp = 1; } catch( Error & e ) { pp(); show_error( e.msg, errno ); tmp = 1; } @@ -1073,7 +1084,7 @@ int main( const int argc, const char * const argv[] ) if( delete_output_on_interrupt && one_to_one ) close_and_set_permissions( in_statsp ); if( input_filename.size() && !keep_input_files && one_to_one && - ( program_mode != m_decompress || !ignore_errors ) ) + ( program_mode != m_decompress || !cl_opts.ignore_errors ) ) std::remove( input_filename.c_str() ); } if( delete_output_on_interrupt ) close_and_set_permissions( 0 ); // -o |