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
];
265 char const *pptr
= NULL
;
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
);
338 q
= stpcpy (q
, nptr
);
355 /* Do not allow it to end in a slash */
356 while (q
> buf
&& ISSLASH (q
[-1]))
363 xheader_xhdr_name (struct tar_stat_info
*st
)
366 assign_string (&exthdr_name
, "%d/PaxHeaders.%p/%f");
367 return xheader_format_name (st
, exthdr_name
, 0);
370 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
373 xheader_ghdr_name (void)
375 if (!globexthdr_name
)
378 const char *tmp
= getenv ("TMPDIR");
381 len
= strlen (tmp
) + sizeof (GLOBAL_HEADER_TEMPLATE
); /* Includes nul */
382 globexthdr_name
= xmalloc (len
);
383 strcpy(globexthdr_name
, tmp
);
384 strcat(globexthdr_name
, GLOBAL_HEADER_TEMPLATE
);
387 return xheader_format_name (NULL
, globexthdr_name
, global_header_count
+ 1);
391 xheader_write (char type
, char *name
, time_t t
, struct xheader
*xhdr
)
401 if (globexthdr_mtime_option
)
402 t
= globexthdr_mtime
;
406 if (exthdr_mtime_option
)
410 header
= start_private_header (name
, size
, t
);
411 header
->header
.typeflag
= type
;
413 simple_finish_header (header
);
421 header
= find_next_block ();
425 memcpy (header
->buffer
, p
, len
);
427 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
430 set_next_block_after (header
);
433 xheader_destroy (xhdr
);
436 global_header_count
++;
440 xheader_write_global (struct xheader
*xhdr
)
442 if (keyword_global_override_list
)
444 struct keyword_list
*kp
;
447 for (kp
= keyword_global_override_list
; kp
; kp
= kp
->next
)
448 code_string (kp
->value
, kp
->pattern
, xhdr
);
454 xheader_finish (xhdr
);
455 xheader_write (XGLTYPE
, name
= xheader_ghdr_name (), time (NULL
), xhdr
);
461 xheader_xattr_init (struct tar_stat_info
*st
)
463 st
->xattr_map
= NULL
;
464 st
->xattr_map_size
= 0;
466 st
->acls_a_ptr
= NULL
;
468 st
->acls_d_ptr
= NULL
;
470 st
->cntx_name
= NULL
;
474 xheader_xattr_free (struct xattr_array
*xattr_map
, size_t xattr_map_size
)
478 while (scan
< xattr_map_size
)
480 free (xattr_map
[scan
].xkey
);
481 free (xattr_map
[scan
].xval_ptr
);
489 xheader_xattr__add (struct xattr_array
**xattr_map
,
490 size_t *xattr_map_size
,
491 const char *key
, const char *val
, size_t len
)
493 size_t pos
= (*xattr_map_size
)++;
495 *xattr_map
= xrealloc (*xattr_map
,
496 *xattr_map_size
* sizeof(struct xattr_array
));
497 (*xattr_map
)[pos
].xkey
= xstrdup (key
);
498 (*xattr_map
)[pos
].xval_ptr
= xmemdup (val
, len
+ 1);
499 (*xattr_map
)[pos
].xval_len
= len
;
502 /* This is reversal function for xattr_encode_keyword. See comment for
503 xattr_encode_keyword() for more info. */
505 xattr_decode_keyword (char *keyword
)
507 char *kpr
, *kpl
; /* keyword pointer left/right */
514 if (kpr
[1] == '3' && kpr
[2] == 'D')
521 else if (kpr
[1] == '2' && kpr
[2] == '5')
541 xheader_xattr_add (struct tar_stat_info
*st
,
542 const char *key
, const char *val
, size_t len
)
544 size_t klen
= strlen (key
);
545 char *xkey
= xmalloc (strlen("SCHILY.xattr.") + klen
+ 1);
548 tmp
= stpcpy (tmp
, "SCHILY.xattr.");
551 xheader_xattr__add (&st
->xattr_map
, &st
->xattr_map_size
, xkey
, val
, len
);
557 xheader_xattr_copy (const struct tar_stat_info
*st
,
558 struct xattr_array
**xattr_map
, size_t *xattr_map_size
)
565 while (scan
< st
->xattr_map_size
)
567 char *key
= st
->xattr_map
[scan
].xkey
;
568 char *val
= st
->xattr_map
[scan
].xval_ptr
;
569 size_t len
= st
->xattr_map
[scan
].xval_len
;
571 xheader_xattr__add(xattr_map
, xattr_map_size
, key
, val
, len
);
578 /* General Interface */
580 #define XHDR_PROTECTED 0x01
581 #define XHDR_GLOBAL 0x02
586 void (*coder
) (struct tar_stat_info
const *, char const *,
587 struct xheader
*, void const *data
);
588 void (*decoder
) (struct tar_stat_info
*, char const *, char const *, size_t);
590 bool prefix
; /* select handler comparing prefix only */
593 /* This declaration must be extern, because ISO C99 section 6.9.2
594 prohibits a tentative definition that has both internal linkage and
595 incomplete type. If we made it static, we'd have to declare its
596 size which would be a maintenance pain; if we put its initializer
597 here, we'd need a boatload of forward declarations, which would be
598 even more of a pain. */
599 extern struct xhdr_tab
const xhdr_tab
[];
601 static struct xhdr_tab
const *
602 locate_handler (char const *keyword
)
604 struct xhdr_tab
const *p
;
606 for (p
= xhdr_tab
; p
->keyword
; p
++)
609 if (strncmp (p
->keyword
, keyword
, strlen(p
->keyword
)) == 0)
614 if (strcmp (p
->keyword
, keyword
) == 0)
622 xheader_protected_pattern_p (const char *pattern
)
624 struct xhdr_tab
const *p
;
626 for (p
= xhdr_tab
; p
->keyword
; p
++)
627 if (!p
->prefix
&& (p
->flags
& XHDR_PROTECTED
)
628 && fnmatch (pattern
, p
->keyword
, 0) == 0)
634 xheader_protected_keyword_p (const char *keyword
)
636 struct xhdr_tab
const *p
;
638 for (p
= xhdr_tab
; p
->keyword
; p
++)
639 if (!p
->prefix
&& (p
->flags
& XHDR_PROTECTED
)
640 && strcmp (p
->keyword
, keyword
) == 0)
645 /* Decode a single extended header record, advancing *PTR to the next record.
646 Return true on success, false otherwise. */
648 decode_record (struct xheader
*xhdr
,
650 void (*handler
) (void *, char const *, char const *, size_t),
660 size_t len_max
= xhdr
->buffer
+ xhdr
->size
- start
;
662 while (*p
== ' ' || *p
== '\t')
668 ERROR ((0, 0, _("Malformed extended header: missing length")));
673 len
= u
= strtoumax (p
, &len_lim
, 10);
674 if (len
!= u
|| errno
== ERANGE
)
676 ERROR ((0, 0, _("Extended header length is out of allowed range")));
682 int len_len
= len_lim
- p
;
683 ERROR ((0, 0, _("Extended header length %*s is out of range"),
690 for (p
= len_lim
; *p
== ' ' || *p
== '\t'; p
++)
695 _("Malformed extended header: missing blank after length")));
701 if (! (p
&& p
< nextp
))
703 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
707 if (nextp
[-1] != '\n')
709 ERROR ((0, 0, _("Malformed extended header: missing newline")));
713 *p
= nextp
[-1] = '\0';
714 handler (data
, keyword
, p
+ 1, nextp
- p
- 2); /* '=' + trailing '\n' */
722 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
724 for (; kp
; kp
= kp
->next
)
726 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
728 t
->decoder (st
, t
->keyword
, kp
->value
, strlen (kp
->value
));
733 decx (void *data
, char const *keyword
, char const *value
, size_t size
)
735 struct xhdr_tab
const *t
;
736 struct tar_stat_info
*st
= data
;
738 if (xheader_keyword_deleted_p (keyword
)
739 || xheader_keyword_override_p (keyword
))
742 t
= locate_handler (keyword
);
744 t
->decoder (st
, keyword
, value
, size
);
746 WARNOPT (WARN_UNKNOWN_KEYWORD
,
747 (0, 0, _("Ignoring unknown extended header keyword '%s'"),
752 xheader_decode (struct tar_stat_info
*st
)
754 run_override_list (keyword_global_override_list
, st
);
755 run_override_list (global_header_override_list
, st
);
759 char *p
= st
->xhdr
.buffer
+ BLOCKSIZE
;
760 while (decode_record (&st
->xhdr
, &p
, decx
, st
))
763 run_override_list (keyword_override_list
, st
);
767 decg (void *data
, char const *keyword
, char const *value
,
768 size_t size
__attribute__((unused
)))
770 struct keyword_list
**kwl
= data
;
771 struct xhdr_tab
const *tab
= locate_handler (keyword
);
772 if (tab
&& (tab
->flags
& XHDR_GLOBAL
))
773 tab
->decoder (data
, keyword
, value
, size
);
775 xheader_list_append (kwl
, keyword
, value
);
779 xheader_decode_global (struct xheader
*xhdr
)
783 char *p
= xhdr
->buffer
+ BLOCKSIZE
;
785 xheader_list_destroy (&global_header_override_list
);
786 while (decode_record (xhdr
, &p
, decg
, &global_header_override_list
))
792 xheader_init (struct xheader
*xhdr
)
796 xhdr
->stk
= xmalloc (sizeof *xhdr
->stk
);
797 obstack_init (xhdr
->stk
);
802 xheader_store (char const *keyword
, struct tar_stat_info
*st
,
805 struct xhdr_tab
const *t
;
809 t
= locate_handler (keyword
);
812 if (xheader_keyword_deleted_p (keyword
)
813 || xheader_keyword_override_p (keyword
))
815 xheader_init (&st
->xhdr
);
816 t
->coder (st
, keyword
, &st
->xhdr
, data
);
820 xheader_read (struct xheader
*xhdr
, union block
*p
, size_t size
)
826 xhdr
->buffer
= xmalloc (size
+ 1);
827 xhdr
->buffer
[size
] = '\0';
837 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
839 memcpy (&xhdr
->buffer
[j
], p
->buffer
, len
);
840 set_next_block_after (p
);
842 p
= find_next_block ();
850 /* xattr_encode_keyword() substitutes '=' ~~> '%3D' and '%' ~~> '%25'
851 in extended attribute keywords. This is needed because the '=' character
852 has special purpose in extended attribute header - it splits keyword and
853 value part of header. If there was the '=' occurrence allowed inside
854 keyword, there would be no unambiguous way how to decode this extended
857 (http://lists.gnu.org/archive/html/bug-tar/2012-10/msg00017.html)
860 xattr_encode_keyword(const char *keyword
)
862 static char *encode_buffer
= NULL
;
863 static size_t encode_buffer_size
= 0;
864 size_t bp
; /* keyword/buffer pointers */
868 encode_buffer_size
= 256;
869 encode_buffer
= xmalloc (encode_buffer_size
);
874 for (bp
= 0; *keyword
!= 0; ++bp
, ++keyword
)
878 if (bp
+ 2 /* enough for URL encoding also.. */ >= encode_buffer_size
)
880 encode_buffer
= x2realloc (encode_buffer
, &encode_buffer_size
);
885 strcpy (encode_buffer
+ bp
, "%25");
890 strcpy (encode_buffer
+ bp
, "%3D");
894 encode_buffer
[bp
] = c
;
897 encode_buffer
[bp
] = 0;
899 return encode_buffer
;
903 xheader_print_n (struct xheader
*xhdr
, char const *keyword
,
904 char const *value
, size_t vsize
)
908 char nbuf
[UINTMAX_STRSIZE_BOUND
];
912 keyword
= xattr_encode_keyword (keyword
);
913 klen
= strlen (keyword
);
914 len
= klen
+ vsize
+ 3; /* ' ' + '=' + '\n' */
919 np
= umaxtostr (len
+ p
, nbuf
);
920 n
= nbuf
+ sizeof nbuf
- 1 - np
;
924 x_obstack_grow (xhdr
, np
, n
);
925 x_obstack_1grow (xhdr
, ' ');
926 x_obstack_grow (xhdr
, keyword
, klen
);
927 x_obstack_1grow (xhdr
, '=');
928 x_obstack_grow (xhdr
, value
, vsize
);
929 x_obstack_1grow (xhdr
, '\n');
933 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
935 xheader_print_n (xhdr
, keyword
, value
, strlen (value
));
939 xheader_finish (struct xheader
*xhdr
)
941 struct keyword_list
*kp
;
943 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
944 code_string (kp
->value
, kp
->pattern
, xhdr
);
946 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
950 xheader_destroy (struct xheader
*xhdr
)
954 obstack_free (xhdr
->stk
, NULL
);
965 /* Buildable strings */
968 xheader_string_begin (struct xheader
*xhdr
)
970 xhdr
->string_length
= 0;
974 xheader_string_add (struct xheader
*xhdr
, char const *s
)
979 xhdr
->string_length
+= strlen (s
);
980 x_obstack_grow (xhdr
, s
, strlen (s
));
984 xheader_string_end (struct xheader
*xhdr
, char const *keyword
)
990 char nbuf
[UINTMAX_STRSIZE_BOUND
];
998 len
= strlen (keyword
) + xhdr
->string_length
+ 3; /* ' ' + '=' + '\n' */
1003 np
= umaxtostr (len
+ p
, nbuf
);
1004 n
= nbuf
+ sizeof nbuf
- 1 - np
;
1008 p
= strlen (keyword
) + n
+ 2;
1013 _("Generated keyword/value pair is too long (keyword=%s, length=%s)"),
1015 obstack_free (xhdr
->stk
, obstack_finish (xhdr
->stk
));
1018 x_obstack_blank (xhdr
, p
);
1019 x_obstack_1grow (xhdr
, '\n');
1020 cp
= obstack_next_free (xhdr
->stk
) - xhdr
->string_length
- p
- 1;
1021 memmove (cp
+ p
, cp
, xhdr
->string_length
);
1022 cp
= stpcpy (cp
, np
);
1024 cp
= stpcpy (cp
, keyword
);
1030 /* Implementations */
1033 out_of_range_header (char const *keyword
, char const *value
,
1034 uintmax_t minus_minval
, uintmax_t maxval
)
1036 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
1037 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
1038 char *minval_string
= umaxtostr (minus_minval
, minval_buf
+ 1);
1039 char *maxval_string
= umaxtostr (maxval
, maxval_buf
);
1041 *--minval_string
= '-';
1043 /* TRANSLATORS: The first %s is the pax extended header keyword
1044 (atime, gid, etc.). */
1045 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
1046 keyword
, value
, minval_string
, maxval_string
));
1050 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
1053 if (!utf8_convert (true, string
, &outstr
))
1055 /* FIXME: report error */
1056 outstr
= xstrdup (string
);
1058 xheader_print (xhdr
, keyword
, outstr
);
1063 decode_string (char **string
, char const *arg
)
1070 if (!utf8_convert (false, arg
, string
))
1072 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
1073 assign_string (string
, arg
);
1078 code_time (struct timespec t
, char const *keyword
, struct xheader
*xhdr
)
1080 char buf
[TIMESPEC_STRSIZE_BOUND
];
1081 xheader_print (xhdr
, keyword
, code_timespec (t
, buf
));
1084 enum decode_time_status
1086 decode_time_success
,
1088 decode_time_bad_header
1091 static enum decode_time_status
1092 _decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
1095 unsigned long int ns
= 0;
1098 bool negative
= *arg
== '-';
1102 if (ISDIGIT (arg
[negative
]))
1106 intmax_t i
= strtoimax (arg
, &arg_lim
, 10);
1107 if (TYPE_SIGNED (time_t) ? i
< TYPE_MINIMUM (time_t) : i
< 0)
1108 return decode_time_range
;
1113 uintmax_t i
= strtoumax (arg
, &arg_lim
, 10);
1114 if (TYPE_MAXIMUM (time_t) < i
)
1115 return decode_time_range
;
1121 if (errno
== ERANGE
)
1122 return decode_time_range
;
1127 bool trailing_nonzero
= false;
1129 while (ISDIGIT (*++p
))
1130 if (digits
< LOG10_BILLION
)
1132 ns
= 10 * ns
+ (*p
- '0');
1136 trailing_nonzero
|= *p
!= '0';
1138 while (digits
++ < LOG10_BILLION
)
1143 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
1144 I.e., truncate time stamps towards minus infinity while
1145 converting them to internal form. */
1146 ns
+= trailing_nonzero
;
1149 if (s
== TYPE_MINIMUM (time_t))
1150 return decode_time_range
;
1161 return decode_time_success
;
1165 return decode_time_bad_header
;
1169 decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
1171 switch (_decode_time (ts
, arg
, keyword
))
1173 case decode_time_success
:
1175 case decode_time_bad_header
:
1176 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1179 case decode_time_range
:
1180 out_of_range_header (keyword
, arg
, - (uintmax_t) TYPE_MINIMUM (time_t),
1181 TYPE_MAXIMUM (time_t));
1188 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
1190 char sbuf
[UINTMAX_STRSIZE_BOUND
];
1191 xheader_print (xhdr
, keyword
, umaxtostr (value
, sbuf
));
1195 decode_num (uintmax_t *num
, char const *arg
, uintmax_t maxval
,
1196 char const *keyword
)
1201 if (! (ISDIGIT (*arg
)
1202 && (errno
= 0, u
= strtoumax (arg
, &arg_lim
, 10), !*arg_lim
)))
1204 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1209 if (! (u
<= maxval
&& errno
!= ERANGE
))
1211 out_of_range_header (keyword
, arg
, 0, maxval
);
1220 dummy_coder (struct tar_stat_info
const *st
__attribute__ ((unused
)),
1221 char const *keyword
__attribute__ ((unused
)),
1222 struct xheader
*xhdr
__attribute__ ((unused
)),
1223 void const *data
__attribute__ ((unused
)))
1228 dummy_decoder (struct tar_stat_info
*st
__attribute__ ((unused
)),
1229 char const *keyword
__attribute__ ((unused
)),
1230 char const *arg
__attribute__ ((unused
)),
1231 size_t size
__attribute__((unused
)))
1236 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1237 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1239 code_time (st
->atime
, keyword
, xhdr
);
1243 atime_decoder (struct tar_stat_info
*st
,
1244 char const *keyword
,
1246 size_t size
__attribute__((unused
)))
1249 if (decode_time (&ts
, arg
, keyword
))
1254 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1255 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1257 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
1261 gid_decoder (struct tar_stat_info
*st
,
1262 char const *keyword
,
1264 size_t size
__attribute__((unused
)))
1267 if (decode_num (&u
, arg
, TYPE_MAXIMUM (gid_t
), keyword
))
1268 st
->stat
.st_gid
= u
;
1272 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1273 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1275 code_string (st
->gname
, keyword
, xhdr
);
1279 gname_decoder (struct tar_stat_info
*st
,
1280 char const *keyword
__attribute__((unused
)),
1282 size_t size
__attribute__((unused
)))
1284 decode_string (&st
->gname
, arg
);
1288 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
1289 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1291 code_string (st
->link_name
, keyword
, xhdr
);
1295 linkpath_decoder (struct tar_stat_info
*st
,
1296 char const *keyword
__attribute__((unused
)),
1298 size_t size
__attribute__((unused
)))
1300 decode_string (&st
->link_name
, arg
);
1304 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1305 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1307 code_time (st
->ctime
, keyword
, xhdr
);
1311 ctime_decoder (struct tar_stat_info
*st
,
1312 char const *keyword
,
1314 size_t size
__attribute__((unused
)))
1317 if (decode_time (&ts
, arg
, keyword
))
1322 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1323 struct xheader
*xhdr
, void const *data
)
1325 struct timespec
const *mtime
= data
;
1326 code_time (mtime
? *mtime
: st
->mtime
, keyword
, xhdr
);
1330 mtime_decoder (struct tar_stat_info
*st
,
1331 char const *keyword
,
1333 size_t size
__attribute__((unused
)))
1336 if (decode_time (&ts
, arg
, keyword
))
1341 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
1342 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1344 code_string (st
->file_name
, keyword
, xhdr
);
1348 path_decoder (struct tar_stat_info
*st
,
1349 char const *keyword
__attribute__((unused
)),
1351 size_t size
__attribute__((unused
)))
1353 decode_string (&st
->orig_file_name
, arg
);
1354 decode_string (&st
->file_name
, arg
);
1355 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
1359 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1360 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1362 code_num (st
->stat
.st_size
, keyword
, xhdr
);
1366 size_decoder (struct tar_stat_info
*st
,
1367 char const *keyword
,
1369 size_t size
__attribute__((unused
)))
1372 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1373 st
->stat
.st_size
= u
;
1377 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1378 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1380 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
1384 uid_decoder (struct tar_stat_info
*st
,
1385 char const *keyword
,
1387 size_t size
__attribute__((unused
)))
1390 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uid_t
), keyword
))
1391 st
->stat
.st_uid
= u
;
1395 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1396 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1398 code_string (st
->uname
, keyword
, xhdr
);
1402 uname_decoder (struct tar_stat_info
*st
,
1403 char const *keyword
__attribute__((unused
)),
1405 size_t size
__attribute__((unused
)))
1407 decode_string (&st
->uname
, arg
);
1411 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1412 struct xheader
*xhdr
, void const *data
)
1414 size_coder (st
, keyword
, xhdr
, data
);
1418 sparse_size_decoder (struct tar_stat_info
*st
,
1419 char const *keyword
,
1421 size_t size
__attribute__((unused
)))
1424 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1425 st
->stat
.st_size
= u
;
1429 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
1430 struct xheader
*xhdr
,
1431 void const *data
__attribute__ ((unused
)))
1433 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
1437 sparse_numblocks_decoder (struct tar_stat_info
*st
,
1438 char const *keyword
,
1440 size_t size
__attribute__((unused
)))
1443 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1445 st
->sparse_map_size
= u
;
1446 st
->sparse_map
= xcalloc (u
, sizeof st
->sparse_map
[0]);
1447 st
->sparse_map_avail
= 0;
1452 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1453 struct xheader
*xhdr
, void const *data
)
1455 size_t const *pi
= data
;
1456 code_num (st
->sparse_map
[*pi
].offset
, keyword
, xhdr
);
1460 sparse_offset_decoder (struct tar_stat_info
*st
,
1461 char const *keyword
,
1463 size_t size
__attribute__((unused
)))
1466 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1468 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1469 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
1471 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1472 "GNU.sparse.offset", arg
));
1477 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
1478 struct xheader
*xhdr
, void const *data
)
1480 size_t const *pi
= data
;
1481 code_num (st
->sparse_map
[*pi
].numbytes
, keyword
, xhdr
);
1485 sparse_numbytes_decoder (struct tar_stat_info
*st
,
1486 char const *keyword
,
1488 size_t size
__attribute__((unused
)))
1491 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1493 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1494 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
1496 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1502 sparse_map_decoder (struct tar_stat_info
*st
,
1503 char const *keyword
,
1505 size_t size
__attribute__((unused
)))
1509 st
->sparse_map_avail
= 0;
1516 if (!ISDIGIT (*arg
))
1518 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1524 u
= strtoumax (arg
, &delim
, 10);
1528 if (!(u
== e
.offset
&& errno
!= ERANGE
))
1530 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1537 if (!(u
== e
.numbytes
&& errno
!= ERANGE
))
1539 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1542 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1543 st
->sparse_map
[st
->sparse_map_avail
++] = e
;
1546 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1556 else if (*delim
!= ',')
1559 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1569 _("Malformed extended header: invalid %s: odd number of values"),
1574 dumpdir_coder (struct tar_stat_info
const *st
, char const *keyword
,
1575 struct xheader
*xhdr
, void const *data
)
1577 xheader_print_n (xhdr
, keyword
, data
, dumpdir_size (data
));
1581 dumpdir_decoder (struct tar_stat_info
*st
,
1582 char const *keyword
__attribute__((unused
)),
1586 st
->dumpdir
= xmalloc (size
);
1587 memcpy (st
->dumpdir
, arg
, size
);
1591 volume_label_coder (struct tar_stat_info
const *st
, char const *keyword
,
1592 struct xheader
*xhdr
, void const *data
)
1594 code_string (data
, keyword
, xhdr
);
1598 volume_label_decoder (struct tar_stat_info
*st
,
1599 char const *keyword
__attribute__((unused
)),
1601 size_t size
__attribute__((unused
)))
1603 decode_string (&volume_label
, arg
);
1607 volume_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1608 struct xheader
*xhdr
, void const *data
)
1610 off_t
const *v
= data
;
1611 code_num (*v
, keyword
, xhdr
);
1615 volume_size_decoder (struct tar_stat_info
*st
,
1616 char const *keyword
,
1617 char const *arg
, size_t size
)
1620 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1621 continued_file_size
= u
;
1624 /* FIXME: Merge with volume_size_coder */
1626 volume_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1627 struct xheader
*xhdr
, void const *data
)
1629 off_t
const *v
= data
;
1630 code_num (*v
, keyword
, xhdr
);
1634 volume_offset_decoder (struct tar_stat_info
*st
,
1635 char const *keyword
,
1636 char const *arg
, size_t size
)
1639 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1640 continued_file_offset
= u
;
1644 volume_filename_decoder (struct tar_stat_info
*st
,
1645 char const *keyword
__attribute__((unused
)),
1647 size_t size
__attribute__((unused
)))
1649 decode_string (&continued_file_name
, arg
);
1653 xattr_selinux_coder (struct tar_stat_info
const *st
, char const *keyword
,
1654 struct xheader
*xhdr
, void const *data
)
1656 code_string (st
->cntx_name
, keyword
, xhdr
);
1660 xattr_selinux_decoder (struct tar_stat_info
*st
,
1661 char const *keyword
, char const *arg
, size_t size
)
1663 decode_string (&st
->cntx_name
, arg
);
1667 xattr_acls_a_coder (struct tar_stat_info
const *st
, char const *keyword
,
1668 struct xheader
*xhdr
, void const *data
)
1670 xheader_print_n (xhdr
, keyword
, st
->acls_a_ptr
, st
->acls_a_len
);
1674 xattr_acls_a_decoder (struct tar_stat_info
*st
,
1675 char const *keyword
, char const *arg
, size_t size
)
1677 st
->acls_a_ptr
= xmemdup (arg
, size
+ 1);
1678 st
->acls_a_len
= size
;
1682 xattr_acls_d_coder (struct tar_stat_info
const *st
, char const *keyword
,
1683 struct xheader
*xhdr
, void const *data
)
1685 xheader_print_n (xhdr
, keyword
, st
->acls_d_ptr
, st
->acls_d_len
);
1689 xattr_acls_d_decoder (struct tar_stat_info
*st
,
1690 char const *keyword
, char const *arg
, size_t size
)
1692 st
->acls_d_ptr
= xmemdup (arg
, size
+ 1);
1693 st
->acls_d_len
= size
;
1697 xattr_coder (struct tar_stat_info
const *st
, char const *keyword
,
1698 struct xheader
*xhdr
, void const *data
)
1700 struct xattr_array
*xattr_map
= st
->xattr_map
;
1701 const size_t *off
= data
;
1702 xheader_print_n (xhdr
, keyword
,
1703 xattr_map
[*off
].xval_ptr
, xattr_map
[*off
].xval_len
);
1707 xattr_decoder (struct tar_stat_info
*st
,
1708 char const *keyword
, char const *arg
, size_t size
)
1713 size_t klen_raw
= strlen (keyword
);
1714 xkey
= alloca (klen_raw
+ 1);
1715 memcpy (xkey
, keyword
, klen_raw
+ 1) /* including null-terminating */;
1718 xstr
= alloca (size
+ 1);
1719 memcpy (xstr
, arg
, size
+ 1); /* separator included, for GNU tar '\n' */;
1721 xattr_decode_keyword (xkey
);
1723 xheader_xattr_add (st
, xkey
+ strlen("SCHILY.xattr."), xstr
, size
);
1727 sparse_major_coder (struct tar_stat_info
const *st
, char const *keyword
,
1728 struct xheader
*xhdr
, void const *data
)
1730 code_num (st
->sparse_major
, keyword
, xhdr
);
1734 sparse_major_decoder (struct tar_stat_info
*st
,
1735 char const *keyword
,
1740 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1741 st
->sparse_major
= u
;
1745 sparse_minor_coder (struct tar_stat_info
const *st
, char const *keyword
,
1746 struct xheader
*xhdr
, void const *data
)
1748 code_num (st
->sparse_minor
, keyword
, xhdr
);
1752 sparse_minor_decoder (struct tar_stat_info
*st
,
1753 char const *keyword
,
1758 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1759 st
->sparse_minor
= u
;
1762 struct xhdr_tab
const xhdr_tab
[] = {
1763 { "atime", atime_coder
, atime_decoder
, 0, false },
1764 { "comment", dummy_coder
, dummy_decoder
, 0, false },
1765 { "charset", dummy_coder
, dummy_decoder
, 0, false },
1766 { "ctime", ctime_coder
, ctime_decoder
, 0, false },
1767 { "gid", gid_coder
, gid_decoder
, 0, false },
1768 { "gname", gname_coder
, gname_decoder
, 0, false },
1769 { "linkpath", linkpath_coder
, linkpath_decoder
, 0, false },
1770 { "mtime", mtime_coder
, mtime_decoder
, 0, false },
1771 { "path", path_coder
, path_decoder
, 0, false },
1772 { "size", size_coder
, size_decoder
, 0, false },
1773 { "uid", uid_coder
, uid_decoder
, 0, false },
1774 { "uname", uname_coder
, uname_decoder
, 0, false },
1776 /* Sparse file handling */
1777 { "GNU.sparse.name", path_coder
, path_decoder
,
1778 XHDR_PROTECTED
, false },
1779 { "GNU.sparse.major", sparse_major_coder
, sparse_major_decoder
,
1780 XHDR_PROTECTED
, false },
1781 { "GNU.sparse.minor", sparse_minor_coder
, sparse_minor_decoder
,
1782 XHDR_PROTECTED
, false },
1783 { "GNU.sparse.realsize", sparse_size_coder
, sparse_size_decoder
,
1784 XHDR_PROTECTED
, false },
1785 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
1786 XHDR_PROTECTED
, false },
1788 /* tar 1.14 - 1.15.90 keywords. */
1789 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
,
1790 XHDR_PROTECTED
, false },
1791 /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
1792 headers, and each of them was meaningful. It confilcted with POSIX specs,
1793 which requires that "when extended header records conflict, the last one
1794 given in the header shall take precedence." */
1795 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
1796 XHDR_PROTECTED
, false },
1797 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
1798 XHDR_PROTECTED
, false },
1799 /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
1800 { "GNU.sparse.map", NULL
/* Unused, see pax_dump_header() */,
1801 sparse_map_decoder
, 0, false },
1803 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
,
1804 XHDR_PROTECTED
, false },
1806 /* Keeps the tape/volume label. May be present only in the global headers.
1807 Equivalent to GNUTYPE_VOLHDR. */
1808 { "GNU.volume.label", volume_label_coder
, volume_label_decoder
,
1809 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1811 /* These may be present in a first global header of the archive.
1812 They provide the same functionality as GNUTYPE_MULTIVOL header.
1813 The GNU.volume.size keeps the real_s_sizeleft value, which is
1814 otherwise kept in the size field of a multivolume header. The
1815 GNU.volume.offset keeps the offset of the start of this volume,
1816 otherwise kept in oldgnu_header.offset. */
1817 { "GNU.volume.filename", volume_label_coder
, volume_filename_decoder
,
1818 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1819 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
,
1820 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1821 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
,
1822 XHDR_PROTECTED
| XHDR_GLOBAL
, false },
1824 /* We get the SELinux value from filecon, so add a namespace for SELinux
1825 instead of storing it in SCHILY.xattr.* (which would be RAW). */
1826 { "RHT.security.selinux",
1827 xattr_selinux_coder
, xattr_selinux_decoder
, 0, false },
1829 /* ACLs, use the star format... */
1830 { "SCHILY.acl.access",
1831 xattr_acls_a_coder
, xattr_acls_a_decoder
, 0, false },
1833 { "SCHILY.acl.default",
1834 xattr_acls_d_coder
, xattr_acls_d_decoder
, 0, false },
1836 /* We are storing all extended attributes using this rule even if some of them
1837 were stored by some previous rule (duplicates) -- we just have to make sure
1838 they are restored *only once* during extraction later on. */
1839 { "SCHILY.xattr", xattr_coder
, xattr_decoder
, 0, true },
1841 { NULL
, NULL
, NULL
, 0, false }