diff options
Diffstat (limited to 'main.cc')
-rw-r--r-- | main.cc | 62 |
1 files changed, 30 insertions, 32 deletions
@@ -1,6 +1,6 @@ /* Plzip - Parallel compressor compatible with lzip Copyright (C) 2009 Laszlo Ersek. - Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. + Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 @@ -59,6 +59,10 @@ #include "arg_parser.h" #include "lzip.h" +#ifndef O_BINARY +#define O_BINARY 0 +#endif + #if CHAR_BIT != 8 #error "Environments where CHAR_BIT != 8 are not supported." #endif @@ -68,15 +72,9 @@ namespace { const char * const Program_name = "Plzip"; const char * const program_name = "plzip"; -const char * const program_year = "2013"; +const char * const program_year = "2014"; const char * invocation_name = 0; -#ifdef O_BINARY -const int o_binary = O_BINARY; -#else -const int o_binary = 0; -#endif - struct { const char * from; const char * to; } const known_extensions[] = { { ".lz", "" }, { ".tlz", ".tar" }, @@ -247,7 +245,8 @@ int open_instream( const char * const name, struct stat * const in_statsp, } else { - infd = open( name, O_RDONLY | o_binary ); + do infd = open( name, O_RDONLY | O_BINARY ); + while( infd < 0 && errno == EINTR ); if( infd < 0 ) { if( verbosity >= 0 ) @@ -306,10 +305,11 @@ void set_d_outname( const std::string & name, const int i ) bool open_outstream( const bool force ) { - int flags = O_CREAT | O_WRONLY | o_binary; + 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 ); + do outfd = open( output_filename.c_str(), flags, outfd_mode ); + while( outfd < 0 && errno == EINTR ); if( outfd < 0 && verbosity >= 0 ) { if( errno == EEXIST ) @@ -346,10 +346,14 @@ void close_and_set_permissions( const struct stat * const in_statsp ) bool warning = false; if( in_statsp ) { + const mode_t mode = in_statsp->st_mode; // fchown will in many cases return with EPERM, which can be safely ignored. - if( ( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) != 0 && - errno != EPERM ) || - fchmod( outfd, in_statsp->st_mode ) != 0 ) warning = true; + if( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) == 0 ) + { if( fchmod( outfd, mode ) != 0 ) warning = true; } + else + if( errno != EPERM || + fchmod( outfd, mode & ~( S_ISUID | S_ISGID | S_ISVTX ) ) != 0 ) + warning = true; } if( close( outfd ) != 0 ) cleanup_and_fail( 1 ); outfd = -1; @@ -455,27 +459,21 @@ void cleanup_and_fail( const int retval ) void show_progress( const int packet_size, const Pretty_print * const p, - const struct stat * const in_statsp ) + const unsigned long long cfile_size ) { - static unsigned long long cfile_size = 0; // file_size / 100 + static unsigned long long csize = 0; // file_size / 100 static unsigned long long pos = 0; static const Pretty_print * pp = 0; - static pthread_mutex_t mutex; + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; if( p ) // initialize static vars - { - if( !pp ) xinit( &mutex ); // init mutex only once - pos = 0; pp = p; - cfile_size = ( in_statsp && S_ISREG( in_statsp->st_mode ) ) ? - in_statsp->st_size / 100 : 0; - return; - } + { csize = cfile_size; pos = 0; pp = p; return; } if( pp ) { xlock( &mutex ); pos += packet_size; - if( cfile_size > 0 ) - std::fprintf( stderr, "%4llu%%", pos / cfile_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 ); @@ -485,8 +483,8 @@ void show_progress( const int packet_size, int main( const int argc, const char * const argv[] ) { - // Mapping from gzip/bzip2 style 1..9 compression modes - // to the corresponding LZMA compression modes. + /* Mapping from gzip/bzip2 style 1..9 compression modes + to the corresponding LZMA compression modes. */ const Lzma_options option_mapping[] = { { 1 << 20, 5 }, // -0 @@ -566,8 +564,7 @@ int main( const int argc, const char * const argv[] ) const char * const arg = parser.argument( argind ).c_str(); switch( code ) { - case '0': - case '1': case '2': case '3': case '4': + 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 'b': break; @@ -692,8 +689,9 @@ int main( const int argc, const char * const argv[] ) int tmp; if( program_mode == m_compress ) { - show_progress( 0, &pp, in_statsp ); // initialize static vars - if( verbosity >= 2 ) show_progress( 0 ); // show initial zero size + if( verbosity >= 2 ) + show_progress( 0, &pp, ( in_statsp && S_ISREG( in_statsp->st_mode ) ) ? + in_statsp->st_size / 100 : 0 ); // init tmp = compress( data_size, encoder_options.dictionary_size, encoder_options.match_len_limit, num_workers, infd, outfd, pp, debug_level ); |