bug-tar
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-tar] Reading out the label (and _only_ the label)


From: Sergey Poznyakoff
Subject: Re: [Bug-tar] Reading out the label (and _only_ the label)
Date: Wed, 28 Sep 2005 22:49:22 +0300

I have installed the following patch. It implements --test-label
option. 

If no command line argument is given, the option prints the label
from the archive and exits with code 0. 

Otherwise, if an argument is specified, the label is compared against
its value. In this case tar exits with code 0 if the two strings match,
and with code 2 if they do not. So, in your script you can use something
like

  if tar --test-label -f $ARCHIVE 'My label' ; then
     .... label is present
  fi

When used with --test-label, tar reads only the first block from the
archive.

The diff below is taken against the CVS version. It should apply to
version 1.15.1, though.

Regards,
Sergey


2005-09-28  Sergey Poznyakoff  <address@hidden>

      * src/common.h (test_label_option): New variable;
      * src/list.c (print_header): Special handling if
        test_label_option is set.
      * src/names.c (all_names_found): If test_label_option
        is set return true.
      * src/tar.c: New option --test-label tests the
        archive volume label.

Index: src/common.h
===================================================================
RCS file: /cvsroot/tar/tar/src/common.h,v
retrieving revision 1.59
diff -p -u -r1.59 common.h
--- src/common.h        17 Sep 2005 06:41:55 -0000      1.59
+++ src/common.h        28 Sep 2005 19:31:49 -0000
@@ -313,6 +313,9 @@ GLOBAL dev_t root_device;
 
 /* Unquote filenames */
 GLOBAL bool unquote_option;
+
+GLOBAL bool test_label_option;
+
 
 /* Declarations for each module.  */
 
Index: src/list.c
===================================================================
RCS file: /cvsroot/tar/tar/src/list.c,v
retrieving revision 1.93
diff -p -u -r1.93 list.c
--- src/list.c  17 Sep 2005 06:41:56 -0000      1.93
+++ src/list.c  28 Sep 2005 19:31:52 -0000
@@ -210,7 +210,7 @@ list_archive (void)
   decode_header (current_header, &current_stat_info, &current_format, 0);
   if (verbose_option)
     print_header (&current_stat_info, -1);
-
+  
   if (incremental_option && current_header->header.typeflag == GNUTYPE_DUMPDIR)
     {
       off_t size;
@@ -1009,6 +1009,9 @@ print_header (struct tar_stat_info *st, 
   int pad;
   int sizelen;
 
+  if (test_label_option && current_header->header.typeflag != GNUTYPE_VOLHDR)
+    return;
+  
   if (block_number_option)
     {
       char buf[UINTMAX_STRSIZE_BOUND];
Index: src/names.c
===================================================================
RCS file: /cvsroot/tar/tar/src/names.c,v
retrieving revision 1.53
diff -p -u -r1.53 names.c
--- src/names.c 2 Jun 2005 21:02:58 -0000       1.53
+++ src/names.c 28 Sep 2005 19:31:54 -0000
@@ -538,6 +538,8 @@ all_names_found (struct tar_stat_info *p
   struct name const *cursor;
   size_t len;
 
+  if (test_label_option)
+    return true;
   if (!p->file_name || occurrence_option == 0 || p->had_trailing_slash)
     return false;
   len = strlen (p->file_name);
Index: src/tar.c
===================================================================
RCS file: /cvsroot/tar/tar/src/tar.c,v
retrieving revision 1.123
diff -p -u -r1.123 tar.c
--- src/tar.c   22 Jun 2005 06:24:39 -0000      1.123
+++ src/tar.c   28 Sep 2005 19:31:57 -0000
@@ -230,6 +230,7 @@ enum
   SHOW_OMITTED_DIRS_OPTION,
   STRIP_COMPONENTS_OPTION,
   SUFFIX_OPTION,
+  TEST_LABEL_OPTION,
   TO_COMMAND_OPTION,
   TOTALS_OPTION,
   UNQUOTE_OPTION,
@@ -311,6 +312,9 @@ static struct argp_option options[] = {
    N_("process only the NUMBERth occurrence of each file in the archive. This 
option is valid only in conjunction with one of the subcommands --delete, 
--diff, --extract or --list and when a list of files is given either on the 
command line or via -T option. NUMBER defaults to 1."), 21 },
   {"seek", 'n', NULL, 0,
    N_("archive is seekable"), 21 },
+  {"test-label", TEST_LABEL_OPTION, NULL, 0,
+   N_("List volume label and exit"), 21 },
+    
 
   {NULL, 0, NULL, 0,
    N_("Overwrite control:"), 30},
@@ -1036,6 +1040,11 @@ parse_opt (int key, char *arg, struct ar
       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
@@ -1762,7 +1771,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 -',
        




reply via email to

[Prev in Thread] Current Thread [Next in Thread]