]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
xz: Change the way coder_run() and list_run() are called in main().
authorJia Tan <jiat0218@gmail.com>
Sat, 21 Oct 2023 15:07:08 +0000 (23:07 +0800)
committerJia Tan <jiat0218@gmail.com>
Mon, 29 Jan 2024 13:40:53 +0000 (21:40 +0800)
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.

src/xz/args.c
src/xz/coder.c
src/xz/coder.h
src/xz/list.c
src/xz/list.h
src/xz/main.c

index 825cf92ec6a5def79e978fbd3c438a78b5f18a5a..4dadda2a2a9b3dcb7ad2aa560e7ccd09e94c9b0a 100644 (file)
@@ -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
index 2ba64694d3ad8a3d1278d431e8c7befe24e790cd..a281d76b29cd395eda89c99a9ed7acc586b77e70 100644 (file)
@@ -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;
 
index 7dfa466e206cd6067032f397a048cd4baed47e5e..752e48011c1226003f428f382a6a509ead550eb9 100644 (file)
@@ -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.
index 869b6f79b9bc9d5b72a393fd35b17beb748773c0..1853088f8e5172483efc5d63594ae437f741fac3 100644 (file)
@@ -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;
index a4c6ec7dc429f372130913398eae621729a146ef..9b292393c944297273d12627740a748658fe2f5f 100644 (file)
@@ -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
index 9c9028332adee75de2fcffc34e5009320e0bc942..05e9f5e37478f76dcbc41cfda8af511c79bfd7f4 100644 (file)
@@ -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)