1 /* POSIX extended headers for tar.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2012
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 static 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
)
172 time_t t
= u
= strtoumax (input
, &p
, 10);
173 if (t
!= u
|| *p
|| errno
== ERANGE
)
174 ERROR ((0, 0, _("Time stamp is out of allowed range")));
178 assign_string (sval
, input
);
183 xheader_set_keyword_equal (char *kw
, char *eq
)
194 while (p
> kw
&& isspace ((unsigned char) *p
))
199 for (p
= eq
+ 1; *p
&& isspace ((unsigned char) *p
); p
++)
202 if (strcmp (kw
, "delete") == 0)
204 if (xheader_protected_pattern_p (p
))
205 USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), quote (p
)));
206 xheader_list_append (&keyword_pattern_list
, p
, NULL
);
208 else if (strcmp (kw
, "exthdr.name") == 0)
209 assign_string (&exthdr_name
, p
);
210 else if (strcmp (kw
, "globexthdr.name") == 0)
211 assign_string (&globexthdr_name
, p
);
212 else if (strcmp (kw
, "exthdr.mtime") == 0)
213 assign_time_option (&exthdr_mtime_option
, &exthdr_mtime
, p
);
214 else if (strcmp (kw
, "globexthdr.mtime") == 0)
215 assign_time_option (&globexthdr_mtime_option
, &globexthdr_mtime
, p
);
218 if (xheader_protected_keyword_p (kw
))
219 USAGE_ERROR ((0, 0, _("Keyword %s cannot be overridden"), kw
));
221 xheader_list_append (&keyword_global_override_list
, kw
, p
);
223 xheader_list_append (&keyword_override_list
, kw
, p
);
228 xheader_set_option (char *string
)
231 for (token
= strtok (string
, ","); token
; token
= strtok (NULL
, ","))
233 char *p
= strchr (token
, '=');
235 xheader_set_single_keyword (token
);
237 xheader_set_keyword_equal (token
, p
);
242 string Includes: Replaced By:
243 %d The directory name of the file,
244 equivalent to the result of the
245 dirname utility on the translated
247 %f The filename of the file, equivalent
248 to the result of the basename
249 utility on the translated file name.
250 %p The process ID of the pax process.
251 %n The value of the 3rd argument.
252 %% A '%' character. */
255 xheader_format_name (struct tar_stat_info
*st
, const char *fmt
, size_t n
)
258 size_t len
= strlen (fmt
);
264 char pidbuf
[UINTMAX_STRSIZE_BOUND
];
266 char nbuf
[UINTMAX_STRSIZE_BOUND
];
267 char const *nptr
= NULL
;
269 for (p
= fmt
; *p
&& (p
= strchr (p
, '%')); )
281 dirp
= dir_name (st
->orig_file_name
);
282 dir
= safer_name_suffix (dirp
, false, absolute_names_option
);
283 len
+= strlen (dir
) - 2;
290 base
= last_component (st
->orig_file_name
);
291 len
+= strlen (base
) - 2;
296 pptr
= umaxtostr (getpid (), pidbuf
);
297 len
+= pidbuf
+ sizeof pidbuf
- 1 - pptr
- 2;
301 nptr
= umaxtostr (n
, nbuf
);
302 len
+= nbuf
+ sizeof nbuf
- 1 - nptr
- 2;
308 buf
= xmalloc (len
+ 1);
309 for (q
= buf
, p
= fmt
; *p
; )
328 q
= stpcpy (q
, base
);
333 q
= stpcpy (q
, pptr
);
340 q
= stpcpy (q
, nptr
);
344 /* else fall through */
358 /* Do not allow it to end in a slash */
359 while (q
> buf
&& ISSLASH (q
[-1]))
366 xheader_xhdr_name (struct tar_stat_info
*st
)
369 assign_string (&exthdr_name
, "%d/PaxHeaders.%p/%f");
370 return xheader_format_name (st
, exthdr_name
, 0);
373 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
376 xheader_ghdr_name (void)
378 if (!globexthdr_name
)
381 const char *tmp
= getenv ("TMPDIR");
384 len
= strlen (tmp
) + sizeof (GLOBAL_HEADER_TEMPLATE
); /* Includes nul */
385 globexthdr_name
= xmalloc (len
);
386 strcpy(globexthdr_name
, tmp
);
387 strcat(globexthdr_name
, GLOBAL_HEADER_TEMPLATE
);
390 return xheader_format_name (NULL
, globexthdr_name
, global_header_count
+ 1);
394 xheader_write (char type
, char *name
, time_t t
, struct xheader
*xhdr
)
404 if (globexthdr_mtime_option
)
405 t
= globexthdr_mtime
;
409 if (exthdr_mtime_option
)
413 header
= start_private_header (name
, size
, t
);
414 header
->header
.typeflag
= type
;
416 simple_finish_header (header
);
424 header
= find_next_block ();
428 memcpy (header
->buffer
, p
, len
);
430 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
433 set_next_block_after (header
);
436 xheader_destroy (xhdr
);
439 global_header_count
++;
443 xheader_write_global (struct xheader
*xhdr
)
445 if (keyword_global_override_list
)
447 struct keyword_list
*kp
;
450 for (kp
= keyword_global_override_list
; kp
; kp
= kp
->next
)
451 code_string (kp
->value
, kp
->pattern
, xhdr
);
457 xheader_finish (xhdr
);
458 xheader_write (XGLTYPE
, name
= xheader_ghdr_name (), time (NULL
), xhdr
);
464 xheader_xattr_init (struct tar_stat_info
*st
)
466 st
->xattr_map
= NULL
;
467 st
->xattr_map_size
= 0;
469 st
->acls_a_ptr
= NULL
;
471 st
->acls_d_ptr
= NULL
;
473 st
->cntx_name
= NULL
;
477 xheader_xattr_free (struct xattr_array
*xattr_map
, size_t xattr_map_size
)
481 while (scan
< xattr_map_size
)
483 free (xattr_map
[scan
].xkey
);
484 free (xattr_map
[scan
].xval_ptr
);
492 xheader_xattr__add (struct xattr_array
**xattr_map
,
493 size_t *xattr_map_size
,
494 const char *key
, const char *val
, size_t len
)
496 size_t pos
= (*xattr_map_size
)++;
498 *xattr_map
= xrealloc (*xattr_map
,
499 *xattr_map_size
* sizeof(struct xattr_array
));
500 (*xattr_map
)[pos
].xkey
= xstrdup (key
);
501 (*xattr_map
)[pos
].xval_ptr
= xmemdup (val
, len
+ 1);
502 (*xattr_map
)[pos
].xval_len
= len
;
505 /* This is reversal function for xattr_encode_keyword. See comment for
506 xattr_encode_keyword() for more info. */
508 xattr_decode_keyword (char *keyword
)
510 char *kpr
, *kpl
; /* keyword pointer left/right */
517 if (kpr
[1] == '3' && kpr
[2] == 'D')
524 else if (kpr
[1] == '2' && kpr
[2] == '5')
544 xheader_xattr_add (struct tar_stat_info
*st
,
545 const char *key
, const char *val
, size_t len
)
547 size_t klen
= strlen (key
);
548 char *xkey
= xmalloc (strlen("SCHILY.xattr.") + klen
+ 1);
551 tmp
= stpcpy (tmp
, "SCHILY.xattr.");
554 xheader_xattr__add (&st
->xattr_map
, &st
->xattr_map_size
, xkey
, val
, len
);
560 xheader_xattr_copy (const struct tar_stat_info
*st
,
561 struct xattr_array
**xattr_map
, size_t *xattr_map_size
)
568 while (scan
< st
->xattr_map_size
)
570 char *key
= st
->xattr_map
[scan
].xkey
;
571 char *val
= st
->xattr_map
[scan
].xval_ptr
;
572 size_t len
= st
->xattr_map
[scan
].xval_len
;
574 xheader_xattr__add(xattr_map
, xattr_map_size
, key
, val
, len
);
581 /* General Interface */
583 #define XHDR_PROTECTED 0x01
584 #define XHDR_GLOBAL 0x02
589 void (*coder
) (struct tar_stat_info
const *, char const *,
590 struct xheader
*, void const *data
);
591 void (*decoder
) (struct tar_stat_info
*, char const *, char const *, size_t);
593 bool prefix
; /* select handler comparing prefix only */
596 /* This declaration must be extern, because ISO C99 section 6.9.2
597 prohibits a tentative definition that has both internal linkage and
598 incomplete type. If we made it static, we'd have to declare its
599 size which would be a maintenance pain; if we put its initializer
600 here, we'd need a boatload of forward declarations, which would be
601 even more of a pain. */
602 extern struct xhdr_tab
const xhdr_tab
[];
604 static struct xhdr_tab
const *
605 locate_handler (char const *keyword
)
607 struct xhdr_tab
const *p
;
609 for (p
= xhdr_tab
; p
->keyword
; p
++)
612 if (strncmp (p
->keyword
, keyword
, strlen(p
->keyword
)) == 0)
617 if (strcmp (p
->keyword
, keyword
) == 0)
625 xheader_protected_pattern_p (const char *pattern
)
627 struct xhdr_tab
const *p
;
629 for (p
= xhdr_tab
; p
->keyword
; p
++)
630 if (!p
->prefix
&& (p
->flags
& XHDR_PROTECTED
)
631 && fnmatch (pattern
, p
->keyword
, 0) == 0)
637 xheader_protected_keyword_p (const char *keyword
)
639 struct xhdr_tab
const *p
;
641 for (p
= xhdr_tab
; p
->keyword
; p
++)
642 if (!p
->prefix
&& (p
->flags
& XHDR_PROTECTED
)
643 && strcmp (p
->keyword
, keyword
) == 0)
648 /* Decode a single extended header record, advancing *PTR to the next record.
649 Return true on success, false otherwise. */
651 decode_record (struct xheader
*xhdr
,
653 void (*handler
) (void *, char const *, char const *, size_t),
663 size_t len_max
= xhdr
->buffer
+ xhdr
->size
- start
;
665 while (*p
== ' ' || *p
== '\t')
671 ERROR ((0, 0, _("Malformed extended header: missing length")));
676 len
= u
= strtoumax (p
, &len_lim
, 10);
677 if (len
!= u
|| errno
== ERANGE
)
679 ERROR ((0, 0, _("Extended header length is out of allowed range")));
685 int len_len
= len_lim
- p
;
686 ERROR ((0, 0, _("Extended header length %*s is out of range"),
693 for (p
= len_lim
; *p
== ' ' || *p
== '\t'; p
++)
698 _("Malformed extended header: missing blank after length")));
704 if (! (p
&& p
< nextp
))
706 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
710 if (nextp
[-1] != '\n')
712 ERROR ((0, 0, _("Malformed extended header: missing newline")));
716 *p
= nextp
[-1] = '\0';
717 handler (data
, keyword
, p
+ 1, nextp
- p
- 2); /* '=' + trailing '\n' */
725 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
727 for (; kp
; kp
= kp
->next
)
729 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
731 t
->decoder (st
, t
->keyword
, kp
->value
, strlen (kp
->value
));
736 decx (void *data
, char const *keyword
, char const *value
, size_t size
)
738 struct xhdr_tab
const *t
;
739 struct tar_stat_info
*st
= data
;
741 if (xheader_keyword_deleted_p (keyword
)
742 || xheader_keyword_override_p (keyword
))
745 t
= locate_handler (keyword
);
747 t
->decoder (st
, keyword
, value
, size
);
749 WARNOPT (WARN_UNKNOWN_KEYWORD
,
750 (0, 0, _("Ignoring unknown extended header keyword '%s'"),
755 xheader_decode (struct tar_stat_info
*st
)
757 run_override_list (keyword_global_override_list
, st
);
758 run_override_list (global_header_override_list
, st
);
762 char *p
= st
->xhdr
.buffer
+ BLOCKSIZE
;
763 while (decode_record (&st
->xhdr
, &p
, decx
, st
))
766 run_override_list (keyword_override_list
, st
);
770 decg (void *data
, char const *keyword
, char const *value
,
771 size_t size
__attribute__((unused
)))
773 struct keyword_list
**kwl
= data
;
774 struct xhdr_tab
const *tab
= locate_handler (keyword
);
775 if (tab
&& (tab
->flags
& XHDR_GLOBAL
))
776 tab
->decoder (data
, keyword
, value
, size
);
778 xheader_list_append (kwl
, keyword
, value
);
782 xheader_decode_global (struct xheader
*xhdr
)
786 char *p
= xhdr
->buffer
+ BLOCKSIZE
;
788 xheader_list_destroy (&global_header_override_list
);
789 while (decode_record (xhdr
, &p
, decg
, &global_header_override_list
))
795 xheader_init (struct xheader
*xhdr
)
799 xhdr
->stk
= xmalloc (sizeof *xhdr
->stk
);
800 obstack_init (xhdr
->stk
);
805 xheader_store (char const *keyword
, struct tar_stat_info
*st
,
808 struct xhdr_tab
const *t
;
812 t
= locate_handler (keyword
);
815 if (xheader_keyword_deleted_p (keyword
)
816 || xheader_keyword_override_p (keyword
))
818 xheader_init (&st
->xhdr
);
819 t
->coder (st
, keyword
, &st
->xhdr
, data
);
823 xheader_read (struct xheader
*xhdr
, union block
*p
, size_t size
)
829 xhdr
->buffer
= xmalloc (size
+ 1);
830 xhdr
->buffer
[size
] = '\0';
840 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
842 memcpy (&xhdr
->buffer
[j
], p
->buffer
, len
);
843 set_next_block_after (p
);
845 p
= find_next_block ();
853 /* xattr_encode_keyword() substitutes '=' ~~> '%3D' and '%' ~~> '%25'
854 in extended attribute keywords. This is needed because the '=' character
855 has special purpose in extended attribute header - it splits keyword and
856 value part of header. If there was the '=' occurrence allowed inside
857 keyword, there would be no unambiguous way how to decode this extended
860 (http://lists.gnu.org/archive/html/bug-tar/2012-10/msg00017.html)
863 xattr_encode_keyword(const char *keyword
)
865 static char *encode_buffer
= NULL
;
866 static size_t encode_buffer_size
= 0;
867 size_t bp
; /* keyword/buffer pointers */
871 encode_buffer_size
= 256;
872 encode_buffer
= xmalloc (encode_buffer_size
);
877 for (bp
= 0; *keyword
!= 0; ++bp
, ++keyword
)
881 if (bp
+ 2 /* enough for URL encoding also.. */ >= encode_buffer_size
)
883 encode_buffer
= x2realloc (encode_buffer
, &encode_buffer_size
);
888 strcpy (encode_buffer
+ bp
, "%25");
893 strcpy (encode_buffer
+ bp
, "%3D");
897 encode_buffer
[bp
] = c
;
900 encode_buffer
[bp
] = 0;
902 return encode_buffer
;
906 xheader_print_n (struct xheader
*xhdr
, char const *keyword
,
907 char const *value
, size_t vsize
)
911 char nbuf
[UINTMAX_STRSIZE_BOUND
];
915 keyword
= xattr_encode_keyword (keyword
);
916 klen
= strlen (keyword
);
917 len
= klen
+ vsize
+ 3; /* ' ' + '=' + '\n' */
922 np
= umaxtostr (len
+ p
, nbuf
);
923 n
= nbuf
+ sizeof nbuf
- 1 - np
;
927 x_obstack_grow (xhdr
, np
, n
);
928 x_obstack_1grow (xhdr
, ' ');
929 x_obstack_grow (xhdr
, keyword
, klen
);
930 x_obstack_1grow (xhdr
, '=');
931 x_obstack_grow (xhdr
, value
, vsize
);
932 x_obstack_1grow (xhdr
, '\n');
936 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
938 xheader_print_n (xhdr
, keyword
, value
, strlen (value
));
942 xheader_finish (struct xheader
*xhdr
)
944 struct keyword_list
*kp
;
946 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
947 code_string (kp
->value
, kp
->pattern
, xhdr
);
949 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
953 xheader_destroy (struct xheader
*xhdr
)
957 obstack_free (xhdr
->stk
, NULL
);
968 /* Buildable strings */
971 xheader_string_begin (struct xheader
*xhdr
)
973 xhdr
->string_length
= 0;
977 xheader_string_add (struct xheader
*xhdr
, char const *s
)
982 xhdr
->string_length
+= strlen (s
);
983 x_obstack_grow (xhdr
, s
, strlen (s
));
987 xheader_string_end (struct xheader
*xhdr
, char const *keyword
)
993 char nbuf
[UINTMAX_STRSIZE_BOUND
];
1001 len
= strlen (keyword
) + xhdr
->string_length
+ 3; /* ' ' + '=' + '\n' */
1006 np
= umaxtostr (len
+ p
, nbuf
);
1007 n
= nbuf
+ sizeof nbuf
- 1 - np
;
1011 p
= strlen (keyword
) + n
+ 2;
1016 _("Generated keyword/value pair is too long (keyword=%s, length=%s)"),
1018 obstack_free (xhdr
->stk
, obstack_finish (xhdr
->stk
));
1021 x_obstack_blank (xhdr
, p
);
1022 x_obstack_1grow (xhdr
, '\n');
1023 cp
= obstack_next_free (xhdr
->stk
) - xhdr
->string_length
- p
- 1;
1024 memmove (cp
+ p
, cp
, xhdr
->string_length
);
1025 cp
= stpcpy (cp
, np
);
1027 cp
= stpcpy (cp
, keyword
);
1033 /* Implementations */
1036 out_of_range_header (char const *keyword
, char const *value
,
1037 uintmax_t minus_minval
, uintmax_t maxval
)
1039 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
1040 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
1041 char *minval_string
= umaxtostr (minus_minval
, minval_buf
+ 1);
1042 char *maxval_string
= umaxtostr (maxval
, maxval_buf
);
1044 *--minval_string
= '-';
1046 /* TRANSLATORS: The first %s is the pax extended header keyword
1047 (atime, gid, etc.). */
1048 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
1049 keyword
, value
, minval_string
, maxval_string
));
1053 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
1056 if (!utf8_convert (true, string
, &outstr
))
1058 /* FIXME: report error */
1059 outstr
= xstrdup (string
);
1061 xheader_print (xhdr
, keyword
, outstr
);
1066 decode_string (char **string
, char const *arg
)
1073 if (!utf8_convert (false, arg
, string
))
1075 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
1076 assign_string (string
, arg
);
1081 code_time (struct timespec t
, char const *keyword
, struct xheader
*xhdr
)
1083 char buf
[TIMESPEC_STRSIZE_BOUND
];
1084 xheader_print (xhdr
, keyword
, code_timespec (t
, buf
));
1087 enum decode_time_status
1089 decode_time_success
,
1091 decode_time_bad_header
1094 static enum decode_time_status
1095 _decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
1098 unsigned long int ns
= 0;
1101 bool negative
= *arg
== '-';
1105 if (ISDIGIT (arg
[negative
]))
1109 intmax_t i
= strtoimax (arg
, &arg_lim
, 10);
1110 if (TYPE_SIGNED (time_t) ? i
< TYPE_MINIMUM (time_t) : i
< 0)
1111 return decode_time_range
;
1116 uintmax_t i
= strtoumax (arg
, &arg_lim
, 10);
1117 if (TYPE_MAXIMUM (time_t) < i
)
1118 return decode_time_range
;
1124 if (errno
== ERANGE
)
1125 return decode_time_range
;
1130 bool trailing_nonzero
= false;
1132 while (ISDIGIT (*++p
))
1133 if (digits
< LOG10_BILLION
)
1135 ns
= 10 * ns
+ (*p
- '0');
1139 trailing_nonzero
|= *p
!= '0';
1141 while (digits
++ < LOG10_BILLION
)
1146 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
1147 I.e., truncate time stamps towards minus infinity while
1148 converting them to internal form. */
1149 ns
+= trailing_nonzero
;
1152 if (s
== TYPE_MINIMUM (time_t))
1153 return decode_time_range
;
1164 return decode_time_success
;
1168 return decode_time_bad_header
;
1172 decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
1174 switch (_decode_time (ts
, arg
, keyword
))
1176 case decode_time_success
:
1178 case decode_time_bad_header
:
1179 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1182 case decode_time_range
:
1183 out_of_range_header (keyword
, arg
, - (uintmax_t) TYPE_MINIMUM (time_t),
1184 TYPE_MAXIMUM (time_t));
1191 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
1193 char sbuf
[UINTMAX_STRSIZE_BOUND
];
1194 xheader_print (xhdr
, keyword
, umaxtostr (value
, sbuf
));
1198 decode_num (uintmax_t *num
, char const *arg
, uintmax_t maxval
,
1199 char const *keyword
)
1204 if (! (ISDIGIT (*arg
)
1205 && (errno
= 0, u
= strtoumax (arg
, &arg_lim
, 10), !*arg_lim
)))
1207 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1212 if (! (u
<= maxval
&& errno
!= ERANGE
))
1214 out_of_range_header (keyword
, arg
, 0, maxval
);
1223 dummy_coder (struct tar_stat_info
const *st
__attribute__ ((unused
)),
1224 char const *keyword
__attribute__ ((unused
)),
1225 struct xheader
*xhdr
__attribute__ ((unused
)),
1226 void const *data
__attribute__ ((unused
)))
1231 dummy_decoder (struct tar_stat_info
*st
__attribute__ ((unused
)),
1232 char const *keyword
__attribute__ ((unused
)),
1233 char const *arg
__attribute__ ((unused
)),
1234 size_t size
__attribute__((unused
)))
1239 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1240 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1242 code_time (st
->atime
, keyword
, xhdr
);
1246 atime_decoder (struct tar_stat_info
*st
,
1247 char const *keyword
,
1249 size_t size
__attribute__((unused
)))
1252 if (decode_time (&ts
, arg
, keyword
))
1257 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1258 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1260 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
1264 gid_decoder (struct tar_stat_info
*st
,
1265 char const *keyword
,
1267 size_t size
__attribute__((unused
)))
1270 if (decode_num (&u
, arg
, TYPE_MAXIMUM (gid_t
), keyword
))
1271 st
->stat
.st_gid
= u
;
1275 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1276 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1278 code_string (st
->gname
, keyword
, xhdr
);
1282 gname_decoder (struct tar_stat_info
*st
,
1283 char const *keyword
__attribute__((unused
)),
1285 size_t size
__attribute__((unused
)))
1287 decode_string (&st
->gname
, arg
);
1291 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
1292 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1294 code_string (st
->link_name
, keyword
, xhdr
);
1298 linkpath_decoder (struct tar_stat_info
*st
,
1299 char const *keyword
__attribute__((unused
)),
1301 size_t size
__attribute__((unused
)))
1303 decode_string (&st
->link_name
, arg
);
1307 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1308 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1310 code_time (st
->ctime
, keyword
, xhdr
);
1314 ctime_decoder (struct tar_stat_info
*st
,
1315 char const *keyword
,
1317 size_t size
__attribute__((unused
)))
1320 if (decode_time (&ts
, arg
, keyword
))
1325 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1326 struct xheader
*xhdr
, void const *data
)
1328 struct timespec
const *mtime
= data
;
1329 code_time (mtime
? *mtime
: st
->mtime
, keyword
, xhdr
);
1333 mtime_decoder (struct tar_stat_info
*st
,
1334 char const *keyword
,
1336 size_t size
__attribute__((unused
)))
1339 if (decode_time (&ts
, arg
, keyword
))
1344 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
1345 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1347 code_string (st
->file_name
, keyword
, xhdr
);
1351 path_decoder (struct tar_stat_info
*st
,
1352 char const *keyword
__attribute__((unused
)),
1354 size_t size
__attribute__((unused
)))
1356 decode_string (&st
->orig_file_name
, arg
);
1357 decode_string (&st
->file_name
, arg
);
1358 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
1362 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1363 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1365 code_num (st
->stat
.st_size
, keyword
, xhdr
);
1369 size_decoder (struct tar_stat_info
*st
,
1370 char const *keyword
,
1372 size_t size
__attribute__((unused
)))
1375 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1376 st
->stat
.st_size
= u
;
1380 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1381 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1383 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
1387 uid_decoder (struct tar_stat_info
*st
,
1388 char const *keyword
,
1390 size_t size
__attribute__((unused
)))
1393 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uid_t
), keyword
))
1394 st
->stat
.st_uid
= u
;
1398 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1399 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1401 code_string (st
->uname
, keyword
, xhdr
);
1405 uname_decoder (struct tar_stat_info
*st
,
1406 char const *keyword
__attribute__((unused
)),
1408 size_t size
__attribute__((unused
)))
1410 decode_string (&st
->uname
, arg
);
1414 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1415 struct xheader
*xhdr
, void const *data
)
1417 size_coder (st
, keyword
, xhdr
, data
);
1421 sparse_size_decoder (struct tar_stat_info
*st
,
1422 char const *keyword
,
1424 size_t size
__attribute__((unused
)))
1427 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1428 st
->stat
.st_size
= u
;
1432 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
1433 struct xheader
*xhdr
,
1434 void const *data
__attribute__ ((unused
)))
1436 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
1440 sparse_numblocks_decoder (struct tar_stat_info
*st
,
1441 char const *keyword
,
1443 size_t size
__attribute__((unused
)))
1446 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1448 st
->sparse_map_size
= u
;
1449 st
->sparse_map
= xcalloc (u
, sizeof st
->sparse_map
[0]);
1450 st
->sparse_map_avail
= 0;
1455 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1456 struct xheader
*xhdr
, void const *data
)
1458 size_t const *pi
= data
;
1459 code_num (st
->sparse_map
[*pi
].offset
, keyword
, xhdr
);
1463 sparse_offset_decoder (struct tar_stat_info
*st
,
1464 char const *keyword
,
1466 size_t size
__attribute__((unused
)))
1469 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1471 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1472 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
1474 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1475 "GNU.sparse.offset", arg
));
1480 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
1481 struct xheader
*xhdr
, void const *data
)
1483 size_t const *pi
= data
;
1484 code_num (st
->sparse_map
[*pi
].numbytes
, keyword
, xhdr
);
1488 sparse_numbytes_decoder (struct tar_stat_info
*st
,
1489 char const *keyword
,
1491 size_t size
__attribute__((unused
)))
1494 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1496 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1497 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
1499 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1505 sparse_map_decoder (struct tar_stat_info
*st
,
1506 char const *keyword
,
1508 size_t size
__attribute__((unused
)))
1512 st
->sparse_map_avail
= 0;
1519 if (!ISDIGIT (*arg
))
1521 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1527 u
= strtoumax (arg
, &delim
, 10);
1531 if (!(u
== e
.offset
&& errno
!= ERANGE
))
1533 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1540 if (!(u
== e
.numbytes
&& errno
!= ERANGE
))
1542 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1545 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1546 st
->sparse_map
[st
->sparse_map_avail
++] = e
;
1549 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1559 else if (*delim
!= ',')
1562 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1572 _("Malformed extended header: invalid %s: odd number of values"),
1577 dumpdir_coder (struct tar_stat_info
const *st
, char const *keyword
,
1578 struct xheader
*xhdr
, void const *data
)
1580 xheader_print_n (xhdr
, keyword
, data
, dumpdir_size (data
));
1584 dumpdir_decoder (struct tar_stat_info
*st
,
1585 char const *keyword
__attribute__((unused
)),
1589 st
->dumpdir
= xmalloc (size
);
1590 memcpy (st
->dumpdir
, arg
, size
);
1594 volume_label_coder (struct tar_stat_info
const *st
, char const *keyword
,
1595 struct xheader
*xhdr
, void const *data
)
1597 code_string (data
, keyword
, xhdr
);
1601 volume_label_decoder (struct tar_stat_info
*st
,
1602 char const *keyword
__attribute__((unused
)),
1604 size_t size
__attribute__((unused
)))
1606 decode_string (&volume_label
, arg
);
1610 volume_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1611 struct xheader
*xhdr
, void const *data
)
1613 off_t
const *v
= data
;
1614 code_num (*v
, keyword
, xhdr
);
1618 volume_size_decoder (struct tar_stat_info
*st
,
1619 char const *keyword
,
1620 char const *arg
, size_t size
)
1623 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1624 continued_file_size
= u
;
1627 /* FIXME: Merge with volume_size_coder */
1629 volume_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1630 struct xheader
*xhdr
, void const *data
)
1632 off_t
const *v
= data
;
1633 code_num (*v
, keyword
, xhdr
);
1637 volume_offset_decoder (struct tar_stat_info
*st
,
1638 char const *keyword
,
1639 char const *arg
, size_t size
)
1642 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1643 continued_file_offset
= u
;
1647 volume_filename_decoder (struct tar_stat_info
*st
,
1648 char const *keyword
__attribute__((unused
)),
1650 size_t size
__attribute__((unused
)))
1652 decode_string (&continued_file_name
, arg
);
1656 xattr_selinux_coder (struct tar_stat_info
const *st
, char const *keyword
,
1657 struct xheader
*xhdr
, void const *data
)
1659 code_string (st
->cntx_name
, keyword
, xhdr
);
1663 xattr_selinux_decoder (struct tar_stat_info
*st
,
1664 char const *keyword
, char const *arg
, size_t size
)
1666 decode_string (&st
->cntx_name
, arg
);
1670 xattr_acls_a_coder (struct tar_stat_info
const *st
, char const *keyword
,
1671 struct xheader
*xhdr
, void const *data
)
1673 xheader_print_n (xhdr
, keyword
, st
->acls_a_ptr
, st
->acls_a_len
);
1677 xattr_acls_a_decoder (struct tar_stat_info
*st
,
1678 char const *keyword
, char const *arg
, size_t size
)
1680 st
->acls_a_ptr
= xmemdup (arg
, size
+ 1);
1681 st
->acls_a_len
= size
;
1685 xattr_acls_d_coder (struct tar_stat_info
const *st
, char const *keyword
,
1686 struct xheader
*xhdr
, void const *data
)
1688 xheader_print_n (xhdr
, keyword
, st
->acls_d_ptr
, st
->acls_d_len
);
1692 xattr_acls_d_decoder (struct tar_stat_info
*st
,
1693 char const *keyword
, char const *arg
, size_t size
)
1695 st
->acls_d_ptr
= xmemdup (arg
, size
+ 1);
1696 st
->acls_d_len
= size
;
1700 xattr_coder (struct tar_stat_info
const *st
, char const *keyword
,
1701 struct xheader
*xhdr
, void const *data
)
1703 struct xattr_array
*xattr_map
= st
->xattr_map
;
1704 const size_t *off
= data
;
1705 xheader_print_n (xhdr
, keyword
,
1706 xattr_map
[*off
].xval_ptr
, xattr_map
[*off
].xval_len
);
1710 xattr_decoder (struct tar_stat_info
*st
,
1711 char const *keyword
, char const *arg
, size_t size
)
1716 size_t klen_raw
= strlen (keyword
);
1717 xkey
= alloca (klen_raw
+ 1);
1718 memcpy (xkey
, keyword
, klen_raw
+ 1) /* including null-terminating */;
1721 xstr
= alloca (size
+ 1);
1722 memcpy (xstr
, arg
, size
+ 1); /* separator included, for GNU tar '\n' */;
1724 xattr_decode_keyword (xkey
);
1726 xheader_xattr_add (st
, xkey
+ strlen("SCHILY.xattr."), xstr
, size
);
1730 sparse_major_coder (struct tar_stat_info
const *st
, char const *keyword
,
1731 struct xheader
*xhdr
, void const *data
)
1733 code_num (st
->sparse_major
, keyword
, xhdr
);
1737 sparse_major_decoder (struct tar_stat_info
*st
,
1738 char const *keyword
,
1743 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1744 st
->sparse_major
= u
;
1748 sparse_minor_coder (struct tar_stat_info
const *st
, char const *keyword
,
1749 struct xheader
*xhdr
, void const *data
)
1751 code_num (st
->sparse_minor
, keyword
, xhdr
);
1755 sparse_minor_decoder (struct tar_stat_info
*st
,
1756 char const *keyword
,
1761 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1762 st
->sparse_minor
= u
;
1765 struct xhdr_tab
const xhdr_tab
[] = {
1766 { "atime", atime_coder
, atime_decoder
, 0, false },
1767 { "comment", dummy_coder
, dummy_decoder
, 0, false },
1768 { "charset", dummy_coder
, dummy_decoder
, 0, false },
1769 { "ctime", ctime_coder
, ctime_decoder
, 0, false },
1770 { "gid", gid_coder
, gid_decoder
, 0, false },
1771 { "gname", gname_coder
, gname_decoder
, 0, false },
1772 { "linkpath", linkpath_coder
, linkpath_decoder
, 0, false },
1773 { "mtime", mtime_coder
, mtime_decoder
, 0, false },
1774 { "path", path_coder
, path_decoder
, 0, false },
1775 { "size", size_coder
, size_decoder
, 0, false },
1776 { "uid", uid_coder
, uid_decoder
, 0, false },
1777 { "uname", uname_coder
, uname_decoder
, 0, false },
1779 /* Sparse file handling */
1780 { "GNU.sparse.name", path_coder
, path_decoder
,
1781 XHDR_PROTECTED
, false },
1782 { "GNU.sparse.major", sparse_major_coder
, sparse_major_decoder
,
1783 XHDR_PROTECTED
, false },
1784 { "GNU.sparse.minor", sparse_minor_coder
, sparse_minor_decoder
,
1785 XHDR_PROTECTED
, false },
1786 { "GNU.sparse.realsize", sparse_size_coder
, sparse_size_decoder
,
1787 XHDR_PROTECTED
, false },
1788 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
1789 XHDR_PROTECTED
, false },
1791 /* tar 1.14 - 1.15.90 keywords. */
1792 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
,
1793 XHDR_PROTECTED
, false },
1794 /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
1795 headers, and each of them was meaningful. It confilcted with POSIX specs,
1796 which requires that "when extended header records conflict, the last one
1797 given in the header shall take precedence." */
1798 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
1799 XHDR_PROTECTED
, false },
1800 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
1801 XHDR_PROTECTED
, false },
1802 /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
1803 { "GNU.sparse.map", NULL
/* Unused, see pax_dump_header() */,
1804 sparse_map_decoder
, 0, false },
1806 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
,
1807 XHDR_PROTECTED
, false },
1809 /* Keeps the tape/volume label. May be present only in the global headers.
1810 Equivalent to GNUTYPE_VOLHDR. */
1811 { "GNU.volume.label", volume_label_coder
, volume_label_decoder
,
1812 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1814 /* These may be present in a first global header of the archive.
1815 They provide the same functionality as GNUTYPE_MULTIVOL header.
1816 The GNU.volume.size keeps the real_s_sizeleft value, which is
1817 otherwise kept in the size field of a multivolume header. The
1818 GNU.volume.offset keeps the offset of the start of this volume,
1819 otherwise kept in oldgnu_header.offset. */
1820 { "GNU.volume.filename", volume_label_coder
, volume_filename_decoder
,
1821 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1822 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
,
1823 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1824 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
,
1825 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1827 /* We get the SELinux value from filecon, so add a namespace for SELinux
1828 instead of storing it in SCHILY.xattr.* (which would be RAW). */
1829 { "RHT.security.selinux",
1830 xattr_selinux_coder
, xattr_selinux_decoder
, 0, false },
1832 /* ACLs, use the star format... */
1833 { "SCHILY.acl.access",
1834 xattr_acls_a_coder
, xattr_acls_a_decoder
, 0, false },
1836 { "SCHILY.acl.default",
1837 xattr_acls_d_coder
, xattr_acls_d_decoder
, 0, false },
1839 /* We are storing all extended attributes using this rule even if some of them
1840 were stored by some previous rule (duplicates) -- we just have to make sure
1841 they are restored *only once* during extraction later on. */
1842 { "SCHILY.xattr", xattr_coder
, xattr_decoder
, 0, true },
1844 { NULL
, NULL
, NULL
, 0, false }