]> Dogcows Code - chaz/tar/blobdiff - src/tar.c
* NEWS: New option --atime-preserve=system, which uses O_NOATIME.
[chaz/tar] / src / tar.c
index 692120e0aa0306f4395996812dd5b6963b65da74..b0cddaa5d29d28ddd36f5c459fd8aec69886260b 100644 (file)
--- a/src/tar.c
+++ b/src/tar.c
 #define GLOBAL
 #include "common.h"
 
+#include <argmatch.h>
 #include <getdate.h>
 #include <localedir.h>
 #include <rmt.h>
 #include <prepargs.h>
 #include <quotearg.h>
 #include <xstrtol.h>
+#include <stdopen.h>
 
 /* Local declarations.  */
 
@@ -74,12 +76,15 @@ request_stdin (const char *option)
   stdin_used_by = option;
 }
 
-/* Returns true if and only if the user typed 'y' or 'Y'.  */
+extern int rpmatch (char const *response);
+
+/* Returns true if and only if the user typed an affirmative response.  */
 int
 confirm (const char *message_action, const char *message_name)
 {
   static FILE *confirm_file;
   static int confirm_file_EOF;
+  bool status = false;
 
   if (!confirm_file)
     {
@@ -99,22 +104,24 @@ confirm (const char *message_action, const char *message_name)
   fprintf (stdlis, "%s %s?", message_action, quote (message_name));
   fflush (stdlis);
 
-  {
-    int reply = confirm_file_EOF ? EOF : getc (confirm_file);
-    int character;
+  if (!confirm_file_EOF)
+    {
+      char *response = NULL;
+      size_t response_size = 0;
+      if (getline (&response, &response_size, confirm_file) < 0)
+       confirm_file_EOF = 1;
+      else
+       status = rpmatch (response) > 0;
+      free (response);
+    }
 
-    for (character = reply;
-        character != '\n';
-        character = getc (confirm_file))
-      if (character == EOF)
-       {
-         confirm_file_EOF = 1;
-         fputc ('\n', stdlis);
-         fflush (stdlis);
-         break;
-       }
-    return reply == 'y' || reply == 'Y';
-  }
+  if (confirm_file_EOF)
+    {
+      fputc ('\n', stdlis);
+      fflush (stdlis);
+    }
+
+  return status;
 }
 
 static struct fmttab {
@@ -130,7 +137,7 @@ static struct fmttab {
 #endif
   { "gnu",     GNU_FORMAT },
   { "pax",     POSIX_FORMAT }, /* An alias for posix */
-  { NULL,       0 }
+  { NULL,      0 }
 };
 
 static void
@@ -162,7 +169,7 @@ archive_format_string (enum archive_format fmt)
 static void
 assert_format(unsigned fmt_mask)
 {
-  if ((FORMAT_MASK(archive_format) & fmt_mask) == 0)
+  if ((FORMAT_MASK (archive_format) & fmt_mask) == 0)
     USAGE_ERROR ((0, 0,
                  _("GNU features wanted on incompatible archive format")));
 }
@@ -171,13 +178,6 @@ assert_format(unsigned fmt_mask)
 \f
 /* Options.  */
 
-/* For long options that unconditionally set a single flag, we have getopt
-   do it.  For the others, we share the code for the equivalent short
-   named option, the name of which is stored in the otherwise-unused `val'
-   field of the `struct option'; for long options that have no equivalent
-   short option, we use non-characters as pseudo short options,
-   starting at CHAR_MAX + 1 and going upwards.  */
-
 enum
 {
   ANCHORED_OPTION = CHAR_MAX + 1,
@@ -228,8 +228,10 @@ enum
   SAME_OWNER_OPTION,
   SHOW_DEFAULTS_OPTION,
   SHOW_OMITTED_DIRS_OPTION,
+  SHOW_STORED_NAMES_OPTION,
   STRIP_COMPONENTS_OPTION,
   SUFFIX_OPTION,
+  TEST_LABEL_OPTION,
   TO_COMMAND_OPTION,
   TOTALS_OPTION,
   UNQUOTE_OPTION,
@@ -263,9 +265,10 @@ The version control may be set with --backup or VERSION_CONTROL, values are:\n\n
    Available option letters are DEIJQY and aeqy. Consider the following
    assignments:
 
-   [For Solaris tar compatibility]
+   [For Solaris tar compatibility =/= Is it important at all?]
    e  exit immediately with a nonzero exit status if unexpected errors occur
    E  use extended headers (--format=posix)
+
    [q  alias for --occurrence=1 =/= this would better be used for quiet?]
    [I  same as T =/= will harm star compatibility]
 
@@ -295,6 +298,8 @@ static struct argp_option options[] = {
   {"concatenate", 0, 0, OPTION_ALIAS, NULL, 10},
   {"delete", DELETE_OPTION, 0, 0,
    N_("delete from the archive (not on mag tapes!)"), 10 },
+  {"test-label", TEST_LABEL_OPTION, NULL, 0,
+   N_("Test archive volume label and exit"), 10 },
 
   {NULL, 0, NULL, 0,
    N_("Operation modifiers:"), 20},
@@ -353,8 +358,11 @@ static struct argp_option options[] = {
    N_("force NAME as group for added files"), 51 },
   {"mode", MODE_OPTION, N_("CHANGES"), 0,
    N_("force (symbolic) mode CHANGES for added files"), 51 },
-  {"atime-preserve", ATIME_PRESERVE_OPTION, 0, 0,
-   N_("don't change access times on dumped files"), 51 },
+  {"atime-preserve", ATIME_PRESERVE_OPTION,
+   N_("METHOD"), OPTION_ARG_OPTIONAL,
+   N_("preserve access times on dumped files, either by restoring the times"
+      " after reading (METHOD='replace'; default) or by not setting the times"
+      " in the first place (METHOD='system')"), 51 },
   {"touch", 'm', 0, 0,
    N_("don't extract file modified time"), 51 },
   {"same-owner", SAME_OWNER_OPTION, 0, 0,
@@ -550,6 +558,9 @@ static struct argp_option options[] = {
    N_("Show tar defaults"), 102 },
   {"show-omitted-dirs", SHOW_OMITTED_DIRS_OPTION, 0, 0,
    N_("When listing or extracting, list each directory that does not match search criteria"), 102 },
+  {"show-stored-names", SHOW_STORED_NAMES_OPTION, 0, 0,
+   N_("When creating archive in verbose mode, list member names as stored in the archive"),
+   102 },
 
   {NULL, 0, NULL, 0,
    N_("Compatibility options:"), 110 },
@@ -571,6 +582,17 @@ static struct argp_option options[] = {
   {0, 0, 0, 0, 0, 0}
 };
 
+static char const *const atime_preserve_args[] =
+{
+  "replace", "system", NULL
+};
+static enum atime_preserve const atime_preserve_types[] =
+{
+  replace_atime_preserve, system_atime_preserve
+};
+ARGMATCH_VERIFY (atime_preserve_args, atime_preserve_types);
+
+
 struct tar_args {
   char const *textual_date_option;
   int exclude_options;
@@ -656,7 +678,7 @@ read_name_from_file (FILE *fp, struct obstack *stk)
 {
   int c;
   size_t counter = 0;
-  
+
   for (c = getc (fp); c != EOF && c != filename_terminator; c = getc (fp))
     {
       if (c == 0)
@@ -721,7 +743,7 @@ update_argv (const char *filename, struct argp_state *state)
   size_t new_argc;
   bool is_stdin = false;
   enum read_file_list_state read_state;
-  
+
   if (!strcmp (filename, "-"))
     {
       is_stdin = true;
@@ -741,7 +763,7 @@ update_argv (const char *filename, struct argp_state *state)
   if (read_state == file_list_zero)
     {
       size_t size;
-      
+
       WARN ((0, 0, N_("%s: file name read contains nul character"),
             quotearg_colon (filename)));
 
@@ -751,7 +773,7 @@ update_argv (const char *filename, struct argp_state *state)
       for (; size > 0; size--, p++)
        if (*p)
          obstack_1grow (&argv_stk, *p);
-        else 
+        else
          obstack_1grow (&argv_stk, '\n');
       obstack_1grow (&argv_stk, 0);
       count = 1;
@@ -973,15 +995,14 @@ parse_opt (int key, char *arg, struct argp_state *state)
              stat_error (arg);
              USAGE_ERROR ((0, 0, _("Date sample file not found")));
            }
-         newer_mtime_option.tv_sec = st.st_mtime;
-         newer_mtime_option.tv_nsec = TIMESPEC_NS (st.st_mtim);
+         newer_mtime_option = get_stat_mtime (&st);
        }
       else
        {
          if (! get_date (&newer_mtime_option, arg, NULL))
            {
              WARN ((0, 0, _("Substituting %s for unknown date format %s"),
-                    tartime (newer_mtime_option.tv_sec), quote (arg)));
+                    tartime (newer_mtime_option, false), quote (arg)));
              newer_mtime_option.tv_nsec = 0;
            }
          else
@@ -1037,6 +1058,11 @@ parse_opt (int key, char *arg, struct argp_state *state)
       verbose_option++;
       break;
 
+    case TEST_LABEL_OPTION:
+      set_subcommand_option (LIST_SUBCOMMAND);
+      test_label_option = true;
+      break;
+
     case 'T':
       update_argv (arg, state);
       /* Indicate we've been given -T option. This is for backward
@@ -1106,7 +1132,15 @@ parse_opt (int key, char *arg, struct argp_state *state)
       break;
 
     case ATIME_PRESERVE_OPTION:
-      atime_preserve_option = true;
+      atime_preserve_option =
+       (arg
+        ? XARGMATCH ("--atime-preserve", arg,
+                     atime_preserve_args, atime_preserve_types)
+        : replace_atime_preserve);
+      if (! O_NOATIME && atime_preserve_option == system_atime_preserve)
+       FATAL_ERROR ((0, 0,
+                     _("--atime-preserve='system' is not supported"
+                       " on this platform\n")));
       break;
 
     case CHECKPOINT_OPTION:
@@ -1309,6 +1343,10 @@ parse_opt (int key, char *arg, struct argp_state *state)
       show_omitted_dirs_option = true;
       break;
 
+    case SHOW_STORED_NAMES_OPTION:
+      show_stored_names_option = true;
+      break;
+
     case SUFFIX_OPTION:
       backup_option = true;
       args->backup_suffix_string = arg;
@@ -1652,15 +1690,10 @@ decode_options (int argc, char **argv)
        archive_format = DEFAULT_ARCHIVE_FORMAT;
     }
 
-  if (volume_label_option && subcommand_option == CREATE_SUBCOMMAND)
-    assert_format (FORMAT_MASK (OLDGNU_FORMAT)
-                  | FORMAT_MASK (GNU_FORMAT));
-
-
-  if (incremental_option || multi_volume_option)
-    assert_format (FORMAT_MASK (OLDGNU_FORMAT) | FORMAT_MASK (GNU_FORMAT));
-
-  if (sparse_option)
+  if ((volume_label_option && subcommand_option == CREATE_SUBCOMMAND)
+      || incremental_option
+      || multi_volume_option
+      || sparse_option)
     assert_format (FORMAT_MASK (OLDGNU_FORMAT)
                   | FORMAT_MASK (GNU_FORMAT)
                   | FORMAT_MASK (POSIX_FORMAT));
@@ -1714,22 +1747,27 @@ decode_options (int argc, char **argv)
 
   if (volume_label_option)
     {
-      size_t volume_label_max_len =
-       (sizeof current_header->header.name
-        - 1 /* for trailing '\0' */
-        - (multi_volume_option
-           ? (sizeof " Volume "
-              - 1 /* for null at end of " Volume " */
-              + INT_STRLEN_BOUND (int) /* for volume number */
-              - 1 /* for sign, as 0 <= volno */)
-           : 0));
-      if (volume_label_max_len < strlen (volume_label_option))
-       USAGE_ERROR ((0, 0,
-                     ngettext ("%s: Volume label is too long (limit is %lu byte)",
-                               "%s: Volume label is too long (limit is %lu bytes)",
-                               volume_label_max_len),
-                     quotearg_colon (volume_label_option),
-                     (unsigned long) volume_label_max_len));
+      if (archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
+       {
+         size_t volume_label_max_len =
+           (sizeof current_header->header.name
+            - 1 /* for trailing '\0' */
+            - (multi_volume_option
+               ? (sizeof " Volume "
+                  - 1 /* for null at end of " Volume " */
+                  + INT_STRLEN_BOUND (int) /* for volume number */
+                  - 1 /* for sign, as 0 <= volno */)
+               : 0));
+         if (volume_label_max_len < strlen (volume_label_option))
+           USAGE_ERROR ((0, 0,
+                         ngettext ("%s: Volume label is too long (limit is %lu byte)",
+                                   "%s: Volume label is too long (limit is %lu bytes)",
+                                   volume_label_max_len),
+                         quotearg_colon (volume_label_option),
+                         (unsigned long) volume_label_max_len));
+       }
+      /* else FIXME
+        Label length in PAX format is limited by the volume size. */
     }
 
   if (verify_option)
@@ -1744,8 +1782,11 @@ decode_options (int argc, char **argv)
     {
       if (multi_volume_option)
        USAGE_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
-      if (subcommand_option == UPDATE_SUBCOMMAND)
+      if (subcommand_option == UPDATE_SUBCOMMAND
+         || subcommand_option == APPEND_SUBCOMMAND)
        USAGE_ERROR ((0, 0, _("Cannot update compressed archives")));
+      if (subcommand_option == CAT_SUBCOMMAND)
+       USAGE_ERROR ((0, 0, _("Cannot concatenate compressed archives")));
     }
 
   /* It is no harm to use --pax-option on non-pax archives in archive
@@ -1763,7 +1804,15 @@ decode_options (int argc, char **argv)
   if (recursive_unlink_option)
     old_files_option = UNLINK_FIRST_OLD_FILES;
 
-  if (utc_option)
+
+  if (test_label_option)
+    {
+      /* --test-label is silent if the user has specified the label name to
+        compare against. */
+      if (args.input_files == 0)
+       verbose_option++;
+    }
+  else if (utc_option)
     verbose_option = 2;
 
   /* Forbid using -c with no input files whatsoever.  Check that `-f -',
@@ -1819,16 +1868,10 @@ decode_options (int argc, char **argv)
 
   if (verbose_option && args.textual_date_option)
     {
-      /* FIXME: tartime should support nanoseconds, too, so that this
-        comparison doesn't complain about lost nanoseconds.  */
-      char const *treated_as = tartime (newer_mtime_option.tv_sec);
+      char const *treated_as = tartime (newer_mtime_option, true);
       if (strcmp (args.textual_date_option, treated_as) != 0)
-       WARN ((0, 0,
-              ngettext ("Treating date `%s' as %s + %ld nanosecond",
-                        "Treating date `%s' as %s + %ld nanoseconds",
-                        newer_mtime_option.tv_nsec),
-              args.textual_date_option, treated_as,
-              newer_mtime_option.tv_nsec));
+       WARN ((0, 0, _("Treating date `%s' as %s"),
+              args.textual_date_option, treated_as));
     }
 }
 
@@ -1850,6 +1893,9 @@ main (int argc, char **argv)
   filename_terminator = '\n';
   set_quoting_style (0, escape_quoting_style);
 
+  /* Make sure we have first three descriptors available */
+  stdopen ();
+
   /* Pre-allocate a few structures.  */
 
   allocated_archive_names = 10;
@@ -1953,5 +1999,20 @@ tar_stat_destroy (struct tar_stat_info *st)
   free (st->uname);
   free (st->gname);
   free (st->sparse_map);
+  free (st->dumpdir);
   memset (st, 0, sizeof (*st));
 }
+
+/* Format mask for all available formats that support nanosecond
+   timestamp resolution. */
+#define NS_PRECISION_FORMAT_MASK FORMAT_MASK (POSIX_FORMAT)
+
+/* Same as timespec_cmp, but ignore nanoseconds if current archive
+   format does not provide sufficient resolution.  */
+int
+tar_timespec_cmp (struct timespec a, struct timespec b)
+{
+  if (!(FORMAT_MASK (current_format) & NS_PRECISION_FORMAT_MASK))
+    a.tv_nsec = b.tv_nsec = 0;
+  return timespec_cmp (a, b);
+}
This page took 0.035562 seconds and 4 git commands to generate.