]> Dogcows Code - chaz/tar/blobdiff - src/xheader.c
(xheader_format_name): Fix memory leak.
[chaz/tar] / src / xheader.c
index 493a955c49ff0c7e8df62d613243e4dea4cc188d..d880bd5600a5ef9f3873b07632a3cab23b5f684b 100644 (file)
@@ -225,15 +225,17 @@ xheader_set_option (char *string)
                               to the result of the basename
                               utility on the translated file name.
      %p                       The process ID of the pax process.
+     %n                       The value of the 3rd argument.  
      %%                       A '%' character. */
 
-static char *
-xheader_format_name (struct tar_stat_info *st, const char *fmt, bool allow_n)
+char *
+xheader_format_name (struct tar_stat_info *st, const char *fmt, size_t n)
 {
   char *buf;
   size_t len = strlen (fmt);
   char *q;
   const char *p;
+  char *dirp = NULL;
   char *dir = NULL;
   char *base = NULL;
   char pidbuf[UINTMAX_STRSIZE_BOUND];
@@ -252,9 +254,10 @@ xheader_format_name (struct tar_stat_info *st, const char *fmt, bool allow_n)
        case 'd':
          if (st)
            {
-             dir = safer_name_suffix (dir_name (st->orig_file_name),
-                                      false, absolute_names_option);
-             len += strlen (dir) - 1;
+             if (!dirp)
+               dirp = dir_name (st->orig_file_name);
+             dir = safer_name_suffix (dirp, false, absolute_names_option);
+             len += strlen (dir) - 2;
            }
          break;
 
@@ -262,21 +265,18 @@ xheader_format_name (struct tar_stat_info *st, const char *fmt, bool allow_n)
          if (st)
            {
              base = base_name (st->orig_file_name);
-             len += strlen (base) - 1;
+             len += strlen (base) - 2;
            }
          break;
 
        case 'p':
          pptr = umaxtostr (getpid (), pidbuf);
-         len += pidbuf + sizeof pidbuf - 1 - pptr - 1;
+         len += pidbuf + sizeof pidbuf - 1 - pptr - 2;
          break;
 
        case 'n':
-         if (allow_n)
-           {
-             nptr = umaxtostr (global_header_count + 1, nbuf);
-             len += nbuf + sizeof nbuf - 1 - nptr - 1;
-           }
+         nptr = umaxtostr (n, nbuf);
+         len += nbuf + sizeof nbuf - 1 - nptr - 2;
          break;
        }
       p++;
@@ -316,6 +316,7 @@ xheader_format_name (struct tar_stat_info *st, const char *fmt, bool allow_n)
                {
                  q = stpcpy (q, nptr);
                  p += 2;
+                 break;
                }
              /* else fall through */
 
@@ -329,6 +330,8 @@ xheader_format_name (struct tar_stat_info *st, const char *fmt, bool allow_n)
        *q++ = *p++;
     }
 
+  free (dirp);
+  
   /* Do not allow it to end in a slash */
   while (q > buf && ISSLASH (q[-1]))
     q--;
@@ -341,7 +344,7 @@ xheader_xhdr_name (struct tar_stat_info *st)
 {
   if (!exthdr_name)
     assign_string (&exthdr_name, "%d/PaxHeaders.%p/%f");
-  return xheader_format_name (st, exthdr_name, false);
+  return xheader_format_name (st, exthdr_name, 0);
 }
 
 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
@@ -361,7 +364,7 @@ xheader_ghdr_name (void)
       strcat(globexthdr_name, GLOBAL_HEADER_TEMPLATE);
     }
 
-  return xheader_format_name (NULL, globexthdr_name, true);
+  return xheader_format_name (NULL, globexthdr_name, global_header_count + 1);
 }
 
 void
@@ -371,6 +374,17 @@ xheader_write (char type, char *name, struct xheader *xhdr)
   size_t size;
   char *p;
 
+  if (multi_volume_option)
+    {
+      /* Estimate the total size of the extended header and, in case
+        if XHDTYPE, the ustar header following it, and make sure that
+        these fit into the current volume */
+      size_t hblocks = 1 + (extended_header.size + BLOCKSIZE - 1) / BLOCKSIZE;
+      if (type == XHDTYPE)
+       hblocks++;
+      multi_volume_fixup (hblocks);
+    }
+  
   size = xhdr->size;
   header = start_private_header (name, size);
   header->header.typeflag = type;
@@ -396,6 +410,47 @@ xheader_write (char type, char *name, struct xheader *xhdr)
     }
   while (size > 0);
   xheader_destroy (xhdr);
