1
0
Fork 0

Merging upstream version 0.17.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-17 21:15:18 +01:00
parent dc1796cdfc
commit 68f8c54f95
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
29 changed files with 2935 additions and 2272 deletions

387
main.cc
View file

@ -1,24 +1,24 @@
/* Tarlz - Archiver with multimember lzip compression
Copyright (C) 2013-2019 Antonio Diaz Diaz.
/* Tarlz - Archiver with multimember lzip compression
Copyright (C) 2013-2020 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
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
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
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Exit status: 0 for a normal exit, 1 for environmental problems
(file not found, 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.
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.
*/
#define _FILE_OFFSET_BITS 64
@ -29,6 +29,7 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <string>
#include <vector>
#include <fcntl.h>
@ -59,9 +60,8 @@ int verbosity = 0;
namespace {
const char * const program_name = "tarlz";
const char * const program_year = "2019";
const char * const program_year = "2020";
const char * invocation_name = program_name; // default value
bool dereference = false;
void show_help( const long num_online )
@ -86,47 +86,49 @@ void show_help( const long num_online )
"can be used to recover some of the damaged members.\n"
"\nUsage: %s [options] [files]\n", invocation_name );
std::printf( "\nOptions:\n"
" --help display this help and exit\n"
" -V, --version output version information and exit\n"
" -A, --concatenate append archives to the end of an archive\n"
" -B, --data-size=<bytes> set target size of input data blocks [2x8=16 MiB]\n"
" -c, --create create a new archive\n"
" -C, --directory=<dir> change to directory <dir>\n"
" -d, --diff find differences between archive and file system\n"
" --ignore-ids ignore differences in owner and group IDs\n"
" --delete delete files/directories from an archive\n"
" --exclude=<pattern> exclude files matching a shell pattern\n"
" -f, --file=<archive> use archive file <archive>\n"
" -h, --dereference follow symlinks; archive the files they point to\n"
" -n, --threads=<n> set number of (de)compression threads [%ld]\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"
" -0 .. -9 set compression level [default 6]\n"
" --uncompressed don't compress the archive created\n"
" --asolid create solidly compressed appendable archive\n"
" --bsolid create per block compressed archive (default)\n"
" --dsolid create per directory compressed archive\n"
" --no-solid create per file compressed archive\n"
" --solid create solidly compressed archive\n"
" --anonymous equivalent to '--owner=root --group=root'\n"
" --owner=<owner> use <owner> name/ID for files added\n"
" --group=<group> use <group> name/ID for files added\n"
" --keep-damaged don't delete partially extracted files\n"
" --missing-crc exit with error status if missing extended CRC\n"
" --out-slots=<n> number of 1 MiB output packets buffered [64]\n"
/* " --permissive allow repeated extended headers and records\n"*/,
" --help display this help and exit\n"
" -V, --version output version information and exit\n"
" -A, --concatenate append archives to the end of an archive\n"
" -B, --data-size=<bytes> set target size of input data blocks [2x8=16 MiB]\n"
" -c, --create create a new archive\n"
" -C, --directory=<dir> change to directory <dir>\n"
" -d, --diff find differences between archive and file system\n"
" --ignore-ids ignore differences in owner and group IDs\n"
" --delete delete files/directories from an archive\n"
" --exclude=<pattern> exclude files matching a shell pattern\n"
" -f, --file=<archive> use archive file <archive>\n"
" -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"
" -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"
" -0 .. -9 set compression level [default 6]\n"
" --uncompressed don't compress the archive created\n"
" --asolid create solidly compressed appendable archive\n"
" --bsolid create per block compressed archive (default)\n"
" --dsolid create per directory compressed archive\n"
" --no-solid create per file compressed archive\n"
" --solid create solidly compressed archive\n"
" --anonymous equivalent to '--owner=root --group=root'\n"
" --owner=<owner> use <owner> name/ID for files added to archive\n"
" --group=<group> use <group> name/ID for files added to archive\n"
" --keep-damaged don't delete partially extracted files\n"
" --missing-crc exit with error status if missing extended CRC\n"
" --out-slots=<n> number of 1 MiB output packets buffered [64]\n"
/* " --permissive allow repeated extended headers and records\n"*/,
num_online );
if( verbosity >= 1 )
{
std::printf( " --debug=<level> (0-1) print debug statistics to stderr\n" );
std::printf( " --debug=<level> (0-1) print debug statistics to stderr\n" );
}
std::printf( "\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"
"invalid input file, 3 for an internal consistency error (eg, bug) which\n"
"caused tarlz to panic.\n"
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"
"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" );
}
@ -193,6 +195,17 @@ unsigned long long getnum( const char * const ptr,
}
void set_archive_name( std::string & archive_name, const std::string & new_name )
{
static bool first_call = true;
if( first_call ) { if( new_name != "-" ) archive_name = new_name;
first_call = false; return; }
show_error( "Only one archive can be specified.", 0, true );
std::exit( 1 );
}
void set_mode( Program_mode & program_mode, const Program_mode new_mode )
{
if( program_mode != m_none && program_mode != new_mode )
@ -204,28 +217,58 @@ void set_mode( Program_mode & program_mode, const Program_mode new_mode )
}
void set_owner( const char * const arg )
void set_mtime( long long & mtime, const char * arg )
{
if( *arg == '@' )
{ mtime = getnum( arg + 1, 0, ( 1ULL << 33 ) - 1 ); return; }
else if( *arg == '.' || *arg == '/' )
{
struct stat st;
if( stat( arg, &st ) == 0 ) { mtime = st.st_mtime; return; }
show_file_error( arg, "Can't stat", errno ); std::exit( 1 );
}
else // format 'YYYY-MM-DD HH:MM:SS'
{
unsigned y, mo, d, h, m, s;
const int n = std::sscanf( arg, "%u-%u-%u %u:%u:%u",
&y, &mo, &d, &h, &m, &s );
if( n == 6 && y >= 1970 && mo >= 1 )
{
struct tm t;
t.tm_year = y - 1900; t.tm_mon = mo - 1; t.tm_mday = d;
t.tm_hour = h; t.tm_min = m; t.tm_sec = s; t.tm_isdst = -1;
mtime = std::mktime( &t ); if( mtime >= 0 ) return;
}
}
show_error( "Invalid mtime.", 0, true ); std::exit( 1 );
}
void set_owner( int & owner, const char * const arg )
{
const struct passwd * const pw = getpwnam( arg );
if( pw ) cl_owner = pw->pw_uid;
if( pw ) owner = pw->pw_uid;
else if( std::isdigit( (unsigned char)arg[0] ) )
cl_owner = getnum( arg, 0, INT_MAX );
owner = getnum( arg, 0, INT_MAX );
else if( std::strcmp( arg, "root" ) == 0 ) owner = 0;
else { show_file_error( arg, "Invalid owner" ); std::exit( 1 ); }
}
void set_group( const char * const arg )
void set_group( int & group, const char * const arg )
{
const struct group * const gr = getgrnam( arg );
if( gr ) cl_group = gr->gr_gid;
if( gr ) group = gr->gr_gid;
else if( std::isdigit( (unsigned char)arg[0] ) )
cl_group = getnum( arg, 0, INT_MAX );
group = getnum( arg, 0, INT_MAX );
else if( std::strcmp( arg, "root" ) == 0 ) group = 0;
else { show_file_error( arg, "Invalid group" ); std::exit( 1 ); }
}
} // end namespace
int hstat( const char * const filename, struct stat * const st )
int hstat( const char * const filename, struct stat * const st,
const bool dereference )
{ return dereference ? stat( filename, st ) : lstat( filename, st ); }
@ -251,10 +294,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()
// in case of an error.
//
/* 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()
in case of an error.
*/
void cleanup_and_fail( const int retval )
{
// calling 'exit' more than once results in undefined behavior
@ -278,10 +321,21 @@ void show_error( const char * const msg, const int errcode, const bool help )
}
void format_file_error( std::string & estr, const char * const filename,
const char * const msg, const int errcode )
{
if( verbosity < 0 ) return;
estr += program_name; estr += ": "; estr += filename; estr += ": ";
estr += msg;
if( errcode > 0 ) { estr += ": "; estr += std::strerror( errcode ); }
estr += '\n';
}
void show_file_error( const char * const filename, const char * const msg,
const int errcode )
{
if( verbosity >= 0 )
if( verbosity >= 0 && msg && msg[0] )
std::fprintf( stderr, "%s: %s: %s%s%s\n", program_name, filename, msg,
( errcode > 0 ) ? ": " : "",
( errcode > 0 ) ? std::strerror( errcode ) : "" );
@ -298,128 +352,131 @@ void internal_error( const char * const msg )
int main( const int argc, const char * const argv[] )
{
std::string archive_name;
int debug_level = 0;
int level = 6; // compression level, < 0 means uncompressed
int num_workers = -1; // start this many worker threads
int out_slots = 64;
Program_mode program_mode = m_none;
bool ignore_ids = false;
bool keep_damaged = false;
bool missing_crc = false;
bool permissive = false;
if( argc > 0 ) invocation_name = argv[0];
if( LZ_version()[0] < '1' )
{ show_error( "Bad library version. At least lzlib 1.0 is required." );
#if !defined LZ_API_VERSION || LZ_API_VERSION < 1 // compile-time test
#error "lzlib 1.8 or newer needed."
#elif LZ_API_VERSION >= 2
if( LZ_api_version() < 1 ) // runtime test
{ show_error( "Wrong library version. At least lzlib 1.8 is required." );
return 1; }
#endif
enum { opt_ano = 256, opt_aso, opt_bso, opt_crc, opt_dbg, opt_del, opt_dso,
opt_exc, opt_grp, opt_hlp, opt_id, opt_kd, opt_nso, opt_out, opt_own,
opt_per, opt_sol, opt_un };
opt_exc, opt_grp, opt_hlp, opt_id, opt_kd, opt_mti, opt_nso, opt_out,
opt_own, opt_per, opt_sol, opt_un };
const Arg_parser::Option options[] =
{
{ '0', 0, Arg_parser::no },
{ '1', 0, Arg_parser::no },
{ '2', 0, Arg_parser::no },
{ '3', 0, Arg_parser::no },
{ '4', 0, Arg_parser::no },
{ '5', 0, Arg_parser::no },
{ '6', 0, Arg_parser::no },
{ '7', 0, Arg_parser::no },
{ '8', 0, Arg_parser::no },
{ '9', 0, Arg_parser::no },
{ 'A', "concatenate", Arg_parser::no },
{ 'B', "data-size", Arg_parser::yes },
{ 'c', "create", Arg_parser::no },
{ 'C', "directory", Arg_parser::yes },
{ 'd', "diff", Arg_parser::no },
{ 'f', "file", Arg_parser::yes },
{ 'h', "dereference", Arg_parser::no },
{ 'H', "format", Arg_parser::yes },
{ 'n', "threads", Arg_parser::yes },
{ 'q', "quiet", Arg_parser::no },
{ 'r', "append", Arg_parser::no },
{ 't', "list", Arg_parser::no },
{ 'v', "verbose", Arg_parser::no },
{ 'V', "version", Arg_parser::no },
{ 'x', "extract", Arg_parser::no },
{ opt_ano, "anonymous", Arg_parser::no },
{ opt_aso, "asolid", Arg_parser::no },
{ opt_bso, "bsolid", Arg_parser::no },
{ opt_dbg, "debug", Arg_parser::yes },
{ opt_del, "delete", Arg_parser::no },
{ opt_dso, "dsolid", Arg_parser::no },
{ opt_exc, "exclude", Arg_parser::yes },
{ opt_grp, "group", Arg_parser::yes },
{ opt_hlp, "help", Arg_parser::no },
{ opt_id, "ignore-ids", Arg_parser::no },
{ opt_kd, "keep-damaged", Arg_parser::no },
{ opt_crc, "missing-crc", Arg_parser::no },
{ opt_nso, "no-solid", Arg_parser::no },
{ opt_out, "out-slots", Arg_parser::yes },
{ opt_own, "owner", Arg_parser::yes },
{ opt_per, "permissive", Arg_parser::no },
{ opt_sol, "solid", Arg_parser::no },
{ opt_un, "uncompressed", Arg_parser::no },
{ 0 , 0, Arg_parser::no } };
{ '0', 0, Arg_parser::no },
{ '1', 0, Arg_parser::no },
{ '2', 0, Arg_parser::no },
{ '3', 0, Arg_parser::no },
{ '4', 0, Arg_parser::no },
{ '5', 0, Arg_parser::no },
{ '6', 0, Arg_parser::no },
{ '7', 0, Arg_parser::no },
{ '8', 0, Arg_parser::no },
{ '9', 0, Arg_parser::no },
{ 'A', "concatenate", Arg_parser::no },
{ 'B', "data-size", Arg_parser::yes },
{ 'c', "create", Arg_parser::no },
{ 'C', "directory", Arg_parser::yes },
{ 'd', "diff", Arg_parser::no },
{ 'f', "file", Arg_parser::yes },
{ 'h', "dereference", Arg_parser::no },
{ 'H', "format", Arg_parser::yes },
{ 'n', "threads", Arg_parser::yes },
{ 'p', "preserve-permissions", Arg_parser::no },
{ 'q', "quiet", Arg_parser::no },
{ 'r', "append", Arg_parser::no },
{ 't', "list", Arg_parser::no },
{ 'v', "verbose", Arg_parser::no },
{ 'V', "version", Arg_parser::no },
{ 'x', "extract", Arg_parser::no },
{ opt_ano, "anonymous", Arg_parser::no },
{ opt_aso, "asolid", Arg_parser::no },
{ opt_bso, "bsolid", Arg_parser::no },
{ opt_dbg, "debug", Arg_parser::yes },
{ opt_del, "delete", Arg_parser::no },
{ opt_dso, "dsolid", Arg_parser::no },
{ opt_exc, "exclude", Arg_parser::yes },
{ opt_grp, "group", Arg_parser::yes },
{ opt_hlp, "help", Arg_parser::no },
{ opt_id, "ignore-ids", Arg_parser::no },
{ opt_kd, "keep-damaged", Arg_parser::no },
{ opt_crc, "missing-crc", Arg_parser::no },
{ opt_mti, "mtime", Arg_parser::yes },
{ opt_nso, "no-solid", Arg_parser::no },
{ opt_out, "out-slots", Arg_parser::yes },
{ opt_own, "owner", Arg_parser::yes },
{ opt_per, "permissive", Arg_parser::no },
{ opt_sol, "solid", Arg_parser::no },
{ opt_un, "uncompressed", Arg_parser::no },
{ 0, 0, Arg_parser::no } };
const Arg_parser parser( argc, argv, options, true );
const Arg_parser parser( argc, argv, options, true ); // in_order
if( parser.error().size() ) // bad option
{ show_error( parser.error().c_str(), 0, true ); return 1; }
Cl_options cl_opts( parser );
const long num_online = std::max( 1L, sysconf( _SC_NPROCESSORS_ONLN ) );
long max_workers = sysconf( _SC_THREAD_THREADS_MAX );
if( max_workers < 1 || max_workers > INT_MAX / (int)sizeof (pthread_t) )
max_workers = INT_MAX / sizeof (pthread_t);
int filenames = 0;
for( int argind = 0; argind < parser.arguments(); ++argind )
{
const int code = parser.code( argind );
if( !code ) // skip non-options
{ if( parser.argument( argind ).size() ) ++filenames; continue; }
{
if( parser.argument( argind ).empty() )
{ show_error( "Empty non-option argument." ); return 1; }
++cl_opts.filenames; continue;
}
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':
case '5': case '6': case '7': case '8': case '9':
level = code - '0'; break;
case 'A': set_mode( program_mode, m_concatenate ); break;
case 'B': cl_data_size = getnum( arg, min_data_size, max_data_size );
cl_opts.level = code - '0'; break;
case 'A': set_mode( cl_opts.program_mode, m_concatenate ); break;
case 'B': cl_opts.data_size = getnum( arg, min_data_size, max_data_size );
break;
case 'c': set_mode( program_mode, m_create ); break;
case 'c': set_mode( cl_opts.program_mode, m_create ); break;
case 'C': break; // skip chdir
case 'd': set_mode( program_mode, m_diff ); break;
case 'f': if( sarg != "-" ) archive_name = sarg; break;
case 'h': dereference = true; break;
case 'd': set_mode( cl_opts.program_mode, m_diff ); break;
case 'f': set_archive_name( cl_opts.archive_name, sarg ); break;
case 'h': cl_opts.dereference = true; break;
case 'H': break; // ignore format
case 'n': num_workers = getnum( arg, 0, max_workers ); break;
case 'n': cl_opts.num_workers = getnum( arg, 0, max_workers ); break;
case 'p': cl_opts.preserve_permissions = true; break;
case 'q': verbosity = -1; break;
case 'r': set_mode( program_mode, m_append ); break;
case 't': set_mode( program_mode, m_list ); break;
case 'r': set_mode( cl_opts.program_mode, m_append ); break;
case 't': set_mode( cl_opts.program_mode, m_list ); break;
case 'v': if( verbosity < 4 ) ++verbosity; break;
case 'V': show_version(); return 0;
case 'x': set_mode( program_mode, m_extract ); break;
case opt_ano: set_owner( "root" ); set_group( "root" ); break;
case opt_aso: solidity = asolid; break;
case opt_bso: solidity = bsolid; break;
case opt_crc: missing_crc = true; break;
case opt_dbg: debug_level = getnum( arg, 0, 3 ); break;
case opt_del: set_mode( program_mode, m_delete ); break;
case opt_dso: solidity = dsolid; break;
case 'x': set_mode( cl_opts.program_mode, m_extract ); 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;
case opt_bso: cl_opts.solidity = bsolid; break;
case opt_crc: cl_opts.missing_crc = true; break;
case opt_dbg: cl_opts.debug_level = getnum( arg, 0, 3 ); break;
case opt_del: set_mode( cl_opts.program_mode, m_delete ); break;
case opt_dso: cl_opts.solidity = dsolid; break;
case opt_exc: Exclude::add_pattern( sarg ); break;
case opt_grp: set_group( arg ); break;
case opt_grp: set_group( cl_opts.group, arg ); break;
case opt_hlp: show_help( num_online ); return 0;
case opt_id: ignore_ids = true; break;
case opt_kd: keep_damaged = true; break;
case opt_nso: solidity = no_solid; break;
case opt_out: out_slots = getnum( arg, 1, 1024 ); break;
case opt_own: set_owner( arg ); break;
case opt_per: permissive = true; break;
case opt_sol: solidity = solid; break;
case opt_un: level = -1; break;
case opt_id: cl_opts.ignore_ids = true; break;
case opt_kd: cl_opts.keep_damaged = true; break;
case opt_mti: set_mtime( cl_opts.mtime, arg ); break;
case opt_nso: cl_opts.solidity = no_solid; break;
case opt_out: cl_opts.out_slots = getnum( arg, 1, 1024 ); break;
case opt_own: set_owner( cl_opts.owner, arg ); break;
case opt_per: cl_opts.permissive = true; break;
case opt_sol: cl_opts.solidity = solid; break;
case opt_un: cl_opts.level = -1; break;
default : internal_error( "uncaught option" );
}
} // end process options
@ -429,22 +486,18 @@ int main( const int argc, const char * const argv[] )
setmode( STDOUT_FILENO, O_BINARY );
#endif
if( num_workers < 0 ) num_workers = std::min( num_online, max_workers );
if( cl_opts.num_workers < 0 ) // 0 disables multi-threading
cl_opts.num_workers = std::min( num_online, max_workers );
switch( program_mode )
switch( cl_opts.program_mode )
{
case m_none: show_error( "Missing operation.", 0, true ); return 2;
case m_none: show_error( "Missing operation.", 0, true ); return 1;
case m_append:
case m_create: return encode( archive_name, parser, filenames, level,
num_workers, out_slots, debug_level,
program_mode == m_append, dereference );
case m_concatenate: return concatenate( archive_name, parser, filenames );
case m_delete: return delete_members( archive_name, parser, filenames,
missing_crc, permissive );
case m_create: return encode( cl_opts );
case m_concatenate: return concatenate( cl_opts );
case m_delete: return delete_members( cl_opts );
case m_diff:
case m_extract:
case m_list: return decode( archive_name, parser, filenames,
num_workers, debug_level, program_mode,
ignore_ids, keep_damaged, missing_crc, permissive );
case m_list: return decode( cl_opts );
}
}