1 /* POSIX extended headers for tar.
3 Copyright (C) 2003, 2004, 2005 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 2, 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. */
31 static bool xheader_protected_pattern_p (char const *pattern
);
32 static bool xheader_protected_keyword_p (char const *keyword
);
33 static void xheader_set_single_keyword (char *) __attribute__ ((noreturn
));
35 /* Used by xheader_finish() */
36 static void code_string (char const *string
, char const *keyword
,
37 struct xheader
*xhdr
);
38 static void extended_header_init (void);
40 /* Number of global headers written so far. */
41 static size_t global_header_count
;
42 /* FIXME: Possibly it should be reset after changing the volume.
43 POSIX %n specification says that it is expanded to the sequence
44 number of current global header in *the* archive. However, for
45 multi-volume archives this will yield duplicate header names
46 in different volumes, which I'd like to avoid. The best way
47 to solve this would be to use per-archive header count as required
48 by POSIX *and* set globexthdr.name to, say,
49 $TMPDIR/GlobalHead.%p.$NUMVOLUME.%n.
51 However it should wait until buffer.c is finally rewritten */
54 /* Interface functions to obstacks */
57 x_obstack_grow (struct xheader
*xhdr
, const char *ptr
, size_t length
)
59 obstack_grow (xhdr
->stk
, ptr
, length
);
64 x_obstack_1grow (struct xheader
*xhdr
, char c
)
66 obstack_1grow (xhdr
->stk
, c
);
71 x_obstack_blank (struct xheader
*xhdr
, size_t length
)
73 obstack_blank (xhdr
->stk
, length
);
82 struct keyword_list
*next
;
88 /* List of keyword patterns set by delete= option */
89 static struct keyword_list
*keyword_pattern_list
;
91 /* List of keyword/value pairs set by `keyword=value' option */
92 static struct keyword_list
*keyword_global_override_list
;
94 /* List of keyword/value pairs set by `keyword:=value' option */
95 static struct keyword_list
*keyword_override_list
;
97 /* List of keyword/value pairs decoded from the last 'g' type header */
98 static struct keyword_list
*global_header_override_list
;
100 /* Template for the name field of an 'x' type header */
101 static char *exthdr_name
;
103 /* Template for the name field of a 'g' type header */
104 static char *globexthdr_name
;
107 xheader_keyword_deleted_p (const char *kw
)
109 struct keyword_list
*kp
;
111 for (kp
= keyword_pattern_list
; kp
; kp
= kp
->next
)
112 if (fnmatch (kp
->pattern
, kw
, 0) == 0)
118 xheader_keyword_override_p (const char *keyword
)
120 struct keyword_list
*kp
;
122 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
123 if (strcmp (kp
->pattern
, keyword
) == 0)
129 xheader_list_append (struct keyword_list
**root
, char const *kw
,
132 struct keyword_list
*kp
= xmalloc (sizeof *kp
);
133 kp
->pattern
= xstrdup (kw
);
134 kp
->value
= value
? xstrdup (value
) : NULL
;
140 xheader_list_destroy (struct keyword_list
**root
)
144 struct keyword_list
*kw
= *root
;
147 struct keyword_list
*next
= kw
->next
;
158 xheader_set_single_keyword (char *kw
)
160 USAGE_ERROR ((0, 0, _("Keyword %s is unknown or not yet imlemented"), kw
));
164 xheader_set_keyword_equal (char *kw
, char *eq
)
175 while (p
> kw
&& isspace (*p
))
180 for (p
= eq
+ 1; *p
&& isspace (*p
); p
++)
183 if (strcmp (kw
, "delete") == 0)
185 if (xheader_protected_pattern_p (p
))
186 USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), quote (p
)));
187 xheader_list_append (&keyword_pattern_list
, p
, NULL
);
189 else if (strcmp (kw
, "exthdr.name") == 0)
190 assign_string (&exthdr_name
, p
);
191 else if (strcmp (kw
, "globexthdr.name") == 0)
192 assign_string (&globexthdr_name
, p
);
195 if (xheader_protected_keyword_p (kw
))
196 USAGE_ERROR ((0, 0, _("Keyword %s cannot be overridden"), kw
));
198 xheader_list_append (&keyword_global_override_list
, kw
, p
);
200 xheader_list_append (&keyword_override_list
, kw
, p
);
205 xheader_set_option (char *string
)
208 for (token
= strtok (string
, ","); token
; token
= strtok (NULL
, ","))
210 char *p
= strchr (token
, '=');
212 xheader_set_single_keyword (token
);
214 xheader_set_keyword_equal (token
, p
);
219 string Includes: Replaced By:
220 %d The directory name of the file,
221 equivalent to the result of the
222 dirname utility on the translated
224 %f The filename of the file, equivalent
225 to the result of the basename
226 utility on the translated file name.
227 %p The process ID of the pax process.
228 %% A '%' character. */
231 xheader_format_name (struct tar_stat_info
*st
, const char *fmt
, bool allow_n
)
234 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
, '%')); )
255 dir
= safer_name_suffix (dir_name (st
->orig_file_name
),
256 false, absolute_names_option
);
257 len
+= strlen (dir
) - 1;
264 base
= base_name (st
->orig_file_name
);
265 len
+= strlen (base
) - 1;
270 pptr
= umaxtostr (getpid (), pidbuf
);
271 len
+= pidbuf
+ sizeof pidbuf
- 1 - pptr
- 1;
277 nptr
= umaxtostr (global_header_count
+ 1, nbuf
);
278 len
+= nbuf
+ sizeof nbuf
- 1 - nptr
- 1;
285 buf
= xmalloc (len
+ 1);
286 for (q
= buf
, p
= fmt
; *p
; )
305 q
= stpcpy (q
, base
);
310 q
= stpcpy (q
, pptr
);
317 q
= stpcpy (q
, nptr
);
320 /* else fall through */
332 /* Do not allow it to end in a slash */
333 while (q
> buf
&& ISSLASH (q
[-1]))
340 xheader_xhdr_name (struct tar_stat_info
*st
)
343 assign_string (&exthdr_name
, "%d/PaxHeaders.%p/%f");
344 return xheader_format_name (st
, exthdr_name
, false);
347 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
350 xheader_ghdr_name (void)
352 if (!globexthdr_name
)
355 const char *tmp
= getenv ("TMPDIR");
358 len
= strlen (tmp
) + sizeof (GLOBAL_HEADER_TEMPLATE
); /* Includes nul */
359 globexthdr_name
= xmalloc (len
);
360 strcpy(globexthdr_name
, tmp
);
361 strcat(globexthdr_name
, GLOBAL_HEADER_TEMPLATE
);
364 return xheader_format_name (NULL
, globexthdr_name
, true);
368 xheader_write (char type
, char *name
, struct xheader
*xhdr
)
375 header
= start_private_header (name
, size
);
376 header
->header
.typeflag
= type
;
378 simple_finish_header (header
);
386 header
= find_next_block ();
390 memcpy (header
->buffer
, p
, len
);
392 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
395 set_next_block_after (header
);
398 xheader_destroy (xhdr
);
402 xheader_write_global (void)
405 struct keyword_list
*kp
;
407 if (!keyword_global_override_list
)
410 extended_header_init ();
411 for (kp
= keyword_global_override_list
; kp
; kp
= kp
->next
)
412 code_string (kp
->value
, kp
->pattern
, &extended_header
);
413 xheader_finish (&extended_header
);
414 xheader_write (XGLTYPE
, name
= xheader_ghdr_name (),
417 global_header_count
++;
421 /* General Interface */
426 void (*coder
) (struct tar_stat_info
const *, char const *,
427 struct xheader
*, void *data
);
428 void (*decoder
) (struct tar_stat_info
*, char const *, size_t);
432 /* This declaration must be extern, because ISO C99 section 6.9.2
433 prohibits a tentative definition that has both internal linkage and
434 incomplete type. If we made it static, we'd have to declare its
435 size which would be a maintenance pain; if we put its initializer
436 here, we'd need a boatload of forward declarations, which would be
437 even more of a pain. */
438 extern struct xhdr_tab
const xhdr_tab
[];
440 static struct xhdr_tab
const *
441 locate_handler (char const *keyword
)
443 struct xhdr_tab
const *p
;
445 for (p
= xhdr_tab
; p
->keyword
; p
++)
446 if (strcmp (p
->keyword
, keyword
) == 0)
452 xheader_protected_pattern_p (const char *pattern
)
454 struct xhdr_tab
const *p
;
456 for (p
= xhdr_tab
; p
->keyword
; p
++)
457 if (p
->protect
&& fnmatch (pattern
, p
->keyword
, 0) == 0)
463 xheader_protected_keyword_p (const char *keyword
)
465 struct xhdr_tab
const *p
;
467 for (p
= xhdr_tab
; p
->keyword
; p
++)
468 if (p
->protect
&& strcmp (p
->keyword
, keyword
) == 0)
473 /* Decode a single extended header record, advancing *PTR to the next record.
474 Return true on success, false otherwise. */
476 decode_record (char **ptr
,
477 void (*handler
) (void *, char const *, char const *, size_t),
482 unsigned long int len
;
486 size_t len_max
= extended_header
.buffer
+ extended_header
.size
- start
;
488 while (*p
== ' ' || *p
== '\t')
494 ERROR ((0, 0, _("Malformed extended header: missing length")));
499 len
= strtoul (p
, &len_lim
, 10);
503 int len_len
= len_lim
- p
;
504 ERROR ((0, 0, _("Extended header length %*s is out of range"),
511 for (p
= len_lim
; *p
== ' ' || *p
== '\t'; p
++)
516 _("Malformed extended header: missing blank after length")));
522 if (! (p
&& p
< nextp
))
524 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
528 if (nextp
[-1] != '\n')
530 ERROR ((0, 0, _("Malformed extended header: missing newline")));
534 *p
= nextp
[-1] = '\0';
535 handler (data
, keyword
, p
+ 1, nextp
- p
- 2); /* '=' + trailing '\n' */
543 run_override_list (struct keyword_list
*kp
, struct tar_stat_info
*st
)
545 for (; kp
; kp
= kp
->next
)
547 struct xhdr_tab
const *t
= locate_handler (kp
->pattern
);
549 t
->decoder (st
, kp
->value
, strlen (kp
->value
));
554 decx (void *data
, char const *keyword
, char const *value
, size_t size
)
556 struct xhdr_tab
const *t
;
557 struct tar_stat_info
*st
= data
;
559 if (xheader_keyword_deleted_p (keyword
)
560 || xheader_keyword_override_p (keyword
))
563 t
= locate_handler (keyword
);
565 t
->decoder (st
, value
, size
);
567 ERROR((0, 0, _("Ignoring unknown extended header keyword `%s'"),
572 xheader_decode (struct tar_stat_info
*st
)
574 run_override_list (keyword_global_override_list
, st
);
575 run_override_list (global_header_override_list
, st
);
577 if (extended_header
.size
)
579 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
580 while (decode_record (&p
, decx
, st
))
583 run_override_list (keyword_override_list
, st
);
587 decg (void *data
, char const *keyword
, char const *value
,
588 size_t size
__attribute__((unused
)))
590 struct keyword_list
**kwl
= data
;
591 xheader_list_append (kwl
, keyword
, value
);
595 xheader_decode_global (void)
597 if (extended_header
.size
)
599 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
601 xheader_list_destroy (&global_header_override_list
);
602 while (decode_record (&p
, decg
, &global_header_override_list
))
608 extended_header_init (void)
610 if (!extended_header
.stk
)
612 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
613 obstack_init (extended_header
.stk
);
618 xheader_store (char const *keyword
, struct tar_stat_info
const *st
, void *data
)
620 struct xhdr_tab
const *t
;
622 if (extended_header
.buffer
)
624 t
= locate_handler (keyword
);
627 if (xheader_keyword_deleted_p (keyword
)
628 || xheader_keyword_override_p (keyword
))
630 extended_header_init ();
631 t
->coder (st
, keyword
, &extended_header
, data
);
635 xheader_read (union block
*p
, size_t size
)
640 free (extended_header
.buffer
);
642 extended_header
.size
= size
;
643 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
644 extended_header
.buffer
= xmalloc (size
+ 1);
645 extended_header
.buffer
[size
] = '\0';
654 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
655 set_next_block_after (p
);
657 p
= find_next_block ();
666 xheader_print_n (struct xheader
*xhdr
, char const *keyword
,
667 char const *value
, size_t vsize
)
669 size_t len
= strlen (keyword
) + vsize
+ 3; /* ' ' + '=' + '\n' */
672 char nbuf
[UINTMAX_STRSIZE_BOUND
];
678 np
= umaxtostr (len
+ p
, nbuf
);
679 n
= nbuf
+ sizeof nbuf
- 1 - np
;
683 x_obstack_grow (xhdr
, np
, n
);
684 x_obstack_1grow (xhdr
, ' ');
685 x_obstack_grow (xhdr
, keyword
, strlen (keyword
));
686 x_obstack_1grow (xhdr
, '=');
687 x_obstack_grow (xhdr
, value
, vsize
);
688 x_obstack_1grow (xhdr
, '\n');
692 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
694 xheader_print_n (xhdr
, keyword
, value
, strlen (value
));
698 xheader_finish (struct xheader
*xhdr
)
700 struct keyword_list
*kp
;
702 for (kp
= keyword_override_list
; kp
; kp
= kp
->next
)
703 code_string (kp
->value
, kp
->pattern
, xhdr
);
705 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
709 xheader_destroy (struct xheader
*xhdr
)
713 obstack_free (xhdr
->stk
, NULL
);
724 /* Buildable strings */
725 static uintmax_t string_length
;
728 xheader_string_begin ()
734 xheader_string_add (char const *s
)
736 if (extended_header
.buffer
)
738 extended_header_init ();
739 string_length
+= strlen (s
);
740 x_obstack_grow (&extended_header
, s
, strlen (s
));
744 xheader_string_end (char const *keyword
)
749 char nbuf
[UINTMAX_STRSIZE_BOUND
];
753 if (extended_header
.buffer
)
755 extended_header_init ();
757 len
= strlen (keyword
) + string_length
+ 3; /* ' ' + '=' + '\n' */
762 np
= umaxtostr (len
+ p
, nbuf
);
763 n
= nbuf
+ sizeof nbuf
- 1 - np
;
767 p
= strlen (keyword
) + n
+ 2;
768 x_obstack_blank (&extended_header
, p
);
769 x_obstack_1grow (&extended_header
, '\n');
770 cp
= obstack_next_free (extended_header
.stk
) - string_length
- p
- 1;
771 memmove (cp
+ p
, cp
, string_length
);
772 cp
= stpcpy (cp
, np
);
774 cp
= stpcpy (cp
, keyword
);
779 /* Implementations */
782 out_of_range_header (char const *keyword
, char const *value
,
783 uintmax_t minus_minval
, uintmax_t maxval
)
785 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
786 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
787 char *minval_string
= umaxtostr (minus_minval
, minval_buf
+ 1);
788 char *maxval_string
= umaxtostr (maxval
, maxval_buf
);
790 *--minval_string
= '-';
792 /* TRANSLATORS: The first %s is the pax extended header keyword
793 (atime, gid, etc.). */
794 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
795 keyword
, value
, minval_string
, maxval_string
));
799 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
802 if (!utf8_convert (true, string
, &outstr
))
804 /* FIXME: report error */
805 outstr
= xstrdup (string
);
807 xheader_print (xhdr
, keyword
, outstr
);
812 decode_string (char **string
, char const *arg
)
819 if (!utf8_convert (false, arg
, string
))
821 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
822 assign_string (string
, arg
);
827 code_time (struct timespec t
, char const *keyword
, struct xheader
*xhdr
)
829 char buf
[TIMESPEC_STRSIZE_BOUND
];
830 xheader_print (xhdr
, keyword
, code_timespec (t
, buf
));
834 decode_time (struct timespec
*ts
, char const *arg
, char const *keyword
)
837 unsigned long int ns
= 0;
840 bool negative
= *arg
== '-';
844 if (ISDIGIT (arg
[negative
]))
848 intmax_t i
= strtoimax (arg
, &arg_lim
, 10);
849 if (TYPE_SIGNED (time_t) ? i
< TYPE_MINIMUM (time_t) : i
< 0)
855 uintmax_t i
= strtoumax (arg
, &arg_lim
, 10);
856 if (TYPE_MAXIMUM (time_t) < i
)
869 bool trailing_nonzero
= false;
871 while (ISDIGIT (*++p
))
872 if (digits
< LOG10_BILLION
)
874 ns
= 10 * ns
+ (*p
- '0');
878 trailing_nonzero
|= *p
!= '0';
880 while (digits
++ < LOG10_BILLION
)
885 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
886 I.e., truncate time stamps towards minus infinity while
887 converting them to internal form. */
888 ns
+= trailing_nonzero
;
891 if (s
== TYPE_MINIMUM (time_t))
907 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
912 out_of_range_header (keyword
, arg
, - (uintmax_t) TYPE_MINIMUM (time_t),
913 TYPE_MAXIMUM (time_t));
918 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
920 char sbuf
[UINTMAX_STRSIZE_BOUND
];
921 xheader_print (xhdr
, keyword
, umaxtostr (value
, sbuf
));
925 decode_num (uintmax_t *num
, char const *arg
, uintmax_t maxval
,
931 if (! (ISDIGIT (*arg
)
932 && (errno
= 0, u
= strtoumax (arg
, &arg_lim
, 10), !*arg_lim
)))
934 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
939 if (! (u
<= maxval
&& errno
!= ERANGE
))
941 out_of_range_header (keyword
, arg
, 0, maxval
);
950 dummy_coder (struct tar_stat_info
const *st
__attribute__ ((unused
)),
951 char const *keyword
__attribute__ ((unused
)),
952 struct xheader
*xhdr
__attribute__ ((unused
)),
953 void *data
__attribute__ ((unused
)))
958 dummy_decoder (struct tar_stat_info
*st
__attribute__ ((unused
)),
959 char const *arg
__attribute__ ((unused
)),
960 size_t size
__attribute__((unused
)))
965 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
966 struct xheader
*xhdr
, void *data
__attribute__ ((unused
)))
968 code_time (st
->atime
, keyword
, xhdr
);
972 atime_decoder (struct tar_stat_info
*st
, char const *arg
,
973 size_t size
__attribute__((unused
)))
976 if (decode_time (&ts
, arg
, "atime"))
981 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
982 struct xheader
*xhdr
, void *data
__attribute__ ((unused
)))
984 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
988 gid_decoder (struct tar_stat_info
*st
, char const *arg
,
989 size_t size
__attribute__((unused
)))
992 if (decode_num (&u
, arg
, TYPE_MAXIMUM (gid_t
), "gid"))
997 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
998 struct xheader
*xhdr
, void *data
__attribute__ ((unused
)))
1000 code_string (st
->gname
, keyword
, xhdr
);
1004 gname_decoder (struct tar_stat_info
*st
, char const *arg
,
1005 size_t size
__attribute__((unused
)))
1007 decode_string (&st
->gname
, arg
);
1011 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
1012 struct xheader
*xhdr
, void *data
__attribute__ ((unused
)))
1014 code_string (st
->link_name
, keyword
, xhdr
);
1018 linkpath_decoder (struct tar_stat_info
*st
, char const *arg
,
1019 size_t size
__attribute__((unused
)))
1021 decode_string (&st
->link_name
, arg
);
1025 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1026 struct xheader
*xhdr
, void *data
__attribute__ ((unused
)))
1028 code_time (st
->ctime
, keyword
, xhdr
);
1032 ctime_decoder (struct tar_stat_info
*st
, char const *arg
,
1033 size_t size
__attribute__((unused
)))
1036 if (decode_time (&ts
, arg
, "ctime"))
1041 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
1042 struct xheader
*xhdr
, void *data
__attribute__ ((unused
)))
1044 code_time (st
->mtime
, keyword
, xhdr
);
1048 mtime_decoder (struct tar_stat_info
*st
, char const *arg
,
1049 size_t size
__attribute__((unused
)))
1052 if (decode_time (&ts
, arg
, "mtime"))
1057 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
1058 struct xheader
*xhdr
, void *data
__attribute__ ((unused
)))
1060 code_string (st
->file_name
, keyword
, xhdr
);
1064 path_decoder (struct tar_stat_info
*st
, char const *arg
,
1065 size_t size
__attribute__((unused
)))
1067 decode_string (&st
->orig_file_name
, arg
);
1068 decode_string (&st
->file_name
, arg
);
1069 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
1073 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1074 struct xheader
*xhdr
, void *data
__attribute__ ((unused
)))
1076 code_num (st
->stat
.st_size
, keyword
, xhdr
);
1080 size_decoder (struct tar_stat_info
*st
, char const *arg
,
1081 size_t size
__attribute__((unused
)))
1084 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), "size"))
1085 st
->stat
.st_size
= u
;
1089 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
1090 struct xheader
*xhdr
, void *data
__attribute__ ((unused
)))
1092 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
1096 uid_decoder (struct tar_stat_info
*st
, char const *arg
,
1097 size_t size
__attribute__((unused
)))
1100 if (decode_num (&u
, arg
, TYPE_MAXIMUM (uid_t
), "uid"))
1101 st
->stat
.st_uid
= u
;
1105 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
1106 struct xheader
*xhdr
, void *data
__attribute__ ((unused
)))
1108 code_string (st
->uname
, keyword
, xhdr
);
1112 uname_decoder (struct tar_stat_info
*st
, char const *arg
,
1113 size_t size
__attribute__((unused
)))
1115 decode_string (&st
->uname
, arg
);
1119 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
1120 struct xheader
*xhdr
, void *data
)
1122 size_coder (st
, keyword
, xhdr
, data
);
1126 sparse_size_decoder (struct tar_stat_info
*st
, char const *arg
,
1127 size_t size
__attribute__((unused
)))
1130 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), "GNU.sparse.size"))
1131 st
->stat
.st_size
= u
;
1135 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
1136 struct xheader
*xhdr
,
1137 void *data
__attribute__ ((unused
)))
1139 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
1143 sparse_numblocks_decoder (struct tar_stat_info
*st
, char const *arg
,
1144 size_t size
__attribute__((unused
)))
1147 if (decode_num (&u
, arg
, SIZE_MAX
, "GNU.sparse.numblocks"))
1149 st
->sparse_map_size
= u
;
1150 st
->sparse_map
= xcalloc (u
, sizeof st
->sparse_map
[0]);
1151 st
->sparse_map_avail
= 0;
1156 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
1157 struct xheader
*xhdr
, void *data
)
1160 code_num (st
->sparse_map
[*pi
].offset
, keyword
, xhdr
);
1164 sparse_offset_decoder (struct tar_stat_info
*st
, char const *arg
,
1165 size_t size
__attribute__((unused
)))
1168 if (decode_num (&u
, arg
, TYPE_MAXIMUM (off_t
), "GNU.sparse.offset"))
1170 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1171 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
1173 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1174 "GNU.sparse.offset", arg
));
1179 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
1180 struct xheader
*xhdr
, void *data
)
1183 code_num (st
->sparse_map
[*pi
].numbytes
, keyword
, xhdr
);
1187 sparse_numbytes_decoder (struct tar_stat_info
*st
, char const *arg
,
1188 size_t size
__attribute__((unused
)))
1191 if (decode_num (&u
, arg
, SIZE_MAX
, "GNU.sparse.numbytes"))
1193 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1194 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
1196 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1197 "GNU.sparse.numbytes", arg
));
1202 sparse_map_decoder (struct tar_stat_info
*st
, char const *arg
,
1203 size_t size
__attribute__((unused
)))
1206 static char *keyword
= "GNU.sparse.map";
1208 st
->sparse_map_avail
= 0;
1215 if (!ISDIGIT (*arg
))
1217 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1223 u
= strtoumax (arg
, &delim
, 10);
1227 if (!(u
== e
.offset
&& errno
!= ERANGE
))
1229 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (off_t
));
1236 if (!(u
== e
.numbytes
&& errno
!= ERANGE
))
1238 out_of_range_header (keyword
, arg
, 0, TYPE_MAXIMUM (size_t));
1241 if (st
->sparse_map_avail
< st
->sparse_map_size
)
1242 st
->sparse_map
[st
->sparse_map_avail
++] = e
;
1245 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1246 "GNU.sparse.numbytes", arg
));
1255 else if (*delim
!= ',')
1258 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1268 _("Malformed extended header: invalid %s: odd number of values"),
1273 dumpdir_coder (struct tar_stat_info
const *st
, char const *keyword
,
1274 struct xheader
*xhdr
, void *data
)
1276 xheader_print_n (xhdr
, keyword
, data
, dumpdir_size (data
));
1280 dumpdir_decoder (struct tar_stat_info
*st
, char const *arg
,
1283 st
->dumpdir
= xmalloc (size
);
1284 memcpy (st
->dumpdir
, arg
, size
);
1287 struct xhdr_tab
const xhdr_tab
[] = {
1288 { "atime", atime_coder
, atime_decoder
, false },
1289 { "comment", dummy_coder
, dummy_decoder
, false },
1290 { "charset", dummy_coder
, dummy_decoder
, false },
1291 { "ctime", ctime_coder
, ctime_decoder
, false },
1292 { "gid", gid_coder
, gid_decoder
, false },
1293 { "gname", gname_coder
, gname_decoder
, false },
1294 { "linkpath", linkpath_coder
, linkpath_decoder
, false },
1295 { "mtime", mtime_coder
, mtime_decoder
, false },
1296 { "path", path_coder
, path_decoder
, false },
1297 { "size", size_coder
, size_decoder
, false },
1298 { "uid", uid_coder
, uid_decoder
, false },
1299 { "uname", uname_coder
, uname_decoder
, false },
1301 /* Sparse file handling */
1302 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
, true },
1303 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
,
1305 /* tar 1.14 - 1.15.1 keywords. Multiplse instances of these appeared in 'x'
1306 headers, and each of them was meaningful. It confilcted with POSIX specs,
1307 which requires that "when extended header records conflict, the last one
1308 given in the header shall take precedence." */
1309 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
,
1311 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
,
1313 /* tar >=1.16 keyword, introduced to remove the above-mentioned conflict. */
1314 { "GNU.sparse.map", NULL
/* Unused, see pax_dump_header() */,
1315 sparse_map_decoder
, false },
1317 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
,
1320 #if 0 /* GNU private keywords (not yet implemented) */
1322 /* Keeps the tape/volume header. May be present only in the global headers.
1323 Equivalent to GNUTYPE_VOLHDR. */
1324 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
, false },
1326 /* These may be present in a first global header of the archive.
1327 They provide the same functionality as GNUTYPE_MULTIVOL header.
1328 The GNU.volume.size keeps the real_s_sizeleft value, which is
1329 otherwise kept in the size field of a multivolume header. The
1330 GNU.volume.offset keeps the offset of the start of this volume,
1331 otherwise kept in oldgnu_header.offset. */
1332 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
, false },
1333 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
, false },
1336 { NULL
, NULL
, NULL
, false }