summaryrefslogtreecommitdiffstats
path: root/ztest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'ztest.cc')
-rw-r--r--ztest.cc84
1 files changed, 44 insertions, 40 deletions
diff --git a/ztest.cc b/ztest.cc
index 1145c90..5037fbf 100644
--- a/ztest.cc
+++ b/ztest.cc
@@ -1,5 +1,5 @@
/* Ztest - verify the integrity of compressed files
- Copyright (C) 2010-2018 Antonio Diaz Diaz.
+ Copyright (C) 2010-2019 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
@@ -51,11 +51,12 @@ namespace {
void show_help()
{
std::printf( "Ztest verifies the integrity of the specified compressed files.\n"
- "Uncompressed files are ignored. If no files are specified, or if a file\n"
- "is specified as '-', the integrity of compressed data read from standard\n"
- "input is verified. Data read from standard input must be all in the same\n"
- "compression format.\n"
- "\nThe supported formats are bzip2, gzip, lzip and xz.\n"
+ "Uncompressed files are ignored. If a file is specified as '-', the\n"
+ "integrity of compressed data read from standard input is verified. Data\n"
+ "read from standard input must be all in the same compression format.\n"
+ "\nIf no files are specified, recursive searches examine the current\n"
+ "working directory, and nonrecursive searches read standard input.\n"
+ "\nThe formats supported are bzip2, gzip, lzip and xz.\n"
"\nNote that error detection in the xz format is broken. First, some xz\n"
"files lack integrity information. Second, not all xz decompressors can\n"
"verify the integrity of all xz files. Third, section 2.1.1.2 'Stream\n"
@@ -67,18 +68,19 @@ void show_help()
"problems (file not found, invalid flags, I/O errors, etc), 2 if any\n"
"compressed file is corrupt or invalid.\n"
"\nOptions:\n"
- " -h, --help display this help and exit\n"
- " -V, --version output version information and exit\n"
- " -M, --format=<list> process only the formats in <list>\n"
- " -N, --no-rcfile don't read runtime configuration file\n"
- " -O, --force-format=<fmt> force given format (bz2, gz, lz, xz)\n"
- " -q, --quiet suppress all messages\n"
- " -r, --recursive operate recursively on directories\n"
- " -v, --verbose be verbose (a 2nd -v gives more)\n"
- " --bz2=<command> set compressor and options for bzip2 format\n"
- " --gz=<command> set compressor and options for gzip format\n"
- " --lz=<command> set compressor and options for lzip format\n"
- " --xz=<command> set compressor and options for xz format\n" );
+ " -h, --help display this help and exit\n"
+ " -V, --version output version information and exit\n"
+ " -M, --format=<list> process only the formats in <list>\n"
+ " -N, --no-rcfile don't read runtime configuration file\n"
+ " -O, --force-format=<fmt> force given format (bz2, gz, lz, xz)\n"
+ " -q, --quiet suppress all messages\n"
+ " -r, --recursive operate recursively on directories\n"
+ " -R, --dereference-recursive recursively follow symbolic links\n"
+ " -v, --verbose be verbose (a 2nd -v gives more)\n"
+ " --bz2=<command> set compressor and options for bzip2 format\n"
+ " --gz=<command> set compressor and options for gzip format\n"
+ " --lz=<command> set compressor and options for lzip format\n"
+ " --xz=<command> set compressor and options for xz format\n" );
show_help_addr();
}
@@ -87,7 +89,7 @@ int open_instream( const std::string & input_filename )
{
const int infd = open( input_filename.c_str(), O_RDONLY | O_BINARY );
if( infd < 0 )
- show_error2( "Can't open input file", input_filename.c_str() );
+ show_file_error( input_filename.c_str(), "Can't open input file", errno );
return infd;
}
@@ -211,10 +213,8 @@ int ztest_file( const int infd, int format_index,
int main( const int argc, const char * const argv[] )
{
enum { bz2_opt = 256, gz_opt, lz_opt, xz_opt };
- int infd = -1;
int format_index = -1;
- bool recursive = false;
- std::string input_filename;
+ int recursive = 0; // 1 = '-r', 2 = '-R'
std::list< std::string > filenames;
std::vector< const char * > ztest_args; // args to ztest, maybe empty
invocation_name = argv[0];
@@ -222,19 +222,20 @@ int main( const int argc, const char * const argv[] )
const Arg_parser::Option options[] =
{
- { 'h', "help", Arg_parser::no },
- { 'M', "format", Arg_parser::yes },
- { 'N', "no-rcfile", Arg_parser::no },
- { 'O', "force-format", Arg_parser::yes },
- { 'q', "quiet", Arg_parser::no },
- { 'r', "recursive", Arg_parser::no },
- { 'v', "verbose", Arg_parser::no },
- { 'V', "version", Arg_parser::no },
- { bz2_opt, "bz2", Arg_parser::yes },
- { gz_opt, "gz", Arg_parser::yes },
- { lz_opt, "lz", Arg_parser::yes },
- { xz_opt, "xz", Arg_parser::yes },
- { 0 , 0, Arg_parser::no } };
+ { 'h', "help", Arg_parser::no },
+ { 'M', "format", Arg_parser::yes },
+ { 'N', "no-rcfile", Arg_parser::no },
+ { 'O', "force-format", Arg_parser::yes },
+ { 'q', "quiet", Arg_parser::no },
+ { 'r', "recursive", Arg_parser::no },
+ { 'R', "dereference-recursive", Arg_parser::no },
+ { 'v', "verbose", Arg_parser::no },
+ { 'V', "version", Arg_parser::no },
+ { bz2_opt, "bz2", Arg_parser::yes },
+ { gz_opt, "gz", Arg_parser::yes },
+ { lz_opt, "lz", Arg_parser::yes },
+ { xz_opt, "xz", Arg_parser::yes },
+ { 0 , 0, Arg_parser::no } };
const Arg_parser parser( argc, argv, options );
if( parser.error().size() ) // bad option
@@ -255,7 +256,8 @@ int main( const int argc, const char * const argv[] )
case 'N': break;
case 'O': format_index = parse_format_type( arg ); break;
case 'q': verbosity = -1; ztest_args.push_back( "-q" ); break;
- case 'r': recursive = true; break;
+ case 'r': recursive = 1; break;
+ case 'R': recursive = 2; break;
case 'v': if( verbosity < 4 ) ++verbosity;
ztest_args.push_back( "-v" ); break;
case 'V': show_version(); return 0;
@@ -275,13 +277,15 @@ int main( const int argc, const char * const argv[] )
for( ; argind < parser.arguments(); ++argind )
filenames.push_back( parser.argument( argind ) );
- if( filenames.empty() ) filenames.push_back( "-" );
+ if( filenames.empty() ) filenames.push_back( recursive ? "." : "-" );
+ std::string input_filename;
int retval = 0;
bool error = false;
bool stdin_used = false;
while( next_filename( filenames, input_filename, error, recursive ) )
{
+ int infd;
if( input_filename.empty() )
{
if( stdin_used ) continue; else stdin_used = true;
@@ -299,12 +303,12 @@ int main( const int argc, const char * const argv[] )
else tmp = ztest_file( infd, format_index, input_filename, ztest_args );
if( tmp > retval ) retval = tmp;
- if( input_filename.size() ) { close( infd ); infd = -1; }
+ if( input_filename.size() ) close( infd );
}
- if( std::fclose( stdout ) != 0 )
+ if( std::fclose( stdout ) != 0 ) // in case decompressor writes to stdout
{
- show_error( "Can't close stdout", errno );
+ show_error( "Error closing stdout", errno );
error = true;
}
if( error && retval == 0 ) retval = 1;