From: Jia Tan Date: Sat, 21 Oct 2023 15:07:08 +0000 (+0800) Subject: xz: Change the way coder_run() and list_run() are called in main(). X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b10b2e4a8fe50629e6e45e2cf7b9193ccd9bda36;p=thirdparty%2Fxz.git xz: Change the way coder_run() and list_run() are called in main(). Previously, a function pointer was used to determine if coder_run() or list_run() should be called in the main entry processing loop. This was replaced by an extra function call to process_entry(). coder_run() and list_run() were changed to accept a file_pair * argument instead of a filename. The common repeated code was moved to process_entry() instead. --- diff --git a/src/xz/args.c b/src/xz/args.c index 825cf92e..4dadda2a 100644 --- a/src/xz/args.c +++ b/src/xz/args.c @@ -789,11 +789,17 @@ args_parse(args_info *args, int argc, char **argv) #else // List mode is only available when decoders are enabled and is // only valid with .xz files. - if (opt_mode == MODE_LIST - && opt_format != FORMAT_XZ - && opt_format != FORMAT_AUTO) - message_fatal(_("--list works only on .xz files " - "(--format=xz or --format=auto)")); + if (opt_mode == MODE_LIST) { + if (opt_format != FORMAT_XZ && opt_format != FORMAT_AUTO) + message_fatal(_("--list works only on .xz files " + "(--format=xz or --format=auto)")); + + // Unset opt_stdout so that io_open_src() won't accept + // special files. + opt_stdout = false; + // Set opt_force so that io_open_src() will follow symlinks. + opt_force = true; + } #endif #ifdef HAVE_LZIP_DECODER diff --git a/src/xz/coder.c b/src/xz/coder.c index 2ba64694..a281d76b 100644 --- a/src/xz/coder.c +++ b/src/xz/coder.c @@ -1429,16 +1429,8 @@ coder_passthru(file_pair *pair) extern void -coder_run(const char *filename) +coder_run(file_pair *pair) { - // Set and possibly print the filename for the progress message. - message_filename(filename); - - // Try to open the input file. - file_pair *pair = io_open_src(filename); - if (pair == NULL) - return; - // Assume that something goes wrong. bool success = false; diff --git a/src/xz/coder.h b/src/xz/coder.h index 7dfa466e..752e4801 100644 --- a/src/xz/coder.h +++ b/src/xz/coder.h @@ -83,7 +83,7 @@ extern void coder_add_filter(lzma_vli id, void *options); extern void coder_set_compression_settings(void); /// Compress or decompress the given file -extern void coder_run(const char *filename); +extern void coder_run(file_pair *pair); #ifndef NDEBUG /// Free the memory allocated for the coder and kill the worker threads. diff --git a/src/xz/list.c b/src/xz/list.c index 869b6f79..1853088f 100644 --- a/src/xz/list.c +++ b/src/xz/list.c @@ -1274,26 +1274,10 @@ list_totals(void) extern void -list_file(const char *filename) +list_file(file_pair *pair) { - message_filename(filename); - - if (filename == stdin_filename) { - message_error(_("--list does not support reading from " - "standard input")); - return; - } - init_field_widths(); - // Unset opt_stdout so that io_open_src() won't accept special files. - // Set opt_force so that io_open_src() will follow symlinks. - opt_stdout = false; - opt_force = true; - file_pair *pair = io_open_src(filename); - if (pair == NULL) - return; - xz_file_info xfi = XZ_FILE_INFO_INIT; if (!parse_indexes(&xfi, pair)) { bool fail; diff --git a/src/xz/list.h b/src/xz/list.h index a4c6ec7d..9b292393 100644 --- a/src/xz/list.h +++ b/src/xz/list.h @@ -11,7 +11,7 @@ /////////////////////////////////////////////////////////////////////////////// /// \brief List information about the given .xz file -extern void list_file(const char *filename); +extern void list_file(file_pair *pair); /// \brief Show the totals after all files have been listed diff --git a/src/xz/main.c b/src/xz/main.c index 9c902833..05e9f5e3 100644 --- a/src/xz/main.c +++ b/src/xz/main.c @@ -146,6 +146,34 @@ read_name(const args_info *args) } +static void +process_entry(const char *path) +{ + // Set and possibly print the filename for the progress message. + message_filename(path); + + // Open the entry + file_pair *pair = io_open_src(path); + if (pair == NULL) + return; + +#ifdef HAVE_DECODERS + if (opt_mode == MODE_LIST) { + if (path == stdin_filename) { + message_error(_("--list does not support reading from " + "standard input")); + return; + } + + list_file(pair); + return; + } +#endif + + coder_run(pair); +} + + int main(int argc, char **argv) { @@ -256,14 +284,6 @@ main(int argc, char **argv) io_allow_sandbox(); #endif - // coder_run() handles compression, decompression, and testing. - // list_file() is for --list. - void (*run)(const char *filename) = &coder_run; -#ifdef HAVE_DECODERS - if (opt_mode == MODE_LIST) - run = &list_file; -#endif - // Process the files given on the command line. Note that if no names // were given, args_parse() gave us a fake "-" filename. for (unsigned i = 0; i < args.arg_count && !user_abort; ++i) { @@ -298,7 +318,7 @@ main(int argc, char **argv) } // Do the actual compression or decompression. - run(args.arg_names[i]); + process_entry(args.arg_names[i]); } // If --files or --files0 was used, process the filenames from the @@ -314,7 +334,7 @@ main(int argc, char **argv) // read_name() doesn't return empty names. assert(name[0] != '\0'); - run(name); + process_entry(name); } if (args.files_name != stdin_filename)