1 /* POSIX extended headers for tar.
3 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #define obstack_chunk_alloc xmalloc
28 #define obstack_chunk_free free
31 bool xheader_protected_pattern_p (const char *pattern
);
32 bool xheader_protected_keyword_p (const char *keyword
);
34 /* Used by xheader_finish() */
35 static void code_string (char const *string
, char const *keyword
,
36 struct xheader
*xhdr
);
37 static void extended_header_init ();
39 /* Number of global headers written so far. */
40 static size_t global_header_count
;
41 /* FIXME: Possibly it should be reset after changing the volume.
42 POSIX %n specification says that it is expanded to the sequence
43 number of current global header in *the* archive. However, for
44 multi-volume archives this will yield duplicate header names
45 in different volumes, which I'd like to avoid. The best way
46 to solve this would be to use per-archive header count as required
47 by POSIX *and* set globexthdr.name to, say,
48 $TMPDIR/GlobalHead.%p.$NUMVOLUME.%n.
50 However it should wait until buffer.c is finally rewritten */
57 struct keyword_list
*next
;
63 /* List of keyword patterns set by delete= option */
64 static struct keyword_list
*keyword_pattern_list
;
65 /* List of keyword/value pairs set by `keyword=value' option */
66 static struct keyword_list
*keyword_global_override_list
;
67 /* List of keyword/value pairs set by `keyword:=value' option */
68 static struct keyword_list
*keyword_override_list
;
69 /* Template for the name field of an 'x' type header */
70 static char *exthdr_name
;
71 /* Template for the name field of a 'g' type header */
72 static char *globexthdr_name
;
75 xheader_keyword_deleted_p (const char *kw
)
77 struct keyword_list
*kp
;
79 for (kp
= keyword_pattern_list
; kp
; kp
= kp
->next
)
80 if (fnmatch (kp
->pattern
, kw
, 0) == 0)
86 xheader_keyword_override_p (const char *keyword
)
88 struct keyword_list
*kp
;
90 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
91 if (strcmp (kp
->pattern
, keyword
) == 0)
97 xheader_list_append (struct keyword_list
**root
, char *kw
, char *value
)
99 struct keyword_list
*kp
= xmalloc (sizeof *kp
);
100 kp
->pattern
= xstrdup (kw
);
101 kp
->value
= value
? xstrdup (value
) : NULL
;
107 xheader_set_single_keyword (char *kw
)
109 USAGE_ERROR ((0, 0, "Keyword %s is unknown or not yet imlemented", kw
));
113 xheader_set_keyword_equal (char *kw
, char *eq
)
124 while (p
> kw
&& isspace (*p
))
129 for (p
= eq
+ 1; *p
&& isspace (*p
); p
++)
132 if (strcmp (kw
, "delete") == 0)
134 if (xheader_protected_pattern_p (p
))
135 USAGE_ERROR ((0, 0, "Pattern %s cannot be used", p
));
136 xheader_list_append (&keyword_pattern_list
, p
, NULL
);
138 else if (strcmp (kw
, "exthdr.name") == 0)
139 assign_string (&exthdr_name
, p
);
140 else if (strcmp (kw
, "globexthdr.name") == 0)
141 assign_string (&globexthdr_name
, p
);
144 if (xheader_protected_keyword_p (kw
))
145 USAGE_ERROR ((0, 0, "Keyword %s cannot be overridden", kw
));
147 xheader_list_append (&keyword_global_override_list
, kw
, p
);
149 xheader_list_append (&keyword_override_list
, kw
, p
);
154 xheader_set_option (char *string
)
157 for (token
= strtok (string
, ","); token
; token
= strtok (NULL
, ","))
159 char *p
= strchr (token
, '=');
161 xheader_set_single_keyword (token
);
163 xheader_set_keyword_equal (token
, p
);
168 string Includes: Replaced By:
169 %d The directory name of the file,
170 equivalent to the result of the
171 dirname utility on the translated
173 %f The filename of the file, equivalent
174 to the result of the basename
175 utility on the translated pathname.
176 %p The process ID of the pax process.
177 %% A '%' character. */
180 xheader_format_name (struct tar_stat_info
*st
, const char *fmt
, bool allow_n
)
183 size_t len
= strlen (fmt
);
186 char *dirname
= NULL
;
187 char *basename
= NULL
;
191 for (p
= fmt
; *p
&& (p
= strchr (p
, '%')); )
202 dirname
= safer_name_suffix (dir_name (st
->orig_file_name
),
204 len
+= strlen (dirname
) - 1;
211 basename
= base_name (st
->orig_file_name
);
212 len
+= strlen (basename
) - 1;
217 snprintf (pidbuf
, sizeof pidbuf
, "%lu",
218 (unsigned long) getpid ());
219 len
+= strlen (pidbuf
) - 1;
225 snprintf (nbuf
, sizeof nbuf
, "%lu",
226 (unsigned long) global_header_count
+ 1);
227 len
+= strlen (nbuf
) - 1;
234 buf
= xmalloc (len
+ 1);
235 for (q
= buf
, p
= fmt
; *p
; )
248 q
= stpcpy (q
, dirname
);
254 q
= stpcpy (q
, basename
);
259 q
= stpcpy (q
, pidbuf
);
266 q
= stpcpy (q
, nbuf
);
269 /* else fall through */
281 /* Do not allow it to end in a slash */
282 while (q
> buf
&& ISSLASH (q
[-1]))
289 xheader_xhdr_name (struct tar_stat_info
*st
)
292 assign_string (&exthdr_name
, "%d/PaxHeaders.%p/%f");
293 return xheader_format_name (st
, exthdr_name
, false);
296 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
301 if (!globexthdr_name
)
304 const char *tmp
= getenv ("TMPDIR");
307 len
= strlen (tmp
) + sizeof (GLOBAL_HEADER_TEMPLATE
); /* Includes nul */
308 globexthdr_name
= xmalloc (len
);
309 strcpy(globexthdr_name
, tmp
);
310 strcat(globexthdr_name
, GLOBAL_HEADER_TEMPLATE
);
313 return xheader_format_name (NULL
, globexthdr_name
, true);
317 xheader_write (char type
, char *name
, struct xheader
*xhdr
)
324 header
= start_private_header (name
, size
);
325 header
->header
.typeflag
= type
;
327 simple_finish_header (header
);
335 header
= find_next_block ();
339 memcpy (header
->buffer
, p
, len
);
341 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
344 set_next_block_after (header
);
347 xheader_destroy (xhdr
);
351 xheader_write_global ()
354 struct keyword_list
*kp
;
356 if (!keyword_global_override_list
)
359 extended_header_init ();
360 for (kp
= keyword_global_override_list
; kp
; kp
= kp
->next
)
361 code_string (kp
->value
, kp
->pattern
, &extended_header
);
362 xheader_finish (&extended_header
);
363 xheader_write (XGLTYPE
, name
= xheader_ghdr_name (),
366 global_header_count
++;
370 /* General Interface */
375 void (*coder
) (struct tar_stat_info
const *, char const *,
376 struct xheader
*, void *data
);
377 void (*decoder
) (struct tar_stat_info
*, char const *);
381 /* This declaration must be extern, because ISO C99 section 6.9.2
382 prohibits a tentative definition that has both internal linkage and
383 incomplete type. If we made it static, we'd have to declare its
384 size which would be a maintenance pain; if we put its initializer
385 here, we'd need a boatload of forward declarations, which would be
386 even more of a pain. */
387 extern struct xhdr_tab
const xhdr_tab
[];
389 static struct xhdr_tab
const *
390 locate_handler (char const *keyword
)
392 struct xhdr_tab
const *p
;
394 for (p
= xhdr_tab
; p
->keyword
; p
++)
395 if (strcmp (p
->keyword
, keyword
) == 0)
401 xheader_protected_pattern_p (const char *pattern
)
403 struct xhdr_tab
const *p
;
405 for (p
= xhdr_tab
; p
->keyword
; p
++)
406 if (p
->protect
&& fnmatch (pattern
, p
->keyword
, 0) == 0)
412 xheader_protected_keyword_p (const char *keyword
)
414 struct xhdr_tab
const *p
;
416 for (p
= xhdr_tab
; p
->keyword
; p
++)
417 if (p
->protect
&& strcmp (p
->keyword
, keyword
) == 0)
422 /* Decodes a single extended header record. Advances P to the next
424 Returns true on success, false otherwise. */
426 decode_record (char **p
, struct tar_stat_info
*st
)
432 struct xhdr_tab
const *t
;
437 len
= strtoul (*p
, p
, 10);
441 _("Malformed extended header: missing whitespace after the length")));
446 for (;*p
< start
+ len
; ++*p
)
452 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
459 if (xheader_keyword_deleted_p (keyword
)
460 || xheader_keyword_override_p (keyword
))
462 t
= locate_handler (keyword
);
472 t
->decoder (st
, value
);
481 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
483 for (; kp
; kp
= kp
->next
)
485 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
487 t
->decoder (st
, kp
->value
);
492 xheader_decode (struct tar_stat_info
*st
)
494 run_override_list (keyword_global_override_list
, st
);
496 if (extended_header
.size
)
498 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
499 char *endp
= &extended_header
.buffer
[extended_header
.size
-1];
502 if (!decode_record (&p
, st
))
505 run_override_list (keyword_override_list
, st
);
509 extended_header_init ()
511 if (!extended_header
.stk
)
513 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
514 obstack_init (extended_header
.stk
);
519 xheader_store (char const *keyword
, struct tar_stat_info
const *st
, void *data
)
521 struct xhdr_tab
const *t
;
524 if (extended_header
.buffer
)
526 t
= locate_handler (keyword
);
529 if (xheader_keyword_deleted_p (keyword
)
530 || xheader_keyword_override_p (keyword
))
532 extended_header_init ();
533 t
->coder (st
, keyword
, &extended_header
, data
);
537 xheader_read (union block
*p
, size_t size
)
542 free (extended_header
.buffer
);
544 extended_header
.size
= size
;
545 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
546 extended_header
.buffer
= xmalloc (size
+ 1);
555 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
556 set_next_block_after (p
);
558 p
= find_next_block ();
567 format_uintmax (uintmax_t val
, char *buf
, size_t s
)
574 while ((val
/= 10) != 0);
578 char *p
= buf
+ s
- 1;
582 *p
-- = val
% 10 + '0';
584 while ((val
/= 10) != 0);
593 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
595 size_t len
= strlen (keyword
) + strlen (value
) + 3; /* ' ' + '=' + '\n' */
602 n
= format_uintmax (len
+ p
, NULL
, 0);
606 format_uintmax (len
+ n
, nbuf
, n
);
607 obstack_grow (xhdr
->stk
, nbuf
, n
);
608 obstack_1grow (xhdr
->stk
, ' ');
609 obstack_grow (xhdr
->stk
, keyword
, strlen (keyword
));
610 obstack_1grow (xhdr
->stk
, '=');
611 obstack_grow (xhdr
->stk
, value
, strlen (value
));
612 obstack_1grow (xhdr
->stk
, '\n');
616 xheader_finish (struct xheader
*xhdr
)
618 struct keyword_list
*kp
;
620 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
621 code_string (kp
->value
, kp
->pattern
, xhdr
);
623 obstack_1grow (xhdr
->stk
, 0);
624 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
625 xhdr
->size
= strlen (xhdr
->buffer
);
629 xheader_destroy (struct xheader
*xhdr
)
633 obstack_free (xhdr
->stk
, NULL
);
644 /* Implementations */
646 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
648 xheader_print (xhdr
, keyword
, string
);
652 code_time (time_t t
, unsigned long nano
,
653 char const *keyword
, struct xheader
*xhdr
)
656 size_t s
= format_uintmax (t
, NULL
, 0);
657 if (s
+ 11 >= sizeof sbuf
)
659 format_uintmax (t
, sbuf
, s
);
661 s
+= format_uintmax (nano
, sbuf
+ s
, 9);
663 xheader_print (xhdr
, keyword
, sbuf
);
667 decode_time (char const *arg
, time_t *secs
, unsigned long *nsecs
)
671 if (xstrtoumax (arg
, &p
, 10, &u
, "") == LONGINT_OK
)
674 if (*p
== '.' && xstrtoumax (p
+1, NULL
, 10, &u
, "") == LONGINT_OK
)
680 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
683 size_t s
= format_uintmax (value
, NULL
, 0);
684 format_uintmax (value
, sbuf
, s
);
686 xheader_print (xhdr
, keyword
, sbuf
);
690 dummy_coder (struct tar_stat_info
const *st
, char const *keyword
,
691 struct xheader
*xhdr
, void *data
)
696 dummy_decoder (struct tar_stat_info
*st
, char const *arg
)
701 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
702 struct xheader
*xhdr
, void *data
)
704 code_time (st
->stat
.st_atime
, st
->atime_nsec
, keyword
, xhdr
);
708 atime_decoder (struct tar_stat_info
*st
, char const *arg
)
710 decode_time (arg
, &st
->stat
.st_atime
, &st
->atime_nsec
);
714 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
715 struct xheader
*xhdr
, void *data
)
717 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
721 gid_decoder (struct tar_stat_info
*st
, char const *arg
)
724 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
729 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
730 struct xheader
*xhdr
, void *data
)
732 code_string (st
->gname
, keyword
, xhdr
);
736 gname_decoder (struct tar_stat_info
*st
, char const *arg
)
738 assign_string (&st
->gname
, arg
);
742 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
743 struct xheader
*xhdr
, void *data
)
745 code_string (st
->link_name
, keyword
, xhdr
);
749 linkpath_decoder (struct tar_stat_info
*st
, char const *arg
)
751 assign_string (&st
->link_name
, arg
);
755 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
756 struct xheader
*xhdr
, void *data
)
758 code_time (st
->stat
.st_ctime
, st
->ctime_nsec
, keyword
, xhdr
);
762 ctime_decoder (struct tar_stat_info
*st
, char const *arg
)
764 decode_time (arg
, &st
->stat
.st_ctime
, &st
->ctime_nsec
);
768 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
769 struct xheader
*xhdr
, void *data
)
771 code_time (st
->stat
.st_mtime
, st
->mtime_nsec
, keyword
, xhdr
);
775 mtime_decoder (struct tar_stat_info
*st
, char const *arg
)
777 decode_time (arg
, &st
->stat
.st_mtime
, &st
->mtime_nsec
);
781 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
782 struct xheader
*xhdr
, void *data
)
784 code_string (st
->file_name
, keyword
, xhdr
);
788 path_decoder (struct tar_stat_info
*st
, char const *arg
)
790 assign_string (&st
->orig_file_name
, arg
);
791 assign_string (&st
->file_name
, arg
);
792 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
796 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
797 struct xheader
*xhdr
, void *data
)
799 code_num (st
->stat
.st_size
, keyword
, xhdr
);
803 size_decoder (struct tar_stat_info
*st
, char const *arg
)
806 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
807 st
->stat
.st_size
= u
;
811 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
812 struct xheader
*xhdr
, void *data
)
814 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
818 uid_decoder (struct tar_stat_info
*st
, char const *arg
)
821 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
826 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
827 struct xheader
*xhdr
, void *data
)
829 code_string (st
->uname
, keyword
, xhdr
);
833 uname_decoder (struct tar_stat_info
*st
, char const *arg
)
835 assign_string (&st
->uname
, arg
);
839 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
840 struct xheader
*xhdr
, void *data
)
842 size_coder (st
, keyword
, xhdr
, data
);
846 sparse_size_decoder (struct tar_stat_info
*st
, char const *arg
)
849 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
850 st
->archive_file_size
= u
;
854 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
855 struct xheader
*xhdr
, void *data
)
857 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
861 sparse_numblocks_decoder (struct tar_stat_info
*st
, char const *arg
)
864 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
866 st
->sparse_map_size
= u
;
867 st
->sparse_map
= calloc(st
->sparse_map_size
, sizeof(st
->sparse_map
[0]));
868 st
->sparse_map_avail
= 0;
873 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
874 struct xheader
*xhdr
, void *data
)
876 size_t i
= *(size_t*)data
;
877 code_num (st
->sparse_map
[i
].offset
, keyword
, xhdr
);
881 sparse_offset_decoder (struct tar_stat_info
*st
, char const *arg
)
884 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
885 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
889 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
890 struct xheader
*xhdr
, void *data
)
892 size_t i
= *(size_t*)data
;
893 code_num (st
->sparse_map
[i
].numbytes
, keyword
, xhdr
);
897 sparse_numbytes_decoder (struct tar_stat_info
*st
, char const *arg
)
900 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
902 if (st
->sparse_map_avail
== st
->sparse_map_size
)
904 size_t newsize
= st
->sparse_map_size
*= 2;
905 st
->sparse_map
= xrealloc (st
->sparse_map
,
907 * sizeof st
->sparse_map
[0]);
909 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
913 struct xhdr_tab
const xhdr_tab
[] = {
914 { "atime", atime_coder
, atime_decoder
},
915 { "comment", dummy_coder
, dummy_decoder
},
916 { "charset", dummy_coder
, dummy_decoder
},
917 { "ctime", ctime_coder
, ctime_decoder
},
918 { "gid", gid_coder
, gid_decoder
},
919 { "gname", gname_coder
, gname_decoder
},
920 { "linkpath", linkpath_coder
, linkpath_decoder
},
921 { "mtime", mtime_coder
, mtime_decoder
},
922 { "path", path_coder
, path_decoder
},
923 { "size", size_coder
, size_decoder
},
924 { "uid", uid_coder
, uid_decoder
},
925 { "uname", uname_coder
, uname_decoder
},
927 /* Sparse file handling */
928 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
, true },
929 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
931 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
933 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
936 #if 0 /* GNU private keywords (not yet implemented) */
938 /* The next directory entry actually contains the names of files
939 that were in the directory at the time the dump was made.
940 Supersedes GNUTYPE_DUMPDIR header type. */
941 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
},
943 /* Keeps the tape/volume header. May be present only in the global headers.
944 Equivalent to GNUTYPE_VOLHDR. */
945 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
},
947 /* These may be present in a first global header of the archive.
948 They provide the same functionality as GNUTYPE_MULTIVOL header.
949 The GNU.volume.size keeps the real_s_sizeleft value, which is
950 otherwise kept in the size field of a multivolume header. The
951 GNU.volume.offset keeps the offset of the start of this volume,
952 otherwise kept in oldgnu_header.offset. */
953 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
},
954 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
},