1 /* POSIX extended headers for tar.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010 Free Software
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 static bool xheader_protected_pattern_p (char const *pattern
);
30 static bool xheader_protected_keyword_p (char const *keyword
);
31 static void xheader_set_single_keyword (char *) __attribute__ ((noreturn
));
33 /* Used by xheader_finish() */
34 static void code_string (char const *string
, char const *keyword
,
35 struct xheader
*xhdr
);
37 /* Number of global headers written so far. */
38 static size_t global_header_count
;
39 /* FIXME: Possibly it should be reset after changing the volume.
40 POSIX %n specification says that it is expanded to the sequence
41 number of current global header in *the* archive. However, for
42 multi-volume archives this will yield duplicate header names
43 in different volumes, which I'd like to avoid. The best way
44 to solve this would be to use per-archive header count as required
45 by POSIX *and* set globexthdr.name to, say,
46 $TMPDIR/GlobalHead.%p.$NUMVOLUME.%n.
48 However it should wait until buffer.c is finally rewritten */
51 /* Interface functions to obstacks */
54 x_obstack_grow (struct xheader
*xhdr
, const char *ptr
, size_t length
)
56 obstack_grow (xhdr
->stk
, ptr
, length
);
61 x_obstack_1grow (struct xheader
*xhdr
, char c
)
63 obstack_1grow (xhdr
->stk
, c
);
68 x_obstack_blank (struct xheader
*xhdr
, size_t length
)
70 obstack_blank (xhdr
->stk
, length
);
79 struct keyword_list
*next
;
85 /* List of keyword patterns set by delete= option */
86 static struct keyword_list
*keyword_pattern_list
;
88 /* List of keyword/value pairs set by `keyword=value' option */
89 static struct keyword_list
*keyword_global_override_list
;
91 /* List of keyword/value pairs set by `keyword:=value' option */
92 static struct keyword_list
*keyword_override_list
;
94 /* List of keyword/value pairs decoded from the last 'g' type header */
95 static struct keyword_list
*global_header_override_list
;
97 /* Template for the name field of an 'x' type header */
98 static char *exthdr_name
;
100 static char *exthdr_mtime_option
;
101 static time_t exthdr_mtime
;
103 /* Template for the name field of a 'g' type header */
104 static char *globexthdr_name
;
106 static char *globexthdr_mtime_option
;
107 static time_t globexthdr_mtime
;
110 xheader_keyword_deleted_p (const char *kw
)
112 struct keyword_list
*kp
;
114 for (kp
= keyword_pattern_list
; kp
; kp
= kp
->next
)
115 if (fnmatch (kp
->pattern
, kw
, 0) == 0)
121 xheader_keyword_override_p (const char *keyword
)
123 struct keyword_list
*kp
;
125 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
126 if (strcmp (kp
->pattern
, keyword
) == 0)
132 xheader_list_append (struct keyword_list
**root
, char const *kw
,
135 struct keyword_list
*kp
= xmalloc (sizeof *kp
);
136 kp
->pattern
= xstrdup (kw
);
137 kp
->value
= value
? xstrdup (value
) : NULL
;
143 xheader_list_destroy (struct keyword_list
**root
)
147 struct keyword_list
*kw
= *root
;
150 struct keyword_list
*next
= kw
->next
;
161 xheader_set_single_keyword (char *kw
)
163 USAGE_ERROR ((0, 0, _("Keyword %s is unknown or not yet implemented"), kw
));
167 assign_time_option (char **sval
, time_t *tval
, const char *input
)
171 time_t t
= u
= strtoumax (input
, &p
, 10);
172 if (t
!= u
|| *p
|| errno
== ERANGE
)
173 ERROR ((0, 0, _("Time stamp is out of allowed range")));
177 assign_string (sval
, input
);
182 xheader_set_keyword_equal (char *kw
, char *eq
)
193 while (p
> kw
&& isspace ((unsigned char) *p
))
198 for (p
= eq
+ 1; *p
&& isspace ((unsigned char) *p
); p
++)
201 if (strcmp (kw
, "delete") == 0)
203 if (xheader_protected_pattern_p (p
))
204 USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), quote (p
)));
205 xheader_list_append (&keyword_pattern_list
, p
, NULL
);
207 else if (strcmp (kw
, "exthdr.name") == 0)
208 assign_string (&exthdr_name
, p
);
209 else if (strcmp (kw
, "globexthdr.name") == 0)
210 assign_string (&globexthdr_name
, p
);
211 else if (strcmp (kw
, "exthdr.mtime") == 0)
212 assign_time_option (&exthdr_mtime_option
, &exthdr_mtime
, p
);
213 else if (strcmp (kw
, "globexthdr.mtime") == 0)
214 assign_time_option (&globexthdr_mtime_option
, &globexthdr_mtime
, p
);
217 if (xheader_protected_keyword_p (kw
))
218 USAGE_ERROR ((0, 0, _("Keyword %s cannot be overridden"), kw
));
220 xheader_list_append (&keyword_global_override_list
, kw
, p
);
222 xheader_list_append (&keyword_override_list
, kw
, p
);
227 xheader_set_option (char *string
)
230 for (token
= strtok (string
, ","); token
; token
= strtok (NULL
, ","))
232 char *p
= strchr (token
, '=');
234 xheader_set_single_keyword (token
);
236 xheader_set_keyword_equal (token
, p
);
241 string Includes: Replaced By:
242 %d The directory name of the file,
243 equivalent to the result of the
244 dirname utility on the translated
246 %f The filename of the file, equivalent
247 to the result of the basename
248 utility on the translated file name.
249 %p The process ID of the pax process.
250 %n The value of the 3rd argument.
251 %% A '%' character. */
254 xheader_format_name (struct tar_stat_info
*st
, const char *fmt
, size_t n
)
257 size_t len
= strlen (fmt
);
263 char pidbuf
[UINTMAX_STRSIZE_BOUND
];
265 char nbuf
[UINTMAX_STRSIZE_BOUND
];
266 char const *nptr
= NULL
;
268 for (p
= fmt
; *p
&& (p
= strchr (p
, '%')); )
280 dirp
= dir_name (st
->orig_file_name
);
281 dir
= safer_name_suffix (dirp
, false, absolute_names_option
);
282 len
+= strlen (dir
) - 2;
289 base
= last_component (st
->orig_file_name
);
290 len
+= strlen (base
) - 2;
295 pptr
= umaxtostr (getpid (), pidbuf
);
296 len
+= pidbuf
+ sizeof pidbuf
- 1 - pptr
- 2;
300 nptr
= umaxtostr (n
, nbuf
);
301 len
+= nbuf
+ sizeof nbuf
- 1 - nptr
- 2;
307 buf
= xmalloc (len
+ 1);
308 for (q
= buf
, p
= fmt
; *p
; )
327 q
= stpcpy (q
, base
);
332 q
= stpcpy (q
, pptr
);
339 q
= stpcpy (q
, nptr
);
343 /* else fall through */
357 /* Do not allow it to end in a slash */
358 while (q
> buf
&& ISSLASH (q
[-1]))
365 xheader_xhdr_name (struct tar_stat_info
*st
)
368 assign_string (&exthdr_name
, "%d/PaxHeaders.%p/%f");
369 return xheader_format_name (st
, exthdr_name
, 0);
372 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
375 xheader_ghdr_name (void)
377 if (!globexthdr_name
)
380 const char *tmp
= getenv ("TMPDIR");
383 len
= strlen (tmp
) + sizeof (GLOBAL_HEADER_TEMPLATE
); /* Includes nul */
384 globexthdr_name
= xmalloc (len
);
385 strcpy(globexthdr_name
, tmp
);
386 strcat(globexthdr_name
, GLOBAL_HEADER_TEMPLATE
);
389 return xheader_format_name (NULL
, globexthdr_name
, global_header_count
+ 1);
393 xheader_write (char type
, char *name
, time_t t
, struct xheader
*xhdr
)
403 if (globexthdr_mtime_option
)
404 t
= globexthdr_mtime
;
408 if (exthdr_mtime_option
)
412 header
= start_private_header (name
, size
, t
);
413 header
->header
.typeflag
= type
;
415 simple_finish_header (header
);
423 header
= find_next_block ();
427 memcpy (header
->buffer
, p
, len
);
429 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
432 set_next_block_after (header
);
435 xheader_destroy (xhdr
);
438 global_header_count
++;
442 xheader_write_global (struct xheader
*xhdr
)
444 if (keyword_global_override_list
)
446 struct keyword_list
*kp
;
449 for (kp
= keyword_global_override_list
; kp
; kp
= kp
->next
)
450 code_string (kp
->value
, kp
->pattern
, xhdr
);
456 xheader_finish (xhdr
);
457 xheader_write (XGLTYPE
, name
= xheader_ghdr_name (), time (NULL
), xhdr
);
463 /* General Interface */
465 #define XHDR_PROTECTED 0x01
466 #define XHDR_GLOBAL 0x02
471 void (*coder
) (struct tar_stat_info
const *, char const *,
472 struct xheader
*, void const *data
);
473 void (*decoder
) (struct tar_stat_info
*, char const *, char const *, size_t);
477 /* This declaration must be extern, because ISO C99 section 6.9.2
478 prohibits a tentative definition that has both internal linkage and
479 incomplete type. If we made it static, we'd have to declare its
480 size which would be a maintenance pain; if we put its initializer
481 here, we'd need a boatload of forward declarations, which would be
482 even more of a pain. */
483 extern struct xhdr_tab
const xhdr_tab
[];
485 static struct xhdr_tab
const *
486 locate_handler (char const *keyword
)
488 struct xhdr_tab
const *p
;
490 for (p
= xhdr_tab
; p
->keyword
; p
++)
491 if (strcmp (p
->keyword
, keyword
) == 0)
497 xheader_protected_pattern_p (const char *pattern
)
499 struct xhdr_tab
const *p
;
501 for (p
= xhdr_tab
; p
->keyword
; p
++)
502 if ((p
->flags
& XHDR_PROTECTED
) && fnmatch (pattern
, p
->keyword
, 0) == 0)
508 xheader_protected_keyword_p (const char *keyword
)
510 struct xhdr_tab
const *p
;
512 for (p
= xhdr_tab
; p
->keyword
; p
++)
513 if ((p
->flags
& XHDR_PROTECTED
) && strcmp (p
->keyword
, keyword
) == 0)
518 /* Decode a single extended header record, advancing *PTR to the next record.
519 Return true on success, false otherwise. */
521 decode_record (struct xheader
*xhdr
,
523 void (*handler
) (void *, char const *, char const *, size_t),
533 size_t len_max
= xhdr
->buffer
+ xhdr
->size
- start
;
535 while (*p
== ' ' || *p
== '\t')
541 ERROR ((0, 0, _("Malformed extended header: missing length")));
546 len
= u
= strtoumax (p
, &len_lim
, 10);
547 if (len
!= u
|| errno
== ERANGE
)
549 ERROR ((0, 0, _("Extended header length is out of allowed range")));
555 int len_len
= len_lim
- p
;
556 ERROR ((0, 0, _("Extended header length %*s is out of range"),
563 for (p
= len_lim
; *p
== ' ' || *p
== '\t'; p
++)
568 _("Malformed extended header: missing blank after length")));
574 if (! (p
&& p
< nextp
))
576 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
580 if (nextp
[-1] != '\n')
582 ERROR ((0, 0, _("Malformed extended header: missing newline")));
586 *p
= nextp
[-1] = '\0';
587 handler (data
, keyword
, p
+ 1, nextp
- p
- 2); /* '=' + trailing '\n' */
595 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
597 for (; kp
; kp
= kp
->next
)
599 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
601 t
->decoder (st
, t
->keyword
, kp
->value
, strlen (kp
->value
));
606 decx (void *data
, char const *keyword
, char const *value
, size_t size
)
608 struct xhdr_tab
const *t
;
609 struct tar_stat_info
*st
= data
;
611 if (xheader_keyword_deleted_p (keyword
)
612 || xheader_keyword_override_p (keyword
))
615 t
= locate_handler (keyword
);
617 t
->decoder (st
, keyword
, value
, size
);
619 WARNOPT (WARN_UNKNOWN_KEYWORD
,
620 (0, 0, _("Ignoring unknown extended header keyword `%s'"),
625 xheader_decode (struct tar_stat_info
*st
)
627 run_override_list (keyword_global_override_list
, st
);
628 run_override_list (global_header_override_list
, st
);
632 char *p
= st
->xhdr
.buffer
+ BLOCKSIZE
;
633 while (decode_record (&st
->xhdr
, &p
, decx
, st
))
636 run_override_list (keyword_override_list
, st
);
640 decg (void *data
, char const *keyword
, char const *value
,
641 size_t size
__attribute__((unused
)))
643 struct keyword_list
**kwl
= data
;
644 struct xhdr_tab
const *tab
= locate_handler (keyword
);
645 if (tab
&& (tab
->flags
& XHDR_GLOBAL
))
646 tab
->decoder (data
, keyword
, value
, size
);
648 xheader_list_append (kwl
, keyword
, value
);
652 xheader_decode_global (struct xheader
*xhdr
)
656 char *p
= xhdr
->buffer
+ BLOCKSIZE
;
658 xheader_list_destroy (&global_header_override_list
);
659 while (decode_record (xhdr
, &p
, decg
, &global_header_override_list
))
665 xheader_init (struct xheader
*xhdr
)
669 xhdr
->stk
= xmalloc (sizeof *xhdr
->stk
);
670 obstack_init (xhdr
->stk
);
675 xheader_store (char const *keyword
, struct tar_stat_info
*st
,
678 struct xhdr_tab
const *t
;
682 t
= locate_handler (keyword
);
685 if (xheader_keyword_deleted_p (keyword
)
686 || xheader_keyword_override_p (keyword
))
688 xheader_init (&st
->xhdr
);
689 t
->coder (st
, keyword
, &st
->xhdr
, data
);
693 xheader_read (struct xheader
*xhdr
, union block
*p
, size_t size
)
699 xhdr
->buffer
= xmalloc (size
+ 1);
700 xhdr
->buffer
[size
] = '\0';
710 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
712 memcpy (&xhdr
->buffer
[j
], p
->buffer
, len
);
713 set_next_block_after (p
);
715 p
= find_next_block ();
724 xheader_print_n (struct xheader
*xhdr
, char const *keyword
,
725 char const *value
, size_t vsize
)
727 size_t len
= strlen (keyword
) + vsize
+ 3; /* ' ' + '=' + '\n' */
730 char nbuf
[UINTMAX_STRSIZE_BOUND
];
736 np
= umaxtostr (len
+ p
, nbuf
);
737 n
= nbuf
+ sizeof nbuf
- 1 - np
;
741 x_obstack_grow (xhdr
, np
, n
);
742 x_obstack_1grow (xhdr
, ' ');
743 x_obstack_grow (xhdr
, keyword
, strlen (keyword
));
744 x_obstack_1grow (xhdr
, '=');
745 x_obstack_grow (xhdr
, value
, vsize
);
746 x_obstack_1grow (xhdr
, '\n');
750 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
752 xheader_print_n (xhdr
, keyword
, value
, strlen (value
));
756 xheader_finish (struct xheader
*xhdr
)
758 struct keyword_list
*kp
;
760 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
761 code_string (kp
->value
, kp
->pattern
, xhdr
);
763 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
767 xheader_destroy (struct xheader
*xhdr
)
771 obstack_free (xhdr
->stk
, NULL
);
782 /* Buildable strings */
785 xheader_string_begin (struct xheader
*xhdr
)
787 xhdr
->string_length
= 0;
791 xheader_string_add (struct xheader
*xhdr
, char const *s
)
796 xhdr
->string_length
+= strlen (s
);
797 x_obstack_grow (xhdr
, s
, strlen (s
));
801 xheader_string_end (struct xheader
*xhdr
, char const *keyword
)
807 char nbuf
[UINTMAX_STRSIZE_BOUND
];
815 len
= strlen (keyword
) + xhdr
->string_length
+ 3; /* ' ' + '=' + '\n' */
820 np
= umaxtostr (len
+ p
, nbuf
);
821 n
= nbuf
+ sizeof nbuf
- 1 - np
;
825 p
= strlen (keyword
) + n
+ 2;
830 _("Generated keyword/value pair is too long (keyword=%s, length=%s)"),
832 obstack_free (xhdr
->stk
, obstack_finish (xhdr
->stk
));
835 x_obstack_blank (xhdr
, p
);
836 x_obstack_1grow (xhdr
, '\n');
837 cp
= obstack_next_free (xhdr
->stk
) - xhdr
->string_length
- p
- 1;
838 memmove (cp
+ p
, cp
, xhdr
->string_length
);
839 cp
= stpcpy (cp
, np
);
841 cp
= stpcpy (cp
, keyword
);
847 /* Implementations */
850 out_of_range_header (char const *keyword
, char const *value
,
851 uintmax_t minus_minval
, uintmax_t maxval
)
853 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
854 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
855 char *minval_string
= umaxtostr (minus_minval
, minval_buf
+ 1);
856 char *maxval_string
= umaxtostr (maxval
, maxval_buf
);
858 *--minval_string
= '-';
860 /* TRANSLATORS: The first %s is the pax extended header keyword
861 (atime, gid, etc.). */
862 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
863 keyword
, value
, minval_string
, maxval_string
));
867 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
870 if (!utf8_convert (true, string
, &outstr
))
872 /* FIXME: report error */
873 outstr
= xstrdup (string
);
875 xheader_print (xhdr
, keyword
, outstr
);
880 decode_string (char **string
, char const *arg
)
887 if (!utf8_convert (false, arg
, string
))
889 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
890 assign_string (string
, arg
);
895 code_time (struct timespec t
, char const *keyword
, struct xheader
*xhdr
)
897 char buf
[TIMESPEC_STRSIZE_BOUND
];
898 xheader_print (xhdr
, keyword
, code_timespec (t
, buf
));
901 enum decode_time_status
905 decode_time_bad_header
908 static enum decode_time_status
909 _decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
912 unsigned long int ns
= 0;
915 bool negative
= *arg
== '-';
919 if (ISDIGIT (arg
[negative
]))
923 intmax_t i
= strtoimax (arg
, &arg_lim
, 10);
924 if (TYPE_SIGNED (time_t) ? i
< TYPE_MINIMUM (time_t) : i
< 0)
925 return decode_time_range
;
930 uintmax_t i
= strtoumax (arg
, &arg_lim
, 10);
931 if (TYPE_MAXIMUM (time_t) < i
)
932 return decode_time_range
;
939 return decode_time_range
;
944 bool trailing_nonzero
= false;
946 while (ISDIGIT (*++p
))
947 if (digits
< LOG10_BILLION
)
949 ns
= 10 * ns
+ (*p
- '0');
953 trailing_nonzero
|= *p
!= '0';
955 while (digits
++ < LOG10_BILLION
)
960 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
961 I.e., truncate time stamps towards minus infinity while
962 converting them to internal form. */
963 ns
+= trailing_nonzero
;
966 if (s
== TYPE_MINIMUM (time_t))
967 return decode_time_range
;
978 return decode_time_success
;
982 return decode_time_bad_header
;
986 decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
988 switch (_decode_time (ts
, arg
, keyword
))
990 case decode_time_success
:
992 case decode_time_bad_header
:
993 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
996 case decode_time_range
:
997 out_of_range_header (keyword
, arg
, - (uintmax_t) TYPE_MINIMUM (time_t),
998 TYPE_MAXIMUM (time_t));
1007 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
1009 char sbuf
[UINTMAX_STRSIZE_BOUND
];
1010 xheader_print (xhdr
, keyword
, umaxtostr (value
, sbuf
));
1014 decode_num (uintmax_t *num
, char const *arg
, uintmax_t maxval
,
1015 char const *keyword
)
1020 if (! (ISDIGIT (*arg
)
1021 && (errno
= 0, u
= strtoumax (arg
, &arg_lim
, 10), !*arg_lim
)))
1023 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1028 if (! (u
<= maxval
&& errno
!= ERANGE
))
1030 out_of_range_header (keyword
, arg
, 0, maxval
);
1039 dummy_coder (struct tar_stat_info
const *st
__attribute__ ((unused
)),
1040 char const *keyword
__attribute__ ((unused
)),
1041 struct xheader
*xhdr
__attribute__ ((unused
)),
1042 void const *data
__attribute__ ((unused
)))
1047 dummy_decoder (struct tar_stat_info
*st
__attribute__ ((unused
)),
1048 char const *keyword
__attribute__ ((unused
)),
1049 char const *arg
__attribute__ ((unused
)),
1050 size_t size
__attribute__((unused
)))
1055 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1056 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1058 code_time (st
->atime
, keyword
, xhdr
);
1062 atime_decoder (struct tar_stat_info
*st
,
1063 char const *keyword
,
1065 size_t size
__attribute__((unused
)))
1068 if (decode_time (&ts
, arg
, keyword
))
1073 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1074 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1076 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
1080 gid_decoder (struct tar_stat_info
*st
,
1081 char const *keyword
,
1083 size_t size
__attribute__((unused
)))
1086 if (decode_num (&u
, arg
, TYPE_MAXIMUM (gid_t
), keyword
))
1087 st
->stat
.st_gid
= u
;
1091 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1092 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1094 code_string (st
->gname
, keyword
, xhdr
);
1098 gname_decoder (struct tar_stat_info
*st
,
1099 char const *keyword
__attribute__((unused
)),
1101 size_t size
__attribute__((unused
)))
1103 decode_string (&st
->gname
, arg
);
1107 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
1108 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1110 code_string (st
->link_name
, keyword
, xhdr
);
1114 linkpath_decoder (struct tar_stat_info
*st
,
1115 char const *keyword
__attribute__((unused
)),
1117 size_t size
__attribute__((unused
)))
1119 decode_string (&st
->link_name
, arg
);
1123 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1124 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1126 code_time (st
->ctime
, keyword
, xhdr
);
1130 ctime_decoder (struct tar_stat_info
*st
,
1131 char const *keyword
,
1133 size_t size
__attribute__((unused
)))
1136 if (decode_time (&ts
, arg
, keyword
))
1141 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1142 struct xheader
*xhdr
, void const *data
)
1144 struct timespec
const *mtime
= data
;
1145 code_time (mtime
? *mtime
: st
->mtime
, keyword
, xhdr
);
1149 mtime_decoder (struct tar_stat_info
*st
,
1150 char const *keyword
,
1152 size_t size
__attribute__((unused
)))
1155 if (decode_time (&ts
, arg
, keyword
))
1160 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
1161 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1163 code_string (st
->file_name
, keyword
, xhdr
);
1167 path_decoder (struct tar_stat_info
*st
,
1168 char const *keyword
__attribute__((unused
)),
1170 size_t size
__attribute__((unused
)))
1172 decode_string (&st
->orig_file_name
, arg
);
1173 decode_string (&st
->file_name
, arg
);
1174 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
1178 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1179 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1181 code_num (st
->stat
.st_size
, keyword
, xhdr
);
1185 size_decoder (struct tar_stat_info
*st
,
1186 char const *keyword
,
1188 size_t size
__attribute__((unused
)))
1191 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1192 st
->stat
.st_size
= u
;
1196 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1197 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1199 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
1203 uid_decoder (struct tar_stat_info
*st
,
1204 char const *keyword
,
1206 size_t size
__attribute__((unused
)))
1209 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uid_t
), keyword
))
1210 st
->stat
.st_uid
= u
;
1214 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1215 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1217 code_string (st
->uname
, keyword
, xhdr
);
1221 uname_decoder (struct tar_stat_info
*st
,
1222 char const *keyword
__attribute__((unused
)),
1224 size_t size
__attribute__((unused
)))
1226 decode_string (&st
->uname
, arg
);
1230 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1231 struct xheader
*xhdr
, void const *data
)
1233 size_coder (st
, keyword
, xhdr
, data
);
1237 sparse_size_decoder (struct tar_stat_info
*st
,
1238 char const *keyword
,
1240 size_t size
__attribute__((unused
)))
1243 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1244 st
->stat
.st_size
= u
;
1248 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
1249 struct xheader
*xhdr
,
1250 void const *data
__attribute__ ((unused
)))
1252 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
1256 sparse_numblocks_decoder (struct tar_stat_info
*st
,
1257 char const *keyword
,
1259 size_t size
__attribute__((unused
)))
1262 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1264 st
->sparse_map_size
= u
;
1265 st
->sparse_map
= xcalloc (u
, sizeof st
->sparse_map
[0]);
1266 st
->sparse_map_avail
= 0;
1271 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1272 struct xheader
*xhdr
, void const *data
)
1274 size_t const *pi
= data
;
1275 code_num (st
->sparse_map
[*pi
].offset
, keyword
, xhdr
);
1279 sparse_offset_decoder (struct tar_stat_info
*st
,
1280 char const *keyword
,
1282 size_t size
__attribute__((unused
)))
1285 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1287 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1288 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
1290 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1291 "GNU.sparse.offset", arg
));
1296 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
1297 struct xheader
*xhdr
, void const *data
)
1299 size_t const *pi
= data
;
1300 code_num (st
->sparse_map
[*pi
].numbytes
, keyword
, xhdr
);
1304 sparse_numbytes_decoder (struct tar_stat_info
*st
,
1305 char const *keyword
,
1307 size_t size
__attribute__((unused
)))
1310 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1312 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1313 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
1315 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1321 sparse_map_decoder (struct tar_stat_info
*st
,
1322 char const *keyword
,
1324 size_t size
__attribute__((unused
)))
1328 st
->sparse_map_avail
= 0;
1335 if (!ISDIGIT (*arg
))
1337 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1343 u
= strtoumax (arg
, &delim
, 10);
1347 if (!(u
== e
.offset
&& errno
!= ERANGE
))
1349 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1356 if (!(u
== e
.numbytes
&& errno
!= ERANGE
))
1358 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (size_t));
1361 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1362 st
->sparse_map
[st
->sparse_map_avail
++] = e
;
1365 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1375 else if (*delim
!= ',')
1378 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1388 _("Malformed extended header: invalid %s: odd number of values"),
1393 dumpdir_coder (struct tar_stat_info
const *st
, char const *keyword
,
1394 struct xheader
*xhdr
, void const *data
)
1396 xheader_print_n (xhdr
, keyword
, data
, dumpdir_size (data
));
1400 dumpdir_decoder (struct tar_stat_info
*st
,
1401 char const *keyword
__attribute__((unused
)),
1405 st
->dumpdir
= xmalloc (size
);
1406 memcpy (st
->dumpdir
, arg
, size
);
1410 volume_label_coder (struct tar_stat_info
const *st
, char const *keyword
,
1411 struct xheader
*xhdr
, void const *data
)
1413 code_string (data
, keyword
, xhdr
);
1417 volume_label_decoder (struct tar_stat_info
*st
,
1418 char const *keyword
__attribute__((unused
)),
1420 size_t size
__attribute__((unused
)))
1422 decode_string (&volume_label
, arg
);
1426 volume_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1427 struct xheader
*xhdr
, void const *data
)
1429 off_t
const *v
= data
;
1430 code_num (*v
, keyword
, xhdr
);
1434 volume_size_decoder (struct tar_stat_info
*st
,
1435 char const *keyword
,
1436 char const *arg
, size_t size
)
1439 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1440 continued_file_size
= u
;
1443 /* FIXME: Merge with volume_size_coder */
1445 volume_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1446 struct xheader
*xhdr
, void const *data
)
1448 off_t
const *v
= data
;
1449 code_num (*v
, keyword
, xhdr
);
1453 volume_offset_decoder (struct tar_stat_info
*st
,
1454 char const *keyword
,
1455 char const *arg
, size_t size
)
1458 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1459 continued_file_offset
= u
;
1463 volume_filename_decoder (struct tar_stat_info
*st
,
1464 char const *keyword
__attribute__((unused
)),
1466 size_t size
__attribute__((unused
)))
1468 decode_string (&continued_file_name
, arg
);
1472 sparse_major_coder (struct tar_stat_info
const *st
, char const *keyword
,
1473 struct xheader
*xhdr
, void const *data
)
1475 code_num (st
->sparse_major
, keyword
, xhdr
);
1479 sparse_major_decoder (struct tar_stat_info
*st
,
1480 char const *keyword
,
1485 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1486 st
->sparse_major
= u
;
1490 sparse_minor_coder (struct tar_stat_info
const *st
, char const *keyword
,
1491 struct xheader
*xhdr
, void const *data
)
1493 code_num (st
->sparse_minor
, keyword
, xhdr
);
1497 sparse_minor_decoder (struct tar_stat_info
*st
,
1498 char const *keyword
,
1503 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1504 st
->sparse_minor
= u
;
1507 struct xhdr_tab
const xhdr_tab
[] = {
1508 { "atime", atime_coder
, atime_decoder
, 0 },
1509 { "comment", dummy_coder
, dummy_decoder
, 0 },
1510 { "charset", dummy_coder
, dummy_decoder
, 0 },
1511 { "ctime", ctime_coder
, ctime_decoder
, 0 },
1512 { "gid", gid_coder
, gid_decoder
, 0 },
1513 { "gname", gname_coder
, gname_decoder
, 0 },
1514 { "linkpath", linkpath_coder
, linkpath_decoder
, 0 },
1515 { "mtime", mtime_coder
, mtime_decoder
, 0 },
1516 { "path", path_coder
, path_decoder
, 0 },
1517 { "size", size_coder
, size_decoder
, 0 },
1518 { "uid", uid_coder
, uid_decoder
, 0 },
1519 { "uname", uname_coder
, uname_decoder
, 0 },
1521 /* Sparse file handling */
1522 { "GNU.sparse.name", path_coder
, path_decoder
,
1524 { "GNU.sparse.major", sparse_major_coder
, sparse_major_decoder
,
1526 { "GNU.sparse.minor", sparse_minor_coder
, sparse_minor_decoder
,
1528 { "GNU.sparse.realsize", sparse_size_coder
, sparse_size_decoder
,
1530 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
1533 /* tar 1.14 - 1.15.90 keywords. */
1534 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
,
1536 /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
1537 headers, and each of them was meaningful. It confilcted with POSIX specs,
1538 which requires that "when extended header records conflict, the last one
1539 given in the header shall take precedence." */
1540 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
1542 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
1544 /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
1545 { "GNU.sparse.map", NULL
/* Unused, see pax_dump_header() */,
1546 sparse_map_decoder
, 0 },
1548 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
,
1551 /* Keeps the tape/volume label. May be present only in the global headers.
1552 Equivalent to GNUTYPE_VOLHDR. */
1553 { "GNU.volume.label", volume_label_coder
, volume_label_decoder
,
1554 XHDR_PROTECTED
| XHDR_GLOBAL
},
1556 /* These may be present in a first global header of the archive.
1557 They provide the same functionality as GNUTYPE_MULTIVOL header.
1558 The GNU.volume.size keeps the real_s_sizeleft value, which is
1559 otherwise kept in the size field of a multivolume header. The
1560 GNU.volume.offset keeps the offset of the start of this volume,
1561 otherwise kept in oldgnu_header.offset. */
1562 { "GNU.volume.filename", volume_label_coder
, volume_filename_decoder
,
1563 XHDR_PROTECTED
| XHDR_GLOBAL
},
1564 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
,
1565 XHDR_PROTECTED
| XHDR_GLOBAL
},
1566 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
,
1567 XHDR_PROTECTED
| XHDR_GLOBAL
},
1569 { NULL
, NULL
, NULL
, 0 }