+
+  if (type == XGLTYPE)
+    global_header_count++;
+}
+
+/* SIZE is guaranteed to be divisible by BLOCKSIZE */
+void
+xheader_eof (size_t size)
+{
+  union block *header;
+  char *name;
+  int first_block = 1;
+  int nl = 0;
+  
+  size -= BLOCKSIZE;
+  name = xheader_ghdr_name ();
+  header = start_private_header (name, size);
+  header->header.typeflag = XGLTYPE;
+  free (name);
+  simple_finish_header (header);
+  if (size)
+    nl = 1;
+  while (size > 0)
+    {
+      size_t len;
+
+      header = find_next_block ();
+      len = BLOCKSIZE;
+      if (len > size)
+       len = size;
+      memset (header->buffer, 0, len);
+      if (first_block)
+       {
+         first_block = 0;
+         sprintf (header->buffer, "%d GNU.volume.eof=", size);
+       }
+      size -= len;
+      set_next_block_after (header);
+    }
+  if (nl)
+    header->buffer[BLOCKSIZE-1] = '\n';
 }
 
 void
@@ -414,7 +469,6 @@ xheader_write_global (void)
   xheader_write (XGLTYPE, name = xheader_ghdr_name (),
                 &extended_header);
   free (name);
-  global_header_count++;
 }
 
 \f
@@ -614,6 +668,20 @@ extended_header_init (void)
     }
 }
 
+void
+xheader_save (struct xheader *xhdr)
+{
+  *xhdr = extended_header;
+  memset (&extended_header, 0, sizeof extended_header);
+}
+
+void
+xheader_restore (struct xheader *xhdr)
+{
+  xheader_destroy (&extended_header);
+  extended_header = *xhdr;
+}
+
 void
 xheader_store (char const *keyword, struct tar_stat_info const *st,
               void const *data)
@@ -1285,6 +1353,60 @@ dumpdir_decoder (struct tar_stat_info *st, char const *arg,
   memcpy (st->dumpdir, arg, size);
 }
 
+static void
+volume_label_coder (struct tar_stat_info const *st, char const *keyword,
+                   struct xheader *xhdr, void const *data)
+{
+  code_string (data, keyword, xhdr);
+}
+
+static void
+volume_label_decoder (struct tar_stat_info *st, char const *arg, size_t size)
+{
+  decode_string (&volume_label, arg);
+}
+
+static void
+volume_size_coder (struct tar_stat_info const *st, char const *keyword,
+                  struct xheader *xhdr, void const *data)
+{
+  off_t v = *(off_t*)data;
+  code_num (v, keyword, xhdr);
+}
+
+static void
+volume_size_decoder (struct tar_stat_info *st, char const *arg, size_t size)
+{
+  uintmax_t u;
+  if (decode_num (&u, arg, TYPE_MAXIMUM (uintmax_t), "GNU.volume.size"))
+    continued_file_size = u;
+}
+
+/* FIXME: Merge with volume_size_coder */
+static void
+volume_offset_coder (struct tar_stat_info const *st, char const *keyword,
+                    struct xheader *xhdr, void const *data)
+{
+  off_t v = *(off_t*)data;
+  code_num (v, keyword, xhdr);
+}
+
+static void
+volume_offset_decoder (struct tar_stat_info *st, char const *arg, size_t size)
+{
+  uintmax_t u;
+  if (decode_num (&u, arg, TYPE_MAXIMUM (uintmax_t), "GNU.volume.offset"))
+    continued_file_offset = u;
+}
+
+static void
+volume_filename_decoder (struct tar_stat_info *st, char const *arg,
+                        size_t size)
+{
+  decode_string (&continued_file_name, arg);
+}
+  
+
 struct xhdr_tab const xhdr_tab[] = {
   { "atime",   atime_coder,    atime_decoder,    false },
   { "comment", dummy_coder,    dummy_decoder,    false },
@@ -1318,11 +1440,9 @@ struct xhdr_tab const xhdr_tab[] = {
   { "GNU.dumpdir",           dumpdir_coder, dumpdir_decoder,
     true },
 
-#if 0 /* GNU private keywords (not yet implemented) */
-
-  /* Keeps the tape/volume header. May be present only in the global headers.
+  /* Keeps the tape/volume label. May be present only in the global headers.
      Equivalent to GNUTYPE_VOLHDR.  */
-  { "GNU.volume.header", volume_header_coder, volume_header_decoder, false },
+  { "GNU.volume.label", volume_label_coder, volume_label_decoder, true },
 
   /* These may be present in a first global header of the archive.
      They provide the same functionality as GNUTYPE_MULTIVOL header.
@@ -1330,9 +1450,10 @@ struct xhdr_tab const xhdr_tab[] = {
      otherwise kept in the size field of a multivolume header.  The
      GNU.volume.offset keeps the offset of the start of this volume,
      otherwise kept in oldgnu_header.offset.  */
-  { "GNU.volume.size", volume_size_coder, volume_size_decoder, false },
-  { "GNU.volume.offset", volume_offset_coder, volume_offset_decoder, false },
-#endif
+  { "GNU.volume.filename", volume_label_coder, volume_filename_decoder,
+    true },
+  { "GNU.volume.size", volume_size_coder, volume_size_decoder, true },
+  { "GNU.volume.offset", volume_offset_coder, volume_offset_decoder, true },
 
   { NULL, NULL, NULL, false }
 };
This page took 0.02987 seconds and 4 git commands to generate.