diff options
Diffstat (limited to 'ztest.cc')
-rw-r--r-- | ztest.cc | 67 |
1 files changed, 43 insertions, 24 deletions
@@ -1,18 +1,18 @@ -/* Ztest - verify the integrity of compressed files - Copyright (C) 2010-2019 Antonio Diaz Diaz. +/* Ztest - verify the integrity of compressed files + Copyright (C) 2010-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/>. */ #define _FILE_OFFSET_BITS 64 @@ -50,13 +50,17 @@ namespace { void show_help() { - std::printf( "Ztest verifies the integrity of the specified compressed files.\n" + std::printf( "ztest verifies the integrity of the compressed files specified.\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" + "read from standard input must be all in the same compressed format. If\n" + "a file fails to decompress, does not exist, can't be opened, or is a\n" + "terminal, ztest continues verifying the rest of the files. A final\n" + "diagnostic is shown at verbosity level 1 or higher if any file fails the\n" + "test when testing multiple files.\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" + "\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" @@ -72,7 +76,7 @@ void show_help() " -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" + " -O, --force-format=<fmt> force the format given (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" @@ -97,10 +101,10 @@ int open_instream( const std::string & input_filename ) int ztest_stdin( const int infd, int format_index, const std::vector< const char * > & ztest_args ) { - const uint8_t * magic_data = 0; + uint8_t magic_data[magic_buf_size]; int magic_size = 0; if( format_index < 0 ) - format_index = test_format( infd, &magic_data, &magic_size ); + format_index = test_format( infd, magic_data, &magic_size ); const char * const compressor_name = get_compressor_name( format_index ); if( !compressor_name ) { show_error( "Unknown data format read from stdin." ); return 2; } @@ -161,10 +165,10 @@ int ztest_file( const int infd, int format_index, const std::vector< const char * > & ztest_args ) { static int disable_xz = -1; // tri-state bool - const uint8_t * magic_data = 0; + uint8_t magic_data[magic_buf_size]; int magic_size = 0; if( format_index < 0 ) - format_index = test_format( infd, &magic_data, &magic_size ); + format_index = test_format( infd, magic_data, &magic_size ); const char * const compressor_name = get_compressor_name( format_index ); if( !compressor_name ) return 0; // ignore this file @@ -217,8 +221,8 @@ int main( const int argc, const char * const argv[] ) 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]; program_name = "ztest"; + invocation_name = ( argc > 0 ) ? argv[0] : program_name; const Arg_parser::Option options[] = { @@ -280,16 +284,17 @@ int main( const int argc, const char * const argv[] ) if( filenames.empty() ) filenames.push_back( recursive ? "." : "-" ); std::string input_filename; + int files_tested = 0, failed_tests = 0; 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( input_filename == "." ) { if( stdin_used ) continue; else stdin_used = true; - infd = STDIN_FILENO; + infd = STDIN_FILENO; input_filename = "-"; } else { @@ -297,13 +302,23 @@ int main( const int argc, const char * const argv[] ) if( infd < 0 ) { error = true; continue; } } + if( isatty( infd ) ) // for example /dev/tty + { + show_file_error( input_filename == "-" ? "(stdin)" : input_filename.c_str(), + "I won't read compressed data from a terminal." ); + close( infd ); error = true; continue; + } + int tmp; if( infd == STDIN_FILENO ) tmp = ztest_stdin( infd, format_index, ztest_args ); else tmp = ztest_file( infd, format_index, input_filename, ztest_args ); if( tmp > retval ) retval = tmp; + ++files_tested; if( tmp ) ++failed_tests; - if( input_filename.size() ) close( infd ); + if( close( infd ) != 0 ) + { show_file_error( input_filename.c_str(), "Error closing input file", + errno ); error = true; } } if( std::fclose( stdout ) != 0 ) // in case decompressor writes to stdout @@ -312,5 +327,9 @@ int main( const int argc, const char * const argv[] ) error = true; } if( error && retval == 0 ) retval = 1; + if( failed_tests > 0 && verbosity >= 1 && files_tested > 1 ) + std::fprintf( stderr, "%s: warning: %d %s failed the test.\n", + program_name, failed_tests, + ( failed_tests == 1 ) ? "file" : "files" ); return retval; } |