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 WARN((0, 0, _("Ignoring unknown extended header keyword `%s'"),
579 xheader_decode (struct tar_stat_info
*st
)
581 run_override_list (keyword_global_override_list
, st
);
582 run_override_list (global_header_override_list
, st
);
586 char *p
= st
->xhdr
.buffer
+ BLOCKSIZE
;
587 while (decode_record (&st
->xhdr
, &p
, decx
, st
))
590 run_override_list (keyword_override_list
, st
);
594 decg (void *data
, char const *keyword
, char const *value
,
595 size_t size
__attribute__((unused
)))
597 struct keyword_list
**kwl
= data
;
598 xheader_list_append (kwl
, keyword
, value
);
602 xheader_decode_global (struct xheader
*xhdr
)
606 char *p
= xhdr
->buffer
+ BLOCKSIZE
;
608 xheader_list_destroy (&global_header_override_list
);
609 while (decode_record (xhdr
, &p
, decg
, &global_header_override_list
))
615 xheader_init (struct xheader
*xhdr
)
619 xhdr
->stk
= xmalloc (sizeof *xhdr
->stk
);
620 obstack_init (xhdr
->stk
);
625 xheader_store (char const *keyword
, struct tar_stat_info
*st
,
628 struct xhdr_tab
const *t
;
632 t
= locate_handler (keyword
);
635 if (xheader_keyword_deleted_p (keyword
)
636 || xheader_keyword_override_p (keyword
))
638 xheader_init (&st
->xhdr
);
639 t
->coder (st
, keyword
, &st
->xhdr
, data
);
643 xheader_read (struct xheader
*xhdr
, union block
*p
, size_t size
)
650 xhdr
->buffer
= xmalloc (size
+ 1);
651 xhdr
->buffer
[size
] = '\0';
660 memcpy (&xhdr
->buffer
[j
], p
->buffer
, len
);
661 set_next_block_after (p
);
663 p
= find_next_block ();
672 xheader_print_n (struct xheader
*xhdr
, char const *keyword
,
673 char const *value
, size_t vsize
)
675 size_t len
= strlen (keyword
) + vsize
+ 3; /* ' ' + '=' + '\n' */
678 char nbuf
[UINTMAX_STRSIZE_BOUND
];
684 np
= umaxtostr (len
+ p
, nbuf
);
685 n
= nbuf
+ sizeof nbuf
- 1 - np
;
689 x_obstack_grow (xhdr
, np
, n
);
690 x_obstack_1grow (xhdr
, ' ');
691 x_obstack_grow (xhdr
, keyword
, strlen (keyword
));
692 x_obstack_1grow (xhdr
, '=');
693 x_obstack_grow (xhdr
, value
, vsize
);
694 x_obstack_1grow (xhdr
, '\n');
698 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
700 xheader_print_n (xhdr
, keyword
, value
, strlen (value
));
704 xheader_finish (struct xheader
*xhdr
)
706 struct keyword_list
*kp
;
708 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
709 code_string (kp
->value
, kp
->pattern
, xhdr
);
711 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
715 xheader_destroy (struct xheader
*xhdr
)
719 obstack_free (xhdr
->stk
, NULL
);
730 /* Buildable strings */
733 xheader_string_begin (struct xheader
*xhdr
)
735 xhdr
->string_length
= 0;
739 xheader_string_add (struct xheader
*xhdr
, char const *s
)
744 xhdr
->string_length
+= strlen (s
);
745 x_obstack_grow (xhdr
, s
, strlen (s
));
749 xheader_string_end (struct xheader
*xhdr
, char const *keyword
)
755 char nbuf
[UINTMAX_STRSIZE_BOUND
];
763 len
= strlen (keyword
) + xhdr
->string_length
+ 3; /* ' ' + '=' + '\n' */
768 np
= umaxtostr (len
+ p
, nbuf
);
769 n
= nbuf
+ sizeof nbuf
- 1 - np
;
773 p
= strlen (keyword
) + n
+ 2;
778 _("Generated keyword/value pair is too long (keyword=%s, length=%s)"),
780 obstack_free (xhdr
->stk
, obstack_finish (xhdr
->stk
));
783 x_obstack_blank (xhdr
, p
);
784 x_obstack_1grow (xhdr
, '\n');
785 cp
= obstack_next_free (xhdr
->stk
) - xhdr
->string_length
- p
- 1;
786 memmove (cp
+ p
, cp
, xhdr
->string_length
);
787 cp
= stpcpy (cp
, np
);
789 cp
= stpcpy (cp
, keyword
);
795 /* Implementations */
798 out_of_range_header (char const *keyword
, char const *value
,
799 uintmax_t minus_minval
, uintmax_t maxval
)
801 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
802 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
803 char *minval_string
= umaxtostr (minus_minval
, minval_buf
+ 1);
804 char *maxval_string
= umaxtostr (maxval
, maxval_buf
);
806 *--minval_string
= '-';
808 /* TRANSLATORS: The first %s is the pax extended header keyword
809 (atime, gid, etc.). */
810 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
811 keyword
, value
, minval_string
, maxval_string
));
815 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
818 if (!utf8_convert (true, string
, &outstr
))
820 /* FIXME: report error */
821 outstr
= xstrdup (string
);
823 xheader_print (xhdr
, keyword
, outstr
);
828 decode_string (char **string
, char const *arg
)
835 if (!utf8_convert (false, arg
, string
))
837 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
838 assign_string (string
, arg
);
843 code_time (struct timespec t
, char const *keyword
, struct xheader
*xhdr
)
845 char buf
[TIMESPEC_STRSIZE_BOUND
];
846 xheader_print (xhdr
, keyword
, code_timespec (t
, buf
));
849 enum decode_time_status
853 decode_time_bad_header
856 static enum decode_time_status
857 _decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
860 unsigned long int ns
= 0;
863 bool negative
= *arg
== '-';
867 if (ISDIGIT (arg
[negative
]))
871 intmax_t i
= strtoimax (arg
, &arg_lim
, 10);
872 if (TYPE_SIGNED (time_t) ? i
< TYPE_MINIMUM (time_t) : i
< 0)
873 return decode_time_range
;
878 uintmax_t i
= strtoumax (arg
, &arg_lim
, 10);
879 if (TYPE_MAXIMUM (time_t) < i
)
880 return decode_time_range
;
887 return decode_time_range
;
892 bool trailing_nonzero
= false;
894 while (ISDIGIT (*++p
))
895 if (digits
< LOG10_BILLION
)
897 ns
= 10 * ns
+ (*p
- '0');
901 trailing_nonzero
|= *p
!= '0';
903 while (digits
++ < LOG10_BILLION
)
908 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
909 I.e., truncate time stamps towards minus infinity while
910 converting them to internal form. */
911 ns
+= trailing_nonzero
;
914 if (s
== TYPE_MINIMUM (time_t))
915 return decode_time_range
;
926 return decode_time_success
;
930 return decode_time_bad_header
;
934 decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
936 switch (_decode_time (ts
, arg
, keyword
))
938 case decode_time_success
:
940 case decode_time_bad_header
:
941 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
944 case decode_time_range
:
945 out_of_range_header (keyword
, arg
, - (uintmax_t) TYPE_MINIMUM (time_t),
946 TYPE_MAXIMUM (time_t));
955 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
957 char sbuf
[UINTMAX_STRSIZE_BOUND
];
958 xheader_print (xhdr
, keyword
, umaxtostr (value
, sbuf
));
962 decode_num (uintmax_t *num
, char const *arg
, uintmax_t maxval
,
968 if (! (ISDIGIT (*arg
)
969 && (errno
= 0, u
= strtoumax (arg
, &arg_lim
, 10), !*arg_lim
)))
971 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
976 if (! (u
<= maxval
&& errno
!= ERANGE
))
978 out_of_range_header (keyword
, arg
, 0, maxval
);
987 dummy_coder (struct tar_stat_info
const *st
__attribute__ ((unused
)),
988 char const *keyword
__attribute__ ((unused
)),
989 struct xheader
*xhdr
__attribute__ ((unused
)),
990 void const *data
__attribute__ ((unused
)))
995 dummy_decoder (struct tar_stat_info
*st
__attribute__ ((unused
)),
996 char const *keyword
__attribute__ ((unused
)),
997 char const *arg
__attribute__ ((unused
)),
998 size_t size
__attribute__((unused
)))
1003 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1004 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1006 code_time (st
->atime
, keyword
, xhdr
);
1010 atime_decoder (struct tar_stat_info
*st
,
1011 char const *keyword
,
1013 size_t size
__attribute__((unused
)))
1016 if (decode_time (&ts
, arg
, keyword
))
1021 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1022 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1024 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
1028 gid_decoder (struct tar_stat_info
*st
,
1029 char const *keyword
,
1031 size_t size
__attribute__((unused
)))
1034 if (decode_num (&u
, arg
, TYPE_MAXIMUM (gid_t
), keyword
))
1035 st
->stat
.st_gid
= u
;
1039 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1040 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1042 code_string (st
->gname
, keyword
, xhdr
);
1046 gname_decoder (struct tar_stat_info
*st
,
1047 char const *keyword
__attribute__((unused
)),
1049 size_t size
__attribute__((unused
)))
1051 decode_string (&st
->gname
, arg
);
1055 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
1056 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1058 code_string (st
->link_name
, keyword
, xhdr
);
1062 linkpath_decoder (struct tar_stat_info
*st
,
1063 char const *keyword
__attribute__((unused
)),
1065 size_t size
__attribute__((unused
)))
1067 decode_string (&st
->link_name
, arg
);
1071 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1072 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1074 code_time (st
->ctime
, keyword
, xhdr
);
1078 ctime_decoder (struct tar_stat_info
*st
,
1079 char const *keyword
,
1081 size_t size
__attribute__((unused
)))
1084 if (decode_time (&ts
, arg
, keyword
))
1089 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1090 struct xheader
*xhdr
, void const *data
)
1092 struct timespec
const *mtime
= data
;
1093 code_time (mtime
? *mtime
: st
->mtime
, keyword
, xhdr
);
1097 mtime_decoder (struct tar_stat_info
*st
,
1098 char const *keyword
,
1100 size_t size
__attribute__((unused
)))
1103 if (decode_time (&ts
, arg
, keyword
))
1108 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
1109 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1111 code_string (st
->file_name
, keyword
, xhdr
);
1115 path_decoder (struct tar_stat_info
*st
,
1116 char const *keyword
__attribute__((unused
)),
1118 size_t size
__attribute__((unused
)))
1120 decode_string (&st
->orig_file_name
, arg
);
1121 decode_string (&st
->file_name
, arg
);
1122 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
1126 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1127 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1129 code_num (st
->stat
.st_size
, keyword
, xhdr
);
1133 size_decoder (struct tar_stat_info
*st
,
1134 char const *keyword
,
1136 size_t size
__attribute__((unused
)))
1139 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1140 st
->stat
.st_size
= u
;
1144 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1145 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1147 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
1151 uid_decoder (struct tar_stat_info
*st
,
1152 char const *keyword
,
1154 size_t size
__attribute__((unused
)))
1157 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uid_t
), keyword
))
1158 st
->stat
.st_uid
= u
;
1162 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1163 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1165 code_string (st
->uname
, keyword
, xhdr
);
1169 uname_decoder (struct tar_stat_info
*st
,
1170 char const *keyword
__attribute__((unused
)),
1172 size_t size
__attribute__((unused
)))
1174 decode_string (&st
->uname
, arg
);
1178 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1179 struct xheader
*xhdr
, void const *data
)
1181 size_coder (st
, keyword
, xhdr
, data
);
1185 sparse_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 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
1197 struct xheader
*xhdr
,
1198 void const *data
__attribute__ ((unused
)))
1200 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
1204 sparse_numblocks_decoder (struct tar_stat_info
*st
,
1205 char const *keyword
,
1207 size_t size
__attribute__((unused
)))
1210 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1212 st
->sparse_map_size
= u
;
1213 st
->sparse_map
= xcalloc (u
, sizeof st
->sparse_map
[0]);
1214 st
->sparse_map_avail
= 0;
1219 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1220 struct xheader
*xhdr
, void const *data
)
1222 size_t const *pi
= data
;
1223 code_num (st
->sparse_map
[*pi
].offset
, keyword
, xhdr
);
1227 sparse_offset_decoder (struct tar_stat_info
*st
,
1228 char const *keyword
,
1230 size_t size
__attribute__((unused
)))
1233 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1235 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1236 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
1238 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1239 "GNU.sparse.offset", arg
));
1244 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
1245 struct xheader
*xhdr
, void const *data
)
1247 size_t const *pi
= data
;
1248 code_num (st
->sparse_map
[*pi
].numbytes
, keyword
, xhdr
);
1252 sparse_numbytes_decoder (struct tar_stat_info
*st
,
1253 char const *keyword
,
1255 size_t size
__attribute__((unused
)))
1258 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1260 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1261 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
1263 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1269 sparse_map_decoder (struct tar_stat_info
*st
,
1270 char const *keyword
,
1272 size_t size
__attribute__((unused
)))
1276 st
->sparse_map_avail
= 0;
1283 if (!ISDIGIT (*arg
))
1285 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1291 u
= strtoumax (arg
, &delim
, 10);
1295 if (!(u
== e
.offset
&& errno
!= ERANGE
))
1297 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1304 if (!(u
== e
.numbytes
&& errno
!= ERANGE
))
1306 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (size_t));
1309 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1310 st
->sparse_map
[st
->sparse_map_avail
++] = e
;
1313 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1323 else if (*delim
!= ',')
1326 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1336 _("Malformed extended header: invalid %s: odd number of values"),
1341 dumpdir_coder (struct tar_stat_info
const *st
, char const *keyword
,
1342 struct xheader
*xhdr
, void const *data
)
1344 xheader_print_n (xhdr
, keyword
, data
, dumpdir_size (data
));
1348 dumpdir_decoder (struct tar_stat_info
*st
,
1349 char const *keyword
__attribute__((unused
)),
1353 st
->dumpdir
= xmalloc (size
);
1354 memcpy (st
->dumpdir
, arg
, size
);
1358 volume_label_coder (struct tar_stat_info
const *st
, char const *keyword
,
1359 struct xheader
*xhdr
, void const *data
)
1361 code_string (data
, keyword
, xhdr
);
1365 volume_label_decoder (struct tar_stat_info
*st
,
1366 char const *keyword
__attribute__((unused
)),
1368 size_t size
__attribute__((unused
)))
1370 decode_string (&volume_label
, arg
);
1374 volume_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1375 struct xheader
*xhdr
, void const *data
)
1377 off_t
const *v
= data
;
1378 code_num (*v
, keyword
, xhdr
);
1382 volume_size_decoder (struct tar_stat_info
*st
,
1383 char const *keyword
,
1384 char const *arg
, size_t size
)
1387 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1388 continued_file_size
= u
;
1391 /* FIXME: Merge with volume_size_coder */
1393 volume_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1394 struct xheader
*xhdr
, void const *data
)
1396 off_t
const *v
= data
;
1397 code_num (*v
, keyword
, xhdr
);
1401 volume_offset_decoder (struct tar_stat_info
*st
,
1402 char const *keyword
,
1403 char const *arg
, size_t size
)
1406 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1407 continued_file_offset
= u
;
1411 volume_filename_decoder (struct tar_stat_info
*st
,
1412 char const *keyword
__attribute__((unused
)),
1414 size_t size
__attribute__((unused
)))
1416 decode_string (&continued_file_name
, arg
);
1420 sparse_major_coder (struct tar_stat_info
const *st
, char const *keyword
,
1421 struct xheader
*xhdr
, void const *data
)
1423 code_num (st
->sparse_major
, keyword
, xhdr
);
1427 sparse_major_decoder (struct tar_stat_info
*st
,
1428 char const *keyword
,
1433 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1434 st
->sparse_major
= u
;
1438 sparse_minor_coder (struct tar_stat_info
const *st
, char const *keyword
,
1439 struct xheader
*xhdr
, void const *data
)
1441 code_num (st
->sparse_minor
, keyword
, xhdr
);
1445 sparse_minor_decoder (struct tar_stat_info
*st
,
1446 char const *keyword
,
1451 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1452 st
->sparse_minor
= u
;
1455 struct xhdr_tab
const xhdr_tab
[] = {
1456 { "atime", atime_coder
, atime_decoder
, false },
1457 { "comment", dummy_coder
, dummy_decoder
, false },
1458 { "charset", dummy_coder
, dummy_decoder
, false },
1459 { "ctime", ctime_coder
, ctime_decoder
, false },
1460 { "gid", gid_coder
, gid_decoder
, false },
1461 { "gname", gname_coder
, gname_decoder
, false },
1462 { "linkpath", linkpath_coder
, linkpath_decoder
, false },
1463 { "mtime", mtime_coder
, mtime_decoder
, false },
1464 { "path", path_coder
, path_decoder
, false },
1465 { "size", size_coder
, size_decoder
, false },
1466 { "uid", uid_coder
, uid_decoder
, false },
1467 { "uname", uname_coder
, uname_decoder
, false },
1469 /* Sparse file handling */
1470 { "GNU.sparse.name", path_coder
, path_decoder
,
1472 { "GNU.sparse.major", sparse_major_coder
, sparse_major_decoder
,
1474 { "GNU.sparse.minor", sparse_minor_coder
, sparse_minor_decoder
,
1476 { "GNU.sparse.realsize", sparse_size_coder
, sparse_size_decoder
,
1478 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
1481 /* tar 1.14 - 1.15.90 keywords. */
1482 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
, true },
1483 /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
1484 headers, and each of them was meaningful. It confilcted with POSIX specs,
1485 which requires that "when extended header records conflict, the last one
1486 given in the header shall take precedence." */
1487 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
1489 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
1491 /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
1492 { "GNU.sparse.map", NULL
/* Unused, see pax_dump_header() */,
1493 sparse_map_decoder
, false },
1495 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
,
1498 /* Keeps the tape/volume label. May be present only in the global headers.
1499 Equivalent to GNUTYPE_VOLHDR. */
1500 { "GNU.volume.label", volume_label_coder
, volume_label_decoder
, true },
1502 /* These may be present in a first global header of the archive.
1503 They provide the same functionality as GNUTYPE_MULTIVOL header.
1504 The GNU.volume.size keeps the real_s_sizeleft value, which is
1505 otherwise kept in the size field of a multivolume header. The
1506 GNU.volume.offset keeps the offset of the start of this volume,
1507 otherwise kept in oldgnu_header.offset. */
1508 { "GNU.volume.filename", volume_label_coder
, volume_filename_decoder
,
1510 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
, true },
1511 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
, true },
1513 { NULL
, NULL
, NULL
, false }