diff options
Diffstat (limited to 'main.cc')
-rw-r--r-- | main.cc | 52 |
1 files changed, 32 insertions, 20 deletions
@@ -18,34 +18,30 @@ Exit status: 0 for a normal exit, 1 for environmental problems (file not found, files differ, invalid flags, I/O errors, etc), 2 to indicate a corrupt or invalid input file, 3 for an internal consistency error - (eg, bug) which caused tarlz to panic. + (e.g. bug) which caused tarlz to panic. */ #define _FILE_OFFSET_BITS 64 #include <cctype> #include <cerrno> -#include <climits> #include <cstdio> #include <cstdlib> -#include <cstring> #include <ctime> -#include <string> -#include <vector> #include <fcntl.h> #include <pthread.h> -#include <stdint.h> +#include <stdint.h> // for lzlib.h #include <unistd.h> #include <sys/stat.h> #include <grp.h> #include <pwd.h> #include <lzlib.h> -#if defined(__OS2__) +#if defined __OS2__ #include <io.h> #endif -#include "arg_parser.h" #include "tarlz.h" +#include "arg_parser.h" #ifndef O_BINARY #define O_BINARY 0 @@ -56,10 +52,10 @@ #endif int verbosity = 0; +const char * const program_name = "tarlz"; namespace { -const char * const program_name = "tarlz"; const char * const program_year = "2021"; const char * invocation_name = program_name; // default value @@ -101,12 +97,14 @@ void show_help( const long num_online ) " -h, --dereference follow symlinks; archive the files they point to\n" " --mtime=<date> use <date> as mtime for files added to archive\n" " -n, --threads=<n> set number of (de)compression threads [%ld]\n" + " -o, --output=<file> compress to <file>\n" " -p, --preserve-permissions don't subtract the umask on extraction\n" " -q, --quiet suppress all messages\n" " -r, --append append files to the end of an archive\n" " -t, --list list the contents of an archive\n" " -v, --verbose verbosely list files processed\n" " -x, --extract extract files/directories from an archive\n" + " -z, --compress compress existing POSIX tar archives\n" " -0 .. -9 set compression level [default 6]\n" " --uncompressed don't compress the archive created\n" " --asolid create solidly compressed appendable archive\n" @@ -121,6 +119,7 @@ void show_help( const long num_online ) " --missing-crc exit with error status if missing extended CRC\n" " --out-slots=<n> number of 1 MiB output packets buffered [64]\n" " --check-lib compare version of lzlib.h with liblz.{a,so}\n" + " --warn-newer warn if any file is newer than the archive\n" /* " --permissive allow repeated extended headers and records\n"*/, num_online ); if( verbosity >= 1 ) @@ -129,7 +128,7 @@ void show_help( const long num_online ) } std::printf( "\nExit status: 0 for a normal exit, 1 for environmental problems (file not\n" "found, files differ, invalid flags, I/O errors, etc), 2 to indicate a\n" - "corrupt or invalid input file, 3 for an internal consistency error (eg, bug)\n" + "corrupt or invalid input file, 3 for an internal consistency error (e.g. bug)\n" "which caused tarlz to panic.\n" "\nReport bugs to lzip-bug@nongnu.org\n" "Tarlz home page: http://www.nongnu.org/lzip/tarlz.html\n" ); @@ -315,15 +314,17 @@ int open_instream( const std::string & name ) int open_outstream( const std::string & name, const bool create, - Resizable_buffer * const rbufp ) + Resizable_buffer * const rbufp, const bool force ) { - const int flags = (create ? O_CREAT | O_WRONLY | O_TRUNC : O_RDWR) | O_BINARY; + const int cflags = O_CREAT | O_WRONLY | ( force ? O_TRUNC : O_EXCL ); + const int flags = ( create ? cflags : O_RDWR ) | O_BINARY; const mode_t outfd_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; const int outfd = open( name.c_str(), flags, outfd_mode ); if( outfd < 0 ) { - const char * msg = create ? "Can't create file" : "Error opening file"; + const char * msg = !create ? "Error opening file" : + ( ( errno == EEXIST ) ? "Skipping file" : "Can't create file" ); if( !rbufp ) show_file_error( name.c_str(), msg, errno ); else snprintf( (*rbufp)(), (*rbufp).size(), "%s: %s: %s\n", name.c_str(), @@ -334,10 +335,10 @@ int open_outstream( const std::string & name, const bool create, /* This can be called from any thread, main thread or sub-threads alike, - since they all call common helper functions that call cleanup_and_fail() + since they all call common helper functions that call exit_fail_mt() in case of an error. */ -void cleanup_and_fail( const int retval ) +void exit_fail_mt( const int retval ) { // calling 'exit' more than once results in undefined behavior static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; @@ -395,7 +396,7 @@ int main( const int argc, const char * const argv[] ) enum { opt_ano = 256, opt_aso, opt_bso, opt_chk, opt_crc, opt_dbg, opt_del, opt_dso, opt_exc, opt_grp, opt_hlp, opt_id, opt_kd, opt_mti, opt_nso, - opt_out, opt_own, opt_per, opt_sol, opt_un }; + opt_out, opt_own, opt_per, opt_sol, opt_un, opt_wn }; const Arg_parser::Option options[] = { { '0', 0, Arg_parser::no }, @@ -417,6 +418,7 @@ int main( const int argc, const char * const argv[] ) { 'h', "dereference", Arg_parser::no }, { 'H', "format", Arg_parser::yes }, { 'n', "threads", Arg_parser::yes }, + { 'o', "output", Arg_parser::yes }, { 'p', "preserve-permissions", Arg_parser::no }, { 'q', "quiet", Arg_parser::no }, { 'r', "append", Arg_parser::no }, @@ -424,6 +426,7 @@ int main( const int argc, const char * const argv[] ) { 'v', "verbose", Arg_parser::no }, { 'V', "version", Arg_parser::no }, { 'x', "extract", Arg_parser::no }, + { 'z', "compress", Arg_parser::no }, { opt_ano, "anonymous", Arg_parser::no }, { opt_aso, "asolid", Arg_parser::no }, { opt_bso, "bsolid", Arg_parser::no }, @@ -444,6 +447,7 @@ int main( const int argc, const char * const argv[] ) { opt_per, "permissive", Arg_parser::no }, { opt_sol, "solid", Arg_parser::no }, { opt_un, "uncompressed", Arg_parser::no }, + { opt_wn, "warn-newer", Arg_parser::no }, { 0, 0, Arg_parser::no } }; const Arg_parser parser( argc, argv, options, true ); // in_order @@ -463,7 +467,8 @@ int main( const int argc, const char * const argv[] ) { if( parser.argument( argind ).empty() ) { show_error( "Empty non-option argument." ); return 1; } - ++cl_opts.filenames; continue; + if( parser.argument( argind ) != "-" ) cl_opts.filenames_given = true; + ++cl_opts.num_files; continue; } const std::string & sarg = parser.argument( argind ); const char * const arg = sarg.c_str(); @@ -482,6 +487,7 @@ int main( const int argc, const char * const argv[] ) case 'h': cl_opts.dereference = true; break; case 'H': break; // ignore format case 'n': cl_opts.num_workers = getnum( arg, 0, max_workers ); break; + case 'o': cl_opts.output_filename = sarg; break; case 'p': cl_opts.preserve_permissions = true; break; case 'q': verbosity = -1; break; case 'r': set_mode( cl_opts.program_mode, m_append ); break; @@ -489,6 +495,7 @@ int main( const int argc, const char * const argv[] ) case 'v': if( verbosity < 4 ) ++verbosity; break; case 'V': show_version(); return 0; case 'x': set_mode( cl_opts.program_mode, m_extract ); break; + case 'z': set_mode( cl_opts.program_mode, m_compress ); break; case opt_ano: set_owner( cl_opts.owner, "root" ); set_group( cl_opts.group, "root" ); break; case opt_aso: cl_opts.solidity = asolid; break; @@ -510,15 +517,19 @@ int main( const int argc, const char * const argv[] ) case opt_per: cl_opts.permissive = true; break; case opt_sol: cl_opts.solidity = solid; break; case opt_un: cl_opts.level = -1; break; + case opt_wn: cl_opts.warn_newer = true; break; default : internal_error( "uncaught option" ); } } // end process options -#if !defined LZ_API_VERSION || LZ_API_VERSION < 1 // compile-time test -#error "lzlib 1.8 or newer needed." +#if !defined LZ_API_VERSION || LZ_API_VERSION < 1012 // compile-time test +#error "lzlib 1.12 or newer needed." #endif + if( LZ_api_version() < 1012 ) // runtime test + { show_error( "Wrong library version. At least lzlib 1.12 is required." ); + return 1; } -#if defined(__MSVCRT__) || defined(__OS2__) +#if defined __OS2__ setmode( STDIN_FILENO, O_BINARY ); setmode( STDOUT_FILENO, O_BINARY ); #endif @@ -531,6 +542,7 @@ int main( const int argc, const char * const argv[] ) case m_none: show_error( "Missing operation.", 0, true ); return 1; case m_append: case m_create: return encode( cl_opts ); + case m_compress: return compress( cl_opts ); case m_concatenate: return concatenate( cl_opts ); case m_delete: return delete_members( cl_opts ); case m_diff: |