diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2017-05-07 15:55:05 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2017-05-07 15:55:05 +0000 |
commit | 6b876fb72fd61b2d0a2265c6f6394569bdca3a18 (patch) | |
tree | f9bbba110ba3a8692d8072b483bd7baf8d6d5079 /main.cc | |
parent | Releasing debian version 1.5-5. (diff) | |
download | plzip-6b876fb72fd61b2d0a2265c6f6394569bdca3a18.tar.xz plzip-6b876fb72fd61b2d0a2265c6f6394569bdca3a18.zip |
Merging upstream version 1.6.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'main.cc')
-rw-r--r-- | main.cc | 226 |
1 files changed, 136 insertions, 90 deletions
@@ -1,6 +1,6 @@ /* Plzip - Parallel compressor compatible with lzip Copyright (C) 2009 Laszlo Ersek. - Copyright (C) 2009-2016 Antonio Diaz Diaz. + Copyright (C) 2009-2017 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 @@ -73,10 +73,10 @@ namespace { const char * const Program_name = "Plzip"; const char * const program_name = "plzip"; -const char * const program_year = "2016"; +const char * const program_year = "2017"; const char * invocation_name = 0; -struct { const char * from; const char * to; } const known_extensions[] = { +const struct { const char * from; const char * to; } known_extensions[] = { { ".lz", "" }, { ".tlz", ".tar" }, { 0, 0 } }; @@ -87,7 +87,7 @@ struct Lzma_options int match_len_limit; // 5 .. 273 }; -enum Mode { m_compress, m_decompress, m_test }; +enum Mode { m_compress, m_decompress, m_list, m_test }; std::string output_filename; int outfd = -1; @@ -108,6 +108,7 @@ void show_help( const long num_online ) " -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" + " -l, --list print (un)compressed file sizes\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 standard input, write to <file>\n" @@ -120,7 +121,7 @@ void show_help( const long num_online ) " --best alias for -9\n", num_online ); if( verbosity >= 1 ) { - std::printf( " -D, --debug=<level> (0-1) print debug statistics to stderr\n" ); + std::printf( " --debug=<level> (0-1) print debug statistics to stderr\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" @@ -154,23 +155,38 @@ void show_version() } // end namespace +const char * bad_version( const unsigned version ) + { + static char buf[80]; + snprintf( buf, sizeof buf, "Version %u member format not supported.", + version ); + return buf; + } + + +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 * 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 ) + { num /= factor; if( num % factor != 0 ) exact = false; + p = prefix[i]; np = ""; } + snprintf( buf, bufsize, "%s%4u %sB", np, num, p ); + return buf; + } + + void show_header( const unsigned dictionary_size ) { if( verbosity >= 3 ) - { - const char * const prefix[8] = - { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" }; - enum { factor = 1024 }; - 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 ) - { num /= factor; if( num % factor != 0 ) exact = false; - p = prefix[i]; np = ""; } - std::fprintf( stderr, "dictionary size %s%4u %sB. ", np, num, p ); - } + std::fprintf( stderr, "dictionary %s. ", format_ds( dictionary_size ) ); } namespace { @@ -190,7 +206,7 @@ unsigned long long getnum( const char * const ptr, if( !errno && tail[0] ) { - const int factor = ( tail[1] == 'i' ) ? 1024 : 1000; + const unsigned factor = ( tail[1] == 'i' ) ? 1024 : 1000; int exponent = 0; // 0 = bad multiplier switch( tail[0] ) { @@ -228,7 +244,7 @@ unsigned long long getnum( const char * const ptr, int get_dict_size( const char * const arg ) { char * tail; - const int bits = std::strtol( arg, &tail, 0 ); + const long bits = std::strtol( arg, &tail, 0 ); if( bits >= LZ_min_dictionary_bits() && bits <= LZ_max_dictionary_bits() && *tail == 0 ) return ( 1 << bits ); @@ -239,62 +255,75 @@ int get_dict_size( const char * const arg ) } +void set_mode( Mode & program_mode, const Mode new_mode ) + { + if( program_mode != m_compress && program_mode != new_mode ) + { + show_error( "Only one operation can be specified.", 0, true ); + std::exit( 1 ); + } + program_mode = new_mode; + } + + int extension_index( const std::string & name ) { - for( int i = 0; known_extensions[i].from; ++i ) + for( int eindex = 0; known_extensions[eindex].from; ++eindex ) { - const std::string ext( known_extensions[i].from ); + const std::string ext( known_extensions[eindex].from ); if( name.size() > ext.size() && name.compare( name.size() - ext.size(), ext.size(), ext ) == 0 ) - return i; + return eindex; } return -1; } +} // end namespace int open_instream( const char * const name, struct stat * const in_statsp, - const Mode program_mode, const int eindex, - const bool recompress, const bool to_stdout ) + const bool no_ofile, const bool reg_only ) { - int infd = -1; - if( program_mode == m_compress && !recompress && eindex >= 0 ) - { - if( verbosity >= 0 ) - std::fprintf( stderr, "%s: Input file '%s' already has '%s' suffix.\n", - program_name, name, known_extensions[eindex].from ); - } + int infd = open( name, O_RDONLY | O_BINARY ); + if( infd < 0 ) + show_file_error( name, "Can't open input file", errno ); else { - infd = open( name, O_RDONLY | O_BINARY ); - if( infd < 0 ) + const int i = fstat( infd, in_statsp ); + const mode_t mode = in_statsp->st_mode; + const bool can_read = ( i == 0 && !reg_only && + ( S_ISBLK( mode ) || S_ISCHR( mode ) || + S_ISFIFO( mode ) || S_ISSOCK( mode ) ) ); + if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || !no_ofile ) ) ) { if( verbosity >= 0 ) - std::fprintf( stderr, "%s: Can't open input file '%s': %s\n", - program_name, name, std::strerror( errno ) ); - } - else - { - const int i = fstat( infd, in_statsp ); - const mode_t mode = in_statsp->st_mode; - 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 ); - if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || !no_ofile ) ) ) - { - if( verbosity >= 0 ) - std::fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n", - program_name, name, - ( can_read && !no_ofile ) ? - ",\n and '--stdout' was not specified" : "" ); - close( infd ); - infd = -1; - } + std::fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n", + program_name, name, + ( can_read && !no_ofile ) ? + ",\n and '--stdout' was not specified" : "" ); + close( infd ); + infd = -1; } } return infd; } +namespace { + +int open_instream2( const char * const name, struct stat * const in_statsp, + const Mode program_mode, const int eindex, + const bool recompress, const bool to_stdout ) + { + if( program_mode == m_compress && !recompress && eindex >= 0 ) + { + if( verbosity >= 0 ) + std::fprintf( stderr, "%s: Input file '%s' already has '%s' suffix.\n", + program_name, name, known_extensions[eindex].from ); + return -1; + } + const bool no_ofile = ( to_stdout || program_mode == m_test ); + return open_instream( name, in_statsp, no_ofile, false ); + } + void set_c_outname( const std::string & name ) { @@ -303,15 +332,15 @@ void set_c_outname( const std::string & name ) } -void set_d_outname( const std::string & name, const int i ) +void set_d_outname( const std::string & name, const int eindex ) { - if( i >= 0 ) + if( eindex >= 0 ) { - const std::string from( known_extensions[i].from ); + 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[i].to; + output_filename += known_extensions[eindex].to; return; } } @@ -345,7 +374,8 @@ bool open_outstream( const bool force, const bool from_stdin ) } -bool check_tty( const int infd, const Mode program_mode ) +bool check_tty( const char * const input_filename, const int infd, + const Mode program_mode ) { if( program_mode == m_compress && isatty( outfd ) ) { @@ -355,7 +385,8 @@ bool check_tty( const int infd, const Mode program_mode ) if( ( program_mode == m_decompress || program_mode == m_test ) && isatty( infd ) ) { - show_error( "I won't read compressed data from a terminal.", 0, true ); + show_file_error( input_filename, + "I won't read compressed data from a terminal." ); return false; } return true; @@ -454,6 +485,16 @@ void show_error( const char * const msg, const int errcode, const bool help ) } +void show_file_error( const char * const filename, const char * const msg, + const int errcode ) + { + if( verbosity < 0 ) return; + std::fprintf( stderr, "%s: %s: %s", program_name, filename, msg ); + if( errcode > 0 ) std::fprintf( stderr, ": %s", std::strerror( errcode ) ); + std::fputc( '\n', stderr ); + } + + void internal_error( const char * const msg ) { if( verbosity >= 0 ) @@ -504,7 +545,6 @@ int main( const int argc, const char * const argv[] ) { 3 << 23, 132 }, // -8 { 1 << 25, 273 } }; // -9 Lzma_options encoder_options = option_mapping[6]; // default = "-6" - std::string input_filename; std::string default_output_filename; std::vector< std::string > filenames; int data_size = 0; @@ -528,6 +568,7 @@ int main( const int argc, const char * const argv[] ) if( max_workers < 1 || max_workers > INT_MAX / (int)sizeof (pthread_t) ) max_workers = INT_MAX / sizeof (pthread_t); + enum Optcode { opt_dbg = 256 }; const Arg_parser::Option options[] = { { '0', "fast", Arg_parser::no }, @@ -545,11 +586,11 @@ int main( const int argc, const char * const argv[] ) { 'B', "data-size", Arg_parser::yes }, { 'c', "stdout", Arg_parser::no }, { 'd', "decompress", Arg_parser::no }, - { 'D', "debug", Arg_parser::yes }, { 'f', "force", Arg_parser::no }, { 'F', "recompress", Arg_parser::no }, { 'h', "help", Arg_parser::no }, { 'k', "keep", Arg_parser::no }, + { 'l', "list", Arg_parser::no }, { 'm', "match-length", Arg_parser::yes }, { 'n', "threads", Arg_parser::yes }, { 'o', "output", Arg_parser::yes }, @@ -559,7 +600,8 @@ int main( const int argc, const char * const argv[] ) { 't', "test", Arg_parser::no }, { 'v', "verbose", Arg_parser::no }, { 'V', "version", Arg_parser::no }, - { 0 , 0, Arg_parser::no } }; + { opt_dbg, "debug", Arg_parser::yes }, + { 0 , 0, Arg_parser::no } }; const Arg_parser parser( argc, argv, options ); if( parser.error().size() ) // bad option @@ -570,8 +612,8 @@ 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(); + const std::string & sarg = parser.argument( argind ); + const char * const arg = sarg.c_str(); switch( code ) { case '0': case '1': case '2': case '3': case '4': @@ -579,27 +621,28 @@ int main( const int argc, const char * const argv[] ) encoder_options = option_mapping[code-'0']; break; case 'a': ignore_trailing = false; break; case 'b': break; - case 'B': data_size = getnum( ptr, 2 * LZ_min_dictionary_size(), + case 'B': data_size = getnum( arg, 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( ptr, 0, 3 ); break; + case 'd': set_mode( program_mode, m_decompress ); 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 'l': set_mode( program_mode, m_list ); break; case 'm': encoder_options.match_len_limit = - getnum( ptr, LZ_min_match_len_limit(), + getnum( arg, 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 'n': num_workers = getnum( arg, 1, max_workers ); break; + case 'o': default_output_filename = sarg; break; case 'q': verbosity = -1; break; - case 's': encoder_options.dictionary_size = get_dict_size( ptr ); + case 's': encoder_options.dictionary_size = get_dict_size( arg ); break; case 'S': break; - case 't': program_mode = m_test; break; + case 't': set_mode( program_mode, m_test ); break; case 'v': if( verbosity < 4 ) ++verbosity; break; case 'V': show_version(); return 0; + case opt_dbg: debug_level = getnum( arg, 0, 3 ); break; default : internal_error( "uncaught option." ); } } // end process options @@ -609,6 +652,17 @@ int main( const int argc, const char * const argv[] ) setmode( STDOUT_FILENO, O_BINARY ); #endif + bool filenames_given = false; + for( ; argind < parser.arguments(); ++argind ) + { + filenames.push_back( parser.argument( argind ) ); + if( filenames.back() != "-" ) filenames_given = true; + } + if( filenames.empty() ) filenames.push_back("-"); + + if( program_mode == m_list ) + return list_files( filenames, ignore_trailing ); + if( program_mode == m_test ) outfd = -1; @@ -626,14 +680,6 @@ int main( const int argc, const char * const argv[] ) if( num_workers <= 0 ) num_workers = std::min( num_online, max_workers ); - bool filenames_given = false; - for( ; argind < parser.arguments(); ++argind ) - { - filenames.push_back( parser.argument( argind ) ); - if( filenames.back() != "-" ) filenames_given = true; - } - - if( filenames.empty() ) filenames.push_back("-"); if( !to_stdout && program_mode != m_test && ( filenames_given || default_output_filename.size() ) ) set_signals(); @@ -644,13 +690,13 @@ int main( const int argc, const char * const argv[] ) bool stdin_used = false; for( unsigned i = 0; i < filenames.size(); ++i ) { + std::string input_filename; struct stat in_stats; output_filename.clear(); 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 ) { @@ -672,10 +718,9 @@ int main( const int argc, const char * const argv[] ) } else { - input_filename = filenames[i]; - const int eindex = extension_index( input_filename ); - infd = open_instream( input_filename.c_str(), &in_stats, program_mode, - eindex, recompress, to_stdout ); + const int eindex = extension_index( input_filename = filenames[i] ); + infd = open_instream2( input_filename.c_str(), &in_stats, program_mode, + eindex, recompress, to_stdout ); if( infd < 0 ) { if( retval < 1 ) retval = 1; continue; } if( program_mode != m_test ) { @@ -695,15 +740,16 @@ int main( const int argc, const char * const argv[] ) } } - if( !check_tty( infd, program_mode ) ) + pp.set_name( input_filename ); + if( !check_tty( pp.name(), infd, program_mode ) ) { if( retval < 1 ) retval = 1; + if( program_mode == m_test ) { close( infd ); infd = -1; continue; } cleanup_and_fail( retval ); } 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 ); if( verbosity >= 1 ) pp(); int tmp; if( program_mode == m_compress ) |