diff options
Diffstat (limited to '')
-rw-r--r-- | ztest.cc | 63 |
1 files changed, 38 insertions, 25 deletions
@@ -50,14 +50,16 @@ namespace { void show_help() { - 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 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" + std::printf( "ztest verifies the integrity of the compressed files specified. It\n" + "also warns if an uncompressed file has a compressed file name extension, or\n" + "if a compressed file has a wrong compressed extension. Uncompressed files\n" + "are otherwise ignored. If a file is specified as '-', the integrity of\n" + "compressed data read from standard input is verified. Data read from\n" + "standard input must be all in the same compressed format. If a file fails to\n" + "decompress, does not exist, can't be opened, or is a terminal, ztest\n" + "continues verifying the rest of the files. A final diagnostic is shown at\n" + "verbosity level 1 or higher if any file fails the test when testing multiple\n" + "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, xz, and zstd.\n" @@ -69,14 +71,15 @@ void show_help() "always be verified as reliably as files in 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" + "problems (file not found, invalid command line options, I/O errors, etc),\n" + "2 if any compressed file is corrupt or invalid, or if any file has an\n" + "incorrect file name extension.\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 the format given (bz2, gz, lz, xz, zst)\n" + " -O, --force-format=<fmt> force the input format\n" " -q, --quiet suppress all messages\n" " -r, --recursive operate recursively on directories\n" " -R, --dereference-recursive recursively follow symbolic links\n" @@ -85,7 +88,8 @@ void show_help() " --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" - " --zst=<command> set compressor and options for zstd format\n" ); + " --zst=<command> set compressor and options for zstd format\n" + "\nValid formats for options '-M' and '-O' are 'bz2', 'gz', 'lz', 'xz', and 'zst'.\n" ); show_help_addr(); } @@ -117,10 +121,8 @@ int ztest_stdin( const int infd, int format_index, 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(); _exit( 1 ); } + !feed_data( "-", infd, fda[1], magic_data, magic_size ) ) _exit( 1 ); + if( close( fda[1] ) != 0 ) { show_close_error(); _exit( 1 ); } _exit( 0 ); } if( pid < 0 ) // parent @@ -170,11 +172,17 @@ int ztest_file( const int infd, int format_index, static int disable_zst = -1; // tri-state bool uint8_t magic_data[magic_buf_size]; int magic_size = 0; + const int format_index_e = test_extension( input_filename ); 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 ) + { + if( format_index < 0 && format_index_e >= 0 ) + { show_file_error( input_filename.c_str(), + "Uncompressed file has compressed extension." ); return 2; } return 0; // ignore this file + } if( format_index == fmt_xz ) { if( disable_xz < 0 ) @@ -226,7 +234,12 @@ int ztest_file( const int infd, int format_index, { show_fork_error( compressor_name ); return 1; } const bool isgzxz = ( format_index == fmt_gz || format_index == fmt_xz ); - return wait_for_child( pid, compressor_name, 1, isgzxz ); + int retval = wait_for_child( pid, compressor_name, 1, isgzxz ); + if( retval == 0 && format_index >= 0 && format_index_e >= 0 && + format_index != format_index_e ) + { show_file_error( input_filename.c_str(), + "Compressed file has wrong compressed extension." ); retval = 2; } + return retval; } } // end namespace @@ -235,7 +248,7 @@ 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, zst_opt }; - int format_index = -1; + int format_index = -1; // undefined int recursive = 0; // 1 = '-r', 2 = '-R' std::list< std::string > filenames; std::vector< const char * > ztest_args; // args to ztest, maybe empty @@ -278,18 +291,18 @@ int main( const int argc, const char * const argv[] ) case 'h': show_help(); return 0; case 'M': parse_format_list( arg, pn ); break; case 'N': break; - case 'O': format_index = parse_format_type( arg, pn ); break; + case 'O': format_index = parse_format_type( arg, pn, false ); break; case 'q': verbosity = -1; ztest_args.push_back( "-q" ); 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; - case bz2_opt: parse_compressor( arg, fmt_bz2, 1 ); break; - case gz_opt: parse_compressor( arg, fmt_gz, 1 ); break; - case lz_opt: parse_compressor( arg, fmt_lz, 1 ); break; - case xz_opt: parse_compressor( arg, fmt_xz, 1 ); break; - case zst_opt: parse_compressor( arg, fmt_zst, 1 ); break; + case bz2_opt: parse_compressor( arg, pn, fmt_bz2, 1 ); break; + case gz_opt: parse_compressor( arg, pn, fmt_gz, 1 ); break; + case lz_opt: parse_compressor( arg, pn, fmt_lz, 1 ); break; + case xz_opt: parse_compressor( arg, pn, fmt_xz, 1 ); break; + case zst_opt: parse_compressor( arg, pn, fmt_zst, 1 ); break; default : internal_error( "uncaught option." ); } } // end process options @@ -325,7 +338,7 @@ int main( const int argc, const char * const argv[] ) if( isatty( infd ) ) // for example /dev/tty { - show_file_error( input_filename == "-" ? "(stdin)" : input_filename.c_str(), + show_file_error( name_or_stdin( input_filename.c_str() ), "I won't read compressed data from a terminal." ); close( infd ); error = true; continue; } |