summaryrefslogtreecommitdiffstats
path: root/main.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-01-23 05:31:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-01-23 05:31:48 +0000
commit9845662681ec82b0fa7a43a77d6ca23cf88a840b (patch)
tree1948c183ae1c82950b71f81f4675e5bae9eeb5c6 /main.cc
parentReleasing debian version 1.24~pre1-2. (diff)
downloadlziprecover-9845662681ec82b0fa7a43a77d6ca23cf88a840b.tar.xz
lziprecover-9845662681ec82b0fa7a43a77d6ca23cf88a840b.zip
Merging upstream version 1.24.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'main.cc')
-rw-r--r--main.cc137
1 files changed, 81 insertions, 56 deletions
diff --git a/main.cc b/main.cc
index ed8f1ed..f82118f 100644
--- a/main.cc
+++ b/main.cc
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2023 Antonio Diaz Diaz.
+ Copyright (C) 2009-2024 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
@@ -16,7 +16,7 @@
*/
/*
Exit status: 0 for a normal exit, 1 for environmental problems
- (file not found, invalid command line options, I/O errors, etc), 2 to
+ (file not found, invalid command-line options, I/O errors, etc), 2 to
indicate a corrupt or invalid input file, 3 for an internal consistency
error (e.g., bug) which caused lziprecover to panic.
*/
@@ -76,7 +76,7 @@
#error "Environments where 'size_t' is narrower than 'long' are not supported."
#endif
-bool fits_in_size_t( const unsigned long long size )
+bool fits_in_size_t( const unsigned long long size ) // fits also in long
{ return ( sizeof (long) <= sizeof (size_t) && size <= LONG_MAX ) ||
( sizeof (int) <= sizeof (size_t) && size <= INT_MAX ); }
@@ -141,7 +141,7 @@ void show_help()
" -i, --ignore-errors ignore some errors in -d, -D, -l, -t, --dump\n"
" -k, --keep keep (don't delete) input files\n"
" -l, --list print (un)compressed file sizes\n"
- " -m, --merge correct errors in file using several copies\n"
+ " -m, --merge repair errors in file using several copies\n"
" -o, --output=<file> place the output into <file>\n"
" -q, --quiet suppress all messages\n"
" -R, --byte-repair try to repair a corrupt byte in file\n"
@@ -174,7 +174,7 @@ void show_help()
"\nTo extract all the files from archive 'foo.tar.lz', use the commands\n"
"'tar -xf foo.tar.lz' or 'lziprecover -cd foo.tar.lz | tar -xf -'.\n"
"\nExit status: 0 for a normal exit, 1 for environmental problems\n"
- "(file not found, invalid command line options, I/O errors, etc), 2 to\n"
+ "(file not found, invalid command-line options, I/O errors, etc), 2 to\n"
"indicate a corrupt or invalid input file, 3 for an internal consistency\n"
"error (e.g., bug) which caused lziprecover to panic.\n"
"\nReport bugs to lzip-bug@nongnu.org\n"
@@ -207,15 +207,15 @@ const char * bad_version( const unsigned version )
const char * format_ds( const unsigned dictionary_size )
{
- enum { bufsize = 16, factor = 1024 };
+ enum { bufsize = 16, factor = 1024, n = 3 };
static char buf[bufsize];
- const char * const prefix[3] = { "Ki", "Mi", "Gi" };
+ const char * const prefix[n] = { "Ki", "Mi", "Gi" };
const char * p = "";
const char * np = " ";
unsigned num = dictionary_size;
bool exact = ( num % factor == 0 );
- for( int i = 0; i < 3 && ( num > 9999 || ( exact && num >= factor ) ); ++i )
+ for( int i = 0; i < n && ( 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 );
@@ -280,7 +280,6 @@ next:
namespace {
// Recognized formats: <digit> 'a' m[<match_length>]
-//
int parse_lzip_level( const char * const arg, const char * const option_name )
{
if( *arg == 'a' || std::isdigit( *(const unsigned char *)arg ) ) return *arg;
@@ -408,7 +407,7 @@ void set_d_outname( const std::string & name, const int eindex )
}
output_filename = name; output_filename += ".out";
if( verbosity >= 1 )
- std::fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'\n",
+ std::fprintf( stderr, "%s: %s: Can't guess original name -- using '%s'\n",
program_name, name.c_str(), output_filename.c_str() );
}
@@ -430,9 +429,9 @@ int open_instream( const char * const name, struct stat * const in_statsp,
if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || one_to_one ) ) )
{
if( verbosity >= 0 )
- std::fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n",
+ std::fprintf( stderr, "%s: %s: Input file is not a regular file%s.\n",
program_name, name, ( can_read && one_to_one ) ?
- ",\n and neither '-c' nor '-o' were specified" : "" );
+ ",\n and neither '-c' nor '-o' were specified" : "" );
close( infd );
infd = -1;
}
@@ -457,9 +456,41 @@ int open_truncable_stream( const char * const name,
return fd;
}
+namespace {
+
+bool make_dirs( const std::string & name )
+ {
+ int i = name.size();
+ while( i > 0 && name[i-1] != '/' ) --i; // remove last component
+ while( i > 0 && name[i-1] == '/' ) --i; // remove slash(es)
+ const int dirsize = i; // size of dirname without trailing slash(es)
+
+ for( i = 0; i < dirsize; ) // if dirsize == 0, dirname is '/' or empty
+ {
+ while( i < dirsize && name[i] == '/' ) ++i;
+ const int first = i;
+ while( i < dirsize && name[i] != '/' ) ++i;
+ if( first < i )
+ {
+ const std::string partial( name, 0, i );
+ const mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
+ struct stat st;
+ if( stat( partial.c_str(), &st ) == 0 )
+ { if( !S_ISDIR( st.st_mode ) ) { errno = ENOTDIR; return false; } }
+ else if( mkdir( partial.c_str(), mode ) != 0 && errno != EEXIST )
+ return false; // if EEXIST, another process created the dir
+ }
+ }
+ return true;
+ }
+
+const char * const force_msg =
+ "Output file already exists. Use '--force' to overwrite it.";
+
+} // end namespace
bool open_outstream( const bool force, const bool protect,
- const bool rw, const bool skipping )
+ const bool rw, const bool skipping, const bool to_file )
{
const mode_t usr_rw = S_IRUSR | S_IWUSR;
const mode_t all_rw = usr_rw | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
@@ -467,19 +498,21 @@ bool open_outstream( const bool force, const bool protect,
int flags = O_CREAT | ( rw ? O_RDWR : 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 ) delete_output_on_interrupt = true;
- else if( verbosity >= 0 )
- {
+ outfd = -1;
+ if( output_filename.size() &&
+ output_filename[output_filename.size()-1] == '/' ) errno = EISDIR;
+ else {
+ if( ( !protect || to_file ) && !make_dirs( output_filename ) )
+ { show_file_error( output_filename.c_str(),
+ "Error creating intermediate directory", errno ); return false; }
+ outfd = open( output_filename.c_str(), flags, outfd_mode );
+ if( outfd >= 0 ) { delete_output_on_interrupt = true; return true; }
if( errno == EEXIST )
- std::fprintf( stderr, "%s: Output file '%s' already exists%s.\n",
- program_name, output_filename.c_str(), skipping ?
- ", skipping" : ". Use '--force' to overwrite it" );
- else
- std::fprintf( stderr, "%s: Can't create output file '%s': %s\n",
- program_name, output_filename.c_str(), std::strerror( errno ) );
+ { show_file_error( output_filename.c_str(), skipping ?
+ "Output file already exists, skipping." : force_msg ); return false; }
}
- return ( outfd >= 0 );
+ show_file_error( output_filename.c_str(), "Can't create output file", errno );
+ return false;
}
@@ -487,13 +520,7 @@ bool output_file_exists()
{
struct stat st;
if( stat( output_filename.c_str(), &st ) == 0 )
- {
- if( verbosity >= 0 )
- std::fprintf( stderr, "%s: Output file '%s' already exists."
- " Use '--force' to overwrite it.\n",
- program_name, output_filename.c_str() );
- return true;
- }
+ { show_file_error( output_filename.c_str(), force_msg ); return true; }
return false;
}
@@ -512,12 +539,11 @@ void cleanup_and_fail( const int retval )
if( delete_output_on_interrupt )
{
delete_output_on_interrupt = false;
- if( verbosity >= 0 )
- std::fprintf( stderr, "%s: Deleting output file '%s', if it exists.\n",
- program_name, output_filename.c_str() );
+ show_file_error( output_filename.c_str(),
+ "Deleting output file, if it exists." );
if( outfd >= 0 ) { close( outfd ); outfd = -1; }
if( std::remove( output_filename.c_str() ) != 0 && errno != ENOENT )
- show_error( "WARNING: deletion of output file (apparently) failed." );
+ show_error( "warning: deletion of output file failed", errno );
}
std::exit( retval );
}
@@ -565,7 +591,7 @@ void close_and_set_permissions( const struct stat * const in_statsp )
if( in_statsp )
{
const mode_t mode = in_statsp->st_mode;
- // fchown will in many cases return with EPERM, which can be safely ignored.
+ // fchown in many cases returns with EPERM, which can be safely ignored.
if( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) == 0 )
{ if( fchmod( outfd, mode ) != 0 ) warning = true; }
else
@@ -574,10 +600,8 @@ void close_and_set_permissions( const struct stat * const in_statsp )
warning = true;
}
if( close( outfd ) != 0 )
- {
- show_error( "Error closing output file", errno );
- cleanup_and_fail( 1 );
- }
+ { show_file_error( output_filename.c_str(), "Error closing output file",
+ errno ); cleanup_and_fail( 1 ); }
outfd = -1;
delete_output_on_interrupt = false;
if( in_statsp )
@@ -588,7 +612,8 @@ void close_and_set_permissions( const struct stat * const in_statsp )
if( utime( output_filename.c_str(), &t ) != 0 ) warning = true;
}
if( warning && verbosity >= 1 )
- show_error( "Can't change output file attributes." );
+ show_file_error( output_filename.c_str(),
+ "warning: can't change output file attributes", errno );
}
@@ -622,7 +647,7 @@ bool show_trailing_data( const uint8_t * const data, const int size,
pp( msg.c_str() );
if( ignore_trailing == 0 ) show_file_error( pp.name(), trailing_msg );
}
- return ( ignore_trailing > 0 );
+ return ignore_trailing > 0;
}
@@ -640,7 +665,7 @@ int decompress( const unsigned long long cfile_size, const int infd,
rdec.reset_member_position();
const int size = rdec.read_header_carefully( header, cl_opts.ignore_errors );
if( rdec.finished() || // End Of File
- ( size < Lzip_header::size && !rdec.find_header( header ) ) )
+ ( size < header.size && !rdec.find_header( header ) ) )
{
if( first_member )
{ show_file_error( pp.name(), "File ends unexpectedly at member header." );
@@ -675,8 +700,7 @@ int decompress( const unsigned long long cfile_size, const int infd,
LZ_decoder decoder( rdec, dictionary_size, outfd );
show_dprogress( cfile_size, partial_file_pos, &rdec, &pp ); // init
- const int result =
- decoder.decode_member( pp, cl_opts.ignore_empty, cl_opts.ignore_marking );
+ const int result = decoder.decode_member( cl_opts, pp );
partial_file_pos += rdec.member_position();
if( result != 0 )
{
@@ -705,14 +729,13 @@ int decompress( const unsigned long long cfile_size, const int infd,
void set_signal_handler() { set_signals( signal_handler ); }
-int close_outstream( const struct stat * const in_statsp )
+bool close_outstream( const struct stat * const in_statsp )
{
- if( delete_output_on_interrupt )
- close_and_set_permissions( in_statsp );
+ if( delete_output_on_interrupt ) close_and_set_permissions( in_statsp );
if( outfd >= 0 && close( outfd ) != 0 )
- { show_error( "Error closing stdout", errno ); return 1; }
+ { show_error( "Error closing stdout", errno ); return false; }
outfd = -1;
- return 0;
+ return true;
}
@@ -783,7 +806,7 @@ int main( const int argc, const char * const argv[] )
// '0'..'9' = level, 'a' = all levels
// -5..-273 = match length, -1 = all lengths
int repeated_byte = -1; // 0 to 255, or -1 for all values
- Cl_options cl_opts; // command line options
+ Cl_options cl_opts; // command-line options
bool force = false;
bool keep_input_files = false;
bool to_stdout = false;
@@ -825,14 +848,14 @@ int main( const int argc, const char * const argv[] )
{ opt_cm, "clear-marking", Arg_parser::no },
{ opt_du, "dump", Arg_parser::yes },
{ opt_eer, "empty-error", Arg_parser::no },
- { opt_mer, "marking-error", Arg_parser::no },
{ opt_lt, "loose-trailing", Arg_parser::no },
{ opt_lzl, "lzip-level", Arg_parser::yes },
{ opt_lzn, "lzip-name", Arg_parser::yes },
+ { opt_mer, "marking-error", Arg_parser::no },
{ opt_ref, "reference-file", Arg_parser::yes },
{ opt_rem, "remove", Arg_parser::yes },
{ opt_st, "strip", Arg_parser::yes },
- { 0 , 0, Arg_parser::no } };
+ { 0, 0, Arg_parser::no } };
const Arg_parser parser( argc, argv, options );
if( parser.error().size() ) // bad option
@@ -898,7 +921,7 @@ int main( const int argc, const char * const argv[] )
member_list.parse_ml( arg, pn, cl_opts ); break;
case opt_st: set_mode( program_mode, m_strip );
member_list.parse_ml( arg, pn, cl_opts ); break;
- default : internal_error( "uncaught option." );
+ default: internal_error( "uncaught option." );
}
} // end process options
@@ -1020,11 +1043,11 @@ int main( const int argc, const char * const argv[] )
int retval = 0;
const bool one_to_one = !to_stdout && program_mode != m_test && !to_file;
bool stdin_used = false;
+ struct stat in_stats;
for( unsigned i = 0; i < filenames.size(); ++i )
{
std::string input_filename;
int infd;
- struct stat in_stats;
pp.set_name( filenames[i] );
if( filenames[i] == "-" )
@@ -1087,7 +1110,9 @@ int main( const int argc, const char * const argv[] )
( program_mode != m_decompress || !cl_opts.ignore_errors ) )
std::remove( input_filename.c_str() );
}
- if( delete_output_on_interrupt ) close_and_set_permissions( 0 ); // -o
+ if( delete_output_on_interrupt ) // -o
+ close_and_set_permissions( ( retval == 0 && !stdin_used &&
+ filenames_given && filenames.size() == 1 ) ? &in_stats : 0 );
else if( outfd >= 0 && close( outfd ) != 0 ) // -c
{
show_error( "Error closing stdout", errno );