From: Sergey Poznyakoff Date: Sun, 14 Jan 2024 21:54:33 +0000 (+0200) Subject: When given -c -a, issue a warning if no compressor is associated with the suffix. X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=b4d1fa77b69d4b7a63db9c458a808d058b71bd30;p=thirdparty%2Ftar.git When given -c -a, issue a warning if no compressor is associated with the suffix. * src/suffix.c (find_compression_suffix): Always return stripped archive name length in the last argument. Return 0 if there is no suffix. (find_compression_program): Remove. (set_compression_program_by_suffix): Take third argument, controlling whether to issue a warning if no suitable compression program is found for the suffix. * src/common.h (set_compression_program_by_suffix): Change prototype. * src/buffer.c, src/tar.c: All uses of set_compression_program_by_suffix changed. --- diff --git a/src/buffer.c b/src/buffer.c index 404959da..56273642 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -455,7 +455,8 @@ open_compressed_archive (void) case ct_none: if (shortfile) ERROR ((0, 0, _("This does not look like a tar archive"))); - set_compression_program_by_suffix (archive_name_array[0], NULL); + set_compression_program_by_suffix (archive_name_array[0], NULL, + false); if (!use_compress_program_option) return archive; break; diff --git a/src/common.h b/src/common.h index f5d45c6e..2154f232 100644 --- a/src/common.h +++ b/src/common.h @@ -968,7 +968,8 @@ bool transform_name_fp (char **pinput, int type, bool transform_program_p (void); /* Module suffix.c */ -void set_compression_program_by_suffix (const char *name, const char *defprog); +void set_compression_program_by_suffix (const char *name, const char *defprog, + bool verbose); char *strip_compression_suffix (const char *name); /* Module checkpoint.c */ diff --git a/src/suffix.c b/src/suffix.c index a97d15f3..51490b2d 100644 --- a/src/suffix.c +++ b/src/suffix.c @@ -52,47 +52,61 @@ static struct compression_suffix compression_suffixes[] = { #undef __CAT2__ }; +/* Extract the suffix from archive file NAME, and return a pointer to + compression_suffix associated with it or NULL if none is found. + No matter what is the return value, if RET_LEN is not NULL, store + there the length of NAME with that suffix stripped, or 0 if NAME has + no suffix. */ static struct compression_suffix const * find_compression_suffix (const char *name, size_t *ret_len) { char *suf = strrchr (name, '.'); - if (suf) + if (suf && suf[1] != 0 && suf[1] != '/') { size_t len; struct compression_suffix *p; suf++; len = strlen (suf); + if (ret_len) + *ret_len = strlen (name) - len - 1; for (p = compression_suffixes; p->suffix; p++) { if (p->length == len && memcmp (p->suffix, suf, len) == 0) { - if (ret_len) - *ret_len = strlen (name) - len - 1; return p; } } } + else if (ret_len) + *ret_len = 0; return NULL; } -static const char * -find_compression_program (const char *name, const char *defprog) -{ - struct compression_suffix const *p = find_compression_suffix (name, NULL); - if (p) - return p->program; - return defprog; -} - +/* Select compression program using the suffix of the archive file NAME. + Use DEFPROG, if there is no suffix, or if no program is associated with + the suffix. In the latter case, if VERBOSE is true, issue a warning. + */ void -set_compression_program_by_suffix (const char *name, const char *defprog) +set_compression_program_by_suffix (const char *name, const char *defprog, + bool verbose) { - const char *program = find_compression_program (name, defprog); - if (program) - use_compress_program_option = program; + size_t len; + struct compression_suffix const *p = find_compression_suffix (name, &len); + if (p) + use_compress_program_option = p->program; + else + { + use_compress_program_option = defprog; + if (len > 0 && verbose) + WARN ((0, 0, + _("no compression program is defined for suffix '%s';" + " assuming %s"), + name + len, + defprog ? defprog : "uncompressed archive")); + } } char * diff --git a/src/tar.c b/src/tar.c index 57413e73..bf69d6e5 100644 --- a/src/tar.c +++ b/src/tar.c @@ -2691,7 +2691,8 @@ decode_options (int argc, char **argv) if (args.compress_autodetect && archive_names && strcmp (archive_name_array[0], "-")) set_compression_program_by_suffix (archive_name_array[0], - use_compress_program_option); + use_compress_program_option, + true); break; case EXTRACT_SUBCOMMAND: