From 87c956d4c4604d30b01326ea88f6241ec44f5526 Mon Sep 17 00:00:00 2001 From: Jia Tan Date: Tue, 14 Nov 2023 20:27:04 +0800 Subject: [PATCH] xz: Fix suffix check. 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 | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/xz/args.c b/src/xz/args.c index 17e778c5..a94e0d9e 100644 --- a/src/xz/args.c +++ b/src/xz/args.c @@ -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