summaryrefslogtreecommitdiffstats
path: root/main.cc
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2016-06-01 14:15:42 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2016-06-01 14:15:56 +0000
commit4eb895adce37d5080310984546faf00db136d4ef (patch)
treeaa5e387425ea1b8f34ada52747cbca077a5047e7 /main.cc
parentReleasing debian version 1.4-2. (diff)
downloadplzip-4eb895adce37d5080310984546faf00db136d4ef.tar.xz
plzip-4eb895adce37d5080310984546faf00db136d4ef.zip
Merging upstream version 1.5.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'main.cc')
-rw-r--r--main.cc144
1 files changed, 74 insertions, 70 deletions
diff --git a/main.cc b/main.cc
index a2c1287..676f7d8 100644
--- a/main.cc
+++ b/main.cc
@@ -1,6 +1,6 @@
/* Plzip - Parallel compressor compatible with lzip
Copyright (C) 2009 Laszlo Ersek.
- 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
@@ -67,12 +67,13 @@
#error "Environments where CHAR_BIT != 8 are not supported."
#endif
+int verbosity = 0;
namespace {
const char * const Program_name = "Plzip";
const char * const program_name = "plzip";
-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[] = {
@@ -90,9 +91,6 @@ enum Mode { m_compress, m_decompress, m_test };
std::string output_filename;
int outfd = -1;
-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;
@@ -103,15 +101,16 @@ void show_help( const long num_online )
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"
" -B, --data-size=<bytes> set size of input data blocks [2x8=16 MiB]\n"
- " -c, --stdout send output to standard output\n"
+ " -c, --stdout write to standard output, keep input files\n"
" -d, --decompress decompress\n"
" -f, --force overwrite existing output files\n"
" -F, --recompress force re-compression of compressed files\n"
" -k, --keep keep (don't delete) input files\n"
" -m, --match-length=<bytes> set match length limit in bytes [36]\n"
" -n, --threads=<n> set number of (de)compression threads [%ld]\n"
- " -o, --output=<file> if reading stdin, place the output into <file>\n"
+ " -o, --output=<file> if reading standard input, write to <file>\n"
" -q, --quiet suppress all messages\n"
" -s, --dictionary-size=<bytes> set dictionary size limit in bytes [8 MiB]\n"
" -t, --test test compressed file integrity\n"
@@ -123,13 +122,15 @@ void show_help( const long num_online )
{
std::printf( " -D, --debug=<level> (0-1) print debug statistics to stderr\n" );
}
- std::printf( "If no file names are given, plzip compresses or decompresses\n"
- "from standard input to standard output.\n"
+ std::printf( "If no file names are given, or if a file is '-', plzip compresses or\n"
+ "decompresses 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"
- "The bidimensional parameter space of LZMA can't be mapped to a linear\n"
+ "Dictionary sizes 12 to 29 are interpreted as powers of two, meaning 2^12\n"
+ "to 2^29 bytes.\n"
+ "\nThe bidimensional parameter space of LZMA can't be mapped to a linear\n"
"scale optimal for all files. If your files are large, very repetitive,\n"
- "etc, you may need to use the --match-length and --dictionary-size\n"
+ "etc, you may need to use the --dictionary-size and --match-length\n"
"options directly to achieve optimal performance.\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"
@@ -190,11 +191,9 @@ unsigned long long getnum( const char * const ptr,
if( !errno && tail[0] )
{
const int factor = ( tail[1] == 'i' ) ? 1024 : 1000;
- int exponent = 0;
- bool bad_multiplier = false;
+ int exponent = 0; // 0 = bad multiplier
switch( tail[0] )
{
- case ' ': break;
case 'Y': exponent = 8; break;
case 'Z': exponent = 7; break;
case 'E': exponent = 6; break;
@@ -202,13 +201,10 @@ unsigned long long getnum( const char * const ptr,
case 'T': exponent = 4; break;
case 'G': exponent = 3; break;
case 'M': exponent = 2; break;
- case 'K': if( factor == 1024 ) exponent = 1; else bad_multiplier = true;
- break;
- case 'k': if( factor == 1000 ) exponent = 1; else bad_multiplier = true;
- break;
- default : bad_multiplier = true;
+ case 'K': if( factor == 1024 ) exponent = 1; break;
+ case 'k': if( factor == 1000 ) exponent = 1; break;
}
- if( bad_multiplier )
+ if( exponent <= 0 )
{
show_error( "Bad multiplier in numerical argument.", 0, true );
std::exit( 1 );
@@ -238,7 +234,7 @@ int get_dict_size( const char * const arg )
return ( 1 << bits );
int dictionary_size = getnum( arg, LZ_min_dictionary_size(),
LZ_max_dictionary_size() );
- if( dictionary_size == 65535 ) ++dictionary_size;
+ if( dictionary_size == 65535 ) ++dictionary_size; // no fast encoder
return dictionary_size;
}
@@ -283,7 +279,7 @@ int open_instream( const char * const name, struct stat * const in_statsp,
const bool can_read = ( i == 0 &&
( S_ISBLK( mode ) || S_ISCHR( mode ) ||
S_ISFIFO( mode ) || S_ISSOCK( mode ) ) );
- const bool no_ofile = to_stdout || program_mode == m_test;
+ const bool no_ofile = ( to_stdout || program_mode == m_test );
if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || !no_ofile ) ) )
{
if( verbosity >= 0 )
@@ -326,13 +322,17 @@ void set_d_outname( const std::string & name, const int i )
}
-bool open_outstream( const bool force )
+bool open_outstream( const bool force, const bool from_stdin )
{
+ 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 | 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",
@@ -403,7 +403,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 )
@@ -435,24 +439,18 @@ void set_signals()
} // end namespace
-int verbosity = 0;
-
-
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 );
}
@@ -473,20 +471,18 @@ void show_progress( const int packet_size,
static const Pretty_print * pp = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- if( verbosity >= 2 )
+ if( verbosity < 2 ) return;
+ if( p ) // initialize static vars
+ { csize = cfile_size; pos = 0; pp = p; }
+ if( pp )
{
- if( p ) // initialize static vars
- { csize = cfile_size; pos = 0; pp = p; }
- if( pp )
- {
- xlock( &mutex );
- pos += packet_size;
- if( csize > 0 )
- std::fprintf( stderr, "%4llu%%", pos / csize );
- std::fprintf( stderr, " %.1f MB\r", pos / 1000000.0 );
- pp->reset(); (*pp)(); // restore cursor position
- xunlock( &mutex );
- }
+ xlock( &mutex );
+ pos += packet_size;
+ if( csize > 0 )
+ std::fprintf( stderr, "%4llu%%", pos / csize );
+ std::fprintf( stderr, " %.1f MB\r", pos / 1000000.0 );
+ pp->reset(); (*pp)(); // restore cursor position
+ xunlock( &mutex );
}
}
@@ -497,7 +493,7 @@ int main( const int argc, const char * const argv[] )
to the corresponding LZMA compression modes. */
const Lzma_options option_mapping[] =
{
- { 65535, 16 }, // -0
+ { 65535, 16 }, // -0 (65535,16 chooses fast encoder)
{ 1 << 20, 5 }, // -1
{ 3 << 19, 6 }, // -2
{ 1 << 21, 8 }, // -3
@@ -517,13 +513,15 @@ int main( const int argc, const char * const argv[] )
int num_workers = 0; // start this many worker threads
Mode program_mode = m_compress;
bool force = false;
+ bool ignore_trailing = true;
bool keep_input_files = false;
bool recompress = false;
bool to_stdout = false;
invocation_name = argv[0];
- if( LZ_version()[0] != LZ_version_string[0] )
- internal_error( "bad library version." );
+ if( LZ_version()[0] < '1' )
+ { show_error( "Bad library version. At least lzlib 1.0 is required." );
+ return 1; }
const long num_online = std::max( 1L, sysconf( _SC_NPROCESSORS_ONLN ) );
long max_workers = sysconf( _SC_THREAD_THREADS_MAX );
@@ -542,6 +540,7 @@ int main( const int argc, const char * const argv[] )
{ '7', 0, Arg_parser::no },
{ '8', 0, Arg_parser::no },
{ '9', "best", Arg_parser::no },
+ { 'a', "trailing-error", Arg_parser::no },
{ 'b', "member-size", Arg_parser::yes },
{ 'B', "data-size", Arg_parser::yes },
{ 'c', "stdout", Arg_parser::no },
@@ -572,28 +571,30 @@ 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 '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
encoder_options = option_mapping[code-'0']; break;
+ case 'a': ignore_trailing = false; break;
case 'b': break;
- case 'B': data_size = getnum( arg.c_str(), 2 * LZ_min_dictionary_size(),
+ case 'B': data_size = getnum( ptr, 2 * LZ_min_dictionary_size(),
2 * LZ_max_dictionary_size() ); break;
case 'c': to_stdout = true; break;
case 'd': program_mode = m_decompress; break;
- case 'D': debug_level = getnum( arg.c_str(), 0, 3 ); break;
+ case 'D': debug_level = getnum( ptr, 0, 3 ); break;
case 'f': force = true; break;
case 'F': recompress = true; break;
case 'h': show_help( num_online ); return 0;
case 'k': keep_input_files = true; break;
case 'm': encoder_options.match_len_limit =
- getnum( arg.c_str(), LZ_min_match_len_limit(),
- LZ_max_match_len_limit() ); break;
- case 'n': num_workers = getnum( arg.c_str(), 1, max_workers ); break;
+ getnum( ptr, LZ_min_match_len_limit(),
+ LZ_max_match_len_limit() ); break;
+ case 'n': num_workers = getnum( ptr, 1, max_workers ); break;
case 'o': default_output_filename = arg; break;
case 'q': verbosity = -1; break;
- case 's': encoder_options.dictionary_size = get_dict_size( arg.c_str() );
+ case 's': encoder_options.dictionary_size = get_dict_size( ptr );
break;
case 'S': break;
case 't': program_mode = m_test; break;
@@ -637,9 +638,10 @@ int main( const int argc, const char * const argv[] )
( filenames_given || default_output_filename.size() ) )
set_signals();
- Pretty_print pp( filenames );
+ Pretty_print pp( filenames, verbosity );
int retval = 0;
+ bool stdin_used = false;
for( unsigned i = 0; i < filenames.size(); ++i )
{
struct stat in_stats;
@@ -647,6 +649,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 )
@@ -658,8 +661,7 @@ int main( const int argc, const char * const argv[] )
if( program_mode == m_compress )
set_c_outname( default_output_filename );
else output_filename = default_output_filename;
- outfd_mode = all_rw;
- if( !open_outstream( force ) )
+ if( !open_outstream( force, true ) )
{
if( retval < 1 ) retval = 1;
close( infd ); infd = -1;
@@ -683,8 +685,7 @@ int main( const int argc, const char * const argv[] )
if( program_mode == m_compress )
set_c_outname( input_filename );
else set_d_outname( input_filename, eindex );
- outfd_mode = usr_rw;
- if( !open_outstream( force ) )
+ if( !open_outstream( force, false ) )
{
if( retval < 1 ) retval = 1;
close( infd ); infd = -1;
@@ -694,10 +695,12 @@ int main( const int argc, const char * const argv[] )
}
}
- if( !check_tty( infd, program_mode ) ) return 1;
+ if( !check_tty( infd, program_mode ) )
+ {
+ 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;
const bool infd_isreg = in_statsp && S_ISREG( in_statsp->st_mode );
pp.set_name( input_filename );
@@ -711,7 +714,8 @@ int main( const int argc, const char * const argv[] )
num_workers, infd, outfd, pp, debug_level );
}
else
- tmp = decompress( num_workers, infd, outfd, pp, debug_level, infd_isreg );
+ tmp = decompress( num_workers, infd, outfd, pp, debug_level,
+ ignore_trailing, infd_isreg );
if( tmp > retval ) retval = tmp;
if( tmp && program_mode != m_test ) cleanup_and_fail( retval );