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
33 bool xheader_protected_pattern_p (const char *pattern
);
34 bool xheader_protected_keyword_p (const char *keyword
);
36 /* Used by xheader_finish() */
37 static void code_string (char const *string
, char const *keyword
,
38 struct xheader
*xhdr
);
39 static void extended_header_init ();
41 /* Number of global headers written so far. */
42 static size_t global_header_count
;
43 /* FIXME: Possibly it should be reset after changing the volume.
44 POSIX %n specification says that it is expanded to the sequence
45 number of current global header in *the* archive. However, for
46 multi-volume archives this will yield duplicate header names
47 in different volumes, which I'd like to avoid. The best way
48 to solve this would be to use per-archive header count as required
49 by POSIX *and* set globexthdr.name to, say,
50 $TMPDIR/GlobalHead.%p.$NUMVOLUME.%n.
52 However it should wait until buffer.c is finally rewritten */
59 struct keyword_list
*next
;
65 /* List of keyword patterns set by delete= option */
66 static struct keyword_list
*keyword_pattern_list
;
68 /* List of keyword/value pairs set by `keyword=value' option */
69 static struct keyword_list
*keyword_global_override_list
;
71 /* List of keyword/value pairs set by `keyword:=value' option */
72 static struct keyword_list
*keyword_override_list
;
74 /* List of keyword/value pairs decoded from the last 'g' type header */
75 static struct keyword_list
*global_header_override_list
;
77 /* Template for the name field of an 'x' type header */
78 static char *exthdr_name
;
80 /* Template for the name field of a 'g' type header */
81 static char *globexthdr_name
;
84 xheader_keyword_deleted_p (const char *kw
)
86 struct keyword_list
*kp
;
88 for (kp
= keyword_pattern_list
; kp
; kp
= kp
->next
)
89 if (fnmatch (kp
->pattern
, kw
, 0) == 0)
95 xheader_keyword_override_p (const char *keyword
)
97 struct keyword_list
*kp
;
99 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
100 if (strcmp (kp
->pattern
, keyword
) == 0)
106 xheader_list_append (struct keyword_list
**root
, char const *kw
,
109 struct keyword_list
*kp
= xmalloc (sizeof *kp
);
110 kp
->pattern
= xstrdup (kw
);
111 kp
->value
= value
? xstrdup (value
) : NULL
;
117 xheader_list_destroy (struct keyword_list
**root
)
121 struct keyword_list
*kw
= *root
;
124 struct keyword_list
*next
= kw
->next
;
136 xheader_set_single_keyword (char *kw
)
138 USAGE_ERROR ((0, 0, _("Keyword %s is unknown or not yet imlemented"), kw
));
142 xheader_set_keyword_equal (char *kw
, char *eq
)
153 while (p
> kw
&& isspace (*p
))
158 for (p
= eq
+ 1; *p
&& isspace (*p
); p
++)
161 if (strcmp (kw
, "delete") == 0)
163 if (xheader_protected_pattern_p (p
))
164 USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), p
));
165 xheader_list_append (&keyword_pattern_list
, p
, NULL
);
167 else if (strcmp (kw
, "exthdr.name") == 0)
168 assign_string (&exthdr_name
, p
);
169 else if (strcmp (kw
, "globexthdr.name") == 0)
170 assign_string (&globexthdr_name
, p
);
173 if (xheader_protected_keyword_p (kw
))
174 USAGE_ERROR ((0, 0, _("Keyword %s cannot be overridden"), kw
));
176 xheader_list_append (&keyword_global_override_list
, kw
, p
);
178 xheader_list_append (&keyword_override_list
, kw
, p
);
183 xheader_set_option (char *string
)
186 for (token
= strtok (string
, ","); token
; token
= strtok (NULL
, ","))
188 char *p
= strchr (token
, '=');
190 xheader_set_single_keyword (token
);
192 xheader_set_keyword_equal (token
, p
);
197 to_decimal (uintmax_t value
, char *where
, size_t size
)
204 where
[i
++] = '0' + value
% 10;
207 while (i
< size
&& value
);
208 for (j
= 0, i
--; j
< i
; j
++, i
--)
217 string Includes: Replaced By:
218 %d The directory name of the file,
219 equivalent to the result of the
220 dirname utility on the translated
222 %f The filename of the file, equivalent
223 to the result of the basename
224 utility on the translated pathname.
225 %p The process ID of the pax process.
226 %% A '%' character. */
229 xheader_format_name (struct tar_stat_info
*st
, const char *fmt
, bool allow_n
)
232 size_t len
= strlen (fmt
);
235 char *dirname
= NULL
;
236 char *basename
= NULL
;
240 for (p
= fmt
; *p
&& (p
= strchr (p
, '%')); )
251 dirname
= safer_name_suffix (dir_name (st
->orig_file_name
),
253 len
+= strlen (dirname
) - 1;
260 basename
= base_name (st
->orig_file_name
);
261 len
+= strlen (basename
) - 1;
266 to_decimal (getpid (), pidbuf
, sizeof pidbuf
);
267 len
+= strlen (pidbuf
) - 1;
273 to_decimal (global_header_count
+ 1, pidbuf
, sizeof pidbuf
);
274 len
+= strlen (nbuf
) - 1;
281 buf
= xmalloc (len
+ 1);
282 for (q
= buf
, p
= fmt
; *p
; )
295 q
= stpcpy (q
, dirname
);
301 q
= stpcpy (q
, basename
);
306 q
= stpcpy (q
, pidbuf
);
313 q
= stpcpy (q
, nbuf
);
316 /* else fall through */
328 /* Do not allow it to end in a slash */
329 while (q
> buf
&& ISSLASH (q
[-1]))
336 xheader_xhdr_name (struct tar_stat_info
*st
)
339 assign_string (&exthdr_name
, "%d/PaxHeaders.%p/%f");
340 return xheader_format_name (st
, exthdr_name
, false);
343 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
348 if (!globexthdr_name
)
351 const char *tmp
= getenv ("TMPDIR");
354 len
= strlen (tmp
) + sizeof (GLOBAL_HEADER_TEMPLATE
); /* Includes nul */
355 globexthdr_name
= xmalloc (len
);
356 strcpy(globexthdr_name
, tmp
);
357 strcat(globexthdr_name
, GLOBAL_HEADER_TEMPLATE
);
360 return xheader_format_name (NULL
, globexthdr_name
, true);
364 xheader_write (char type
, char *name
, struct xheader
*xhdr
)
371 header
= start_private_header (name
, size
);
372 header
->header
.typeflag
= type
;
374 simple_finish_header (header
);
382 header
= find_next_block ();
386 memcpy (header
->buffer
, p
, len
);
388 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
391 set_next_block_after (header
);
394 xheader_destroy (xhdr
);
398 xheader_write_global ()
401 struct keyword_list
*kp
;
403 if (!keyword_global_override_list
)
406 extended_header_init ();
407 for (kp
= keyword_global_override_list
; kp
; kp
= kp
->next
)
408 code_string (kp
->value
, kp
->pattern
, &extended_header
);
409 xheader_finish (&extended_header
);
410 xheader_write (XGLTYPE
, name
= xheader_ghdr_name (),
413 global_header_count
++;
417 /* General Interface */
422 void (*coder
) (struct tar_stat_info
const *, char const *,
423 struct xheader
*, void *data
);
424 void (*decoder
) (struct tar_stat_info
*, char const *);
428 /* This declaration must be extern, because ISO C99 section 6.9.2
429 prohibits a tentative definition that has both internal linkage and
430 incomplete type. If we made it static, we'd have to declare its
431 size which would be a maintenance pain; if we put its initializer
432 here, we'd need a boatload of forward declarations, which would be
433 even more of a pain. */
434 extern struct xhdr_tab
const xhdr_tab
[];
436 static struct xhdr_tab
const *
437 locate_handler (char const *keyword
)
439 struct xhdr_tab
const *p
;
441 for (p
= xhdr_tab
; p
->keyword
; p
++)
442 if (strcmp (p
->keyword
, keyword
) == 0)
448 xheader_protected_pattern_p (const char *pattern
)
450 struct xhdr_tab
const *p
;
452 for (p
= xhdr_tab
; p
->keyword
; p
++)
453 if (p
->protect
&& fnmatch (pattern
, p
->keyword
, 0) == 0)
459 xheader_protected_keyword_p (const char *keyword
)
461 struct xhdr_tab
const *p
;
463 for (p
= xhdr_tab
; p
->keyword
; p
++)
464 if (p
->protect
&& strcmp (p
->keyword
, keyword
) == 0)
469 /* Decodes a single extended header record. Advances P to the next
471 Returns true on success, false otherwise. */
473 decode_record (char **p
,
474 void (*handler
) (void *, char const *, char const *),
485 len
= strtoul (*p
, p
, 10);
489 _("Malformed extended header: missing whitespace after the length")));
494 for (;*p
< start
+ len
; ++*p
)
500 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
509 handler (data
, keyword
, *p
+ 1);
518 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
520 for (; kp
; kp
= kp
->next
)
522 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
524 t
->decoder (st
, kp
->value
);
529 decx (void *data
, char const *keyword
, char const *value
)
531 struct xhdr_tab
const *t
;
532 struct tar_stat_info
*st
= data
;
534 if (xheader_keyword_deleted_p (keyword
)
535 || xheader_keyword_override_p (keyword
))
538 t
= locate_handler (keyword
);
540 t
->decoder (st
, value
);
544 xheader_decode (struct tar_stat_info
*st
)
546 run_override_list (keyword_global_override_list
, st
);
547 run_override_list (global_header_override_list
, st
);
549 if (extended_header
.size
)
551 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
552 char *endp
= &extended_header
.buffer
[extended_header
.size
-1];
555 if (!decode_record (&p
, decx
, st
))
558 run_override_list (keyword_override_list
, st
);
562 decg (void *data
, char const *keyword
, char const *value
)
564 struct keyword_list
**kwl
= data
;
565 xheader_list_append (kwl
, keyword
, value
);
569 xheader_decode_global ()
571 if (extended_header
.size
)
573 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
574 char *endp
= &extended_header
.buffer
[extended_header
.size
-1];
576 xheader_list_destroy (&global_header_override_list
);
578 if (!decode_record (&p
, decg
, &global_header_override_list
))
584 extended_header_init ()
586 if (!extended_header
.stk
)
588 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
589 obstack_init (extended_header
.stk
);
594 xheader_store (char const *keyword
, struct tar_stat_info
const *st
, void *data
)
596 struct xhdr_tab
const *t
;
598 if (extended_header
.buffer
)
600 t
= locate_handler (keyword
);
603 if (xheader_keyword_deleted_p (keyword
)
604 || xheader_keyword_override_p (keyword
))
606 extended_header_init ();
607 t
->coder (st
, keyword
, &extended_header
, data
);
611 xheader_read (union block
*p
, size_t size
)
616 free (extended_header
.buffer
);
618 extended_header
.size
= size
;
619 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
620 extended_header
.buffer
= xmalloc (size
+ 1);
629 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
630 set_next_block_after (p
);
632 p
= find_next_block ();
641 format_uintmax (uintmax_t val
, char *buf
, size_t s
)
648 while ((val
/= 10) != 0);
652 char *p
= buf
+ s
- 1;
656 *p
-- = val
% 10 + '0';
658 while ((val
/= 10) != 0);
667 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
669 size_t len
= strlen (keyword
) + strlen (value
) + 3; /* ' ' + '=' + '\n' */
676 n
= format_uintmax (len
+ p
, NULL
, 0);
680 format_uintmax (len
+ n
, nbuf
, n
);
681 obstack_grow (xhdr
->stk
, nbuf
, n
);
682 obstack_1grow (xhdr
->stk
, ' ');
683 obstack_grow (xhdr
->stk
, keyword
, strlen (keyword
));
684 obstack_1grow (xhdr
->stk
, '=');
685 obstack_grow (xhdr
->stk
, value
, strlen (value
));
686 obstack_1grow (xhdr
->stk
, '\n');
690 xheader_finish (struct xheader
*xhdr
)
692 struct keyword_list
*kp
;
694 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
695 code_string (kp
->value
, kp
->pattern
, xhdr
);
697 obstack_1grow (xhdr
->stk
, 0);
698 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
699 xhdr
->size
= strlen (xhdr
->buffer
);
703 xheader_destroy (struct xheader
*xhdr
)
707 obstack_free (xhdr
->stk
, NULL
);
718 /* Implementations */
720 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
723 if (!utf8_convert (true, string
, &outstr
))
725 /* FIXME: report error */
726 outstr
= xstrdup (string
);
728 xheader_print (xhdr
, keyword
, outstr
);
733 decode_string (char **string
, char const *arg
)
740 if (!utf8_convert (false, arg
, string
))
742 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
743 assign_string (string
, arg
);
748 code_time (time_t t
, unsigned long nano
,
749 char const *keyword
, struct xheader
*xhdr
)
752 size_t s
= format_uintmax (t
, NULL
, 0);
753 if (s
+ 11 >= sizeof sbuf
)
755 format_uintmax (t
, sbuf
, s
);
757 s
+= format_uintmax (nano
, sbuf
+ s
, 9);
759 xheader_print (xhdr
, keyword
, sbuf
);
763 decode_time (char const *arg
, time_t *secs
, unsigned long *nsecs
)
767 if (xstrtoumax (arg
, &p
, 10, &u
, "") == LONGINT_OK
)
770 if (*p
== '.' && xstrtoumax (p
+1, NULL
, 10, &u
, "") == LONGINT_OK
)
776 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
779 size_t s
= format_uintmax (value
, NULL
, 0);
780 format_uintmax (value
, sbuf
, s
);
782 xheader_print (xhdr
, keyword
, sbuf
);
786 dummy_coder (struct tar_stat_info
const *st
, char const *keyword
,
787 struct xheader
*xhdr
, void *data
)
792 dummy_decoder (struct tar_stat_info
*st
, char const *arg
)
797 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
798 struct xheader
*xhdr
, void *data
)
800 code_time (st
->stat
.st_atime
, st
->atime_nsec
, keyword
, xhdr
);
804 atime_decoder (struct tar_stat_info
*st
, char const *arg
)
806 decode_time (arg
, &st
->stat
.st_atime
, &st
->atime_nsec
);
810 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
811 struct xheader
*xhdr
, void *data
)
813 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
817 gid_decoder (struct tar_stat_info
*st
, char const *arg
)
820 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
825 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
826 struct xheader
*xhdr
, void *data
)
828 code_string (st
->gname
, keyword
, xhdr
);
832 gname_decoder (struct tar_stat_info
*st
, char const *arg
)
834 decode_string (&st
->gname
, arg
);
838 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
839 struct xheader
*xhdr
, void *data
)
841 code_string (st
->link_name
, keyword
, xhdr
);
845 linkpath_decoder (struct tar_stat_info
*st
, char const *arg
)
847 decode_string (&st
->link_name
, arg
);
851 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
852 struct xheader
*xhdr
, void *data
)
854 code_time (st
->stat
.st_ctime
, st
->ctime_nsec
, keyword
, xhdr
);
858 ctime_decoder (struct tar_stat_info
*st
, char const *arg
)
860 decode_time (arg
, &st
->stat
.st_ctime
, &st
->ctime_nsec
);
864 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
865 struct xheader
*xhdr
, void *data
)
867 code_time (st
->stat
.st_mtime
, st
->mtime_nsec
, keyword
, xhdr
);
871 mtime_decoder (struct tar_stat_info
*st
, char const *arg
)
873 decode_time (arg
, &st
->stat
.st_mtime
, &st
->mtime_nsec
);
877 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
878 struct xheader
*xhdr
, void *data
)
880 code_string (st
->file_name
, keyword
, xhdr
);
884 path_decoder (struct tar_stat_info
*st
, char const *arg
)
886 decode_string (&st
->orig_file_name
, arg
);
887 decode_string (&st
->file_name
, arg
);
888 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
892 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
893 struct xheader
*xhdr
, void *data
)
895 code_num (st
->stat
.st_size
, keyword
, xhdr
);
899 size_decoder (struct tar_stat_info
*st
, char const *arg
)
902 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
903 st
->archive_file_size
= st
->stat
.st_size
= u
;
907 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
908 struct xheader
*xhdr
, void *data
)
910 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
914 uid_decoder (struct tar_stat_info
*st
, char const *arg
)
917 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
922 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
923 struct xheader
*xhdr
, void *data
)
925 code_string (st
->uname
, keyword
, xhdr
);
929 uname_decoder (struct tar_stat_info
*st
, char const *arg
)
931 decode_string (&st
->uname
, arg
);
935 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
936 struct xheader
*xhdr
, void *data
)
938 size_coder (st
, keyword
, xhdr
, data
);
942 sparse_size_decoder (struct tar_stat_info
*st
, char const *arg
)
945 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
946 st
->stat
.st_size
= u
;
950 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
951 struct xheader
*xhdr
, void *data
)
953 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
957 sparse_numblocks_decoder (struct tar_stat_info
*st
, char const *arg
)
960 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
962 st
->sparse_map_size
= u
;
963 st
->sparse_map
= calloc(st
->sparse_map_size
, sizeof(st
->sparse_map
[0]));
964 st
->sparse_map_avail
= 0;
969 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
970 struct xheader
*xhdr
, void *data
)
972 size_t i
= *(size_t*)data
;
973 code_num (st
->sparse_map
[i
].offset
, keyword
, xhdr
);
977 sparse_offset_decoder (struct tar_stat_info
*st
, char const *arg
)
980 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
981 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
985 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
986 struct xheader
*xhdr
, void *data
)
988 size_t i
= *(size_t*)data
;
989 code_num (st
->sparse_map
[i
].numbytes
, keyword
, xhdr
);
993 sparse_numbytes_decoder (struct tar_stat_info
*st
, char const *arg
)
996 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
998 if (st
->sparse_map_avail
== st
->sparse_map_size
)
1000 st
->sparse_map_size
*= 2;
1001 st
->sparse_map
= xrealloc (st
->sparse_map
,
1003 * sizeof st
->sparse_map
[0]);
1005 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
1009 struct xhdr_tab
const xhdr_tab
[] = {
1010 { "atime", atime_coder
, atime_decoder
},
1011 { "comment", dummy_coder
, dummy_decoder
},
1012 { "charset", dummy_coder
, dummy_decoder
},
1013 { "ctime", ctime_coder
, ctime_decoder
},
1014 { "gid", gid_coder
, gid_decoder
},
1015 { "gname", gname_coder
, gname_decoder
},
1016 { "linkpath", linkpath_coder
, linkpath_decoder
},
1017 { "mtime", mtime_coder
, mtime_decoder
},
1018 { "path", path_coder
, path_decoder
},
1019 { "size", size_coder
, size_decoder
},
1020 { "uid", uid_coder
, uid_decoder
},
1021 { "uname", uname_coder
, uname_decoder
},
1023 /* Sparse file handling */
1024 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
, true },
1025 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
1027 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
1029 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
1032 #if 0 /* GNU private keywords (not yet implemented) */
1034 /* The next directory entry actually contains the names of files
1035 that were in the directory at the time the dump was made.
1036 Supersedes GNUTYPE_DUMPDIR header type. */
1037 { "GNU.dump.name", dump_name_coder
, dump_name_decoder
},
1038 { "GNU.dump.status", dump_status_coder
, dump_status_decoder
},
1040 /* Keeps the tape/volume header. May be present only in the global headers.
1041 Equivalent to GNUTYPE_VOLHDR. */
1042 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
},
1044 /* These may be present in a first global header of the archive.
1045 They provide the same functionality as GNUTYPE_MULTIVOL header.
1046 The GNU.volume.size keeps the real_s_sizeleft value, which is
1047 otherwise kept in the size field of a multivolume header. The
1048 GNU.volume.offset keeps the offset of the start of this volume,
1049 otherwise kept in oldgnu_header.offset. */
1050 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
},
1051 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
},
1054 { NULL
, NULL
, NULL
}