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 /* Number of the global headers written so far. Not used yet */
35 static size_t global_header_count
;
42 struct keyword_list
*next
;
48 /* List of keyword patterns set by delete= option */
49 static struct keyword_list
*keyword_pattern_list
;
50 /* List of keyword/value pairs set by `keyword=value' option */
51 static struct keyword_list
*keyword_global_override_list
;
52 /* List of keyword/value pairs set by `keyword:=value' option */
53 static struct keyword_list
*keyword_override_list
;
54 /* Template for the name field of an 'x' type header */
55 static char *exthdr_name
;
56 /* Template for the name field of a 'g' type header */
57 static char *globexthdr_name
;
60 xheader_keyword_deleted_p (const char *kw
)
62 struct keyword_list
*kp
;
64 for (kp
= keyword_pattern_list
; kp
; kp
= kp
->next
)
65 if (fnmatch (kp
->pattern
, kw
, 0) == 0)
71 xheader_keyword_override_p (const char *keyword
)
73 struct keyword_list
*kp
;
75 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
76 if (strcmp (kp
->pattern
, keyword
) == 0)
82 xheader_list_append (struct keyword_list
**root
, char *kw
, char *value
)
84 struct keyword_list
*kp
= xmalloc (sizeof *kp
);
85 kp
->pattern
= xstrdup (kw
);
86 kp
->value
= value
? xstrdup (value
) : NULL
;
92 xheader_set_single_keyword (char *kw
)
94 USAGE_ERROR ((0, 0, "Keyword %s is unknown or not yet imlemented", kw
));
98 xheader_set_keyword_equal (char *kw
, char *eq
)
109 while (p
> kw
&& isspace (*p
))
114 for (p
= eq
+ 1; *p
&& isspace (*p
); p
++)
117 if (strcmp (kw
, "delete") == 0)
119 if (xheader_protected_pattern_p (p
))
120 USAGE_ERROR ((0, 0, "Pattern %s cannot be used", p
));
121 xheader_list_append (&keyword_pattern_list
, p
, NULL
);
123 else if (strcmp (kw
, "exthdr.name") == 0)
124 assign_string (&exthdr_name
, p
);
125 else if (strcmp (kw
, "globexthdr.name") == 0)
126 assign_string (&globexthdr_name
, p
);
129 if (xheader_protected_keyword_p (kw
))
130 USAGE_ERROR ((0, 0, "Keyword %s cannot be overridden", kw
));
132 xheader_list_append (&keyword_global_override_list
, kw
, p
);
134 xheader_list_append (&keyword_override_list
, kw
, p
);
139 xheader_set_option (char *string
)
142 for (token
= strtok (string
, ","); token
; token
= strtok (NULL
, ","))
144 char *p
= strchr (token
, '=');
146 xheader_set_single_keyword (token
);
148 xheader_set_keyword_equal (token
, p
);
153 string Includes: Replaced By:
154 %d The directory name of the file,
155 equivalent to the result of the
156 dirname utility on the translated
158 %f The filename of the file, equivalent
159 to the result of the basename
160 utility on the translated pathname.
161 %p The process ID of the pax process.
162 %% A '%' character. */
165 xheader_format_name (struct tar_stat_info
*st
, const char *fmt
, bool allow_n
)
168 size_t len
= strlen (fmt
);
171 char *dirname
= NULL
;
172 char *basename
= NULL
;
176 for (p
= exthdr_name
; *p
&& (p
= strchr (p
, '%')); )
185 dirname
= safer_name_suffix (dir_name (st
->orig_file_name
), false);
186 len
+= strlen (dirname
) - 1;
190 basename
= base_name (st
->orig_file_name
);
191 len
+= strlen (basename
) - 1;
195 snprintf (pidbuf
, sizeof pidbuf
, "%lu",
196 (unsigned long) getpid ());
197 len
+= strlen (pidbuf
) - 1;
203 snprintf (nbuf
, sizeof nbuf
, "%lu",
204 (unsigned long) global_header_count
+ 1);
205 len
+= strlen (nbuf
) - 1;
212 buf
= xmalloc (len
+ 1);
213 for (q
= buf
, p
= fmt
; *p
; )
225 q
= stpcpy (q
, dirname
);
230 q
= stpcpy (q
, basename
);
235 q
= stpcpy (q
, pidbuf
);
242 q
= stpcpy (q
, nbuf
);
245 /* else fall through */
257 /* Do not allow it to end in a slash */
258 while (q
> buf
&& ISSLASH (q
[-1]))
265 xheader_xhdr_name (struct tar_stat_info
*st
)
267 /* FIXME: POSIX requires the default name to be '%d/PaxHeaders.%p/%f' */
269 return xstrdup ("././@PaxHeader");
270 return xheader_format_name (st
, exthdr_name
, false);
273 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
276 xheader_ghdr_name (struct tar_stat_info
*st
)
278 if (!globexthdr_name
)
281 const char *tmp
= getenv ("TMPDIR");
284 len
= strlen (tmp
) + sizeof (GLOBAL_HEADER_TEMPLATE
); /* Includes nul */
285 globexthdr_name
= xmalloc (len
);
286 strcpy(globexthdr_name
, tmp
);
287 strcat(globexthdr_name
, GLOBAL_HEADER_TEMPLATE
);
290 return xheader_format_name (st
, globexthdr_name
, true);
294 /* General Interface */
296 /* Used by xheader_finish() */
297 static void code_string (char const *string
, char const *keyword
,
298 struct xheader
*xhdr
);
303 void (*coder
) (struct tar_stat_info
const *, char const *,
304 struct xheader
*, void *data
);
305 void (*decoder
) (struct tar_stat_info
*, char const *);
309 /* This declaration must be extern, because ISO C99 section 6.9.2
310 prohibits a tentative definition that has both internal linkage and
311 incomplete type. If we made it static, we'd have to declare its
312 size which would be a maintenance pain; if we put its initializer
313 here, we'd need a boatload of forward declarations, which would be
314 even more of a pain. */
315 extern struct xhdr_tab
const xhdr_tab
[];
317 static struct xhdr_tab
const *
318 locate_handler (char const *keyword
)
320 struct xhdr_tab
const *p
;
322 for (p
= xhdr_tab
; p
->keyword
; p
++)
323 if (strcmp (p
->keyword
, keyword
) == 0)
329 xheader_protected_pattern_p (const char *pattern
)
331 struct xhdr_tab
const *p
;
333 for (p
= xhdr_tab
; p
->keyword
; p
++)
334 if (p
->protect
&& fnmatch (pattern
, p
->keyword
, 0) == 0)
340 xheader_protected_keyword_p (const char *keyword
)
342 struct xhdr_tab
const *p
;
344 for (p
= xhdr_tab
; p
->keyword
; p
++)
345 if (p
->protect
&& strcmp (p
->keyword
, keyword
) == 0)
350 /* Decodes a single extended header record. Advances P to the next
352 Returns true on success, false otherwise. */
354 decode_record (char **p
, struct tar_stat_info
*st
)
360 struct xhdr_tab
const *t
;
365 len
= strtoul (*p
, p
, 10);
369 _("Malformed extended header: missing whitespace after the length")));
374 for (;*p
< start
+ len
; ++*p
)
380 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
387 if (xheader_keyword_deleted_p (keyword
)
388 || xheader_keyword_override_p (keyword
))
390 t
= locate_handler (keyword
);
400 t
->decoder (st
, value
);
409 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
411 for (; kp
; kp
= kp
->next
)
413 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
415 t
->decoder (st
, kp
->value
);
420 xheader_decode (struct tar_stat_info
*st
)
422 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
423 char *endp
= &extended_header
.buffer
[extended_header
.size
-1];
425 run_override_list (keyword_global_override_list
, st
);
428 if (!decode_record (&p
, st
))
431 run_override_list (keyword_override_list
, st
);
435 xheader_store (char const *keyword
, struct tar_stat_info
const *st
, void *data
)
437 struct xhdr_tab
const *t
;
440 if (extended_header
.buffer
)
442 t
= locate_handler (keyword
);
445 if (xheader_keyword_deleted_p (keyword
)
446 || xheader_keyword_override_p (keyword
))
448 if (!extended_header
.stk
)
450 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
451 obstack_init (extended_header
.stk
);
453 t
->coder (st
, keyword
, &extended_header
, data
);
457 xheader_read (union block
*p
, size_t size
)
462 free (extended_header
.buffer
);
464 extended_header
.size
= size
;
465 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
466 extended_header
.buffer
= xmalloc (size
+ 1);
475 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
476 set_next_block_after (p
);
478 p
= find_next_block ();
487 format_uintmax (uintmax_t val
, char *buf
, size_t s
)
494 while ((val
/= 10) != 0);
498 char *p
= buf
+ s
- 1;
502 *p
-- = val
% 10 + '0';
504 while ((val
/= 10) != 0);
513 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
515 size_t len
= strlen (keyword
) + strlen (value
) + 3; /* ' ' + '=' + '\n' */
522 n
= format_uintmax (len
+ p
, NULL
, 0);
526 format_uintmax (len
+ n
, nbuf
, n
);
527 obstack_grow (xhdr
->stk
, nbuf
, n
);
528 obstack_1grow (xhdr
->stk
, ' ');
529 obstack_grow (xhdr
->stk
, keyword
, strlen (keyword
));
530 obstack_1grow (xhdr
->stk
, '=');
531 obstack_grow (xhdr
->stk
, value
, strlen (value
));
532 obstack_1grow (xhdr
->stk
, '\n');
536 xheader_finish (struct xheader
*xhdr
)
538 struct keyword_list
*kp
;
540 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
541 code_string (kp
->value
, kp
->pattern
, xhdr
);
543 obstack_1grow (xhdr
->stk
, 0);
544 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
545 xhdr
->size
= strlen (xhdr
->buffer
);
549 xheader_destroy (struct xheader
*xhdr
)
553 obstack_free (xhdr
->stk
, NULL
);
564 /* Implementations */
566 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
568 xheader_print (xhdr
, keyword
, string
);
572 code_time (time_t t
, unsigned long nano
,
573 char const *keyword
, struct xheader
*xhdr
)
576 size_t s
= format_uintmax (t
, NULL
, 0);
577 if (s
+ 11 >= sizeof sbuf
)
579 format_uintmax (t
, sbuf
, s
);
581 s
+= format_uintmax (nano
, sbuf
+ s
, 9);
583 xheader_print (xhdr
, keyword
, sbuf
);
587 decode_time (char const *arg
, time_t *secs
, unsigned long *nsecs
)
591 if (xstrtoumax (arg
, &p
, 10, &u
, "") == LONGINT_OK
)
594 if (*p
== '.' && xstrtoumax (p
+1, NULL
, 10, &u
, "") == LONGINT_OK
)
600 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
603 size_t s
= format_uintmax (value
, NULL
, 0);
604 format_uintmax (value
, sbuf
, s
);
606 xheader_print (xhdr
, keyword
, sbuf
);
610 dummy_coder (struct tar_stat_info
const *st
, char const *keyword
,
611 struct xheader
*xhdr
, void *data
)
616 dummy_decoder (struct tar_stat_info
*st
, char const *arg
)
621 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
622 struct xheader
*xhdr
, void *data
)
624 code_time (st
->stat
.st_atime
, st
->atime_nsec
, keyword
, xhdr
);
628 atime_decoder (struct tar_stat_info
*st
, char const *arg
)
630 decode_time (arg
, &st
->stat
.st_atime
, &st
->atime_nsec
);
634 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
635 struct xheader
*xhdr
, void *data
)
637 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
641 gid_decoder (struct tar_stat_info
*st
, char const *arg
)
644 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
649 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
650 struct xheader
*xhdr
, void *data
)
652 code_string (st
->gname
, keyword
, xhdr
);
656 gname_decoder (struct tar_stat_info
*st
, char const *arg
)
658 assign_string (&st
->gname
, arg
);
662 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
663 struct xheader
*xhdr
, void *data
)
665 code_string (st
->link_name
, keyword
, xhdr
);
669 linkpath_decoder (struct tar_stat_info
*st
, char const *arg
)
671 assign_string (&st
->link_name
, arg
);
675 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
676 struct xheader
*xhdr
, void *data
)
678 code_time (st
->stat
.st_ctime
, st
->ctime_nsec
, keyword
, xhdr
);
682 ctime_decoder (struct tar_stat_info
*st
, char const *arg
)
684 decode_time (arg
, &st
->stat
.st_ctime
, &st
->ctime_nsec
);
688 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
689 struct xheader
*xhdr
, void *data
)
691 code_time (st
->stat
.st_mtime
, st
->mtime_nsec
, keyword
, xhdr
);
695 mtime_decoder (struct tar_stat_info
*st
, char const *arg
)
697 decode_time (arg
, &st
->stat
.st_mtime
, &st
->mtime_nsec
);
701 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
702 struct xheader
*xhdr
, void *data
)
704 code_string (st
->file_name
, keyword
, xhdr
);
708 path_decoder (struct tar_stat_info
*st
, char const *arg
)
710 assign_string (&st
->orig_file_name
, arg
);
711 assign_string (&st
->file_name
, arg
);
712 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
716 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
717 struct xheader
*xhdr
, void *data
)
719 code_num (st
->stat
.st_size
, keyword
, xhdr
);
723 size_decoder (struct tar_stat_info
*st
, char const *arg
)
726 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
727 st
->stat
.st_size
= u
;
731 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
732 struct xheader
*xhdr
, void *data
)
734 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
738 uid_decoder (struct tar_stat_info
*st
, char const *arg
)
741 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
746 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
747 struct xheader
*xhdr
, void *data
)
749 code_string (st
->uname
, keyword
, xhdr
);
753 uname_decoder (struct tar_stat_info
*st
, char const *arg
)
755 assign_string (&st
->uname
, arg
);
759 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
760 struct xheader
*xhdr
, void *data
)
762 size_coder (st
, keyword
, xhdr
, data
);
766 sparse_size_decoder (struct tar_stat_info
*st
, char const *arg
)
769 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
770 st
->archive_file_size
= u
;
774 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
775 struct xheader
*xhdr
, void *data
)
777 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
781 sparse_numblocks_decoder (struct tar_stat_info
*st
, char const *arg
)
784 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
786 st
->sparse_map_size
= u
;
787 st
->sparse_map
= calloc(st
->sparse_map_size
, sizeof(st
->sparse_map
[0]));
788 st
->sparse_map_avail
= 0;
793 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
794 struct xheader
*xhdr
, void *data
)
796 size_t i
= *(size_t*)data
;
797 code_num (st
->sparse_map
[i
].offset
, keyword
, xhdr
);
801 sparse_offset_decoder (struct tar_stat_info
*st
, char const *arg
)
804 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
805 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
809 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
810 struct xheader
*xhdr
, void *data
)
812 size_t i
= *(size_t*)data
;
813 code_num (st
->sparse_map
[i
].numbytes
, keyword
, xhdr
);
817 sparse_numbytes_decoder (struct tar_stat_info
*st
, char const *arg
)
820 if (xstrtoumax (arg
, NULL
, 10, &u
, "") == LONGINT_OK
)
822 if (st
->sparse_map_avail
== st
->sparse_map_size
)
824 size_t newsize
= st
->sparse_map_size
*= 2;
825 st
->sparse_map
= xrealloc (st
->sparse_map
,
827 * sizeof st
->sparse_map
[0]);
829 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
833 struct xhdr_tab
const xhdr_tab
[] = {
834 { "atime", atime_coder
, atime_decoder
},
835 { "comment", dummy_coder
, dummy_decoder
},
836 { "charset", dummy_coder
, dummy_decoder
},
837 { "ctime", ctime_coder
, ctime_decoder
},
838 { "gid", gid_coder
, gid_decoder
},
839 { "gname", gname_coder
, gname_decoder
},
840 { "linkpath", linkpath_coder
, linkpath_decoder
},
841 { "mtime", mtime_coder
, mtime_decoder
},
842 { "path", path_coder
, path_decoder
},
843 { "size", size_coder
, size_decoder
},
844 { "uid", uid_coder
, uid_decoder
},
845 { "uname", uname_coder
, uname_decoder
},
847 /* Sparse file handling */
848 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
, true },
849 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
851 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
853 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
856 #if 0 /* GNU private keywords (not yet implemented) */
858 /* The next directory entry actually contains the names of files
859 that were in the directory at the time the dump was made.
860 Supersedes GNUTYPE_DUMPDIR header type. */
861 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
},
863 /* Keeps the tape/volume header. May be present only in the global headers.
864 Equivalent to GNUTYPE_VOLHDR. */
865 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
},
867 /* These may be present in a first global header of the archive.
868 They provide the same functionality as GNUTYPE_MULTIVOL header.
869 The GNU.volume.size keeps the real_s_sizeleft value, which is
870 otherwise kept in the size field of a multivolume header. The
871 GNU.volume.offset keeps the offset of the start of this volume,
872 otherwise kept in oldgnu_header.offset. */
873 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
},
874 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
},