"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "ztest.cc" between
zutils-1.8.tar.lz and zutils-1.9.tar.lz

About: Zutils is a collection of utilities able to deal with any combination of compressed and non-compressed files transparently. The utilities zcat, zcmp, zdiff, zgrep and ztest supports the compressors bzip2, gzip, lzip and xz.

ztest.cc  (zutils-1.8.tar.lz):ztest.cc  (zutils-1.9.tar.lz)
/* Ztest - verify the integrity of compressed files /* Ztest - verify the integrity of compressed files
Copyright (C) 2010-2019 Antonio Diaz Diaz. Copyright (C) 2010-2020 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
#include <cerrno> #include <cerrno>
#include <climits> #include <climits>
#include <csignal> #include <csignal>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
skipping to change at line 52 skipping to change at line 52
#ifndef O_BINARY #ifndef O_BINARY
#define O_BINARY 0 #define O_BINARY 0
#endif #endif
namespace { namespace {
#include "recursive.cc" #include "recursive.cc"
void show_help() 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 '-', t he\n" "Uncompressed files are ignored. If a file is specified as '-', t he\n"
"integrity of compressed data read from standard input is verifie d. Data\n" "integrity of compressed data read from standard input is verifie d. Data\n"
"read from standard input must be all in the same compression for "read from standard input must be all in the same compressed form
mat.\n" at. 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 fin
al\n"
"diagnostic is shown at verbosity level 1 or higher if any file f
ails the\n"
"test when testing multiple files.\n"
"\nIf no files are specified, recursive searches examine the curr ent\n" "\nIf no files are specified, recursive searches examine the curr ent\n"
"working directory, and nonrecursive searches read standard input .\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, s ome xz\n" "\nNote that error detection in the xz format is broken. First, s ome xz\n"
"files lack integrity information. Second, not all xz decompresso rs can\n" "files lack integrity information. Second, not all xz decompresso rs can\n"
"verify the integrity of all xz files. Third, section 2.1.1.2 'St ream\n" "verify the integrity of all xz files. Third, section 2.1.1.2 'St ream\n"
"Flags' of the xz format specification allows xz decompressors to produce\n" "Flags' of the xz format specification allows xz decompressors to produce\n"
"garbage output without issuing any warning. Therefore, xz files can't\n" "garbage output without issuing any warning. Therefore, xz files can't\n"
"always be verified as reliably as files in the other formats can .\n" "always be verified as reliably as files in the other formats can .\n"
"\nUsage: ztest [options] [files]\n" "\nUsage: ztest [options] [files]\n"
"\nExit status is 0 if all compressed files verify OK, 1 if envir onmental\n" "\nExit status is 0 if all compressed files verify OK, 1 if envir onmental\n"
"problems (file not found, invalid flags, I/O errors, etc), 2 if any\n" "problems (file not found, invalid flags, I/O errors, etc), 2 if any\n"
"compressed file is corrupt or invalid.\n" "compressed file is corrupt or invalid.\n"
"\nOptions:\n" "\nOptions:\n"
" -h, --help display this help and exit\n" " -h, --help display this help and exit\n"
" -V, --version output version information and ex it\n" " -V, --version output version information and ex it\n"
" -M, --format=<list> process only the formats in <list >\n" " -M, --format=<list> process only the formats in <list >\n"
" -N, --no-rcfile don't read runtime configuration file\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" " -q, --quiet suppress all messages\n"
" -r, --recursive operate recursively on directorie s\n" " -r, --recursive operate recursively on directorie s\n"
" -R, --dereference-recursive recursively follow symbolic links \n" " -R, --dereference-recursive recursively follow symbolic links \n"
" -v, --verbose be verbose (a 2nd -v gives more)\ n" " -v, --verbose be verbose (a 2nd -v gives more)\ n"
" --bz2=<command> set compressor and options for bz ip2 format\n" " --bz2=<command> set compressor and options for bz ip2 format\n"
" --gz=<command> set compressor and options for gz ip format\n" " --gz=<command> set compressor and options for gz ip format\n"
" --lz=<command> set compressor and options for lz ip format\n" " --lz=<command> set compressor and options for lz ip format\n"
" --xz=<command> set compressor and options for xz format\n" ); " --xz=<command> set compressor and options for xz format\n" );
show_help_addr(); show_help_addr();
} }
skipping to change at line 97 skipping to change at line 101
{ {
const int infd = open( input_filename.c_str(), O_RDONLY | O_BINARY ); const int infd = open( input_filename.c_str(), O_RDONLY | O_BINARY );
if( infd < 0 ) if( infd < 0 )
show_file_error( input_filename.c_str(), "Can't open input file", errno ); show_file_error( input_filename.c_str(), "Can't open input file", errno );
return infd; return infd;
} }
int ztest_stdin( const int infd, int format_index, int ztest_stdin( const int infd, int format_index,
const std::vector< const char * > & ztest_args ) const std::vector< const char * > & ztest_args )
{ {
const uint8_t * magic_data = 0; uint8_t magic_data[magic_buf_size];
int magic_size = 0; int magic_size = 0;
if( format_index < 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 ); const char * const compressor_name = get_compressor_name( format_index );
if( !compressor_name ) if( !compressor_name )
{ show_error( "Unknown data format read from stdin." ); return 2; } { show_error( "Unknown data format read from stdin." ); return 2; }
int fda[2]; // pipe from feeder int fda[2]; // pipe from feeder
if( pipe( fda ) < 0 ) if( pipe( fda ) < 0 )
{ show_error( "Can't create pipe", errno ); return 1; } { show_error( "Can't create pipe", errno ); return 1; }
const pid_t pid = fork(); const pid_t pid = fork();
if( pid == 0 ) // child1 (compressor feeder) if( pid == 0 ) // child1 (compressor feeder)
{ {
skipping to change at line 160 skipping to change at line 164
if( retval == 0 && wait_for_child( pid, "data feeder" ) != 0 ) if( retval == 0 && wait_for_child( pid, "data feeder" ) != 0 )
retval = 1; retval = 1;
return retval; return retval;
} }
int ztest_file( const int infd, int format_index, int ztest_file( const int infd, int format_index,
const std::string & input_filename, const std::string & input_filename,
const std::vector< const char * > & ztest_args ) const std::vector< const char * > & ztest_args )
{ {
static int disable_xz = -1; // tri-state bool 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; int magic_size = 0;
if( format_index < 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 ); const char * const compressor_name = get_compressor_name( format_index );
if( !compressor_name ) if( !compressor_name )
return 0; // ignore this file return 0; // ignore this file
if( format_index == fmt_xz ) if( format_index == fmt_xz )
{ {
if( disable_xz < 0 ) if( disable_xz < 0 )
{ {
std::string command( compressor_name ); command += " -V > /dev/null 2>&1"; std::string command( compressor_name ); command += " -V > /dev/null 2>&1";
disable_xz = ( std::system( command.c_str() ) != 0 ); disable_xz = ( std::system( command.c_str() ) != 0 );
} }
skipping to change at line 215 skipping to change at line 219
} // end namespace } // end namespace
int main( const int argc, const char * const argv[] ) int main( const int argc, const char * const argv[] )
{ {
enum { bz2_opt = 256, gz_opt, lz_opt, xz_opt }; enum { bz2_opt = 256, gz_opt, lz_opt, xz_opt };
int format_index = -1; int format_index = -1;
int recursive = 0; // 1 = '-r', 2 = '-R' int recursive = 0; // 1 = '-r', 2 = '-R'
std::list< std::string > filenames; std::list< std::string > filenames;
std::vector< const char * > ztest_args; // args to ztest, maybe empty std::vector< const char * > ztest_args; // args to ztest, maybe empty
invocation_name = argv[0];
program_name = "ztest"; program_name = "ztest";
invocation_name = ( argc > 0 ) ? argv[0] : program_name;
const Arg_parser::Option options[] = const Arg_parser::Option options[] =
{ {
{ 'h', "help", Arg_parser::no }, { 'h', "help", Arg_parser::no },
{ 'M', "format", Arg_parser::yes }, { 'M', "format", Arg_parser::yes },
{ 'N', "no-rcfile", Arg_parser::no }, { 'N', "no-rcfile", Arg_parser::no },
{ 'O', "force-format", Arg_parser::yes }, { 'O', "force-format", Arg_parser::yes },
{ 'q', "quiet", Arg_parser::no }, { 'q', "quiet", Arg_parser::no },
{ 'r', "recursive", Arg_parser::no }, { 'r', "recursive", Arg_parser::no },
{ 'R', "dereference-recursive", Arg_parser::no }, { 'R', "dereference-recursive", Arg_parser::no },
skipping to change at line 278 skipping to change at line 282
setmode( STDIN_FILENO, O_BINARY ); setmode( STDIN_FILENO, O_BINARY );
setmode( STDOUT_FILENO, O_BINARY ); setmode( STDOUT_FILENO, O_BINARY );
#endif #endif
for( ; argind < parser.arguments(); ++argind ) for( ; argind < parser.arguments(); ++argind )
filenames.push_back( parser.argument( argind ) ); filenames.push_back( parser.argument( argind ) );
if( filenames.empty() ) filenames.push_back( recursive ? "." : "-" ); if( filenames.empty() ) filenames.push_back( recursive ? "." : "-" );
std::string input_filename; std::string input_filename;
int files_tested = 0, failed_tests = 0;
int retval = 0; int retval = 0;
bool error = false; bool error = false;
bool stdin_used = false; bool stdin_used = false;
while( next_filename( filenames, input_filename, error, recursive ) ) while( next_filename( filenames, input_filename, error, recursive ) )
{ {
int infd; int infd;
if( input_filename.empty() ) if( input_filename == "." )
{ {
if( stdin_used ) continue; else stdin_used = true; if( stdin_used ) continue; else stdin_used = true;
infd = STDIN_FILENO; infd = STDIN_FILENO; input_filename = "-";
} }
else else
{ {
infd = open_instream( input_filename ); infd = open_instream( input_filename );
if( infd < 0 ) { error = true; continue; } 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; int tmp;
if( infd == STDIN_FILENO ) if( infd == STDIN_FILENO )
tmp = ztest_stdin( infd, format_index, ztest_args ); tmp = ztest_stdin( infd, format_index, ztest_args );
else tmp = ztest_file( infd, format_index, input_filename, ztest_args ); else tmp = ztest_file( infd, format_index, input_filename, ztest_args );
if( tmp > retval ) retval = tmp; 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 if( std::fclose( stdout ) != 0 ) // in case decompressor writes to stdout
{ {
show_error( "Error closing stdout", errno ); show_error( "Error closing stdout", errno );
error = true; error = true;
} }
if( error && retval == 0 ) retval = 1; 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; return retval;
} }
 End of changes. 20 change blocks. 
26 lines changed or deleted 49 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)