summaryrefslogtreecommitdiffstats
path: root/ztest.cc
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-08 04:26:56 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-08 04:26:56 +0000
commitd7d5dfbcf513f94115990102c56eccf177417889 (patch)
tree7b47cf460832a31d6e0c34444e7bf1cddacfd02f /ztest.cc
parentAdding debian version 1.0-1. (diff)
downloadzutils-d7d5dfbcf513f94115990102c56eccf177417889.tar.xz
zutils-d7d5dfbcf513f94115990102c56eccf177417889.zip
Merging upstream version 1.1~rc2.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'ztest.cc')
-rw-r--r--ztest.cc106
1 files changed, 60 insertions, 46 deletions
diff --git a/ztest.cc b/ztest.cc
index b731191..20296b9 100644
--- a/ztest.cc
+++ b/ztest.cc
@@ -18,9 +18,9 @@
void show_ztest_help()
{
std::printf( "Ztest verifies the integrity of the specified compressed files.\n"
- "Non-compressed files are ignored. If no files are specified, 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"
+ "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"
"\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"
@@ -32,100 +32,114 @@ void show_ztest_help()
" -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"
+ " -N, --no-rcfile don't read runtime configuration file\n"
" -q, --quiet suppress all messages\n"
" -r, --recursive operate recursively on directories\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 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();
}
-int ztest_stdin( const int infd, const int format_type,
+int ztest_stdin( const int infd, int format_index,
const std::vector< const char * > & ztest_args )
{
const uint8_t * magic_data = 0;
int magic_size = 0;
- const char * const decompressor_name = ( format_type >= 0 ) ?
- decompressor_names[format_type] :
- test_format( infd, &magic_data, &magic_size );
- if( !decompressor_name )
+ 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 )
{ show_error( "Unknown data format read from stdin." ); return 2; }
int fda[2]; // pipe from feeder
if( pipe( fda ) < 0 )
{ show_error( "Can't create pipe", errno ); return 1; }
const pid_t pid = fork();
- if( pid == 0 ) // child1 (decompressor)
+ if( pid == 0 ) // child1 (compressor)
{
if( dup2( fda[0], STDIN_FILENO ) >= 0 &&
close( fda[0] ) == 0 && close( fda[1] ) == 0 )
{
- const char ** const argv = new const char *[ztest_args.size()+3];
- argv[0] = decompressor_name;
- for( unsigned i = 0; i < ztest_args.size(); ++i )
- argv[i+1] = ztest_args[i];
- argv[ztest_args.size()+1] = "-t";
- argv[ztest_args.size()+2] = 0;
+ 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;
execvp( argv[0], (char **)argv );
}
- show_exec_error( decompressor_name );
+ show_exec_error( compressor_name );
_exit( 1 );
}
- // parent
- if( pid < 0 )
- { show_fork_error( decompressor_name ); return 1; }
+ if( pid < 0 ) // parent
+ { show_fork_error( compressor_name ); return 1; }
const pid_t pid2 = fork();
- if( pid2 == 0 ) // child2 (decompressor feeder)
+ if( pid2 == 0 ) // child2 (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( "decompressor feeder" ); _exit( 1 ); }
+ { show_close_error( "data feeder" ); _exit( 1 ); }
_exit( 0 );
}
- // parent
- if( pid2 < 0 )
- { show_fork_error( "decompressor feeder" ); return 1; }
+ if( pid2 < 0 ) // parent
+ { show_fork_error( "data feeder" ); return 1; }
close( fda[0] ); close( fda[1] );
- int retval = wait_for_child( pid, decompressor_name, 1 );
- if( retval == 0 && wait_for_child( pid2, "decompressor feeder" ) != 0 )
+ int retval = wait_for_child( pid, compressor_name, 1 );
+ if( retval == 0 && wait_for_child( pid2, "data feeder" ) != 0 )
retval = 1;
return retval;
}
-int ztest_file( const int infd, const int format_type,
+int ztest_file( const int infd, int format_index,
const std::string & input_filename,
const std::vector< const char * > & ztest_args )
{
const uint8_t * magic_data = 0;
int magic_size = 0;
- const char * const decompressor_name = ( format_type >= 0 ) ?
- decompressor_names[format_type] :
- test_format( infd, &magic_data, &magic_size );
- if( !decompressor_name )
+ 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 )
return 0; // skip this file
const pid_t pid = fork();
- if( pid == 0 ) // child (decompressor)
+ if( pid == 0 ) // child (compressor)
{
- const char ** const argv = new const char *[ztest_args.size()+5];
- argv[0] = decompressor_name;
- for( unsigned i = 0; i < ztest_args.size(); ++i )
- argv[i+1] = ztest_args[i];
- argv[ztest_args.size()+1] = "-t";
- argv[ztest_args.size()+2] = "--";
- argv[ztest_args.size()+3] = input_filename.c_str();
- argv[ztest_args.size()+4] = 0;
+ 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;
execvp( argv[0], (char **)argv );
- show_exec_error( decompressor_name );
+ show_exec_error( compressor_name );
_exit( 1 );
}
- // parent
- if( pid < 0 )
- { show_fork_error( decompressor_name ); return 1; }
+ if( pid < 0 ) // parent
+ { show_fork_error( compressor_name ); return 1; }
- return wait_for_child( pid, decompressor_name, 1 );
+ return wait_for_child( pid, compressor_name, 1 );
}