]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
When given -c -a, issue a warning if no compressor is associated with the suffix.
authorSergey Poznyakoff <gray@gnu.org>
Sun, 14 Jan 2024 21:54:33 +0000 (23:54 +0200)
committerSergey Poznyakoff <gray@gnu.org>
Sun, 14 Jan 2024 22:00:02 +0000 (00:00 +0200)
* 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.

src/buffer.c
src/common.h
src/suffix.c
src/tar.c

index 404959da7e0cb0e8d5732f7d37787c90ecd279f6..56273642bf63db3e4f7a7ae02a60d3e1edce436a 100644 (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;
index f5d45c6e0020a5ba709d8ddff04d5bccd4fdfe06..2154f2322df86f5a15377c6602ee3d36b14b5329 100644 (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 */
index a97d15f3417078477d6e050bcb9921ed2ec1fe02..51490b2d2d4e9aa7614174aaa4127b3b8d0d5307 100644 (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 *
index 57413e73ddc9b366aae52b420e20cf201c100f1b..bf69d6e527152dabf087bf08dac3475390389891 100644 (file)
--- 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: