summaryrefslogtreecommitdiffstats
path: root/main.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2018-02-13 07:01:01 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2018-02-13 07:01:01 +0000
commit0437b6ffc0da64f949af9f8d12297dafd1973ea2 (patch)
tree475922608b5ef7331bbb6a885b229f9921f5eac9 /main.cc
parentAdding upstream version 1.19. (diff)
downloadlziprecover-0437b6ffc0da64f949af9f8d12297dafd1973ea2.tar.xz
lziprecover-0437b6ffc0da64f949af9f8d12297dafd1973ea2.zip
Adding upstream version 1.20.upstream/1.20
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'main.cc')
-rw-r--r--main.cc382
1 files changed, 205 insertions, 177 deletions
diff --git a/main.cc b/main.cc
index 95ee71b..3307194 100644
--- a/main.cc
+++ b/main.cc
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2017 Antonio Diaz Diaz.
+ Copyright (C) 2009-2018 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,6 +66,7 @@
#error "Environments where CHAR_BIT != 8 are not supported."
#endif
+int verbosity = 0;
std::string output_filename; // global vars for output file
int outfd = -1;
@@ -73,7 +74,6 @@ namespace {
const char * const Program_name = "Lziprecover";
const char * const program_name = "lziprecover";
-const char * const program_year = "2017";
const char * invocation_name = 0;
const struct { const char * from; const char * to; } known_extensions[] = {
@@ -82,10 +82,10 @@ const struct { const char * from; const char * to; } known_extensions[] = {
{ 0, 0 } };
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 };
+ m_debug_repair, m_decompress, m_dump_tdata, m_list, m_merge,
+ m_range_dec, m_remove_tdata, m_repair, m_show_packets, m_split,
+ m_strip_tdata, m_test };
-int verbosity = 0;
bool delete_output_on_interrupt = false;
@@ -101,6 +101,8 @@ void show_help()
"files and test integrity of files.\n"
"\nLziprecover provides random access to the data in multimember files; it\n"
"only decompresses the members containing the desired data.\n"
+ "\nLziprecover facilitates the management of metadata stored as trailing\n"
+ "data in lzip 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 );
@@ -122,7 +124,11 @@ void show_help()
" -R, --repair try to repair a small error 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" );
+ " -v, --verbose be verbose (a 2nd -v gives more)\n"
+ " --loose-trailing allow trailing data seeming corrupt header\n"
+ " --dump-tdata dump trailing data to standard output\n"
+ " --remove-tdata remove trailing data from files in place\n"
+ " --strip-tdata copy files to stdout without trailing data\n" );
if( verbosity >= 1 )
{
std::printf( " -W, --debug-decompress=<pos>,<val> set pos to val and decompress to stdout\n"
@@ -142,17 +148,22 @@ void show_help()
"Lziprecover home page: http://www.nongnu.org/lzip/lziprecover.html\n" );
}
+} // end namespace
-void show_version()
+void Pretty_print::operator()( const char * const msg, FILE * const f ) const
{
- std::printf( "%s %s\n", program_name, PROGVERSION );
- std::printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year );
- std::printf( "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
- "This is free software: you are free to change and redistribute it.\n"
- "There is NO WARRANTY, to the extent permitted by law.\n" );
+ if( verbosity >= 0 )
+ {
+ if( first_post )
+ {
+ first_post = false;
+ std::fputs( padded_name.c_str(), f );
+ if( !msg ) std::fflush( f );
+ }
+ if( msg ) std::fprintf( f, "%s\n", msg );
+ }
}
-} // end namespace
const char * bad_version( const unsigned version )
{
@@ -182,70 +193,16 @@ const char * format_ds( const unsigned dictionary_size )
}
-void show_header( const unsigned dictionary_size, const int vlevel )
+void show_header( const unsigned dictionary_size )
{
- if( verbosity >= vlevel )
- std::fprintf( stderr, "dictionary %s. ", format_ds( dictionary_size ) );
+ std::fprintf( stderr, "dictionary %s, ", format_ds( dictionary_size ) );
}
-namespace {
-long long getnum( const char * const ptr, const long long llimit = -LLONG_MAX,
- const long long ulimit = LLONG_MAX,
- const char ** const tailp = 0 )
- {
- char * tail;
- errno = 0;
- long long result = strtoll( ptr, &tail, 0 );
- if( tail == ptr )
- {
- show_error( "Bad or missing numerical argument.", 0, true );
- std::exit( 1 );
- }
+#include "main_common.cc"
- if( !errno && tail[0] )
- {
- char * const p = tail++;
- int factor;
- bool bsuf; // 'B' suffix is present
- if( tail[0] == 'i' ) { ++tail; factor = 1024; } else factor = 1000;
- if( tail[0] == 'B' ) { ++tail; bsuf = true; } else bsuf = false;
- int exponent = -1; // -1 = bad multiplier
- switch( *p )
- {
- case 'Y': exponent = 8; break;
- case 'Z': exponent = 7; break;
- case 'E': exponent = 6; break;
- case 'P': exponent = 5; break;
- case 'T': exponent = 4; break;
- case 'G': exponent = 3; break;
- case 'M': exponent = 2; break;
- case 'K': if( factor == 1024 ) exponent = 1; break;
- case 'k': if( factor == 1000 ) exponent = 1; break;
- case 'B': if( factor == 1000 && !bsuf ) exponent = 0; break;
- default : if( tailp ) { tail = p; exponent = 0; } break;
- }
- if( exponent < 0 )
- {
- show_error( "Bad multiplier in numerical argument.", 0, true );
- std::exit( 1 );
- }
- for( int i = 0; i < exponent; ++i )
- {
- if( LLONG_MAX / factor >= std::labs( result ) ) result *= factor;
- else { errno = ERANGE; break; }
- }
- }
- if( !errno && ( result < llimit || result > ulimit ) ) errno = ERANGE;
- if( errno )
- {
- show_error( "Numerical argument out of limits." );
- std::exit( 1 );
- }
- if( tailp ) *tailp = tail;
- return result;
- }
+namespace {
// Recognized formats: <begin> <begin>-<end> <begin>,<size> ,<size>
//
@@ -253,13 +210,13 @@ void parse_range( const char * const ptr, Block & range )
{
const char * tail = ptr;
long long value =
- ( ptr[0] == ',' ) ? 0 : getnum( ptr, 0, INT64_MAX - 1, &tail );
+ ( ptr[0] == ',' ) ? 0 : getnum( ptr, 0, 0, INT64_MAX - 1, &tail );
if( tail[0] == 0 || tail[0] == ',' || tail[0] == '-' )
{
range.pos( value );
if( tail[0] == 0 ) { range.size( INT64_MAX - value ); return; }
const bool issize = ( tail[0] == ',' );
- value = getnum( tail + 1, 1, INT64_MAX ); // size
+ value = getnum( tail + 1, 0, 1, INT64_MAX ); // size
if( issize || value > range.pos() )
{
if( !issize ) value -= range.pos();
@@ -276,7 +233,7 @@ void parse_range( const char * const ptr, Block & range )
void parse_pos_value( const char * const ptr, Bad_byte & bad_byte )
{
const char * tail;
- bad_byte.pos = getnum( ptr, 0, INT64_MAX, &tail );
+ bad_byte.pos = getnum( ptr, 0, 0, INT64_MAX, &tail );
if( tail[0] != ',' )
{
show_error( "Bad separator between <pos> and <val>.", 0, true );
@@ -285,7 +242,7 @@ void parse_pos_value( const char * const ptr, Bad_byte & bad_byte )
if( tail[1] == '+' ) { ++tail; bad_byte.mode = Bad_byte::delta; }
else if( tail[1] == 'f' ) { ++tail; bad_byte.mode = Bad_byte::flip; }
else bad_byte.mode = Bad_byte::literal;
- bad_byte.value = getnum( tail + 1, 0, 255 );
+ bad_byte.value = getnum( tail + 1, 0, 0, 255 );
}
@@ -322,6 +279,37 @@ int extension_index( const std::string & name )
return -1;
}
+
+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 eindex )
+ {
+ if( eindex >= 0 )
+ {
+ const std::string from( known_extensions[eindex].from );
+ if( name.size() > from.size() )
+ {
+ output_filename.assign( name, 0, name.size() - from.size() );
+ output_filename += known_extensions[eindex].to;
+ return;
+ }
+ }
+ output_filename = name; output_filename += ".out";
+ if( verbosity >= 1 )
+ std::fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'\n",
+ program_name, name.c_str(), output_filename.c_str() );
+ }
+
} // end namespace
int open_instream( const char * const name, struct stat * const in_statsp,
@@ -351,39 +339,29 @@ int open_instream( const char * const name, struct stat * const in_statsp,
return infd;
}
-namespace {
-void set_a_outname( const std::string & name )
+int open_truncable_stream( const char * const name,
+ struct stat * const in_statsp )
{
- 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 eindex )
- {
- if( eindex >= 0 )
+ int infd = open( name, O_RDWR | O_BINARY );
+ if( infd < 0 )
+ show_file_error( name, "Can't open input file", errno );
+ else
{
- const std::string from( known_extensions[eindex].from );
- if( name.size() > from.size() )
+ const int i = fstat( infd, in_statsp );
+ const mode_t mode = in_statsp->st_mode;
+ if( i != 0 || !S_ISREG( mode ) )
{
- output_filename.assign( name, 0, name.size() - from.size() );
- output_filename += known_extensions[eindex].to;
- return;
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: File '%s' is not a regular file.\n",
+ program_name, name );
+ close( infd );
+ infd = -1;
}
}
- output_filename = name; output_filename += ".out";
- if( verbosity >= 1 )
- std::fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'\n",
- program_name, name.c_str(), output_filename.c_str() );
+ return infd;
}
-} // end namespace
bool open_outstream( const bool force, const bool from_stdin,
const bool rw, const bool skipping )
@@ -504,9 +482,9 @@ unsigned char xdigit( const unsigned value )
bool show_trailing_data( const uint8_t * const data, const int size,
const Pretty_print & pp, const bool all,
- const bool ignore_trailing )
+ const int ignore_trailing ) // -1 = show
{
- if( verbosity >= 4 || !ignore_trailing )
+ if( verbosity >= 4 || ignore_trailing <= 0 )
{
std::string msg;
if( !all ) msg = "first bytes of ";
@@ -522,14 +500,15 @@ bool show_trailing_data( const uint8_t * const data, const int size,
{ if( std::isprint( data[i] ) ) msg += data[i]; else msg += '.'; }
msg += '\'';
pp( msg.c_str() );
- if( !ignore_trailing ) show_file_error( pp.name(), trailing_msg );
+ if( ignore_trailing == 0 ) show_file_error( pp.name(), trailing_msg );
}
- return ignore_trailing;
+ return ( ignore_trailing > 0 );
}
-int decompress( const int infd, const Pretty_print & pp,
- const bool ignore_trailing, const bool testing )
+int decompress( const unsigned long long cfile_size, const int infd,
+ const Pretty_print & pp, const bool ignore_trailing,
+ const bool loose_trailing, const bool testing )
{
int retval = 0;
@@ -543,8 +522,13 @@ 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 || header.verify_prefix( size ) )
- { pp( "File ends unexpectedly at member header." ); retval = 2; }
+ if( first_member )
+ { show_file_error( pp.name(), "File ends unexpectedly at member header." );
+ retval = 2; }
+ else if( header.verify_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;
@@ -554,6 +538,10 @@ int decompress( const int infd, const Pretty_print & pp,
{
if( first_member )
{ show_file_error( pp.name(), bad_magic_msg ); retval = 2; }
+ else if( !loose_trailing && header.verify_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;
break;
@@ -564,10 +552,10 @@ int decompress( const int infd, const Pretty_print & pp,
if( !isvalid_ds( dictionary_size ) )
{ pp( bad_dict_msg ); retval = 2; break; }
- if( verbosity >= 2 || ( verbosity == 1 && first_member ) )
- { pp(); show_header( dictionary_size ); }
+ 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 );
partial_file_pos += rdec.member_position();
if( result != 0 )
@@ -615,7 +603,7 @@ int close_outstream( const struct stat * const in_statsp )
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; }
+ { show_error( "Error closing stdout", errno ); return 1; }
outfd = -1;
return 0;
}
@@ -634,21 +622,6 @@ std::string insert_fixed( std::string name )
}
-void show_error( const char * const msg, const int errcode, const bool help )
- {
- if( verbosity < 0 ) return;
- 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 );
- }
-
-
void show_file_error( const char * const filename, const char * const msg,
const int errcode )
{
@@ -659,14 +632,6 @@ void show_file_error( const char * const filename, const char * const msg,
}
-void internal_error( const char * const msg )
- {
- if( verbosity >= 0 )
- std::fprintf( stderr, "%s: internal error: %s\n", program_name, msg );
- std::exit( 3 );
- }
-
-
void show_error2( const char * const msg1, const char * const name,
const char * const msg2 )
{
@@ -684,47 +649,83 @@ void show_error4( const char * const msg1, const char * const name1,
}
+void show_dprogress( const unsigned long long cfile_size,
+ const unsigned long long partial_size,
+ const Range_decoder * const d,
+ const Pretty_print * const p )
+ {
+ static unsigned long long csize = 0; // file_size / 100
+ static unsigned long long psize = 0;
+ static const Range_decoder * rdec = 0;
+ static const Pretty_print * pp = 0;
+ static int counter = 0;
+ static bool enabled = true;
+
+ if( !enabled ) return;
+ if( p ) // initialize static vars
+ {
+ if( verbosity < 2 || !isatty( STDERR_FILENO ) ) { enabled = false; return; }
+ csize = cfile_size; psize = partial_size; rdec = d; pp = p; counter = 0;
+ }
+ if( rdec && pp && --counter <= 0 )
+ {
+ const unsigned long long pos = psize + rdec->member_position();
+ counter = 7; // update display every 114688 bytes
+ if( csize > 0 )
+ std::fprintf( stderr, "%4llu%% %.1f MB\r", pos / csize, pos / 1000000.0 );
+ else
+ std::fprintf( stderr, " %.1f MB\r", pos / 1000000.0 );
+ pp->reset(); (*pp)(); // restore cursor position
+ }
+ }
+
+
int main( const int argc, const char * const argv[] )
{
Block range( 0, 0 );
Bad_byte bad_byte;
std::string default_output_filename;
std::vector< std::string > filenames;
- int infd = -1;
Mode program_mode = m_none;
bool force = false;
bool ignore_errors = false;
bool ignore_trailing = true;
bool keep_input_files = false;
+ bool loose_trailing = false;
bool to_stdout = false;
invocation_name = argv[0];
+ enum { opt_dtd = 256, opt_lt, opt_rtd, opt_std };
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 },
- { 'f', "force", Arg_parser::no },
- { 'h', "help", Arg_parser::no },
- { 'i', "ignore-errors", Arg_parser::no },
- { 'k', "keep", Arg_parser::no },
- { 'l', "list", Arg_parser::no },
- { 'm', "merge", Arg_parser::no },
- { 'n', "threads", Arg_parser::yes },
- { 'o', "output", Arg_parser::yes },
- { 'q', "quiet", Arg_parser::no },
- { 'R', "repair", Arg_parser::no },
- { 's', "split", Arg_parser::no },
- { 't', "test", Arg_parser::no },
- { 'v', "verbose", Arg_parser::no },
- { 'V', "version", 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 } };
+ { '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 },
+ { 'f', "force", Arg_parser::no },
+ { 'h', "help", Arg_parser::no },
+ { 'i', "ignore-errors", Arg_parser::no },
+ { 'k', "keep", Arg_parser::no },
+ { 'l', "list", Arg_parser::no },
+ { 'm', "merge", Arg_parser::no },
+ { 'n', "threads", Arg_parser::yes },
+ { 'o', "output", Arg_parser::yes },
+ { 'q', "quiet", Arg_parser::no },
+ { 'R', "repair", Arg_parser::no },
+ { 's', "split", Arg_parser::no },
+ { 't', "test", Arg_parser::no },
+ { 'v', "verbose", Arg_parser::no },
+ { 'V', "version", 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 },
+ { opt_dtd, "dump-tdata", Arg_parser::no },
+ { opt_lt, "loose-trailing", Arg_parser::no },
+ { opt_rtd, "remove-tdata", Arg_parser::no },
+ { opt_std, "strip-tdata", Arg_parser::no },
+ { 0 , 0, Arg_parser::no } };
const Arg_parser parser( argc, argv, options );
if( parser.error().size() ) // bad option
@@ -767,6 +768,10 @@ int main( const int argc, const char * const argv[] )
parse_range( arg, range ); break;
case 'Z': set_mode( program_mode, m_debug_repair );
parse_pos_value( arg, bad_byte ); break;
+ case opt_dtd: set_mode( program_mode, m_dump_tdata ); break;
+ case opt_lt: loose_trailing = true; break;
+ case opt_rtd: set_mode( program_mode, m_remove_tdata ); break;
+ case opt_std: set_mode( program_mode, m_strip_tdata ); break;
default : internal_error( "uncaught option." );
}
} // end process options
@@ -796,37 +801,48 @@ int main( const int argc, const char * const argv[] )
case m_alone_to_lz: break;
case m_debug_decompress:
one_file( filenames.size() );
- return debug_decompress( filenames[0], bad_byte, verbosity, false );
+ return debug_decompress( filenames[0], bad_byte, false );
case m_debug_delay:
one_file( filenames.size() );
- return debug_delay( filenames[0], range, verbosity );
+ return debug_delay( filenames[0], range );
case m_debug_repair:
one_file( filenames.size() );
- return debug_repair( filenames[0], bad_byte, verbosity );
+ return debug_repair( filenames[0], bad_byte );
case m_decompress: break;
+ case m_dump_tdata:
+ case m_strip_tdata:
+ if( filenames.size() < 1 )
+ { show_error( "You must specify at least 1 file.", 0, true ); return 1; }
+ if( default_output_filename.size() ) set_signals();
+ return dump_tdata( filenames, default_output_filename, force,
+ program_mode == m_strip_tdata, loose_trailing );
case m_list: break;
case m_merge:
if( filenames.size() < 2 )
{ show_error( "You must specify at least 2 files.", 0, true ); return 1; }
set_signals();
- return merge_files( filenames, default_output_filename, verbosity, force );
+ return merge_files( filenames, default_output_filename, force );
case m_range_dec:
one_file( filenames.size() );
set_signals();
return range_decompress( filenames[0], default_output_filename, range,
- verbosity, force, ignore_errors, ignore_trailing, to_stdout );
+ force, ignore_errors, ignore_trailing,
+ loose_trailing, to_stdout );
+ case m_remove_tdata:
+ if( filenames.size() < 1 )
+ { show_error( "You must specify at least 1 file.", 0, true ); return 1; }
+ return remove_tdata( filenames, loose_trailing );
case m_repair:
one_file( filenames.size() );
set_signals();
- return repair_file( filenames[0], default_output_filename, verbosity,
- force );
+ return repair_file( filenames[0], default_output_filename, force );
case m_show_packets:
one_file( filenames.size() );
- return debug_decompress( filenames[0], bad_byte, verbosity, true );
+ return debug_decompress( filenames[0], bad_byte, true );
case m_split:
one_file( filenames.size() );
set_signals();
- return split_file( filenames[0], default_output_filename, verbosity, force );
+ return split_file( filenames[0], default_output_filename, force );
case m_test: break;
}
}
@@ -837,7 +853,7 @@ int main( const int argc, const char * const argv[] )
if( filenames.empty() ) filenames.push_back("-");
if( program_mode == m_list )
- return list_files( filenames, verbosity, ignore_trailing );
+ return list_files( filenames, ignore_trailing, loose_trailing );
if( program_mode == m_test )
outfd = -1;
@@ -848,13 +864,15 @@ int main( const int argc, const char * const argv[] )
( filenames_given || default_output_filename.size() ) )
set_signals();
- Pretty_print pp( filenames, verbosity );
+ Pretty_print pp( filenames );
+ int failed_tests = 0;
int retval = 0;
bool stdin_used = false;
for( unsigned i = 0; i < filenames.size(); ++i )
{
std::string input_filename;
+ int infd;
struct stat in_stats;
output_filename.clear();
@@ -875,7 +893,7 @@ int main( const int argc, const char * const argv[] )
if( !open_outstream( force, true ) )
{
if( retval < 1 ) retval = 1;
- close( infd ); infd = -1;
+ close( infd );
continue;
}
}
@@ -898,7 +916,7 @@ int main( const int argc, const char * const argv[] )
if( !open_outstream( force, false ) )
{
if( retval < 1 ) retval = 1;
- close( infd ); infd = -1;
+ close( infd );
continue;
}
}
@@ -909,32 +927,42 @@ int main( const int argc, const char * const argv[] )
if( !check_tty( pp.name(), infd, program_mode ) )
{
if( retval < 1 ) retval = 1;
- if( program_mode == m_test ) { close( infd ); infd = -1; continue; }
+ if( program_mode == m_test ) { close( infd ); continue; }
cleanup_and_fail( retval );
}
const struct stat * const in_statsp = input_filename.size() ? &in_stats : 0;
+ const unsigned long long cfile_size =
+ ( in_statsp && S_ISREG( in_statsp->st_mode ) ) ?
+ ( in_statsp->st_size + 99 ) / 100 : 0;
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 );
+ tmp = decompress( cfile_size, infd, pp, ignore_trailing,
+ loose_trailing, program_mode == m_test );
if( tmp > retval ) retval = tmp;
- if( tmp && program_mode != m_test ) cleanup_and_fail( retval );
+ if( tmp )
+ { if( program_mode != m_test ) cleanup_and_fail( retval );
+ else ++failed_tests; }
if( delete_output_on_interrupt )
close_and_set_permissions( in_statsp );
if( input_filename.size() )
{
- close( infd ); infd = -1;
+ close( infd );
if( !keep_input_files && !to_stdout && program_mode != m_test )
std::remove( input_filename.c_str() );
}
}
if( outfd >= 0 && close( outfd ) != 0 )
{
- show_error( "Can't close stdout", errno );
+ show_error( "Error closing stdout", errno );
if( retval < 1 ) retval = 1;
}
+ if( failed_tests > 0 && verbosity >= 1 && filenames.size() > 1 )
+ std::fprintf( stderr, "%s: warning: %d %s failed the test.\n",
+ program_name, failed_tests,
+ ( failed_tests == 1 ) ? "file" : "files" );
return retval;
}