1 /* POSIX extended headers for tar.
3 Copyright (C) 2003, 2004, 2005, 2006 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 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
31 static bool xheader_protected_pattern_p (char const *pattern
);
32 static bool xheader_protected_keyword_p (char const *keyword
);
33 static void xheader_set_single_keyword (char *) __attribute__ ((noreturn
));
35 /* Used by xheader_finish() */
36 static void code_string (char const *string
, char const *keyword
,
37 struct xheader
*xhdr
);
38 static void extended_header_init (void);
40 /* Number of global headers written so far. */
41 static size_t global_header_count
;
42 /* FIXME: Possibly it should be reset after changing the volume.
43 POSIX %n specification says that it is expanded to the sequence
44 number of current global header in *the* archive. However, for
45 multi-volume archives this will yield duplicate header names
46 in different volumes, which I'd like to avoid. The best way
47 to solve this would be to use per-archive header count as required
48 by POSIX *and* set globexthdr.name to, say,
49 $TMPDIR/GlobalHead.%p.$NUMVOLUME.%n.
51 However it should wait until buffer.c is finally rewritten */
54 /* Interface functions to obstacks */
57 x_obstack_grow (struct xheader
*xhdr
, const char *ptr
, size_t length
)
59 obstack_grow (xhdr
->stk
, ptr
, length
);
64 x_obstack_1grow (struct xheader
*xhdr
, char c
)
66 obstack_1grow (xhdr
->stk
, c
);
71 x_obstack_blank (struct xheader
*xhdr
, size_t length
)
73 obstack_blank (xhdr
->stk
, length
);
82 struct keyword_list
*next
;
88 /* List of keyword patterns set by delete= option */
89 static struct keyword_list
*keyword_pattern_list
;
91 /* List of keyword/value pairs set by `keyword=value' option */
92 static struct keyword_list
*keyword_global_override_list
;
94 /* List of keyword/value pairs set by `keyword:=value' option */
95 static struct keyword_list
*keyword_override_list
;
97 /* List of keyword/value pairs decoded from the last 'g' type header */
98 static struct keyword_list
*global_header_override_list
;
100 /* Template for the name field of an 'x' type header */
101 static char *exthdr_name
;
103 /* Template for the name field of a 'g' type header */
104 static char *globexthdr_name
;
107 xheader_keyword_deleted_p (const char *kw
)
109 struct keyword_list
*kp
;
111 for (kp
= keyword_pattern_list
; kp
; kp
= kp
->next
)
112 if (fnmatch (kp
->pattern
, kw
, 0) == 0)
118 xheader_keyword_override_p (const char *keyword
)
120 struct keyword_list
*kp
;
122 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
123 if (strcmp (kp
->pattern
, keyword
) == 0)
129 xheader_list_append (struct keyword_list
**root
, char const *kw
,
132 struct keyword_list
*kp
= xmalloc (sizeof *kp
);
133 kp
->pattern
= xstrdup (kw
);
134 kp
->value
= value
? xstrdup (value
) : NULL
;
140 xheader_list_destroy (struct keyword_list
**root
)
144 struct keyword_list
*kw
= *root
;
147 struct keyword_list
*next
= kw
->next
;
158 xheader_set_single_keyword (char *kw
)
160 USAGE_ERROR ((0, 0, _("Keyword %s is unknown or not yet implemented"), kw
));
164 xheader_set_keyword_equal (char *kw
, char *eq
)
175 while (p
> kw
&& isspace (*p
))
180 for (p
= eq
+ 1; *p
&& isspace (*p
); p
++)
183 if (strcmp (kw
, "delete") == 0)
185 if (xheader_protected_pattern_p (p
))
186 USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), quote (p
)));
187 xheader_list_append (&keyword_pattern_list
, p
, NULL
);
189 else if (strcmp (kw
, "exthdr.name") == 0)
190 assign_string (&exthdr_name
, p
);
191 else if (strcmp (kw
, "globexthdr.name") == 0)
192 assign_string (&globexthdr_name
, p
);
195 if (xheader_protected_keyword_p (kw
))
196 USAGE_ERROR ((0, 0, _("Keyword %s cannot be overridden"), kw
));
198 xheader_list_append (&keyword_global_override_list
, kw
, p
);
200 xheader_list_append (&keyword_override_list
, kw
, p
);
205 xheader_set_option (char *string
)
208 for (token
= strtok (string
, ","); token
; token
= strtok (NULL
, ","))
210 char *p
= strchr (token
, '=');
212 xheader_set_single_keyword (token
);
214 xheader_set_keyword_equal (token
, p
);
219 string Includes: Replaced By:
220 %d The directory name of the file,
221 equivalent to the result of the
222 dirname utility on the translated
224 %f The filename of the file, equivalent
225 to the result of the basename
226 utility on the translated file name.
227 %p The process ID of the pax process.
228 %n The value of the 3rd argument.
229 %% A '%' character. */
232 xheader_format_name (struct tar_stat_info
*st
, const char *fmt
, size_t n
)
235 size_t len
= strlen (fmt
);
241 char pidbuf
[UINTMAX_STRSIZE_BOUND
];
243 char nbuf
[UINTMAX_STRSIZE_BOUND
];
244 char const *nptr
= NULL
;
246 for (p
= fmt
; *p
&& (p
= strchr (p
, '%')); )
258 dirp
= dir_name (st
->orig_file_name
);
259 dir
= safer_name_suffix (dirp
, false, absolute_names_option
);
260 len
+= strlen (dir
) - 2;
267 base
= last_component (st
->orig_file_name
);
268 len
+= strlen (base
) - 2;
273 pptr
= umaxtostr (getpid (), pidbuf
);
274 len
+= pidbuf
+ sizeof pidbuf
- 1 - pptr
- 2;
278 nptr
= umaxtostr (n
, nbuf
);
279 len
+= nbuf
+ sizeof nbuf
- 1 - nptr
- 2;
285 buf
= xmalloc (len
+ 1);
286 for (q
= buf
, p
= fmt
; *p
; )
305 q
= stpcpy (q
, base
);
310 q
= stpcpy (q
, pptr
);
317 q
= stpcpy (q
, nptr
);
321 /* else fall through */
335 /* Do not allow it to end in a slash */
336 while (q
> buf
&& ISSLASH (q
[-1]))
343 xheader_xhdr_name (struct tar_stat_info
*st
)
346 assign_string (&exthdr_name
, "%d/PaxHeaders.%p/%f");
347 return xheader_format_name (st
, exthdr_name
, 0);
350 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
353 xheader_ghdr_name (void)
355 if (!globexthdr_name
)
358 const char *tmp
= getenv ("TMPDIR");
361 len
= strlen (tmp
) + sizeof (GLOBAL_HEADER_TEMPLATE
); /* Includes nul */
362 globexthdr_name
= xmalloc (len
);
363 strcpy(globexthdr_name
, tmp
);
364 strcat(globexthdr_name
, GLOBAL_HEADER_TEMPLATE
);
367 return xheader_format_name (NULL
, globexthdr_name
, global_header_count
+ 1);
371 xheader_write (char type
, char *name
, struct xheader
*xhdr
)
378 header
= start_private_header (name
, size
);
379 header
->header
.typeflag
= type
;
381 simple_finish_header (header
);
389 header
= find_next_block ();
393 memcpy (header
->buffer
, p
, len
);
395 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
398 set_next_block_after (header
);
401 xheader_destroy (xhdr
);
404 global_header_count
++;
408 xheader_write_global (void)
411 struct keyword_list
*kp
;
413 if (!keyword_global_override_list
)
416 extended_header_init ();
417 for (kp
= keyword_global_override_list
; kp
; kp
= kp
->next
)
418 code_string (kp
->value
, kp
->pattern
, &extended_header
);
419 xheader_finish (&extended_header
);
420 xheader_write (XGLTYPE
, name
= xheader_ghdr_name (),
426 /* General Interface */
431 void (*coder
) (struct tar_stat_info
const *, char const *,
432 struct xheader
*, void const *data
);
433 void (*decoder
) (struct tar_stat_info
*, char const *, char const *, size_t);
437 /* This declaration must be extern, because ISO C99 section 6.9.2
438 prohibits a tentative definition that has both internal linkage and
439 incomplete type. If we made it static, we'd have to declare its
440 size which would be a maintenance pain; if we put its initializer
441 here, we'd need a boatload of forward declarations, which would be
442 even more of a pain. */
443 extern struct xhdr_tab
const xhdr_tab
[];
445 static struct xhdr_tab
const *
446 locate_handler (char const *keyword
)
448 struct xhdr_tab
const *p
;
450 for (p
= xhdr_tab
; p
->keyword
; p
++)
451 if (strcmp (p
->keyword
, keyword
) == 0)
457 xheader_protected_pattern_p (const char *pattern
)
459 struct xhdr_tab
const *p
;
461 for (p
= xhdr_tab
; p
->keyword
; p
++)
462 if (p
->protect
&& fnmatch (pattern
, p
->keyword
, 0) == 0)
468 xheader_protected_keyword_p (const char *keyword
)
470 struct xhdr_tab
const *p
;
472 for (p
= xhdr_tab
; p
->keyword
; p
++)
473 if (p
->protect
&& strcmp (p
->keyword
, keyword
) == 0)
478 /* Decode a single extended header record, advancing *PTR to the next record.
479 Return true on success, false otherwise. */
481 decode_record (char **ptr
,
482 void (*handler
) (void *, char const *, char const *, size_t),
492 size_t len_max
= extended_header
.buffer
+ extended_header
.size
- start
;
494 while (*p
== ' ' || *p
== '\t')
500 ERROR ((0, 0, _("Malformed extended header: missing length")));
505 len
= u
= strtoumax (p
, &len_lim
, 10);
506 if (len
!= u
|| errno
== ERANGE
)
508 ERROR ((0, 0, _("Extended header length is out of allowed range")));
514 int len_len
= len_lim
- p
;
515 ERROR ((0, 0, _("Extended header length %*s is out of range"),
522 for (p
= len_lim
; *p
== ' ' || *p
== '\t'; p
++)
527 _("Malformed extended header: missing blank after length")));
533 if (! (p
&& p
< nextp
))
535 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
539 if (nextp
[-1] != '\n')
541 ERROR ((0, 0, _("Malformed extended header: missing newline")));
545 *p
= nextp
[-1] = '\0';
546 handler (data
, keyword
, p
+ 1, nextp
- p
- 2); /* '=' + trailing '\n' */
554 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
556 for (; kp
; kp
= kp
->next
)
558 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
560 t
->decoder (st
, t
->keyword
, kp
->value
, strlen (kp
->value
));
565 decx (void *data
, char const *keyword
, char const *value
, size_t size
)
567 struct xhdr_tab
const *t
;
568 struct tar_stat_info
*st
= data
;
570 if (xheader_keyword_deleted_p (keyword
)
571 || xheader_keyword_override_p (keyword
))
574 t
= locate_handler (keyword
);
576 t
->decoder (st
, keyword
, value
, size
);
578 ERROR((0, 0, _("Ignoring unknown extended header keyword `%s'"),
583 xheader_decode (struct tar_stat_info
*st
)
585 run_override_list (keyword_global_override_list
, st
);
586 run_override_list (global_header_override_list
, st
);
588 if (extended_header
.size
)
590 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
591 while (decode_record (&p
, decx
, st
))
594 run_override_list (keyword_override_list
, st
);
598 decg (void *data
, char const *keyword
, char const *value
,
599 size_t size
__attribute__((unused
)))
601 struct keyword_list
**kwl
= data
;
602 xheader_list_append (kwl
, keyword
, value
);
606 xheader_decode_global (void)
608 if (extended_header
.size
)
610 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
612 xheader_list_destroy (&global_header_override_list
);
613 while (decode_record (&p
, decg
, &global_header_override_list
))
619 extended_header_init (void)
621 if (!extended_header
.stk
)
623 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
624 obstack_init (extended_header
.stk
);
629 xheader_store (char const *keyword
, struct tar_stat_info
const *st
,
632 struct xhdr_tab
const *t
;
634 if (extended_header
.buffer
)
636 t
= locate_handler (keyword
);
639 if (xheader_keyword_deleted_p (keyword
)
640 || xheader_keyword_override_p (keyword
))
642 extended_header_init ();
643 t
->coder (st
, keyword
, &extended_header
, data
);
647 xheader_read (union block
*p
, size_t size
)
651 free (extended_header
.buffer
);
653 extended_header
.size
= size
;
654 extended_header
.buffer
= xmalloc (size
+ 1);
655 extended_header
.buffer
[size
] = '\0';
664 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
665 set_next_block_after (p
);
667 p
= find_next_block ();
676 xheader_print_n (struct xheader
*xhdr
, char const *keyword
,
677 char const *value
, size_t vsize
)
679 size_t len
= strlen (keyword
) + vsize
+ 3; /* ' ' + '=' + '\n' */
682 char nbuf
[UINTMAX_STRSIZE_BOUND
];
688 np
= umaxtostr (len
+ p
, nbuf
);
689 n
= nbuf
+ sizeof nbuf
- 1 - np
;
693 x_obstack_grow (xhdr
, np
, n
);
694 x_obstack_1grow (xhdr
, ' ');
695 x_obstack_grow (xhdr
, keyword
, strlen (keyword
));
696 x_obstack_1grow (xhdr
, '=');
697 x_obstack_grow (xhdr
, value
, vsize
);
698 x_obstack_1grow (xhdr
, '\n');
702 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
704 xheader_print_n (xhdr
, keyword
, value
, strlen (value
));
708 xheader_finish (struct xheader
*xhdr
)
710 struct keyword_list
*kp
;
712 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
713 code_string (kp
->value
, kp
->pattern
, xhdr
);
715 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
719 xheader_destroy (struct xheader
*xhdr
)
723 obstack_free (xhdr
->stk
, NULL
);
734 /* Buildable strings */
735 static uintmax_t string_length
;
738 xheader_string_begin ()
744 xheader_string_add (char const *s
)
746 if (extended_header
.buffer
)
748 extended_header_init ();
749 string_length
+= strlen (s
);
750 x_obstack_grow (&extended_header
, s
, strlen (s
));
754 xheader_string_end (char const *keyword
)
760 char nbuf
[UINTMAX_STRSIZE_BOUND
];
764 if (extended_header
.buffer
)
766 extended_header_init ();
768 len
= strlen (keyword
) + string_length
+ 3; /* ' ' + '=' + '\n' */
773 np
= umaxtostr (len
+ p
, nbuf
);
774 n
= nbuf
+ sizeof nbuf
- 1 - np
;
778 p
= strlen (keyword
) + n
+ 2;
783 _("Generated keyword/value pair is too long (keyword=%s, length=%s)"),
785 obstack_free (extended_header
.stk
, obstack_finish (extended_header
.stk
));
788 x_obstack_blank (&extended_header
, p
);
789 x_obstack_1grow (&extended_header
, '\n');
790 cp
= obstack_next_free (extended_header
.stk
) - string_length
- p
- 1;
791 memmove (cp
+ p
, cp
, string_length
);
792 cp
= stpcpy (cp
, np
);
794 cp
= stpcpy (cp
, keyword
);
800 /* Implementations */
803 out_of_range_header (char const *keyword
, char const *value
,
804 uintmax_t minus_minval
, uintmax_t maxval
)
806 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
807 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
808 char *minval_string
= umaxtostr (minus_minval
, minval_buf
+ 1);
809 char *maxval_string
= umaxtostr (maxval
, maxval_buf
);
811 *--minval_string
= '-';
813 /* TRANSLATORS: The first %s is the pax extended header keyword
814 (atime, gid, etc.). */
815 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
816 keyword
, value
, minval_string
, maxval_string
));
820 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
823 if (!utf8_convert (true, string
, &outstr
))
825 /* FIXME: report error */
826 outstr
= xstrdup (string
);
828 xheader_print (xhdr
, keyword
, outstr
);
833 decode_string (char **string
, char const *arg
)
840 if (!utf8_convert (false, arg
, string
))
842 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
843 assign_string (string
, arg
);
848 code_time (struct timespec t
, char const *keyword
, struct xheader
*xhdr
)
850 char buf
[TIMESPEC_STRSIZE_BOUND
];
851 xheader_print (xhdr
, keyword
, code_timespec (t
, buf
));
854 enum decode_time_status
858 decode_time_bad_header
861 static enum decode_time_status
862 _decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
865 unsigned long int ns
= 0;
868 bool negative
= *arg
== '-';
872 if (ISDIGIT (arg
[negative
]))
876 intmax_t i
= strtoimax (arg
, &arg_lim
, 10);
877 if (TYPE_SIGNED (time_t) ? i
< TYPE_MINIMUM (time_t) : i
< 0)
878 return decode_time_range
;
883 uintmax_t i
= strtoumax (arg
, &arg_lim
, 10);
884 if (TYPE_MAXIMUM (time_t) < i
)
885 return decode_time_range
;
892 return decode_time_range
;
897 bool trailing_nonzero
= false;
899 while (ISDIGIT (*++p
))
900 if (digits
< LOG10_BILLION
)
902 ns
= 10 * ns
+ (*p
- '0');
906 trailing_nonzero
|= *p
!= '0';
908 while (digits
++ < LOG10_BILLION
)
913 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
914 I.e., truncate time stamps towards minus infinity while
915 converting them to internal form. */
916 ns
+= trailing_nonzero
;
919 if (s
== TYPE_MINIMUM (time_t))
920 return decode_time_range
;
931 return decode_time_success
;
935 return decode_time_bad_header
;
939 decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
941 switch (_decode_time (ts
, arg
, keyword
))
943 case decode_time_success
:
945 case decode_time_bad_header
:
946 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
949 case decode_time_range
:
950 out_of_range_header (keyword
, arg
, - (uintmax_t) TYPE_MINIMUM (time_t),
951 TYPE_MAXIMUM (time_t));
960 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
962 char sbuf
[UINTMAX_STRSIZE_BOUND
];
963 xheader_print (xhdr
, keyword
, umaxtostr (value
, sbuf
));
967 decode_num (uintmax_t *num
, char const *arg
, uintmax_t maxval
,
973 if (! (ISDIGIT (*arg
)
974 && (errno
= 0, u
= strtoumax (arg
, &arg_lim
, 10), !*arg_lim
)))
976 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
981 if (! (u
<= maxval
&& errno
!= ERANGE
))
983 out_of_range_header (keyword
, arg
, 0, maxval
);
992 dummy_coder (struct tar_stat_info
const *st
__attribute__ ((unused
)),
993 char const *keyword
__attribute__ ((unused
)),
994 struct xheader
*xhdr
__attribute__ ((unused
)),
995 void const *data
__attribute__ ((unused
)))
1000 dummy_decoder (struct tar_stat_info
*st
__attribute__ ((unused
)),
1001 char const *keyword
__attribute__ ((unused
)),
1002 char const *arg
__attribute__ ((unused
)),
1003 size_t size
__attribute__((unused
)))
1008 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1009 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1011 code_time (st
->atime
, keyword
, xhdr
);
1015 atime_decoder (struct tar_stat_info
*st
,
1016 char const *keyword
,
1018 size_t size
__attribute__((unused
)))
1021 if (decode_time (&ts
, arg
, keyword
))
1026 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1027 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1029 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
1033 gid_decoder (struct tar_stat_info
*st
,
1034 char const *keyword
,
1036 size_t size
__attribute__((unused
)))
1039 if (decode_num (&u
, arg
, TYPE_MAXIMUM (gid_t
), keyword
))
1040 st
->stat
.st_gid
= u
;
1044 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1045 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1047 code_string (st
->gname
, keyword
, xhdr
);
1051 gname_decoder (struct tar_stat_info
*st
,
1052 char const *keyword
__attribute__((unused
)),
1054 size_t size
__attribute__((unused
)))
1056 decode_string (&st
->gname
, arg
);
1060 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
1061 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1063 code_string (st
->link_name
, keyword
, xhdr
);
1067 linkpath_decoder (struct tar_stat_info
*st
,
1068 char const *keyword
__attribute__((unused
)),
1070 size_t size
__attribute__((unused
)))
1072 decode_string (&st
->link_name
, arg
);
1076 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1077 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1079 code_time (st
->ctime
, keyword
, xhdr
);
1083 ctime_decoder (struct tar_stat_info
*st
,
1084 char const *keyword
,
1086 size_t size
__attribute__((unused
)))
1089 if (decode_time (&ts
, arg
, keyword
))
1094 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1095 struct xheader
*xhdr
, void const *data
)
1097 struct timespec
const *mtime
= data
;
1098 code_time (mtime
? *mtime
: st
->mtime
, keyword
, xhdr
);
1102 mtime_decoder (struct tar_stat_info
*st
,
1103 char const *keyword
,
1105 size_t size
__attribute__((unused
)))
1108 if (decode_time (&ts
, arg
, keyword
))
1113 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
1114 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1116 code_string (st
->file_name
, keyword
, xhdr
);
1120 path_decoder (struct tar_stat_info
*st
,
1121 char const *keyword
__attribute__((unused
)),
1123 size_t size
__attribute__((unused
)))
1125 decode_string (&st
->orig_file_name
, arg
);
1126 decode_string (&st
->file_name
, arg
);
1127 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
1131 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1132 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1134 code_num (st
->stat
.st_size
, keyword
, xhdr
);
1138 size_decoder (struct tar_stat_info
*st
,
1139 char const *keyword
,
1141 size_t size
__attribute__((unused
)))
1144 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1145 st
->stat
.st_size
= u
;
1149 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1150 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1152 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
1156 uid_decoder (struct tar_stat_info
*st
,
1157 char const *keyword
,
1159 size_t size
__attribute__((unused
)))
1162 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uid_t
), keyword
))
1163 st
->stat
.st_uid
= u
;
1167 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1168 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1170 code_string (st
->uname
, keyword
, xhdr
);
1174 uname_decoder (struct tar_stat_info
*st
,
1175 char const *keyword
__attribute__((unused
)),
1177 size_t size
__attribute__((unused
)))
1179 decode_string (&st
->uname
, arg
);
1183 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1184 struct xheader
*xhdr
, void const *data
)
1186 size_coder (st
, keyword
, xhdr
, data
);
1190 sparse_size_decoder (struct tar_stat_info
*st
,
1191 char const *keyword
,
1193 size_t size
__attribute__((unused
)))
1196 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1197 st
->stat
.st_size
= u
;
1201 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
1202 struct xheader
*xhdr
,
1203 void const *data
__attribute__ ((unused
)))
1205 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
1209 sparse_numblocks_decoder (struct tar_stat_info
*st
,
1210 char const *keyword
,
1212 size_t size
__attribute__((unused
)))
1215 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1217 st
->sparse_map_size
= u
;
1218 st
->sparse_map
= xcalloc (u
, sizeof st
->sparse_map
[0]);
1219 st
->sparse_map_avail
= 0;
1224 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1225 struct xheader
*xhdr
, void const *data
)
1227 size_t const *pi
= data
;
1228 code_num (st
->sparse_map
[*pi
].offset
, keyword
, xhdr
);
1232 sparse_offset_decoder (struct tar_stat_info
*st
,
1233 char const *keyword
,
1235 size_t size
__attribute__((unused
)))
1238 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1240 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1241 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
1243 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1244 "GNU.sparse.offset", arg
));
1249 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
1250 struct xheader
*xhdr
, void const *data
)
1252 size_t const *pi
= data
;
1253 code_num (st
->sparse_map
[*pi
].numbytes
, keyword
, xhdr
);
1257 sparse_numbytes_decoder (struct tar_stat_info
*st
,
1258 char const *keyword
,
1260 size_t size
__attribute__((unused
)))
1263 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1265 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1266 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
1268 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1274 sparse_map_decoder (struct tar_stat_info
*st
,
1275 char const *keyword
,
1277 size_t size
__attribute__((unused
)))
1281 st
->sparse_map_avail
= 0;
1288 if (!ISDIGIT (*arg
))
1290 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1296 u
= strtoumax (arg
, &delim
, 10);
1300 if (!(u
== e
.offset
&& errno
!= ERANGE
))
1302 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1309 if (!(u
== e
.numbytes
&& errno
!= ERANGE
))
1311 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (size_t));
1314 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1315 st
->sparse_map
[st
->sparse_map_avail
++] = e
;
1318 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1328 else if (*delim
!= ',')
1331 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1341 _("Malformed extended header: invalid %s: odd number of values"),
1346 dumpdir_coder (struct tar_stat_info
const *st
, char const *keyword
,
1347 struct xheader
*xhdr
, void const *data
)
1349 xheader_print_n (xhdr
, keyword
, data
, dumpdir_size (data
));
1353 dumpdir_decoder (struct tar_stat_info
*st
,
1354 char const *keyword
__attribute__((unused
)),
1358 st
->dumpdir
= xmalloc (size
);
1359 memcpy (st
->dumpdir
, arg
, size
);
1363 volume_label_coder (struct tar_stat_info
const *st
, char const *keyword
,
1364 struct xheader
*xhdr
, void const *data
)
1366 code_string (data
, keyword
, xhdr
);
1370 volume_label_decoder (struct tar_stat_info
*st
,
1371 char const *keyword
__attribute__((unused
)),
1373 size_t size
__attribute__((unused
)))
1375 decode_string (&volume_label
, arg
);
1379 volume_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1380 struct xheader
*xhdr
, void const *data
)
1382 off_t
const *v
= data
;
1383 code_num (*v
, keyword
, xhdr
);
1387 volume_size_decoder (struct tar_stat_info
*st
,
1388 char const *keyword
,
1389 char const *arg
, size_t size
)
1392 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1393 continued_file_size
= u
;
1396 /* FIXME: Merge with volume_size_coder */
1398 volume_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1399 struct xheader
*xhdr
, void const *data
)
1401 off_t
const *v
= data
;
1402 code_num (*v
, keyword
, xhdr
);
1406 volume_offset_decoder (struct tar_stat_info
*st
,
1407 char const *keyword
,
1408 char const *arg
, size_t size
)
1411 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1412 continued_file_offset
= u
;
1416 volume_filename_decoder (struct tar_stat_info
*st
,
1417 char const *keyword
__attribute__((unused
)),
1419 size_t size
__attribute__((unused
)))
1421 decode_string (&continued_file_name
, arg
);
1425 sparse_major_coder (struct tar_stat_info
const *st
, char const *keyword
,
1426 struct xheader
*xhdr
, void const *data
)
1428 code_num (st
->sparse_major
, keyword
, xhdr
);
1432 sparse_major_decoder (struct tar_stat_info
*st
,
1433 char const *keyword
,
1438 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1439 st
->sparse_major
= u
;
1443 sparse_minor_coder (struct tar_stat_info
const *st
, char const *keyword
,
1444 struct xheader
*xhdr
, void const *data
)
1446 code_num (st
->sparse_minor
, keyword
, xhdr
);
1450 sparse_minor_decoder (struct tar_stat_info
*st
,
1451 char const *keyword
,
1456 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1457 st
->sparse_minor
= u
;
1460 struct xhdr_tab
const xhdr_tab
[] = {
1461 { "atime", atime_coder
, atime_decoder
, false },
1462 { "comment", dummy_coder
, dummy_decoder
, false },
1463 { "charset", dummy_coder
, dummy_decoder
, false },
1464 { "ctime", ctime_coder
, ctime_decoder
, false },
1465 { "gid", gid_coder
, gid_decoder
, false },
1466 { "gname", gname_coder
, gname_decoder
, false },
1467 { "linkpath", linkpath_coder
, linkpath_decoder
, false },
1468 { "mtime", mtime_coder
, mtime_decoder
, false },
1469 { "path", path_coder
, path_decoder
, false },
1470 { "size", size_coder
, size_decoder
, false },
1471 { "uid", uid_coder
, uid_decoder
, false },
1472 { "uname", uname_coder
, uname_decoder
, false },
1474 /* Sparse file handling */
1475 { "GNU.sparse.name", path_coder
, path_decoder
,
1477 { "GNU.sparse.major", sparse_major_coder
, sparse_major_decoder
,
1479 { "GNU.sparse.minor", sparse_minor_coder
, sparse_minor_decoder
,
1481 { "GNU.sparse.realsize", sparse_size_coder
, sparse_size_decoder
,
1483 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
1486 /* tar 1.14 - 1.15.90 keywords. */
1487 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
, true },
1488 /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
1489 headers, and each of them was meaningful. It confilcted with POSIX specs,
1490 which requires that "when extended header records conflict, the last one
1491 given in the header shall take precedence." */
1492 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
1494 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
1496 /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
1497 { "GNU.sparse.map", NULL
/* Unused, see pax_dump_header() */,
1498 sparse_map_decoder
, false },
1500 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
,
1503 /* Keeps the tape/volume label. May be present only in the global headers.
1504 Equivalent to GNUTYPE_VOLHDR. */
1505 { "GNU.volume.label", volume_label_coder
, volume_label_decoder
, true },
1507 /* These may be present in a first global header of the archive.
1508 They provide the same functionality as GNUTYPE_MULTIVOL header.
1509 The GNU.volume.size keeps the real_s_sizeleft value, which is
1510 otherwise kept in the size field of a multivolume header. The
1511 GNU.volume.offset keeps the offset of the start of this volume,
1512 otherwise kept in oldgnu_header.offset. */
1513 { "GNU.volume.filename", volume_label_coder
, volume_filename_decoder
,
1515 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
, true },
1516 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
, true },
1518 { NULL
, NULL
, NULL
, false }