1 /* POSIX extended headers for tar.
3 Copyright (C) 2003-2020 Free Software Foundation, Inc.
5 This file is part of GNU tar.
7 GNU tar is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU tar is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 static void xheader_init (struct xheader
*xhdr
);
30 static bool xheader_protected_pattern_p (char const *pattern
);
31 static bool xheader_protected_keyword_p (char const *keyword
);
32 static void xheader_set_single_keyword (char *) __attribute__ ((noreturn
));
34 /* Used by xheader_finish() */
35 static void code_string (char const *string
, char const *keyword
,
36 struct xheader
*xhdr
);
38 /* Number of global headers written so far. */
39 static size_t global_header_count
;
40 /* FIXME: Possibly it should be reset after changing the volume.
41 POSIX %n specification says that it is expanded to the sequence
42 number of current global header in *the* archive. However, for
43 multi-volume archives this will yield duplicate header names
44 in different volumes, which I'd like to avoid. The best way
45 to solve this would be to use per-archive header count as required
46 by POSIX *and* set globexthdr.name to, say,
47 $TMPDIR/GlobalHead.%p.$NUMVOLUME.%n.
49 However it should wait until buffer.c is finally rewritten */
52 /* Interface functions to obstacks */
55 x_obstack_grow (struct xheader
*xhdr
, const char *ptr
, size_t length
)
57 obstack_grow (xhdr
->stk
, ptr
, length
);
62 x_obstack_1grow (struct xheader
*xhdr
, char c
)
64 obstack_1grow (xhdr
->stk
, c
);
69 x_obstack_blank (struct xheader
*xhdr
, size_t length
)
71 obstack_blank (xhdr
->stk
, length
);
80 struct keyword_list
*next
;
86 /* List of keyword patterns set by delete= option */
87 static struct keyword_list
*keyword_pattern_list
;
89 /* List of keyword/value pairs set by 'keyword=value' option */
90 static struct keyword_list
*keyword_global_override_list
;
92 /* List of keyword/value pairs set by 'keyword:=value' option */
93 static struct keyword_list
*keyword_override_list
;
95 /* List of keyword/value pairs decoded from the last 'g' type header */
96 static struct keyword_list
*global_header_override_list
;
98 /* Template for the name field of an 'x' type header */
99 static char *exthdr_name
;
101 static char *exthdr_mtime_option
;
102 static time_t exthdr_mtime
;
104 /* Template for the name field of a 'g' type header */
105 static char *globexthdr_name
;
107 static char *globexthdr_mtime_option
;
108 static time_t globexthdr_mtime
;
111 xheader_keyword_deleted_p (const char *kw
)
113 struct keyword_list
*kp
;
115 for (kp
= keyword_pattern_list
; kp
; kp
= kp
->next
)
116 if (fnmatch (kp
->pattern
, kw
, 0) == 0)
122 xheader_keyword_override_p (const char *keyword
)
124 struct keyword_list
*kp
;
126 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
127 if (strcmp (kp
->pattern
, keyword
) == 0)
133 xheader_list_append (struct keyword_list
**root
, char const *kw
,
136 struct keyword_list
*kp
= xmalloc (sizeof *kp
);
137 kp
->pattern
= xstrdup (kw
);
138 kp
->value
= value
? xstrdup (value
) : NULL
;
144 xheader_list_destroy (struct keyword_list
**root
)
148 struct keyword_list
*kw
= *root
;
151 struct keyword_list
*next
= kw
->next
;
162 xheader_set_single_keyword (char *kw
)
164 USAGE_ERROR ((0, 0, _("Keyword %s is unknown or not yet implemented"), kw
));
168 assign_time_option (char **sval
, time_t *tval
, const char *input
)
171 struct timespec t
= decode_timespec (input
, &p
, false);
172 if (! valid_timespec (t
) || *p
)
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
)
188 USAGE_ERROR ((0, 0, _("Malformed pax option: %s"), quote (kw
)));
196 while (p
> kw
&& isspace ((unsigned char) *p
))
201 for (p
= eq
+ 1; *p
&& isspace ((unsigned char) *p
); p
++)
204 if (strcmp (kw
, "delete") == 0)
206 if (xheader_protected_pattern_p (p
))
207 USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), quote (p
)));
208 xheader_list_append (&keyword_pattern_list
, p
, NULL
);
210 else if (strcmp (kw
, "exthdr.name") == 0)
211 assign_string (&exthdr_name
, p
);
212 else if (strcmp (kw
, "globexthdr.name") == 0)
213 assign_string (&globexthdr_name
, p
);
214 else if (strcmp (kw
, "exthdr.mtime") == 0)
215 assign_time_option (&exthdr_mtime_option
, &exthdr_mtime
, p
);
216 else if (strcmp (kw
, "globexthdr.mtime") == 0)
217 assign_time_option (&globexthdr_mtime_option
, &globexthdr_mtime
, p
);
220 if (xheader_protected_keyword_p (kw
))
221 USAGE_ERROR ((0, 0, _("Keyword %s cannot be overridden"), kw
));
223 xheader_list_append (&keyword_global_override_list
, kw
, p
);
225 xheader_list_append (&keyword_override_list
, kw
, p
);
230 xheader_set_option (char *string
)
233 for (token
= strtok (string
, ","); token
; token
= strtok (NULL
, ","))
235 char *p
= strchr (token
, '=');
237 xheader_set_single_keyword (token
);
239 xheader_set_keyword_equal (token
, p
);
244 string Includes: Replaced By:
245 %d The directory name of the file,
246 equivalent to the result of the
247 dirname utility on the translated
249 %f The filename of the file, equivalent
250 to the result of the basename
251 utility on the translated file name.
252 %p The process ID of the pax process.
253 %n The value of the 3rd argument.
254 %% A '%' character. */
257 xheader_format_name (struct tar_stat_info
*st
, const char *fmt
, size_t n
)
266 char pidbuf
[UINTMAX_STRSIZE_BOUND
];
267 char const *pptr
= NULL
;
268 char nbuf
[UINTMAX_STRSIZE_BOUND
];
269 char const *nptr
= NULL
;
272 for (p
= fmt
; *p
; p
++)
274 if (*p
== '%' && p
[1])
286 dirp
= dir_name (st
->orig_file_name
);
287 dir
= safer_name_suffix (dirp
, false, absolute_names_option
);
295 base
= last_component (st
->orig_file_name
);
296 len
+= strlen (base
);
301 pptr
= umaxtostr (getpid (), pidbuf
);
302 len
+= pidbuf
+ sizeof pidbuf
- 1 - pptr
;
306 nptr
= umaxtostr (n
, nbuf
);
307 len
+= nbuf
+ sizeof nbuf
- 1 - nptr
;
318 buf
= xmalloc (len
+ 1);
319 for (q
= buf
, p
= fmt
; *p
; )
338 q
= stpcpy (q
, base
);
343 q
= stpcpy (q
, pptr
);
348 q
= stpcpy (q
, nptr
);
365 /* Do not allow it to end in a slash */
366 while (q
> buf
&& ISSLASH (q
[-1]))
372 /* Table of templates for the names of POSIX extended headers.
373 Indexed by the the type of the header (per-file or global)
374 and POSIX compliance mode (0 or q depending on whether
375 POSIXLY_CORRECT environment variable is set. */
376 static const char *header_template
[][2] = {
377 /* Individual header templates: */
378 { "%d/PaxHeaders/%f", "%d/PaxHeaders.%p/%f" },
379 /* Global header templates: */
380 { "/GlobalHead.%n", "/GlobalHead.%p.%n" }
382 /* Indices to the above table */
387 /* Return the name for the POSIX extended header T */
388 #define HEADER_TEMPLATE(t) header_template[t][posixly_correct]
391 xheader_xhdr_name (struct tar_stat_info
*st
)
394 assign_string (&exthdr_name
, HEADER_TEMPLATE (pax_file_header
));
395 return xheader_format_name (st
, exthdr_name
, 0);
399 xheader_ghdr_name (void)
401 if (!globexthdr_name
)
404 const char *global_header_template
= HEADER_TEMPLATE (pax_global_header
);
405 const char *tmp
= getenv ("TMPDIR");
408 len
= strlen (tmp
) + strlen (global_header_template
) + 1;
409 globexthdr_name
= xmalloc (len
);
410 strcpy(globexthdr_name
, tmp
);
411 strcat(globexthdr_name
, global_header_template
);
414 return xheader_format_name (NULL
, globexthdr_name
, global_header_count
+ 1);
418 xheader_write (char type
, char *name
, time_t t
, struct xheader
*xhdr
)
428 if (globexthdr_mtime_option
)
429 t
= globexthdr_mtime
;
433 if (exthdr_mtime_option
)
437 header
= start_private_header (name
, size
, t
);
438 header
->header
.typeflag
= type
;
440 simple_finish_header (header
);
448 header
= find_next_block ();
452 memcpy (header
->buffer
, p
, len
);
454 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
457 set_next_block_after (header
);
460 xheader_destroy (xhdr
);
463 global_header_count
++;
467 xheader_write_global (struct xheader
*xhdr
)
469 if (keyword_global_override_list
)
471 struct keyword_list
*kp
;
474 for (kp
= keyword_global_override_list
; kp
; kp
= kp
->next
)
475 code_string (kp
->value
, kp
->pattern
, xhdr
);
481 xheader_finish (xhdr
);
482 name
= xheader_ghdr_name ();
483 xheader_write (XGLTYPE
, name
, start_time
.tv_sec
, xhdr
);
488 /* Forbid modifications of the global extended header */
490 xheader_forbid_global (void)
492 if (keyword_global_override_list
)
493 USAGE_ERROR ((0, 0, _("can't update global extended header record")));
497 xheader_xattr_init (struct tar_stat_info
*st
)
499 st
->xattr_map
= NULL
;
500 st
->xattr_map_size
= 0;
502 st
->acls_a_ptr
= NULL
;
504 st
->acls_d_ptr
= NULL
;
506 st
->cntx_name
= NULL
;
510 xheader_xattr_free (struct xattr_array
*xattr_map
, size_t xattr_map_size
)
514 while (scan
< xattr_map_size
)
516 free (xattr_map
[scan
].xkey
);
517 free (xattr_map
[scan
].xval_ptr
);
525 xheader_xattr__add (struct xattr_array
**xattr_map
,
526 size_t *xattr_map_size
,
527 const char *key
, const char *val
, size_t len
)
529 size_t pos
= (*xattr_map_size
)++;
531 *xattr_map
= xrealloc (*xattr_map
,
532 *xattr_map_size
* sizeof(struct xattr_array
));
533 (*xattr_map
)[pos
].xkey
= xstrdup (key
);
534 (*xattr_map
)[pos
].xval_ptr
= xmemdup (val
, len
+ 1);
535 (*xattr_map
)[pos
].xval_len
= len
;
538 /* This is reversal function for xattr_encode_keyword. See comment for
539 xattr_encode_keyword() for more info. */
541 xattr_decode_keyword (char *keyword
)
543 char *kpr
, *kpl
; /* keyword pointer left/right */
550 if (kpr
[1] == '3' && kpr
[2] == 'D')
557 else if (kpr
[1] == '2' && kpr
[2] == '5')
577 xheader_xattr_add (struct tar_stat_info
*st
,
578 const char *key
, const char *val
, size_t len
)
580 size_t klen
= strlen (key
);
581 char *xkey
= xmalloc (strlen("SCHILY.xattr.") + klen
+ 1);
584 tmp
= stpcpy (tmp
, "SCHILY.xattr.");
587 xheader_xattr__add (&st
->xattr_map
, &st
->xattr_map_size
, xkey
, val
, len
);
593 xheader_xattr_copy (const struct tar_stat_info
*st
,
594 struct xattr_array
**xattr_map
, size_t *xattr_map_size
)
601 while (scan
< st
->xattr_map_size
)
603 char *key
= st
->xattr_map
[scan
].xkey
;
604 char *val
= st
->xattr_map
[scan
].xval_ptr
;
605 size_t len
= st
->xattr_map
[scan
].xval_len
;
607 xheader_xattr__add(xattr_map
, xattr_map_size
, key
, val
, len
);
614 /* General Interface */
616 #define XHDR_PROTECTED 0x01
617 #define XHDR_GLOBAL 0x02
622 void (*coder
) (struct tar_stat_info
const *, char const *,
623 struct xheader
*, void const *data
);
624 void (*decoder
) (struct tar_stat_info
*, char const *, char const *, size_t);
626 bool prefix
; /* select handler comparing prefix only */
629 /* This declaration must be extern, because ISO C99 section 6.9.2
630 prohibits a tentative definition that has both internal linkage and
631 incomplete type. If we made it static, we'd have to declare its
632 size which would be a maintenance pain; if we put its initializer
633 here, we'd need a boatload of forward declarations, which would be
634 even more of a pain. */
635 extern struct xhdr_tab
const xhdr_tab
[];
637 static struct xhdr_tab
const *
638 locate_handler (char const *keyword
)
640 struct xhdr_tab
const *p
;
642 for (p
= xhdr_tab
; p
->keyword
; p
++)
645 if (strncmp (p
->keyword
, keyword
, strlen(p
->keyword
)) == 0)
650 if (strcmp (p
->keyword
, keyword
) == 0)
658 xheader_protected_pattern_p (const char *pattern
)
660 struct xhdr_tab
const *p
;
662 for (p
= xhdr_tab
; p
->keyword
; p
++)
663 if (!p
->prefix
&& (p
->flags
& XHDR_PROTECTED
)
664 && fnmatch (pattern
, p
->keyword
, 0) == 0)
670 xheader_protected_keyword_p (const char *keyword
)
672 struct xhdr_tab
const *p
;
674 for (p
= xhdr_tab
; p
->keyword
; p
++)
675 if (!p
->prefix
&& (p
->flags
& XHDR_PROTECTED
)
676 && strcmp (p
->keyword
, keyword
) == 0)
681 /* Decode a single extended header record, advancing *PTR to the next record.
682 Return true on success, false otherwise. */
684 decode_record (struct xheader
*xhdr
,
686 void (*handler
) (void *, char const *, char const *, size_t),
695 size_t len_max
= xhdr
->buffer
+ xhdr
->size
- start
;
697 while (*p
== ' ' || *p
== '\t')
703 ERROR ((0, 0, _("Malformed extended header: missing length")));
707 len
= strtoumax (p
, &len_lim
, 10);
711 int len_len
= len_lim
- p
;
712 ERROR ((0, 0, _("Extended header length %*s is out of range"),
719 for (p
= len_lim
; *p
== ' ' || *p
== '\t'; p
++)
724 _("Malformed extended header: missing blank after length")));
730 if (! (p
&& p
< nextp
))
732 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
736 if (nextp
[-1] != '\n')
738 ERROR ((0, 0, _("Malformed extended header: missing newline")));
742 *p
= nextp
[-1] = '\0';
743 handler (data
, keyword
, p
+ 1, nextp
- p
- 2); /* '=' + trailing '\n' */
751 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
753 for (; kp
; kp
= kp
->next
)
755 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
757 t
->decoder (st
, t
->keyword
, kp
->value
, strlen (kp
->value
));
762 decx (void *data
, char const *keyword
, char const *value
, size_t size
)
764 struct xhdr_tab
const *t
;
765 struct tar_stat_info
*st
= data
;
767 if (xheader_keyword_deleted_p (keyword
)
768 || xheader_keyword_override_p (keyword
))
771 t
= locate_handler (keyword
);
773 t
->decoder (st
, keyword
, value
, size
);
775 WARNOPT (WARN_UNKNOWN_KEYWORD
,
776 (0, 0, _("Ignoring unknown extended header keyword '%s'"),
781 xheader_decode (struct tar_stat_info
*st
)
783 run_override_list (keyword_global_override_list
, st
);
784 run_override_list (global_header_override_list
, st
);
788 char *p
= st
->xhdr
.buffer
+ BLOCKSIZE
;
789 while (decode_record (&st
->xhdr
, &p
, decx
, st
))
792 run_override_list (keyword_override_list
, st
);
794 /* The archived (effective) file size is always set directly in tar header
795 field, possibly overridden by "size" extended header - in both cases,
796 result is now decoded in st->stat.st_size */
797 st
->archive_file_size
= st
->stat
.st_size
;
799 /* The real file size (given by stat()) may be redefined for sparse
800 files in "GNU.sparse.realsize" extended header */
801 if (st
->real_size_set
)
802 st
->stat
.st_size
= st
->real_size
;
806 decg (void *data
, char const *keyword
, char const *value
,
807 size_t size
__attribute__((unused
)))
809 struct keyword_list
**kwl
= data
;
810 struct xhdr_tab
const *tab
= locate_handler (keyword
);
811 if (tab
&& (tab
->flags
& XHDR_GLOBAL
))
812 tab
->decoder (data
, keyword
, value
, size
);
814 xheader_list_append (kwl
, keyword
, value
);
818 xheader_decode_global (struct xheader
*xhdr
)
822 char *p
= xhdr
->buffer
+ BLOCKSIZE
;
824 xheader_list_destroy (&global_header_override_list
);
825 while (decode_record (xhdr
, &p
, decg
, &global_header_override_list
))
831 xheader_init (struct xheader
*xhdr
)
835 xhdr
->stk
= xmalloc (sizeof *xhdr
->stk
);
836 obstack_init (xhdr
->stk
);
841 xheader_store (char const *keyword
, struct tar_stat_info
*st
,
844 struct xhdr_tab
const *t
;
848 t
= locate_handler (keyword
);
851 if (xheader_keyword_deleted_p (keyword
))
853 xheader_init (&st
->xhdr
);
854 if (!xheader_keyword_override_p (keyword
))
855 t
->coder (st
, keyword
, &st
->xhdr
, data
);
859 xheader_read (struct xheader
*xhdr
, union block
*p
, off_t size
)
864 size
= 0; /* Already diagnosed. */
866 if (SIZE_MAX
- BLOCKSIZE
<= size
)
871 xhdr
->buffer
= xmalloc (size
+ 1);
872 xhdr
->buffer
[size
] = '\0';
882 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
884 memcpy (&xhdr
->buffer
[j
], p
->buffer
, len
);
885 set_next_block_after (p
);
887 p
= find_next_block ();
895 /* xattr_encode_keyword() substitutes '=' ~~> '%3D' and '%' ~~> '%25'
896 in extended attribute keywords. This is needed because the '=' character
897 has special purpose in extended attribute header - it splits keyword and
898 value part of header. If there was the '=' occurrence allowed inside
899 keyword, there would be no unambiguous way how to decode this extended
902 (http://lists.gnu.org/archive/html/bug-tar/2012-10/msg00017.html)
905 xattr_encode_keyword(const char *keyword
)
907 static char *encode_buffer
= NULL
;
908 static size_t encode_buffer_size
= 0;
909 size_t bp
; /* keyword/buffer pointers */
913 encode_buffer_size
= 256;
914 encode_buffer
= xmalloc (encode_buffer_size
);
919 for (bp
= 0; *keyword
!= 0; ++bp
, ++keyword
)
923 if (bp
+ 2 /* enough for URL encoding also.. */ >= encode_buffer_size
)
925 encode_buffer
= x2realloc (encode_buffer
, &encode_buffer_size
);
930 strcpy (encode_buffer
+ bp
, "%25");
935 strcpy (encode_buffer
+ bp
, "%3D");
939 encode_buffer
[bp
] = c
;
942 encode_buffer
[bp
] = 0;
944 return encode_buffer
;
948 xheader_print_n (struct xheader
*xhdr
, char const *keyword
,
949 char const *value
, size_t vsize
)
953 char nbuf
[UINTMAX_STRSIZE_BOUND
];
957 keyword
= xattr_encode_keyword (keyword
);
958 klen
= strlen (keyword
);
959 len
= klen
+ vsize
+ 3; /* ' ' + '=' + '\n' */
964 np
= umaxtostr (len
+ p
, nbuf
);
965 n
= nbuf
+ sizeof nbuf
- 1 - np
;
969 x_obstack_grow (xhdr
, np
, n
);
970 x_obstack_1grow (xhdr
, ' ');
971 x_obstack_grow (xhdr
, keyword
, klen
);
972 x_obstack_1grow (xhdr
, '=');
973 x_obstack_grow (xhdr
, value
, vsize
);
974 x_obstack_1grow (xhdr
, '\n');
978 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
980 xheader_print_n (xhdr
, keyword
, value
, strlen (value
));
984 xheader_finish (struct xheader
*xhdr
)
986 struct keyword_list
*kp
;
988 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
989 code_string (kp
->value
, kp
->pattern
, xhdr
);
991 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
995 xheader_destroy (struct xheader
*xhdr
)
999 obstack_free (xhdr
->stk
, NULL
);
1004 free (xhdr
->buffer
);
1010 /* Buildable strings */
1013 xheader_string_begin (struct xheader
*xhdr
)
1015 xhdr
->string_length
= 0;
1019 xheader_string_add (struct xheader
*xhdr
, char const *s
)
1023 xheader_init (xhdr
);
1024 xhdr
->string_length
+= strlen (s
);
1025 x_obstack_grow (xhdr
, s
, strlen (s
));
1029 xheader_string_end (struct xheader
*xhdr
, char const *keyword
)
1035 char nbuf
[UINTMAX_STRSIZE_BOUND
];
1041 xheader_init (xhdr
);
1043 len
= strlen (keyword
) + xhdr
->string_length
+ 3; /* ' ' + '=' + '\n' */
1048 np
= umaxtostr (len
+ p
, nbuf
);
1049 n
= nbuf
+ sizeof nbuf
- 1 - np
;
1053 p
= strlen (keyword
) + n
+ 2;
1058 _("Generated keyword/value pair is too long (keyword=%s, length=%s)"),
1060 obstack_free (xhdr
->stk
, obstack_finish (xhdr
->stk
));
1063 x_obstack_blank (xhdr
, p
);
1064 x_obstack_1grow (xhdr
, '\n');
1065 cp
= (char*) obstack_next_free (xhdr
->stk
) - xhdr
->string_length
- p
- 1;
1066 memmove (cp
+ p
, cp
, xhdr
->string_length
);
1067 cp
= stpcpy (cp
, np
);
1069 cp
= stpcpy (cp
, keyword
);
1075 /* Implementations */
1078 out_of_range_header (char const *keyword
, char const *value
,
1079 intmax_t minval
, uintmax_t maxval
)
1081 char minval_buf
[INT_BUFSIZE_BOUND (intmax_t)];
1082 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
1083 char *minval_string
= imaxtostr (minval
, minval_buf
);
1084 char *maxval_string
= umaxtostr (maxval
, maxval_buf
);
1086 /* TRANSLATORS: The first %s is the pax extended header keyword
1087 (atime, gid, etc.). */
1088 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
1089 keyword
, value
, minval_string
, maxval_string
));
1093 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
1096 if (!utf8_convert (true, string
, &outstr
))
1098 /* FIXME: report error */
1099 outstr
= xstrdup (string
);
1101 xheader_print (xhdr
, keyword
, outstr
);
1106 decode_string (char **string
, char const *arg
)
1113 if (!utf8_convert (false, arg
, string
))
1115 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
1116 assign_string (string
, arg
);
1121 code_time (struct timespec t
, char const *keyword
, struct xheader
*xhdr
)
1123 char buf
[TIMESPEC_STRSIZE_BOUND
];
1124 xheader_print (xhdr
, keyword
, code_timespec (t
, buf
));
1128 decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
1131 struct timespec t
= decode_timespec (arg
, &arg_lim
, true);
1133 if (! valid_timespec (t
))
1135 if (arg
< arg_lim
&& !*arg_lim
)
1136 out_of_range_header (keyword
, arg
, TYPE_MINIMUM (time_t),
1137 TYPE_MAXIMUM (time_t));
1139 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1149 code_signed_num (uintmax_t value
, char const *keyword
,
1150 intmax_t minval
, uintmax_t maxval
, struct xheader
*xhdr
)
1152 char sbuf
[SYSINT_BUFSIZE
];
1153 xheader_print (xhdr
, keyword
, sysinttostr (value
, minval
, maxval
, sbuf
));
1157 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
1159 code_signed_num (value
, keyword
, 0, UINTMAX_MAX
, xhdr
);
1163 decode_signed_num (intmax_t *num
, char const *arg
,
1164 intmax_t minval
, uintmax_t maxval
,
1165 char const *keyword
)
1168 intmax_t u
= strtosysint (arg
, &arg_lim
, minval
, maxval
);
1170 if (errno
== EINVAL
|| *arg_lim
)
1172 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1177 if (errno
== ERANGE
)
1179 out_of_range_header (keyword
, arg
, minval
, maxval
);
1188 decode_num (uintmax_t *num
, char const *arg
, uintmax_t maxval
,
1189 char const *keyword
)
1192 if (! decode_signed_num (&i
, arg
, 0, maxval
, keyword
))
1199 dummy_coder (struct tar_stat_info
const *st
__attribute__ ((unused
)),
1200 char const *keyword
__attribute__ ((unused
)),
1201 struct xheader
*xhdr
__attribute__ ((unused
)),
1202 void const *data
__attribute__ ((unused
)))
1207 dummy_decoder (struct tar_stat_info
*st
__attribute__ ((unused
)),
1208 char const *keyword
__attribute__ ((unused
)),
1209 char const *arg
__attribute__ ((unused
)),
1210 size_t size
__attribute__((unused
)))
1215 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1216 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1218 code_time (st
->atime
, keyword
, xhdr
);
1222 atime_decoder (struct tar_stat_info
*st
,
1223 char const *keyword
,
1225 size_t size
__attribute__((unused
)))
1228 if (decode_time (&ts
, arg
, keyword
))
1233 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1234 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1236 code_signed_num (st
->stat
.st_gid
, keyword
,
1237 TYPE_MINIMUM (gid_t
), TYPE_MAXIMUM (gid_t
), xhdr
);
1241 gid_decoder (struct tar_stat_info
*st
,
1242 char const *keyword
,
1244 size_t size
__attribute__((unused
)))
1247 if (decode_signed_num (&u
, arg
, TYPE_MINIMUM (gid_t
),
1248 TYPE_MAXIMUM (gid_t
), keyword
))
1249 st
->stat
.st_gid
= u
;
1253 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1254 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1256 code_string (st
->gname
, keyword
, xhdr
);
1260 gname_decoder (struct tar_stat_info
*st
,
1261 char const *keyword
__attribute__((unused
)),
1263 size_t size
__attribute__((unused
)))
1265 decode_string (&st
->gname
, arg
);
1269 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
1270 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1272 code_string (st
->link_name
, keyword
, xhdr
);
1276 linkpath_decoder (struct tar_stat_info
*st
,
1277 char const *keyword
__attribute__((unused
)),
1279 size_t size
__attribute__((unused
)))
1281 decode_string (&st
->link_name
, arg
);
1285 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1286 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1288 code_time (st
->ctime
, keyword
, xhdr
);
1292 ctime_decoder (struct tar_stat_info
*st
,
1293 char const *keyword
,
1295 size_t size
__attribute__((unused
)))
1298 if (decode_time (&ts
, arg
, keyword
))
1303 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1304 struct xheader
*xhdr
, void const *data
)
1306 struct timespec
const *mtime
= data
;
1307 code_time (mtime
? *mtime
: st
->mtime
, keyword
, xhdr
);
1311 mtime_decoder (struct tar_stat_info
*st
,
1312 char const *keyword
,
1314 size_t size
__attribute__((unused
)))
1317 if (decode_time (&ts
, arg
, keyword
))
1322 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
1323 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1325 code_string (st
->file_name
, keyword
, xhdr
);
1329 raw_path_decoder (struct tar_stat_info
*st
, char const *arg
)
1331 decode_string (&st
->orig_file_name
, arg
);
1332 decode_string (&st
->file_name
, arg
);
1333 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
1338 path_decoder (struct tar_stat_info
*st
,
1339 char const *keyword
__attribute__((unused
)),
1341 size_t size
__attribute__((unused
)))
1343 if (! st
->sparse_name_done
)
1344 raw_path_decoder (st
, arg
);
1348 sparse_path_decoder (struct tar_stat_info
*st
,
1349 char const *keyword
__attribute__((unused
)),
1351 size_t size
__attribute__((unused
)))
1353 st
->sparse_name_done
= true;
1354 raw_path_decoder (st
, arg
);
1358 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1359 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1361 code_num (st
->stat
.st_size
, keyword
, xhdr
);
1365 size_decoder (struct tar_stat_info
*st
,
1366 char const *keyword
,
1368 size_t size
__attribute__((unused
)))
1371 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1372 st
->stat
.st_size
= u
;
1376 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1377 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1379 code_signed_num (st
->stat
.st_uid
, keyword
,
1380 TYPE_MINIMUM (uid_t
), TYPE_MAXIMUM (uid_t
), xhdr
);
1384 uid_decoder (struct tar_stat_info
*st
,
1385 char const *keyword
,
1387 size_t size
__attribute__((unused
)))
1390 if (decode_signed_num (&u
, arg
, TYPE_MINIMUM (uid_t
),
1391 TYPE_MAXIMUM (uid_t
), keyword
))
1392 st
->stat
.st_uid
= u
;
1396 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1397 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1399 code_string (st
->uname
, keyword
, xhdr
);
1403 uname_decoder (struct tar_stat_info
*st
,
1404 char const *keyword
__attribute__((unused
)),
1406 size_t size
__attribute__((unused
)))
1408 decode_string (&st
->uname
, arg
);
1412 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1413 struct xheader
*xhdr
, void const *data
)
1415 size_coder (st
, keyword
, xhdr
, data
);
1419 sparse_size_decoder (struct tar_stat_info
*st
,
1420 char const *keyword
,
1422 size_t size
__attribute__((unused
)))
1425 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1427 st
->real_size_set
= true;
1433 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
1434 struct xheader
*xhdr
,
1435 void const *data
__attribute__ ((unused
)))
1437 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
1441 sparse_numblocks_decoder (struct tar_stat_info
*st
,
1442 char const *keyword
,
1444 size_t size
__attribute__((unused
)))
1447 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1449 st
->sparse_map_size
= u
;
1450 st
->sparse_map
= xcalloc (u
, sizeof st
->sparse_map
[0]);
1451 st
->sparse_map_avail
= 0;
1456 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1457 struct xheader
*xhdr
, void const *data
)
1459 size_t const *pi
= data
;
1460 code_num (st
->sparse_map
[*pi
].offset
, keyword
, xhdr
);
1464 sparse_offset_decoder (struct tar_stat_info
*st
,
1465 char const *keyword
,
1467 size_t size
__attribute__((unused
)))
1470 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1472 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1473 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
1475 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1476 "GNU.sparse.offset", arg
));
1481 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
1482 struct xheader
*xhdr
, void const *data
)
1484 size_t const *pi
= data
;
1485 code_num (st
->sparse_map
[*pi
].numbytes
, keyword
, xhdr
);
1489 sparse_numbytes_decoder (struct tar_stat_info
*st
,
1490 char const *keyword
,
1492 size_t size
__attribute__((unused
)))
1495 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1497 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1498 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
1500 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1506 sparse_map_decoder (struct tar_stat_info
*st
,
1507 char const *keyword
,
1509 size_t size
__attribute__((unused
)))
1514 st
->sparse_map_avail
= 0;
1520 if (!ISDIGIT (*arg
))
1522 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1528 u
= strtoimax (arg
, &delim
, 10);
1529 if (TYPE_MAXIMUM (off_t
) < u
)
1531 u
= TYPE_MAXIMUM (off_t
);
1537 if (errno
== ERANGE
)
1539 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1546 if (errno
== ERANGE
)
1548 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1551 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1552 st
->sparse_map
[st
->sparse_map_avail
++] = e
;
1555 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1565 else if (*delim
!= ',')
1568 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1578 _("Malformed extended header: invalid %s: odd number of values"),
1583 dumpdir_coder (struct tar_stat_info
const *st
, char const *keyword
,
1584 struct xheader
*xhdr
, void const *data
)
1586 xheader_print_n (xhdr
, keyword
, data
, dumpdir_size (data
));
1590 dumpdir_decoder (struct tar_stat_info
*st
,
1591 char const *keyword
__attribute__((unused
)),
1595 st
->dumpdir
= xmalloc (size
);
1596 memcpy (st
->dumpdir
, arg
, size
);
1600 volume_label_coder (struct tar_stat_info
const *st
, char const *keyword
,
1601 struct xheader
*xhdr
, void const *data
)
1603 code_string (data
, keyword
, xhdr
);
1607 volume_label_decoder (struct tar_stat_info
*st
,
1608 char const *keyword
__attribute__((unused
)),
1610 size_t size
__attribute__((unused
)))
1612 decode_string (&volume_label
, arg
);
1616 volume_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1617 struct xheader
*xhdr
, void const *data
)
1619 off_t
const *v
= data
;
1620 code_num (*v
, keyword
, xhdr
);
1624 volume_size_decoder (struct tar_stat_info
*st
,
1625 char const *keyword
,
1626 char const *arg
, size_t size
)
1629 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1630 continued_file_size
= u
;
1633 /* FIXME: Merge with volume_size_coder */
1635 volume_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1636 struct xheader
*xhdr
, void const *data
)
1638 off_t
const *v
= data
;
1639 code_num (*v
, keyword
, xhdr
);
1643 volume_offset_decoder (struct tar_stat_info
*st
,
1644 char const *keyword
,
1645 char const *arg
, size_t size
)
1648 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1649 continued_file_offset
= u
;
1653 volume_filename_decoder (struct tar_stat_info
*st
,
1654 char const *keyword
__attribute__((unused
)),
1656 size_t size
__attribute__((unused
)))
1658 decode_string (&continued_file_name
, arg
);
1662 xattr_selinux_coder (struct tar_stat_info
const *st
, char const *keyword
,
1663 struct xheader
*xhdr
, void const *data
)
1665 code_string (st
->cntx_name
, keyword
, xhdr
);
1669 xattr_selinux_decoder (struct tar_stat_info
*st
,
1670 char const *keyword
, char const *arg
, size_t size
)
1672 decode_string (&st
->cntx_name
, arg
);
1676 xattr_acls_a_coder (struct tar_stat_info
const *st
, char const *keyword
,
1677 struct xheader
*xhdr
, void const *data
)
1679 xheader_print_n (xhdr
, keyword
, st
->acls_a_ptr
, st
->acls_a_len
);
1683 xattr_acls_a_decoder (struct tar_stat_info
*st
,
1684 char const *keyword
, char const *arg
, size_t size
)
1686 st
->acls_a_ptr
= xmemdup (arg
, size
+ 1);
1687 st
->acls_a_len
= size
;
1691 xattr_acls_d_coder (struct tar_stat_info
const *st
, char const *keyword
,
1692 struct xheader
*xhdr
, void const *data
)
1694 xheader_print_n (xhdr
, keyword
, st
->acls_d_ptr
, st
->acls_d_len
);
1698 xattr_acls_d_decoder (struct tar_stat_info
*st
,
1699 char const *keyword
, char const *arg
, size_t size
)
1701 st
->acls_d_ptr
= xmemdup (arg
, size
+ 1);
1702 st
->acls_d_len
= size
;
1706 xattr_coder (struct tar_stat_info
const *st
, char const *keyword
,
1707 struct xheader
*xhdr
, void const *data
)
1709 struct xattr_array
*xattr_map
= st
->xattr_map
;
1710 const size_t *off
= data
;
1711 xheader_print_n (xhdr
, keyword
,
1712 xattr_map
[*off
].xval_ptr
, xattr_map
[*off
].xval_len
);
1716 xattr_decoder (struct tar_stat_info
*st
,
1717 char const *keyword
, char const *arg
, size_t size
)
1722 size_t klen_raw
= strlen (keyword
);
1723 xkey
= alloca (klen_raw
+ 1);
1724 memcpy (xkey
, keyword
, klen_raw
+ 1) /* including null-terminating */;
1727 xstr
= alloca (size
+ 1);
1728 memcpy (xstr
, arg
, size
+ 1); /* separator included, for GNU tar '\n' */;
1730 xattr_decode_keyword (xkey
);
1732 xheader_xattr_add (st
, xkey
+ strlen("SCHILY.xattr."), xstr
, size
);
1736 sparse_major_coder (struct tar_stat_info
const *st
, char const *keyword
,
1737 struct xheader
*xhdr
, void const *data
)
1739 code_num (st
->sparse_major
, keyword
, xhdr
);
1743 sparse_major_decoder (struct tar_stat_info
*st
,
1744 char const *keyword
,
1749 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1750 st
->sparse_major
= u
;
1754 sparse_minor_coder (struct tar_stat_info
const *st
, char const *keyword
,
1755 struct xheader
*xhdr
, void const *data
)
1757 code_num (st
->sparse_minor
, keyword
, xhdr
);
1761 sparse_minor_decoder (struct tar_stat_info
*st
,
1762 char const *keyword
,
1767 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1768 st
->sparse_minor
= u
;
1771 struct xhdr_tab
const xhdr_tab
[] = {
1772 { "atime", atime_coder
, atime_decoder
, 0, false },
1773 { "comment", dummy_coder
, dummy_decoder
, 0, false },
1774 { "charset", dummy_coder
, dummy_decoder
, 0, false },
1775 { "ctime", ctime_coder
, ctime_decoder
, 0, false },
1776 { "gid", gid_coder
, gid_decoder
, 0, false },
1777 { "gname", gname_coder
, gname_decoder
, 0, false },
1778 { "linkpath", linkpath_coder
, linkpath_decoder
, 0, false },
1779 { "mtime", mtime_coder
, mtime_decoder
, 0, false },
1780 { "path", path_coder
, path_decoder
, 0, false },
1781 { "size", size_coder
, size_decoder
, 0, false },
1782 { "uid", uid_coder
, uid_decoder
, 0, false },
1783 { "uname", uname_coder
, uname_decoder
, 0, false },
1785 /* Sparse file handling */
1786 { "GNU.sparse.name", path_coder
, sparse_path_decoder
,
1787 XHDR_PROTECTED
, false },
1788 { "GNU.sparse.major", sparse_major_coder
, sparse_major_decoder
,
1789 XHDR_PROTECTED
, false },
1790 { "GNU.sparse.minor", sparse_minor_coder
, sparse_minor_decoder
,
1791 XHDR_PROTECTED
, false },
1792 { "GNU.sparse.realsize", sparse_size_coder
, sparse_size_decoder
,
1793 XHDR_PROTECTED
, false },
1794 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
1795 XHDR_PROTECTED
, false },
1797 /* tar 1.14 - 1.15.90 keywords. */
1798 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
,
1799 XHDR_PROTECTED
, false },
1800 /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
1801 headers, and each of them was meaningful. It confilcted with POSIX specs,
1802 which requires that "when extended header records conflict, the last one
1803 given in the header shall take precedence." */
1804 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
1805 XHDR_PROTECTED
, false },
1806 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
1807 XHDR_PROTECTED
, false },
1808 /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
1809 { "GNU.sparse.map", NULL
/* Unused, see pax_dump_header() */,
1810 sparse_map_decoder
, 0, false },
1812 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
,
1813 XHDR_PROTECTED
, false },
1815 /* Keeps the tape/volume label. May be present only in the global headers.
1816 Equivalent to GNUTYPE_VOLHDR. */
1817 { "GNU.volume.label", volume_label_coder
, volume_label_decoder
,
1818 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1820 /* These may be present in a first global header of the archive.
1821 They provide the same functionality as GNUTYPE_MULTIVOL header.
1822 The GNU.volume.size keeps the real_s_sizeleft value, which is
1823 otherwise kept in the size field of a multivolume header. The
1824 GNU.volume.offset keeps the offset of the start of this volume,
1825 otherwise kept in oldgnu_header.offset. */
1826 { "GNU.volume.filename", volume_label_coder
, volume_filename_decoder
,
1827 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1828 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
,
1829 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1830 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
,
1831 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1833 /* We get the SELinux value from filecon, so add a namespace for SELinux
1834 instead of storing it in SCHILY.xattr.* (which would be RAW). */
1835 { "RHT.security.selinux",
1836 xattr_selinux_coder
, xattr_selinux_decoder
, 0, false },
1838 /* ACLs, use the star format... */
1839 { "SCHILY.acl.access",
1840 xattr_acls_a_coder
, xattr_acls_a_decoder
, 0, false },
1842 { "SCHILY.acl.default",
1843 xattr_acls_d_coder
, xattr_acls_d_decoder
, 0, false },
1845 /* We are storing all extended attributes using this rule even if some of them
1846 were stored by some previous rule (duplicates) -- we just have to make sure
1847 they are restored *only once* during extraction later on. */
1848 { "SCHILY.xattr", xattr_coder
, xattr_decoder
, 0, true },
1850 { NULL
, NULL
, NULL
, 0, false }