1 /* POSIX extended headers for tar.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009 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 3, 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 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
28 static bool xheader_protected_pattern_p (char const *pattern
);
29 static bool xheader_protected_keyword_p (char const *keyword
);
30 static void xheader_set_single_keyword (char *) __attribute__ ((noreturn
));
32 /* Used by xheader_finish() */
33 static void code_string (char const *string
, char const *keyword
,
34 struct xheader
*xhdr
);
36 /* Number of global headers written so far. */
37 static size_t global_header_count
;
38 /* FIXME: Possibly it should be reset after changing the volume.
39 POSIX %n specification says that it is expanded to the sequence
40 number of current global header in *the* archive. However, for
41 multi-volume archives this will yield duplicate header names
42 in different volumes, which I'd like to avoid. The best way
43 to solve this would be to use per-archive header count as required
44 by POSIX *and* set globexthdr.name to, say,
45 $TMPDIR/GlobalHead.%p.$NUMVOLUME.%n.
47 However it should wait until buffer.c is finally rewritten */
50 /* Interface functions to obstacks */
53 x_obstack_grow (struct xheader
*xhdr
, const char *ptr
, size_t length
)
55 obstack_grow (xhdr
->stk
, ptr
, length
);
60 x_obstack_1grow (struct xheader
*xhdr
, char c
)
62 obstack_1grow (xhdr
->stk
, c
);
67 x_obstack_blank (struct xheader
*xhdr
, size_t length
)
69 obstack_blank (xhdr
->stk
, length
);
78 struct keyword_list
*next
;
84 /* List of keyword patterns set by delete= option */
85 static struct keyword_list
*keyword_pattern_list
;
87 /* List of keyword/value pairs set by `keyword=value' option */
88 static struct keyword_list
*keyword_global_override_list
;
90 /* List of keyword/value pairs set by `keyword:=value' option */
91 static struct keyword_list
*keyword_override_list
;
93 /* List of keyword/value pairs decoded from the last 'g' type header */
94 static struct keyword_list
*global_header_override_list
;
96 /* Template for the name field of an 'x' type header */
97 static char *exthdr_name
;
99 /* Template for the name field of a 'g' type header */
100 static char *globexthdr_name
;
103 xheader_keyword_deleted_p (const char *kw
)
105 struct keyword_list
*kp
;
107 for (kp
= keyword_pattern_list
; kp
; kp
= kp
->next
)
108 if (fnmatch (kp
->pattern
, kw
, 0) == 0)
114 xheader_keyword_override_p (const char *keyword
)
116 struct keyword_list
*kp
;
118 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
119 if (strcmp (kp
->pattern
, keyword
) == 0)
125 xheader_list_append (struct keyword_list
**root
, char const *kw
,
128 struct keyword_list
*kp
= xmalloc (sizeof *kp
);
129 kp
->pattern
= xstrdup (kw
);
130 kp
->value
= value
? xstrdup (value
) : NULL
;
136 xheader_list_destroy (struct keyword_list
**root
)
140 struct keyword_list
*kw
= *root
;
143 struct keyword_list
*next
= kw
->next
;
154 xheader_set_single_keyword (char *kw
)
156 USAGE_ERROR ((0, 0, _("Keyword %s is unknown or not yet implemented"), kw
));
160 xheader_set_keyword_equal (char *kw
, char *eq
)
171 while (p
> kw
&& isspace ((unsigned char) *p
))
176 for (p
= eq
+ 1; *p
&& isspace ((unsigned char) *p
); p
++)
179 if (strcmp (kw
, "delete") == 0)
181 if (xheader_protected_pattern_p (p
))
182 USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), quote (p
)));
183 xheader_list_append (&keyword_pattern_list
, p
, NULL
);
185 else if (strcmp (kw
, "exthdr.name") == 0)
186 assign_string (&exthdr_name
, p
);
187 else if (strcmp (kw
, "globexthdr.name") == 0)
188 assign_string (&globexthdr_name
, p
);
191 if (xheader_protected_keyword_p (kw
))
192 USAGE_ERROR ((0, 0, _("Keyword %s cannot be overridden"), kw
));
194 xheader_list_append (&keyword_global_override_list
, kw
, p
);
196 xheader_list_append (&keyword_override_list
, kw
, p
);
201 xheader_set_option (char *string
)
204 for (token
= strtok (string
, ","); token
; token
= strtok (NULL
, ","))
206 char *p
= strchr (token
, '=');
208 xheader_set_single_keyword (token
);
210 xheader_set_keyword_equal (token
, p
);
215 string Includes: Replaced By:
216 %d The directory name of the file,
217 equivalent to the result of the
218 dirname utility on the translated
220 %f The filename of the file, equivalent
221 to the result of the basename
222 utility on the translated file name.
223 %p The process ID of the pax process.
224 %n The value of the 3rd argument.
225 %% A '%' character. */
228 xheader_format_name (struct tar_stat_info
*st
, const char *fmt
, size_t n
)
231 size_t len
= strlen (fmt
);
237 char pidbuf
[UINTMAX_STRSIZE_BOUND
];
239 char nbuf
[UINTMAX_STRSIZE_BOUND
];
240 char const *nptr
= NULL
;
242 for (p
= fmt
; *p
&& (p
= strchr (p
, '%')); )
254 dirp
= dir_name (st
->orig_file_name
);
255 dir
= safer_name_suffix (dirp
, false, absolute_names_option
);
256 len
+= strlen (dir
) - 2;
263 base
= last_component (st
->orig_file_name
);
264 len
+= strlen (base
) - 2;
269 pptr
= umaxtostr (getpid (), pidbuf
);
270 len
+= pidbuf
+ sizeof pidbuf
- 1 - pptr
- 2;
274 nptr
= umaxtostr (n
, nbuf
);
275 len
+= nbuf
+ sizeof nbuf
- 1 - nptr
- 2;
281 buf
= xmalloc (len
+ 1);
282 for (q
= buf
, p
= fmt
; *p
; )
301 q
= stpcpy (q
, base
);
306 q
= stpcpy (q
, pptr
);
313 q
= stpcpy (q
, nptr
);
317 /* else fall through */
331 /* Do not allow it to end in a slash */
332 while (q
> buf
&& ISSLASH (q
[-1]))
339 xheader_xhdr_name (struct tar_stat_info
*st
)
342 assign_string (&exthdr_name
, "%d/PaxHeaders.%p/%f");
343 return xheader_format_name (st
, exthdr_name
, 0);
346 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
349 xheader_ghdr_name (void)
351 if (!globexthdr_name
)
354 const char *tmp
= getenv ("TMPDIR");
357 len
= strlen (tmp
) + sizeof (GLOBAL_HEADER_TEMPLATE
); /* Includes nul */
358 globexthdr_name
= xmalloc (len
);
359 strcpy(globexthdr_name
, tmp
);
360 strcat(globexthdr_name
, GLOBAL_HEADER_TEMPLATE
);
363 return xheader_format_name (NULL
, globexthdr_name
, global_header_count
+ 1);
367 xheader_write (char type
, char *name
, struct xheader
*xhdr
)
374 header
= start_private_header (name
, size
);
375 header
->header
.typeflag
= type
;
377 simple_finish_header (header
);
385 header
= find_next_block ();
389 memcpy (header
->buffer
, p
, len
);
391 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
394 set_next_block_after (header
);
397 xheader_destroy (xhdr
);
400 global_header_count
++;
404 xheader_write_global (struct xheader
*xhdr
)
407 struct keyword_list
*kp
;
409 if (!keyword_global_override_list
)
413 for (kp
= keyword_global_override_list
; kp
; kp
= kp
->next
)
414 code_string (kp
->value
, kp
->pattern
, xhdr
);
415 xheader_finish (xhdr
);
416 xheader_write (XGLTYPE
, name
= xheader_ghdr_name (), xhdr
);
421 /* General Interface */
426 void (*coder
) (struct tar_stat_info
const *, char const *,
427 struct xheader
*, void const *data
);
428 void (*decoder
) (struct tar_stat_info
*, char const *, char const *, size_t);
432 /* This declaration must be extern, because ISO C99 section 6.9.2
433 prohibits a tentative definition that has both internal linkage and
434 incomplete type. If we made it static, we'd have to declare its
435 size which would be a maintenance pain; if we put its initializer
436 here, we'd need a boatload of forward declarations, which would be
437 even more of a pain. */
438 extern struct xhdr_tab
const xhdr_tab
[];
440 static struct xhdr_tab
const *
441 locate_handler (char const *keyword
)
443 struct xhdr_tab
const *p
;
445 for (p
= xhdr_tab
; p
->keyword
; p
++)
446 if (strcmp (p
->keyword
, keyword
) == 0)
452 xheader_protected_pattern_p (const char *pattern
)
454 struct xhdr_tab
const *p
;
456 for (p
= xhdr_tab
; p
->keyword
; p
++)
457 if (p
->protect
&& fnmatch (pattern
, p
->keyword
, 0) == 0)
463 xheader_protected_keyword_p (const char *keyword
)
465 struct xhdr_tab
const *p
;
467 for (p
= xhdr_tab
; p
->keyword
; p
++)
468 if (p
->protect
&& strcmp (p
->keyword
, keyword
) == 0)
473 /* Decode a single extended header record, advancing *PTR to the next record.
474 Return true on success, false otherwise. */
476 decode_record (struct xheader
*xhdr
,
478 void (*handler
) (void *, char const *, char const *, size_t),
488 size_t len_max
= xhdr
->buffer
+ xhdr
->size
- start
;
490 while (*p
== ' ' || *p
== '\t')
496 ERROR ((0, 0, _("Malformed extended header: missing length")));
501 len
= u
= strtoumax (p
, &len_lim
, 10);
502 if (len
!= u
|| errno
== ERANGE
)
504 ERROR ((0, 0, _("Extended header length is out of allowed range")));
510 int len_len
= len_lim
- p
;
511 ERROR ((0, 0, _("Extended header length %*s is out of range"),
518 for (p
= len_lim
; *p
== ' ' || *p
== '\t'; p
++)
523 _("Malformed extended header: missing blank after length")));
529 if (! (p
&& p
< nextp
))
531 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
535 if (nextp
[-1] != '\n')
537 ERROR ((0, 0, _("Malformed extended header: missing newline")));
541 *p
= nextp
[-1] = '\0';
542 handler (data
, keyword
, p
+ 1, nextp
- p
- 2); /* '=' + trailing '\n' */
550 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
552 for (; kp
; kp
= kp
->next
)
554 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
556 t
->decoder (st
, t
->keyword
, kp
->value
, strlen (kp
->value
));
561 decx (void *data
, char const *keyword
, char const *value
, size_t size
)
563 struct xhdr_tab
const *t
;
564 struct tar_stat_info
*st
= data
;
566 if (xheader_keyword_deleted_p (keyword
)
567 || xheader_keyword_override_p (keyword
))
570 t
= locate_handler (keyword
);
572 t
->decoder (st
, keyword
, value
, size
);
574 WARNOPT (WARN_UNKNOWN_KEYWORD
,
575 (0, 0, _("Ignoring unknown extended header keyword `%s'"),
580 xheader_decode (struct tar_stat_info
*st
)
582 run_override_list (keyword_global_override_list
, st
);
583 run_override_list (global_header_override_list
, st
);
587 char *p
= st
->xhdr
.buffer
+ BLOCKSIZE
;
588 while (decode_record (&st
->xhdr
, &p
, decx
, st
))
591 run_override_list (keyword_override_list
, st
);
595 decg (void *data
, char const *keyword
, char const *value
,
596 size_t size
__attribute__((unused
)))
598 struct keyword_list
**kwl
= data
;
599 xheader_list_append (kwl
, keyword
, value
);
603 xheader_decode_global (struct xheader
*xhdr
)
607 char *p
= xhdr
->buffer
+ BLOCKSIZE
;
609 xheader_list_destroy (&global_header_override_list
);
610 while (decode_record (xhdr
, &p
, decg
, &global_header_override_list
))
616 xheader_init (struct xheader
*xhdr
)
620 xhdr
->stk
= xmalloc (sizeof *xhdr
->stk
);
621 obstack_init (xhdr
->stk
);
626 xheader_store (char const *keyword
, struct tar_stat_info
*st
,
629 struct xhdr_tab
const *t
;
633 t
= locate_handler (keyword
);
636 if (xheader_keyword_deleted_p (keyword
)
637 || xheader_keyword_override_p (keyword
))
639 xheader_init (&st
->xhdr
);
640 t
->coder (st
, keyword
, &st
->xhdr
, data
);
644 xheader_read (struct xheader
*xhdr
, union block
*p
, size_t size
)
651 xhdr
->buffer
= xmalloc (size
+ 1);
652 xhdr
->buffer
[size
] = '\0';
661 memcpy (&xhdr
->buffer
[j
], p
->buffer
, len
);
662 set_next_block_after (p
);
664 p
= find_next_block ();
673 xheader_print_n (struct xheader
*xhdr
, char const *keyword
,
674 char const *value
, size_t vsize
)
676 size_t len
= strlen (keyword
) + vsize
+ 3; /* ' ' + '=' + '\n' */
679 char nbuf
[UINTMAX_STRSIZE_BOUND
];
685 np
= umaxtostr (len
+ p
, nbuf
);
686 n
= nbuf
+ sizeof nbuf
- 1 - np
;
690 x_obstack_grow (xhdr
, np
, n
);
691 x_obstack_1grow (xhdr
, ' ');
692 x_obstack_grow (xhdr
, keyword
, strlen (keyword
));
693 x_obstack_1grow (xhdr
, '=');
694 x_obstack_grow (xhdr
, value
, vsize
);
695 x_obstack_1grow (xhdr
, '\n');
699 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
701 xheader_print_n (xhdr
, keyword
, value
, strlen (value
));
705 xheader_finish (struct xheader
*xhdr
)
707 struct keyword_list
*kp
;
709 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
710 code_string (kp
->value
, kp
->pattern
, xhdr
);
712 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
716 xheader_destroy (struct xheader
*xhdr
)
720 obstack_free (xhdr
->stk
, NULL
);
731 /* Buildable strings */
734 xheader_string_begin (struct xheader
*xhdr
)
736 xhdr
->string_length
= 0;
740 xheader_string_add (struct xheader
*xhdr
, char const *s
)
745 xhdr
->string_length
+= strlen (s
);
746 x_obstack_grow (xhdr
, s
, strlen (s
));
750 xheader_string_end (struct xheader
*xhdr
, char const *keyword
)
756 char nbuf
[UINTMAX_STRSIZE_BOUND
];
764 len
= strlen (keyword
) + xhdr
->string_length
+ 3; /* ' ' + '=' + '\n' */
769 np
= umaxtostr (len
+ p
, nbuf
);
770 n
= nbuf
+ sizeof nbuf
- 1 - np
;
774 p
= strlen (keyword
) + n
+ 2;
779 _("Generated keyword/value pair is too long (keyword=%s, length=%s)"),
781 obstack_free (xhdr
->stk
, obstack_finish (xhdr
->stk
));
784 x_obstack_blank (xhdr
, p
);
785 x_obstack_1grow (xhdr
, '\n');
786 cp
= obstack_next_free (xhdr
->stk
) - xhdr
->string_length
- p
- 1;
787 memmove (cp
+ p
, cp
, xhdr
->string_length
);
788 cp
= stpcpy (cp
, np
);
790 cp
= stpcpy (cp
, keyword
);
796 /* Implementations */
799 out_of_range_header (char const *keyword
, char const *value
,
800 uintmax_t minus_minval
, uintmax_t maxval
)
802 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
803 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
804 char *minval_string
= umaxtostr (minus_minval
, minval_buf
+ 1);
805 char *maxval_string
= umaxtostr (maxval
, maxval_buf
);
807 *--minval_string
= '-';
809 /* TRANSLATORS: The first %s is the pax extended header keyword
810 (atime, gid, etc.). */
811 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
812 keyword
, value
, minval_string
, maxval_string
));
816 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
819 if (!utf8_convert (true, string
, &outstr
))
821 /* FIXME: report error */
822 outstr
= xstrdup (string
);
824 xheader_print (xhdr
, keyword
, outstr
);
829 decode_string (char **string
, char const *arg
)
836 if (!utf8_convert (false, arg
, string
))
838 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
839 assign_string (string
, arg
);
844 code_time (struct timespec t
, char const *keyword
, struct xheader
*xhdr
)
846 char buf
[TIMESPEC_STRSIZE_BOUND
];
847 xheader_print (xhdr
, keyword
, code_timespec (t
, buf
));
850 enum decode_time_status
854 decode_time_bad_header
857 static enum decode_time_status
858 _decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
861 unsigned long int ns
= 0;
864 bool negative
= *arg
== '-';
868 if (ISDIGIT (arg
[negative
]))
872 intmax_t i
= strtoimax (arg
, &arg_lim
, 10);
873 if (TYPE_SIGNED (time_t) ? i
< TYPE_MINIMUM (time_t) : i
< 0)
874 return decode_time_range
;
879 uintmax_t i
= strtoumax (arg
, &arg_lim
, 10);
880 if (TYPE_MAXIMUM (time_t) < i
)
881 return decode_time_range
;
888 return decode_time_range
;
893 bool trailing_nonzero
= false;
895 while (ISDIGIT (*++p
))
896 if (digits
< LOG10_BILLION
)
898 ns
= 10 * ns
+ (*p
- '0');
902 trailing_nonzero
|= *p
!= '0';
904 while (digits
++ < LOG10_BILLION
)
909 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
910 I.e., truncate time stamps towards minus infinity while
911 converting them to internal form. */
912 ns
+= trailing_nonzero
;
915 if (s
== TYPE_MINIMUM (time_t))
916 return decode_time_range
;
927 return decode_time_success
;
931 return decode_time_bad_header
;
935 decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
937 switch (_decode_time (ts
, arg
, keyword
))
939 case decode_time_success
:
941 case decode_time_bad_header
:
942 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
945 case decode_time_range
:
946 out_of_range_header (keyword
, arg
, - (uintmax_t) TYPE_MINIMUM (time_t),
947 TYPE_MAXIMUM (time_t));
956 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
958 char sbuf
[UINTMAX_STRSIZE_BOUND
];
959 xheader_print (xhdr
, keyword
, umaxtostr (value
, sbuf
));
963 decode_num (uintmax_t *num
, char const *arg
, uintmax_t maxval
,
969 if (! (ISDIGIT (*arg
)
970 && (errno
= 0, u
= strtoumax (arg
, &arg_lim
, 10), !*arg_lim
)))
972 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
977 if (! (u
<= maxval
&& errno
!= ERANGE
))
979 out_of_range_header (keyword
, arg
, 0, maxval
);
988 dummy_coder (struct tar_stat_info
const *st
__attribute__ ((unused
)),
989 char const *keyword
__attribute__ ((unused
)),
990 struct xheader
*xhdr
__attribute__ ((unused
)),
991 void const *data
__attribute__ ((unused
)))
996 dummy_decoder (struct tar_stat_info
*st
__attribute__ ((unused
)),
997 char const *keyword
__attribute__ ((unused
)),
998 char const *arg
__attribute__ ((unused
)),
999 size_t size
__attribute__((unused
)))
1004 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1005 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1007 code_time (st
->atime
, keyword
, xhdr
);
1011 atime_decoder (struct tar_stat_info
*st
,
1012 char const *keyword
,
1014 size_t size
__attribute__((unused
)))
1017 if (decode_time (&ts
, arg
, keyword
))
1022 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1023 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1025 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
1029 gid_decoder (struct tar_stat_info
*st
,
1030 char const *keyword
,
1032 size_t size
__attribute__((unused
)))
1035 if (decode_num (&u
, arg
, TYPE_MAXIMUM (gid_t
), keyword
))
1036 st
->stat
.st_gid
= u
;
1040 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1041 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1043 code_string (st
->gname
, keyword
, xhdr
);
1047 gname_decoder (struct tar_stat_info
*st
,
1048 char const *keyword
__attribute__((unused
)),
1050 size_t size
__attribute__((unused
)))
1052 decode_string (&st
->gname
, arg
);
1056 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
1057 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1059 code_string (st
->link_name
, keyword
, xhdr
);
1063 linkpath_decoder (struct tar_stat_info
*st
,
1064 char const *keyword
__attribute__((unused
)),
1066 size_t size
__attribute__((unused
)))
1068 decode_string (&st
->link_name
, arg
);
1072 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1073 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1075 code_time (st
->ctime
, keyword
, xhdr
);
1079 ctime_decoder (struct tar_stat_info
*st
,
1080 char const *keyword
,
1082 size_t size
__attribute__((unused
)))
1085 if (decode_time (&ts
, arg
, keyword
))
1090 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1091 struct xheader
*xhdr
, void const *data
)
1093 struct timespec
const *mtime
= data
;
1094 code_time (mtime
? *mtime
: st
->mtime
, keyword
, xhdr
);
1098 mtime_decoder (struct tar_stat_info
*st
,
1099 char const *keyword
,
1101 size_t size
__attribute__((unused
)))
1104 if (decode_time (&ts
, arg
, keyword
))
1109 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
1110 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1112 code_string (st
->file_name
, keyword
, xhdr
);
1116 path_decoder (struct tar_stat_info
*st
,
1117 char const *keyword
__attribute__((unused
)),
1119 size_t size
__attribute__((unused
)))
1121 decode_string (&st
->orig_file_name
, arg
);
1122 decode_string (&st
->file_name
, arg
);
1123 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
1127 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1128 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1130 code_num (st
->stat
.st_size
, keyword
, xhdr
);
1134 size_decoder (struct tar_stat_info
*st
,
1135 char const *keyword
,
1137 size_t size
__attribute__((unused
)))
1140 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1141 st
->stat
.st_size
= u
;
1145 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1146 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1148 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
1152 uid_decoder (struct tar_stat_info
*st
,
1153 char const *keyword
,
1155 size_t size
__attribute__((unused
)))
1158 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uid_t
), keyword
))
1159 st
->stat
.st_uid
= u
;
1163 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1164 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1166 code_string (st
->uname
, keyword
, xhdr
);
1170 uname_decoder (struct tar_stat_info
*st
,
1171 char const *keyword
__attribute__((unused
)),
1173 size_t size
__attribute__((unused
)))
1175 decode_string (&st
->uname
, arg
);
1179 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1180 struct xheader
*xhdr
, void const *data
)
1182 size_coder (st
, keyword
, xhdr
, data
);
1186 sparse_size_decoder (struct tar_stat_info
*st
,
1187 char const *keyword
,
1189 size_t size
__attribute__((unused
)))
1192 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1193 st
->stat
.st_size
= u
;
1197 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
1198 struct xheader
*xhdr
,
1199 void const *data
__attribute__ ((unused
)))
1201 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
1205 sparse_numblocks_decoder (struct tar_stat_info
*st
,
1206 char const *keyword
,
1208 size_t size
__attribute__((unused
)))
1211 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1213 st
->sparse_map_size
= u
;
1214 st
->sparse_map
= xcalloc (u
, sizeof st
->sparse_map
[0]);
1215 st
->sparse_map_avail
= 0;
1220 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1221 struct xheader
*xhdr
, void const *data
)
1223 size_t const *pi
= data
;
1224 code_num (st
->sparse_map
[*pi
].offset
, keyword
, xhdr
);
1228 sparse_offset_decoder (struct tar_stat_info
*st
,
1229 char const *keyword
,
1231 size_t size
__attribute__((unused
)))
1234 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1236 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1237 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
1239 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1240 "GNU.sparse.offset", arg
));
1245 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
1246 struct xheader
*xhdr
, void const *data
)
1248 size_t const *pi
= data
;
1249 code_num (st
->sparse_map
[*pi
].numbytes
, keyword
, xhdr
);
1253 sparse_numbytes_decoder (struct tar_stat_info
*st
,
1254 char const *keyword
,
1256 size_t size
__attribute__((unused
)))
1259 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1261 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1262 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
1264 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1270 sparse_map_decoder (struct tar_stat_info
*st
,
1271 char const *keyword
,
1273 size_t size
__attribute__((unused
)))
1277 st
->sparse_map_avail
= 0;
1284 if (!ISDIGIT (*arg
))
1286 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1292 u
= strtoumax (arg
, &delim
, 10);
1296 if (!(u
== e
.offset
&& errno
!= ERANGE
))
1298 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1305 if (!(u
== e
.numbytes
&& errno
!= ERANGE
))
1307 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (size_t));
1310 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1311 st
->sparse_map
[st
->sparse_map_avail
++] = e
;
1314 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1324 else if (*delim
!= ',')
1327 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1337 _("Malformed extended header: invalid %s: odd number of values"),
1342 dumpdir_coder (struct tar_stat_info
const *st
, char const *keyword
,
1343 struct xheader
*xhdr
, void const *data
)
1345 xheader_print_n (xhdr
, keyword
, data
, dumpdir_size (data
));
1349 dumpdir_decoder (struct tar_stat_info
*st
,
1350 char const *keyword
__attribute__((unused
)),
1354 st
->dumpdir
= xmalloc (size
);
1355 memcpy (st
->dumpdir
, arg
, size
);
1359 volume_label_coder (struct tar_stat_info
const *st
, char const *keyword
,
1360 struct xheader
*xhdr
, void const *data
)
1362 code_string (data
, keyword
, xhdr
);
1366 volume_label_decoder (struct tar_stat_info
*st
,
1367 char const *keyword
__attribute__((unused
)),
1369 size_t size
__attribute__((unused
)))
1371 decode_string (&volume_label
, arg
);
1375 volume_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1376 struct xheader
*xhdr
, void const *data
)
1378 off_t
const *v
= data
;
1379 code_num (*v
, keyword
, xhdr
);
1383 volume_size_decoder (struct tar_stat_info
*st
,
1384 char const *keyword
,
1385 char const *arg
, size_t size
)
1388 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1389 continued_file_size
= u
;
1392 /* FIXME: Merge with volume_size_coder */
1394 volume_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1395 struct xheader
*xhdr
, void const *data
)
1397 off_t
const *v
= data
;
1398 code_num (*v
, keyword
, xhdr
);
1402 volume_offset_decoder (struct tar_stat_info
*st
,
1403 char const *keyword
,
1404 char const *arg
, size_t size
)
1407 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1408 continued_file_offset
= u
;
1412 volume_filename_decoder (struct tar_stat_info
*st
,
1413 char const *keyword
__attribute__((unused
)),
1415 size_t size
__attribute__((unused
)))
1417 decode_string (&continued_file_name
, arg
);
1421 sparse_major_coder (struct tar_stat_info
const *st
, char const *keyword
,
1422 struct xheader
*xhdr
, void const *data
)
1424 code_num (st
->sparse_major
, keyword
, xhdr
);
1428 sparse_major_decoder (struct tar_stat_info
*st
,
1429 char const *keyword
,
1434 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1435 st
->sparse_major
= u
;
1439 sparse_minor_coder (struct tar_stat_info
const *st
, char const *keyword
,
1440 struct xheader
*xhdr
, void const *data
)
1442 code_num (st
->sparse_minor
, keyword
, xhdr
);
1446 sparse_minor_decoder (struct tar_stat_info
*st
,
1447 char const *keyword
,
1452 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1453 st
->sparse_minor
= u
;
1456 struct xhdr_tab
const xhdr_tab
[] = {
1457 { "atime", atime_coder
, atime_decoder
, false },
1458 { "comment", dummy_coder
, dummy_decoder
, false },
1459 { "charset", dummy_coder
, dummy_decoder
, false },
1460 { "ctime", ctime_coder
, ctime_decoder
, false },
1461 { "gid", gid_coder
, gid_decoder
, false },
1462 { "gname", gname_coder
, gname_decoder
, false },
1463 { "linkpath", linkpath_coder
, linkpath_decoder
, false },
1464 { "mtime", mtime_coder
, mtime_decoder
, false },
1465 { "path", path_coder
, path_decoder
, false },
1466 { "size", size_coder
, size_decoder
, false },
1467 { "uid", uid_coder
, uid_decoder
, false },
1468 { "uname", uname_coder
, uname_decoder
, false },
1470 /* Sparse file handling */
1471 { "GNU.sparse.name", path_coder
, path_decoder
,
1473 { "GNU.sparse.major", sparse_major_coder
, sparse_major_decoder
,
1475 { "GNU.sparse.minor", sparse_minor_coder
, sparse_minor_decoder
,
1477 { "GNU.sparse.realsize", sparse_size_coder
, sparse_size_decoder
,
1479 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
1482 /* tar 1.14 - 1.15.90 keywords. */
1483 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
, true },
1484 /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
1485 headers, and each of them was meaningful. It confilcted with POSIX specs,
1486 which requires that "when extended header records conflict, the last one
1487 given in the header shall take precedence." */
1488 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
1490 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
1492 /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
1493 { "GNU.sparse.map", NULL
/* Unused, see pax_dump_header() */,
1494 sparse_map_decoder
, false },
1496 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
,
1499 /* Keeps the tape/volume label. May be present only in the global headers.
1500 Equivalent to GNUTYPE_VOLHDR. */
1501 { "GNU.volume.label", volume_label_coder
, volume_label_decoder
, true },
1503 /* These may be present in a first global header of the archive.
1504 They provide the same functionality as GNUTYPE_MULTIVOL header.
1505 The GNU.volume.size keeps the real_s_sizeleft value, which is
1506 otherwise kept in the size field of a multivolume header. The
1507 GNU.volume.offset keeps the offset of the start of this volume,
1508 otherwise kept in oldgnu_header.offset. */
1509 { "GNU.volume.filename", volume_label_coder
, volume_filename_decoder
,
1511 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
, true },
1512 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
, true },
1514 { NULL
, NULL
, NULL
, false }