2025-02-24 05:01:08 +01:00
|
|
|
/* Ztest - verify integrity of compressed files
|
2025-02-24 05:40:39 +01:00
|
|
|
Copyright (C) 2010, 2011, 2012, 2013 Antonio Diaz Diaz.
|
2025-02-24 05:01:08 +01:00
|
|
|
|
|
|
|
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 3 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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
void show_ztest_help()
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
2025-02-24 05:40:39 +01:00
|
|
|
std::printf( "Ztest verifies the integrity of the specified compressed files.\n"
|
2025-02-24 05:43:00 +01:00
|
|
|
"Uncompressed files are ignored. If no files are specified, the integrity\n"
|
|
|
|
"of compressed data read from standard input is verified. Data read from\n"
|
|
|
|
"standard input must be all in the same compression format.\n"
|
2025-02-24 05:40:39 +01:00
|
|
|
"\nThe supported formats are bzip2, gzip, lzip and xz.\n"
|
|
|
|
"\nNote that some xz files lack integrity information, and therefore can't\n"
|
|
|
|
"be verified as reliably as the other formats can.\n"
|
|
|
|
"\nUsage: ztest [options] [files]\n"
|
|
|
|
"\nExit status is 0 if all compressed files verify OK, 1 if environmental\n"
|
|
|
|
"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"
|
|
|
|
" --format=<fmt> force given format (bz2, gz, lz, xz)\n"
|
2025-02-24 05:43:00 +01:00
|
|
|
" -N, --no-rcfile don't read runtime configuration file\n"
|
2025-02-24 05:40:39 +01:00
|
|
|
" -q, --quiet suppress all messages\n"
|
|
|
|
" -r, --recursive operate recursively on directories\n"
|
2025-02-24 05:43:00 +01:00
|
|
|
" -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" );
|
2025-02-24 05:01:08 +01:00
|
|
|
show_help_addr();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 05:43:00 +01:00
|
|
|
int ztest_stdin( const int infd, int format_index,
|
2025-02-24 05:01:08 +01:00
|
|
|
const std::vector< const char * > & ztest_args )
|
|
|
|
{
|
2025-02-24 05:40:39 +01:00
|
|
|
const uint8_t * magic_data = 0;
|
2025-02-24 05:01:08 +01:00
|
|
|
int magic_size = 0;
|
2025-02-24 05:43:00 +01:00
|
|
|
if( format_index < 0 )
|
|
|
|
format_index = test_format( infd, &magic_data, &magic_size );
|
|
|
|
const char * const compressor_name = get_compressor_name( format_index );
|
|
|
|
if( !compressor_name )
|
2025-02-24 05:01:08 +01:00
|
|
|
{ show_error( "Unknown data format read from stdin." ); return 2; }
|
2025-02-24 05:04:54 +01:00
|
|
|
int fda[2]; // pipe from feeder
|
2025-02-24 05:01:08 +01:00
|
|
|
if( pipe( fda ) < 0 )
|
|
|
|
{ show_error( "Can't create pipe", errno ); return 1; }
|
|
|
|
|
2025-02-24 05:04:54 +01:00
|
|
|
const pid_t pid = fork();
|
2025-02-24 05:45:13 +01:00
|
|
|
if( pid == 0 ) // child1 (compressor feeder)
|
|
|
|
{
|
|
|
|
if( close( fda[0] ) != 0 ||
|
|
|
|
!feed_data( infd, fda[1], magic_data, magic_size ) )
|
|
|
|
_exit( 1 );
|
|
|
|
if( close( fda[1] ) != 0 )
|
|
|
|
{ show_close_error( "data feeder" ); _exit( 1 ); }
|
|
|
|
_exit( 0 );
|
|
|
|
}
|
|
|
|
if( pid < 0 ) // parent
|
|
|
|
{ show_fork_error( "data feeder" ); return 1; }
|
|
|
|
|
|
|
|
const pid_t pid2 = fork();
|
|
|
|
if( pid2 == 0 ) // child2 (compressor)
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
|
|
|
if( dup2( fda[0], STDIN_FILENO ) >= 0 &&
|
|
|
|
close( fda[0] ) == 0 && close( fda[1] ) == 0 )
|
|
|
|
{
|
2025-02-24 05:43:00 +01:00
|
|
|
const std::vector< std::string > & compressor_args =
|
|
|
|
get_compressor_args( format_index );
|
|
|
|
const int size = compressor_args.size();
|
|
|
|
const int size2 = ztest_args.size();
|
|
|
|
const char ** const argv = new const char *[size+size2+3];
|
|
|
|
argv[0] = compressor_name;
|
|
|
|
for( int i = 0; i < size; ++i )
|
|
|
|
argv[i+1] = compressor_args[i].c_str();
|
|
|
|
for( int i = 0; i < size2; ++i )
|
|
|
|
argv[i+size+1] = ztest_args[i];
|
|
|
|
argv[size+size2+1] = "-t";
|
|
|
|
argv[size+size2+2] = 0;
|
2025-02-24 05:01:08 +01:00
|
|
|
execvp( argv[0], (char **)argv );
|
|
|
|
}
|
2025-02-24 05:43:00 +01:00
|
|
|
show_exec_error( compressor_name );
|
2025-02-24 05:01:08 +01:00
|
|
|
_exit( 1 );
|
|
|
|
}
|
2025-02-24 05:43:00 +01:00
|
|
|
if( pid2 < 0 ) // parent
|
2025-02-24 05:45:13 +01:00
|
|
|
{ show_fork_error( compressor_name ); return 1; }
|
2025-02-24 05:04:54 +01:00
|
|
|
|
|
|
|
close( fda[0] ); close( fda[1] );
|
2025-02-24 05:45:13 +01:00
|
|
|
const bool isgzxz = ( format_index == fmt_gz || format_index == fmt_xz );
|
|
|
|
int retval = wait_for_child( pid2, compressor_name, 1, isgzxz );
|
|
|
|
if( retval == 0 && wait_for_child( pid, "data feeder" ) != 0 )
|
2025-02-24 05:04:54 +01:00
|
|
|
retval = 1;
|
|
|
|
return retval;
|
2025-02-24 05:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 05:43:00 +01:00
|
|
|
int ztest_file( const int infd, int format_index,
|
2025-02-24 05:40:39 +01:00
|
|
|
const std::string & input_filename,
|
2025-02-24 05:01:08 +01:00
|
|
|
const std::vector< const char * > & ztest_args )
|
|
|
|
{
|
2025-02-24 05:40:39 +01:00
|
|
|
const uint8_t * magic_data = 0;
|
2025-02-24 05:01:08 +01:00
|
|
|
int magic_size = 0;
|
2025-02-24 05:43:00 +01:00
|
|
|
if( format_index < 0 )
|
|
|
|
format_index = test_format( infd, &magic_data, &magic_size );
|
|
|
|
const char * const compressor_name = get_compressor_name( format_index );
|
|
|
|
if( !compressor_name )
|
2025-02-24 05:40:39 +01:00
|
|
|
return 0; // skip this file
|
2025-02-24 05:01:08 +01:00
|
|
|
const pid_t pid = fork();
|
|
|
|
|
2025-02-24 05:43:00 +01:00
|
|
|
if( pid == 0 ) // child (compressor)
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
2025-02-24 05:43:00 +01:00
|
|
|
const std::vector< std::string > & compressor_args =
|
|
|
|
get_compressor_args( format_index );
|
|
|
|
const int size = compressor_args.size();
|
|
|
|
const int size2 = ztest_args.size();
|
|
|
|
const char ** const argv = new const char *[size+size2+5];
|
|
|
|
argv[0] = compressor_name;
|
|
|
|
for( int i = 0; i < size; ++i )
|
|
|
|
argv[i+1] = compressor_args[i].c_str();
|
|
|
|
for( int i = 0; i < size2; ++i )
|
|
|
|
argv[i+size+1] = ztest_args[i];
|
|
|
|
argv[size+size2+1] = "-t";
|
|
|
|
argv[size+size2+2] = "--";
|
|
|
|
argv[size+size2+3] = input_filename.c_str();
|
|
|
|
argv[size+size2+4] = 0;
|
2025-02-24 05:01:08 +01:00
|
|
|
execvp( argv[0], (char **)argv );
|
2025-02-24 05:43:00 +01:00
|
|
|
show_exec_error( compressor_name );
|
2025-02-24 05:01:08 +01:00
|
|
|
_exit( 1 );
|
|
|
|
}
|
2025-02-24 05:43:00 +01:00
|
|
|
if( pid < 0 ) // parent
|
|
|
|
{ show_fork_error( compressor_name ); return 1; }
|
2025-02-24 05:04:54 +01:00
|
|
|
|
2025-02-24 05:45:13 +01:00
|
|
|
const bool isgzxz = ( format_index == fmt_gz || format_index == fmt_xz );
|
|
|
|
return wait_for_child( pid, compressor_name, 1, isgzxz );
|
2025-02-24 05:01:08 +01:00
|
|
|
}
|