1 /* POSIX extended headers for tar.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
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 /* Template for the name field of a 'g' type header */
102 static char *globexthdr_name
;
105 xheader_keyword_deleted_p (const char *kw
)
107 struct keyword_list
*kp
;
109 for (kp
= keyword_pattern_list
; kp
; kp
= kp
->next
)
110 if (fnmatch (kp
->pattern
, kw
, 0) == 0)
116 xheader_keyword_override_p (const char *keyword
)
118 struct keyword_list
*kp
;
120 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
121 if (strcmp (kp
->pattern
, keyword
) == 0)
127 xheader_list_append (struct keyword_list
**root
, char const *kw
,
130 struct keyword_list
*kp
= xmalloc (sizeof *kp
);
131 kp
->pattern
= xstrdup (kw
);
132 kp
->value
= value
? xstrdup (value
) : NULL
;
138 xheader_list_destroy (struct keyword_list
**root
)
142 struct keyword_list
*kw
= *root
;
145 struct keyword_list
*next
= kw
->next
;
156 xheader_set_single_keyword (char *kw
)
158 USAGE_ERROR ((0, 0, _("Keyword %s is unknown or not yet implemented"), kw
));
162 xheader_set_keyword_equal (char *kw
, char *eq
)
173 while (p
> kw
&& isspace (*p
))
178 for (p
= eq
+ 1; *p
&& isspace (*p
); p
++)
181 if (strcmp (kw
, "delete") == 0)
183 if (xheader_protected_pattern_p (p
))
184 USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), quote (p
)));
185 xheader_list_append (&keyword_pattern_list
, p
, NULL
);
187 else if (strcmp (kw
, "exthdr.name") == 0)
188 assign_string (&exthdr_name
, p
);
189 else if (strcmp (kw
, "globexthdr.name") == 0)
190 assign_string (&globexthdr_name
, p
);
193 if (xheader_protected_keyword_p (kw
))
194 USAGE_ERROR ((0, 0, _("Keyword %s cannot be overridden"), kw
));
196 xheader_list_append (&keyword_global_override_list
, kw
, p
);
198 xheader_list_append (&keyword_override_list
, kw
, p
);
203 xheader_set_option (char *string
)
206 for (token
= strtok (string
, ","); token
; token
= strtok (NULL
, ","))
208 char *p
= strchr (token
, '=');
210 xheader_set_single_keyword (token
);
212 xheader_set_keyword_equal (token
, p
);
217 string Includes: Replaced By:
218 %d The directory name of the file,
219 equivalent to the result of the
220 dirname utility on the translated
222 %f The filename of the file, equivalent
223 to the result of the basename
224 utility on the translated file name.
225 %p The process ID of the pax process.
226 %n The value of the 3rd argument.
227 %% A '%' character. */
230 xheader_format_name (struct tar_stat_info
*st
, const char *fmt
, size_t n
)
233 size_t len
= strlen (fmt
);
239 char pidbuf
[UINTMAX_STRSIZE_BOUND
];
241 char nbuf
[UINTMAX_STRSIZE_BOUND
];
242 char const *nptr
= NULL
;
244 for (p
= fmt
; *p
&& (p
= strchr (p
, '%')); )
256 dirp
= dir_name (st
->orig_file_name
);
257 dir
= safer_name_suffix (dirp
, false, absolute_names_option
);
258 len
+= strlen (dir
) - 2;
265 base
= last_component (st
->orig_file_name
);
266 len
+= strlen (base
) - 2;
271 pptr
= umaxtostr (getpid (), pidbuf
);
272 len
+= pidbuf
+ sizeof pidbuf
- 1 - pptr
- 2;
276 nptr
= umaxtostr (n
, nbuf
);
277 len
+= nbuf
+ sizeof nbuf
- 1 - nptr
- 2;
283 buf
= xmalloc (len
+ 1);
284 for (q
= buf
, p
= fmt
; *p
; )
303 q
= stpcpy (q
, base
);
308 q
= stpcpy (q
, pptr
);
315 q
= stpcpy (q
, nptr
);
319 /* else fall through */
333 /* Do not allow it to end in a slash */
334 while (q
> buf
&& ISSLASH (q
[-1]))
341 xheader_xhdr_name (struct tar_stat_info
*st
)
344 assign_string (&exthdr_name
, "%d/PaxHeaders.%p/%f");
345 return xheader_format_name (st
, exthdr_name
, 0);
348 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
351 xheader_ghdr_name (void)
353 if (!globexthdr_name
)
356 const char *tmp
= getenv ("TMPDIR");
359 len
= strlen (tmp
) + sizeof (GLOBAL_HEADER_TEMPLATE
); /* Includes nul */
360 globexthdr_name
= xmalloc (len
);
361 strcpy(globexthdr_name
, tmp
);
362 strcat(globexthdr_name
, GLOBAL_HEADER_TEMPLATE
);
365 return xheader_format_name (NULL
, globexthdr_name
, global_header_count
+ 1);
369 xheader_write (char type
, char *name
, struct xheader
*xhdr
)
376 header
= start_private_header (name
, size
);
377 header
->header
.typeflag
= type
;
379 simple_finish_header (header
);
387 header
= find_next_block ();
391 memcpy (header
->buffer
, p
, len
);
393 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
396 set_next_block_after (header
);
399 xheader_destroy (xhdr
);
402 global_header_count
++;
406 xheader_write_global (struct xheader
*xhdr
)
409 struct keyword_list
*kp
;
411 if (!keyword_global_override_list
)
415 for (kp
= keyword_global_override_list
; kp
; kp
= kp
->next
)
416 code_string (kp
->value
, kp
->pattern
, xhdr
);
417 xheader_finish (xhdr
);
418 xheader_write (XGLTYPE
, name
= xheader_ghdr_name (), xhdr
);
423 /* General Interface */
428 void (*coder
) (struct tar_stat_info
const *, char const *,
429 struct xheader
*, void const *data
);
430 void (*decoder
) (struct tar_stat_info
*, char const *, char const *, size_t);
434 /* This declaration must be extern, because ISO C99 section 6.9.2
435 prohibits a tentative definition that has both internal linkage and
436 incomplete type. If we made it static, we'd have to declare its
437 size which would be a maintenance pain; if we put its initializer
438 here, we'd need a boatload of forward declarations, which would be
439 even more of a pain. */
440 extern struct xhdr_tab
const xhdr_tab
[];
442 static struct xhdr_tab
const *
443 locate_handler (char const *keyword
)
445 struct xhdr_tab
const *p
;
447 for (p
= xhdr_tab
; p
->keyword
; p
++)
448 if (strcmp (p
->keyword
, keyword
) == 0)
454 xheader_protected_pattern_p (const char *pattern
)
456 struct xhdr_tab
const *p
;
458 for (p
= xhdr_tab
; p
->keyword
; p
++)
459 if (p
->protect
&& fnmatch (pattern
, p
->keyword
, 0) == 0)
465 xheader_protected_keyword_p (const char *keyword
)
467 struct xhdr_tab
const *p
;
469 for (p
= xhdr_tab
; p
->keyword
; p
++)
470 if (p
->protect
&& strcmp (p
->keyword
, keyword
) == 0)
475 /* Decode a single extended header record, advancing *PTR to the next record.
476 Return true on success, false otherwise. */
478 decode_record (struct xheader
*xhdr
,
480 void (*handler
) (void *, char const *, char const *, size_t),
490 size_t len_max
= xhdr
->buffer
+ xhdr
->size
- start
;
492 while (*p
== ' ' || *p
== '\t')
498 ERROR ((0, 0, _("Malformed extended header: missing length")));
503 len
= u
= strtoumax (p
, &len_lim
, 10);
504 if (len
!= u
|| errno
== ERANGE
)
506 ERROR ((0, 0, _("Extended header length is out of allowed range")));
512 int len_len
= len_lim
- p
;
513 ERROR ((0, 0, _("Extended header length %*s is out of range"),
520 for (p
= len_lim
; *p
== ' ' || *p
== '\t'; p
++)
525 _("Malformed extended header: missing blank after length")));
531 if (! (p
&& p
< nextp
))
533 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
537 if (nextp
[-1] != '\n')
539 ERROR ((0, 0, _("Malformed extended header: missing newline")));
543 *p
= nextp
[-1] = '\0';
544 handler (data
, keyword
, p
+ 1, nextp
- p
- 2); /* '=' + trailing '\n' */
552 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
554 for (; kp
; kp
= kp
->next
)
556 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
558 t
->decoder (st
, t
->keyword
, kp
->value
, strlen (kp
->value
));
563 decx (void *data
, char const *keyword
, char const *value
, size_t size
)
565 struct xhdr_tab
const *t
;
566 struct tar_stat_info
*st
= data
;
568 if (xheader_keyword_deleted_p (keyword
)
569 || xheader_keyword_override_p (keyword
))
572 t
= locate_handler (keyword
);
574 t
->decoder (st
, keyword
, value
, size
);
576 WARN((0, 0, _("Ignoring unknown extended header keyword `%s'"),
581 xheader_decode (struct tar_stat_info
*st
)
583 run_override_list (keyword_global_override_list
, st
);
584 run_override_list (global_header_override_list
, st
);
588 char *p
= st
->xhdr
.buffer
+ BLOCKSIZE
;
589 while (decode_record (&st
->xhdr
, &p
, decx
, st
))
592 run_override_list (keyword_override_list
, st
);
596 decg (void *data
, char const *keyword
, char const *value
,
597 size_t size
__attribute__((unused
)))
599 struct keyword_list
**kwl
= data
;
600 xheader_list_append (kwl
, keyword
, value
);
604 xheader_decode_global (struct xheader
*xhdr
)
608 char *p
= xhdr
->buffer
+ BLOCKSIZE
;
610 xheader_list_destroy (&global_header_override_list
);
611 while (decode_record (xhdr
, &p
, decg
, &global_header_override_list
))
617 xheader_init (struct xheader
*xhdr
)
621 xhdr
->stk
= xmalloc (sizeof *xhdr
->stk
);
622 obstack_init (xhdr
->stk
);
627 xheader_store (char const *keyword
, struct tar_stat_info
*st
,
630 struct xhdr_tab
const *t
;
634 t
= locate_handler (keyword
);
637 if (xheader_keyword_deleted_p (keyword
)
638 || xheader_keyword_override_p (keyword
))
640 xheader_init (&st
->xhdr
);
641 t
->coder (st
, keyword
, &st
->xhdr
, data
);
645 xheader_read (struct xheader
*xhdr
, union block
*p
, size_t size
)
652 xhdr
->buffer
= xmalloc (size
+ 1);
653 xhdr
->buffer
[size
] = '\0';
662 memcpy (&xhdr
->buffer
[j
], p
->buffer
, len
);
663 set_next_block_after (p
);
665 p
= find_next_block ();
674 xheader_print_n (struct xheader
*xhdr
, char const *keyword
,
675 char const *value
, size_t vsize
)
677 size_t len
= strlen (keyword
) + vsize
+ 3; /* ' ' + '=' + '\n' */
680 char nbuf
[UINTMAX_STRSIZE_BOUND
];
686 np
= umaxtostr (len
+ p
, nbuf
);
687 n
= nbuf
+ sizeof nbuf
- 1 - np
;
691 x_obstack_grow (xhdr
, np
, n
);
692 x_obstack_1grow (xhdr
, ' ');
693 x_obstack_grow (xhdr
, keyword
, strlen (keyword
));
694 x_obstack_1grow (xhdr
, '=');
695 x_obstack_grow (xhdr
, value
, vsize
);
696 x_obstack_1grow (xhdr
, '\n');
700 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
702 xheader_print_n (xhdr
, keyword
, value
, strlen (value
));
706 xheader_finish (struct xheader
*xhdr
)
708 struct keyword_list
*kp
;
710 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
711 code_string (kp
->value
, kp
->pattern
, xhdr
);
713 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
717 xheader_destroy (struct xheader
*xhdr
)
721 obstack_free (xhdr
->stk
, NULL
);
732 /* Buildable strings */
735 xheader_string_begin (struct xheader
*xhdr
)
737 xhdr
->string_length
= 0;
741 xheader_string_add (struct xheader
*xhdr
, char const *s
)
746 xhdr
->string_length
+= strlen (s
);
747 x_obstack_grow (xhdr
, s
, strlen (s
));
751 xheader_string_end (struct xheader
*xhdr
, char const *keyword
)
757 char nbuf
[UINTMAX_STRSIZE_BOUND
];
765 len
= strlen (keyword
) + xhdr
->string_length
+ 3; /* ' ' + '=' + '\n' */
770 np
= umaxtostr (len
+ p
, nbuf
);
771 n
= nbuf
+ sizeof nbuf
- 1 - np
;
775 p
= strlen (keyword
) + n
+ 2;
780 _("Generated keyword/value pair is too long (keyword=%s, length=%s)"),
782 obstack_free (xhdr
->stk
, obstack_finish (xhdr
->stk
));
785 x_obstack_blank (xhdr
, p
);
786 x_obstack_1grow (xhdr
, '\n');
787 cp
= obstack_next_free (xhdr
->stk
) - xhdr
->string_length
- p
- 1;
788 memmove (cp
+ p
, cp
, xhdr
->string_length
);
789 cp
= stpcpy (cp
, np
);
791 cp
= stpcpy (cp
, keyword
);
797 /* Implementations */
800 out_of_range_header (char const *keyword
, char const *value
,
801 uintmax_t minus_minval
, uintmax_t maxval
)
803 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
804 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
805 char *minval_string
= umaxtostr (minus_minval
, minval_buf
+ 1);
806 char *maxval_string
= umaxtostr (maxval
, maxval_buf
);
808 *--minval_string
= '-';
810 /* TRANSLATORS: The first %s is the pax extended header keyword
811 (atime, gid, etc.). */
812 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
813 keyword
, value
, minval_string
, maxval_string
));
817 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
820 if (!utf8_convert (true, string
, &outstr
))
822 /* FIXME: report error */
823 outstr
= xstrdup (string
);
825 xheader_print (xhdr
, keyword
, outstr
);
830 decode_string (char **string
, char const *arg
)
837 if (!utf8_convert (false, arg
, string
))
839 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
840 assign_string (string
, arg
);
845 code_time (struct timespec t
, char const *keyword
, struct xheader
*xhdr
)
847 char buf
[TIMESPEC_STRSIZE_BOUND
];
848 xheader_print (xhdr
, keyword
, code_timespec (t
, buf
));
851 enum decode_time_status
855 decode_time_bad_header
858 static enum decode_time_status
859 _decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
862 unsigned long int ns
= 0;
865 bool negative
= *arg
== '-';
869 if (ISDIGIT (arg
[negative
]))
873 intmax_t i
= strtoimax (arg
, &arg_lim
, 10);
874 if (TYPE_SIGNED (time_t) ? i
< TYPE_MINIMUM (time_t) : i
< 0)
875 return decode_time_range
;
880 uintmax_t i
= strtoumax (arg
, &arg_lim
, 10);
881 if (TYPE_MAXIMUM (time_t) < i
)
882 return decode_time_range
;
889 return decode_time_range
;
894 bool trailing_nonzero
= false;
896 while (ISDIGIT (*++p
))
897 if (digits
< LOG10_BILLION
)
899 ns
= 10 * ns
+ (*p
- '0');
903 trailing_nonzero
|= *p
!= '0';
905 while (digits
++ < LOG10_BILLION
)
910 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
911 I.e., truncate time stamps towards minus infinity while
912 converting them to internal form. */
913 ns
+= trailing_nonzero
;
916 if (s
== TYPE_MINIMUM (time_t))
917 return decode_time_range
;
928 return decode_time_success
;
932 return decode_time_bad_header
;
936 decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
938 switch (_decode_time (ts
, arg
, keyword
))
940 case decode_time_success
:
942 case decode_time_bad_header
:
943 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
946 case decode_time_range
:
947 out_of_range_header (keyword
, arg
, - (uintmax_t) TYPE_MINIMUM (time_t),
948 TYPE_MAXIMUM (time_t));
957 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
959 char sbuf
[UINTMAX_STRSIZE_BOUND
];
960 xheader_print (xhdr
, keyword
, umaxtostr (value
, sbuf
));
964 decode_num (uintmax_t *num
, char const *arg
, uintmax_t maxval
,
970 if (! (ISDIGIT (*arg
)
971 && (errno
= 0, u
= strtoumax (arg
, &arg_lim
, 10), !*arg_lim
)))
973 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
978 if (! (u
<= maxval
&& errno
!= ERANGE
))
980 out_of_range_header (keyword
, arg
, 0, maxval
);
989 dummy_coder (struct tar_stat_info
const *st
__attribute__ ((unused
)),
990 char const *keyword
__attribute__ ((unused
)),
991 struct xheader
*xhdr
__attribute__ ((unused
)),
992 void const *data
__attribute__ ((unused
)))
997 dummy_decoder (struct tar_stat_info
*st
__attribute__ ((unused
)),
998 char const *keyword
__attribute__ ((unused
)),
999 char const *arg
__attribute__ ((unused
)),
1000 size_t size
__attribute__((unused
)))
1005 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1006 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1008 code_time (st
->atime
, keyword
, xhdr
);
1012 atime_decoder (struct tar_stat_info
*st
,
1013 char const *keyword
,
1015 size_t size
__attribute__((unused
)))
1018 if (decode_time (&ts
, arg
, keyword
))
1023 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1024 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1026 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
1030 gid_decoder (struct tar_stat_info
*st
,
1031 char const *keyword
,
1033 size_t size
__attribute__((unused
)))
1036 if (decode_num (&u
, arg
, TYPE_MAXIMUM (gid_t
), keyword
))
1037 st
->stat
.st_gid
= u
;
1041 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1042 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1044 code_string (st
->gname
, keyword
, xhdr
);
1048 gname_decoder (struct tar_stat_info
*st
,
1049 char const *keyword
__attribute__((unused
)),
1051 size_t size
__attribute__((unused
)))
1053 decode_string (&st
->gname
, arg
);
1057 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
1058 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1060 code_string (st
->link_name
, keyword
, xhdr
);
1064 linkpath_decoder (struct tar_stat_info
*st
,
1065 char const *keyword
__attribute__((unused
)),
1067 size_t size
__attribute__((unused
)))
1069 decode_string (&st
->link_name
, arg
);
1073 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1074 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1076 code_time (st
->ctime
, keyword
, xhdr
);
1080 ctime_decoder (struct tar_stat_info
*st
,
1081 char const *keyword
,
1083 size_t size
__attribute__((unused
)))
1086 if (decode_time (&ts
, arg
, keyword
))
1091 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1092 struct xheader
*xhdr
, void const *data
)
1094 struct timespec
const *mtime
= data
;
1095 code_time (mtime
? *mtime
: st
->mtime
, keyword
, xhdr
);
1099 mtime_decoder (struct tar_stat_info
*st
,
1100 char const *keyword
,
1102 size_t size
__attribute__((unused
)))
1105 if (decode_time (&ts
, arg
, keyword
))
1110 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
1111 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1113 code_string (st
->file_name
, keyword
, xhdr
);
1117 path_decoder (struct tar_stat_info
*st
,
1118 char const *keyword
__attribute__((unused
)),
1120 size_t size
__attribute__((unused
)))
1122 decode_string (&st
->orig_file_name
, arg
);
1123 decode_string (&st
->file_name
, arg
);
1124 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
1128 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1129 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1131 code_num (st
->stat
.st_size
, keyword
, xhdr
);
1135 size_decoder (struct tar_stat_info
*st
,
1136 char const *keyword
,
1138 size_t size
__attribute__((unused
)))
1141 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1142 st
->stat
.st_size
= u
;
1146 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1147 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1149 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
1153 uid_decoder (struct tar_stat_info
*st
,
1154 char const *keyword
,
1156 size_t size
__attribute__((unused
)))
1159 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uid_t
), keyword
))
1160 st
->stat
.st_uid
= u
;
1164 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1165 struct xheader
*xhdr
, void const *data
__attribute__ ((unused
)))
1167 code_string (st
->uname
, keyword
, xhdr
);
1171 uname_decoder (struct tar_stat_info
*st
,
1172 char const *keyword
__attribute__((unused
)),
1174 size_t size
__attribute__((unused
)))
1176 decode_string (&st
->uname
, arg
);
1180 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1181 struct xheader
*xhdr
, void const *data
)
1183 size_coder (st
, keyword
, xhdr
, data
);
1187 sparse_size_decoder (struct tar_stat_info
*st
,
1188 char const *keyword
,
1190 size_t size
__attribute__((unused
)))
1193 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1194 st
->stat
.st_size
= u
;
1198 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
1199 struct xheader
*xhdr
,
1200 void const *data
__attribute__ ((unused
)))
1202 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
1206 sparse_numblocks_decoder (struct tar_stat_info
*st
,
1207 char const *keyword
,
1209 size_t size
__attribute__((unused
)))
1212 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1214 st
->sparse_map_size
= u
;
1215 st
->sparse_map
= xcalloc (u
, sizeof st
->sparse_map
[0]);
1216 st
->sparse_map_avail
= 0;
1221 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1222 struct xheader
*xhdr
, void const *data
)
1224 size_t const *pi
= data
;
1225 code_num (st
->sparse_map
[*pi
].offset
, keyword
, xhdr
);
1229 sparse_offset_decoder (struct tar_stat_info
*st
,
1230 char const *keyword
,
1232 size_t size
__attribute__((unused
)))
1235 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), keyword
))
1237 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1238 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
1240 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1241 "GNU.sparse.offset", arg
));
1246 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
1247 struct xheader
*xhdr
, void const *data
)
1249 size_t const *pi
= data
;
1250 code_num (st
->sparse_map
[*pi
].numbytes
, keyword
, xhdr
);
1254 sparse_numbytes_decoder (struct tar_stat_info
*st
,
1255 char const *keyword
,
1257 size_t size
__attribute__((unused
)))
1260 if (decode_num (&u
, arg
, SIZE_MAX
, keyword
))
1262 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1263 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
1265 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1271 sparse_map_decoder (struct tar_stat_info
*st
,
1272 char const *keyword
,
1274 size_t size
__attribute__((unused
)))
1278 st
->sparse_map_avail
= 0;
1285 if (!ISDIGIT (*arg
))
1287 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1293 u
= strtoumax (arg
, &delim
, 10);
1297 if (!(u
== e
.offset
&& errno
!= ERANGE
))
1299 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1306 if (!(u
== e
.numbytes
&& errno
!= ERANGE
))
1308 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (size_t));
1311 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1312 st
->sparse_map
[st
->sparse_map_avail
++] = e
;
1315 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1325 else if (*delim
!= ',')
1328 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1338 _("Malformed extended header: invalid %s: odd number of values"),
1343 dumpdir_coder (struct tar_stat_info
const *st
, char const *keyword
,
1344 struct xheader
*xhdr
, void const *data
)
1346 xheader_print_n (xhdr
, keyword
, data
, dumpdir_size (data
));
1350 dumpdir_decoder (struct tar_stat_info
*st
,
1351 char const *keyword
__attribute__((unused
)),
1355 st
->dumpdir
= xmalloc (size
);
1356 memcpy (st
->dumpdir
, arg
, size
);
1360 volume_label_coder (struct tar_stat_info
const *st
, char const *keyword
,
1361 struct xheader
*xhdr
, void const *data
)
1363 code_string (data
, keyword
, xhdr
);
1367 volume_label_decoder (struct tar_stat_info
*st
,
1368 char const *keyword
__attribute__((unused
)),
1370 size_t size
__attribute__((unused
)))
1372 decode_string (&volume_label
, arg
);
1376 volume_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1377 struct xheader
*xhdr
, void const *data
)
1379 off_t
const *v
= data
;
1380 code_num (*v
, keyword
, xhdr
);
1384 volume_size_decoder (struct tar_stat_info
*st
,
1385 char const *keyword
,
1386 char const *arg
, size_t size
)
1389 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1390 continued_file_size
= u
;
1393 /* FIXME: Merge with volume_size_coder */
1395 volume_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1396 struct xheader
*xhdr
, void const *data
)
1398 off_t
const *v
= data
;
1399 code_num (*v
, keyword
, xhdr
);
1403 volume_offset_decoder (struct tar_stat_info
*st
,
1404 char const *keyword
,
1405 char const *arg
, size_t size
)
1408 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uintmax_t), keyword
))
1409 continued_file_offset
= u
;
1413 volume_filename_decoder (struct tar_stat_info
*st
,
1414 char const *keyword
__attribute__((unused
)),
1416 size_t size
__attribute__((unused
)))
1418 decode_string (&continued_file_name
, arg
);
1422 sparse_major_coder (struct tar_stat_info
const *st
, char const *keyword
,
1423 struct xheader
*xhdr
, void const *data
)
1425 code_num (st
->sparse_major
, keyword
, xhdr
);
1429 sparse_major_decoder (struct tar_stat_info
*st
,
1430 char const *keyword
,
1435 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1436 st
->sparse_major
= u
;
1440 sparse_minor_coder (struct tar_stat_info
const *st
, char const *keyword
,
1441 struct xheader
*xhdr
, void const *data
)
1443 code_num (st
->sparse_minor
, keyword
, xhdr
);
1447 sparse_minor_decoder (struct tar_stat_info
*st
,
1448 char const *keyword
,
1453 if (decode_num (&u
, arg
, TYPE_MAXIMUM (unsigned), keyword
))
1454 st
->sparse_minor
= u
;
1457 struct xhdr_tab
const xhdr_tab
[] = {
1458 { "atime", atime_coder
, atime_decoder
, false },
1459 { "comment", dummy_coder
, dummy_decoder
, false },
1460 { "charset", dummy_coder
, dummy_decoder
, false },
1461 { "ctime", ctime_coder
, ctime_decoder
, false },
1462 { "gid", gid_coder
, gid_decoder
, false },
1463 { "gname", gname_coder
, gname_decoder
, false },
1464 { "linkpath", linkpath_coder
, linkpath_decoder
, false },
1465 { "mtime", mtime_coder
, mtime_decoder
, false },
1466 { "path", path_coder
, path_decoder
, false },
1467 { "size", size_coder
, size_decoder
, false },
1468 { "uid", uid_coder
, uid_decoder
, false },
1469 { "uname", uname_coder
, uname_decoder
, false },
1471 /* Sparse file handling */
1472 { "GNU.sparse.name", path_coder
, path_decoder
,
1474 { "GNU.sparse.major", sparse_major_coder
, sparse_major_decoder
,
1476 { "GNU.sparse.minor", sparse_minor_coder
, sparse_minor_decoder
,
1478 { "GNU.sparse.realsize", sparse_size_coder
, sparse_size_decoder
,
1480 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
1483 /* tar 1.14 - 1.15.90 keywords. */
1484 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
, true },
1485 /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
1486 headers, and each of them was meaningful. It confilcted with POSIX specs,
1487 which requires that "when extended header records conflict, the last one
1488 given in the header shall take precedence." */
1489 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
1491 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
1493 /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
1494 { "GNU.sparse.map", NULL
/* Unused, see pax_dump_header() */,
1495 sparse_map_decoder
, false },
1497 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
,
1500 /* Keeps the tape/volume label. May be present only in the global headers.
1501 Equivalent to GNUTYPE_VOLHDR. */
1502 { "GNU.volume.label", volume_label_coder
, volume_label_decoder
, true },
1504 /* These may be present in a first global header of the archive.
1505 They provide the same functionality as GNUTYPE_MULTIVOL header.
1506 The GNU.volume.size keeps the real_s_sizeleft value, which is
1507 otherwise kept in the size field of a multivolume header. The
1508 GNU.volume.offset keeps the offset of the start of this volume,
1509 otherwise kept in oldgnu_header.offset. */
1510 { "GNU.volume.filename", volume_label_coder
, volume_filename_decoder
,
1512 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
, true },
1513 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
, true },
1515 { NULL
, NULL
, NULL
, false }