]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
xz: Fix suffix check.
authorJia Tan <jiat0218@gmail.com>
Tue, 14 Nov 2023 12:27:04 +0000 (20:27 +0800)
committerJia Tan <jiat0218@gmail.com>
Fri, 22 Dec 2023 12:02:06 +0000 (20:02 +0800)
The suffix refactor done in 99575947a58a60416c570eb78038d18a1ea4cef4
had a small regression where raw format compression to standard out
failed if a suffix was not set. In this case, setting the suffix did
not make sense since a file is not created.

Now, xz should only fail when a suffix is not provided when it is
actually needed.

For instance:

    echo "foo" | xz --format=raw --lzma2 | wc -c

does not need a suffix check since it creates no files. But:

    xz --format=raw --lzma2 --suffix=.bar foo

Needs the suffix to be set since it must create foo.bar.

src/xz/args.c

index 17e778c5db799aadc50d946f8638a0f134f27291..a94e0d9e54a1345b0e25c5c4e467b03416012a4d 100644 (file)
@@ -718,6 +718,28 @@ args_parse(args_info *args, int argc, char **argv)
        if (opt_mode == MODE_COMPRESS && opt_format == FORMAT_AUTO)
                opt_format = FORMAT_XZ;
 
+       // If raw format is used and a custom suffix is not provided,
+       // then only stdout mode can be used when compressing or
+       // decompressing.
+       if (opt_format == FORMAT_RAW && !suffix_is_set() && !opt_stdout
+                       && (opt_mode == MODE_COMPRESS
+                               || opt_mode == MODE_DECOMPRESS)) {
+               if (args->files_name != NULL)
+                       message_fatal(_("With --format=raw, "
+                                       "--suffix=.SUF is required "
+                                       "unless writing to stdout"));
+
+               // If all of the filenames provided are "-" (more than one
+               // "-" could be specified) or no filenames are provided,
+               // then we are only going to be writing to standard out.
+               for (int i = optind; i < argc; i++) {
+                       if (strcmp(argv[i], "-") != 0)
+                               message_fatal(_("With --format=raw, "
+                                               "--suffix=.SUF is required "
+                                               "unless writing to stdout"));
+               }
+       }
+
        // Compression settings need to be validated (options themselves and
        // their memory usage) when compressing to any file format. It has to
        // be done also when uncompressing raw data, since for raw decoding
@@ -727,14 +749,6 @@ args_parse(args_info *args, int argc, char **argv)
                        && opt_mode != MODE_LIST))
                coder_set_compression_settings();
 
-       // If raw format is used and a custom suffix is not provided,
-       // then only stdout mode can be used when compressing or decompressing.
-       if (opt_format == FORMAT_RAW && !suffix_is_set() && !opt_stdout
-                       && (opt_mode == MODE_COMPRESS
-                               || opt_mode == MODE_DECOMPRESS))
-               message_fatal(_("With --format=raw, --suffix=.SUF is "
-                               "required unless writing to stdout"));
-
        // If no filenames are given, use stdin.
        if (argv[optind] == NULL && args->files_name == NULL) {
                // We don't modify or free() the "-" constant. The caller