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.
This commit is contained in:
Sergey Poznyakoff
2024-01-14 23:54:33 +02:00
parent 66eaa2f821
commit b4d1fa77b6
4 changed files with 36 additions and 19 deletions

View File

@@ -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;

View File

@@ -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 */

View File

@@ -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 *

View File

@@ -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: