]> Dogcows Code - chaz/tar/blob - src/xheader.c
(check_time): Use volume_start_time when checking
[chaz/tar] / src / xheader.c
1 /* POSIX extended headers for tar.
2
3 Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4
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
8 version.
9
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.
14
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. */
18
19 #include <system.h>
20
21 #include <fnmatch.h>
22 #include <hash.h>
23 #include <inttostr.h>
24 #include <quotearg.h>
25 #include <stpcpy.h>
26
27 #include "common.h"
28
29 #include <fnmatch.h>
30
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));
34
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);
39
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.
50
51 However it should wait until buffer.c is finally rewritten */
52
53 \f
54 /* Interface functions to obstacks */
55
56 static void
57 x_obstack_grow (struct xheader *xhdr, const char *ptr, size_t length)
58 {
59 obstack_grow (xhdr->stk, ptr, length);
60 xhdr->size += length;
61 }
62
63 static void
64 x_obstack_1grow (struct xheader *xhdr, char c)
65 {
66 obstack_1grow (xhdr->stk, c);
67 xhdr->size++;
68 }
69
70 static void
71 x_obstack_blank (struct xheader *xhdr, size_t length)
72 {
73 obstack_blank (xhdr->stk, length);
74 xhdr->size += length;
75 }
76
77 \f
78 /* Keyword options */
79
80 struct keyword_list
81 {
82 struct keyword_list *next;
83 char *pattern;
84 char *value;
85 };
86
87
88 /* List of keyword patterns set by delete= option */
89 static struct keyword_list *keyword_pattern_list;
90
91 /* List of keyword/value pairs set by `keyword=value' option */
92 static struct keyword_list *keyword_global_override_list;
93
94 /* List of keyword/value pairs set by `keyword:=value' option */
95 static struct keyword_list *keyword_override_list;
96
97 /* List of keyword/value pairs decoded from the last 'g' type header */
98 static struct keyword_list *global_header_override_list;
99
100 /* Template for the name field of an 'x' type header */
101 static char *exthdr_name;
102
103 /* Template for the name field of a 'g' type header */
104 static char *globexthdr_name;
105
106 bool
107 xheader_keyword_deleted_p (const char *kw)
108 {
109 struct keyword_list *kp;
110
111 for (kp = keyword_pattern_list; kp; kp = kp->next)
112 if (fnmatch (kp->pattern, kw, 0) == 0)
113 return true;
114 return false;
115 }
116
117 static bool
118 xheader_keyword_override_p (const char *keyword)
119 {
120 struct keyword_list *kp;
121
122 for (kp = keyword_override_list; kp; kp = kp->next)
123 if (strcmp (kp->pattern, keyword) == 0)
124 return true;
125 return false;
126 }
127
128 static void
129 xheader_list_append (struct keyword_list **root, char const *kw,
130 char const *value)
131 {
132 struct keyword_list *kp = xmalloc (sizeof *kp);
133 kp->pattern = xstrdup (kw);
134 kp->value = value ? xstrdup (value) : NULL;
135 kp->next = *root;
136 *root = kp;
137 }
138
139 static void
140 xheader_list_destroy (struct keyword_list **root)
141 {
142 if (root)
143 {
144 struct keyword_list *kw = *root;
145 while (kw)
146 {
147 struct keyword_list *next = kw->next;
148 free (kw->pattern);
149 free (kw->value);
150 free (kw);
151 kw = next;
152 }
153 *root = NULL;
154 }
155 }
156
157 static void
158 xheader_set_single_keyword (char *kw)
159 {
160 USAGE_ERROR ((0, 0, _("Keyword %s is unknown or not yet implemented"), kw));
161 }
162
163 static void
164 xheader_set_keyword_equal (char *kw, char *eq)
165 {
166 bool global = true;
167 char *p = eq;
168
169 if (eq[-1] == ':')
170 {
171 p--;
172 global = false;
173 }
174
175 while (p > kw && isspace (*p))
176 p--;
177
178 *p = 0;
179
180 for (p = eq + 1; *p && isspace (*p); p++)
181 ;
182
183 if (strcmp (kw, "delete") == 0)
184 {
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);
188 }
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);
193 else
194 {
195 if (xheader_protected_keyword_p (kw))
196 USAGE_ERROR ((0, 0, _("Keyword %s cannot be overridden"), kw));
197 if (global)
198 xheader_list_append (&keyword_global_override_list, kw, p);
199 else
200 xheader_list_append (&keyword_override_list, kw, p);
201 }
202 }
203
204 void
205 xheader_set_option (char *string)
206 {
207 char *token;
208 for (token = strtok (string, ","); token; token = strtok (NULL, ","))
209 {
210 char *p = strchr (token, '=');
211 if (!p)
212 xheader_set_single_keyword (token);
213 else
214 xheader_set_keyword_equal (token, p);
215 }
216 }
217
218 /*
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
223 file name.
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 %n The value of the 3rd argument.
229 %% A '%' character. */
230
231 char *
232 xheader_format_name (struct tar_stat_info *st, const char *fmt, size_t n)
233 {
234 char *buf;
235 size_t len = strlen (fmt);
236 char *q;
237 const char *p;
238 char *dirp = NULL;
239 char *dir = NULL;
240 char *base = NULL;
241 char pidbuf[UINTMAX_STRSIZE_BOUND];
242 char const *pptr;
243 char nbuf[UINTMAX_STRSIZE_BOUND];
244 char const *nptr = NULL;
245
246 for (p = fmt; *p && (p = strchr (p, '%')); )
247 {
248 switch (p[1])
249 {
250 case '%':
251 len--;
252 break;
253
254 case 'd':
255 if (st)
256 {
257 if (!dirp)
258 dirp = dir_name (st->orig_file_name);
259 dir = safer_name_suffix (dirp, false, absolute_names_option);
260 len += strlen (dir) - 2;
261 }
262 break;
263
264 case 'f':
265 if (st)
266 {
267 base = base_name (st->orig_file_name);
268 len += strlen (base) - 2;
269 }
270 break;
271
272 case 'p':
273 pptr = umaxtostr (getpid (), pidbuf);
274 len += pidbuf + sizeof pidbuf - 1 - pptr - 2;
275 break;
276
277 case 'n':
278 nptr = umaxtostr (n, nbuf);
279 len += nbuf + sizeof nbuf - 1 - nptr - 2;
280 break;
281 }
282 p++;
283 }
284
285 buf = xmalloc (len + 1);
286 for (q = buf, p = fmt; *p; )
287 {
288 if (*p == '%')
289 {
290 switch (p[1])
291 {
292 case '%':
293 *q++ = *p++;
294 p++;
295 break;
296
297 case 'd':
298 if (dir)
299 q = stpcpy (q, dir);
300 p += 2;
301 break;
302
303 case 'f':
304 if (base)
305 q = stpcpy (q, base);
306 p += 2;
307 break;
308
309 case 'p':
310 q = stpcpy (q, pptr);
311 p += 2;
312 break;
313
314 case 'n':
315 if (nptr)
316 {
317 q = stpcpy (q, nptr);
318 p += 2;
319 break;
320 }
321 /* else fall through */
322
323 default:
324 *q++ = *p++;
325 if (*p)
326 *q++ = *p++;
327 }
328 }
329 else
330 *q++ = *p++;
331 }
332
333 free (dirp);
334
335 /* Do not allow it to end in a slash */
336 while (q > buf && ISSLASH (q[-1]))
337 q--;
338 *q = 0;
339 return buf;
340 }
341
342 char *
343 xheader_xhdr_name (struct tar_stat_info *st)
344 {
345 if (!exthdr_name)
346 assign_string (&exthdr_name, "%d/PaxHeaders.%p/%f");
347 return xheader_format_name (st, exthdr_name, 0);
348 }
349
350 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
351
352 char *
353 xheader_ghdr_name (void)
354 {
355 if (!globexthdr_name)
356 {
357 size_t len;
358 const char *tmp = getenv ("TMPDIR");
359 if (!tmp)
360 tmp = "/tmp";
361 len = strlen (tmp) + sizeof (GLOBAL_HEADER_TEMPLATE); /* Includes nul */
362 globexthdr_name = xmalloc (len);
363 strcpy(globexthdr_name, tmp);
364 strcat(globexthdr_name, GLOBAL_HEADER_TEMPLATE);
365 }
366
367 return xheader_format_name (NULL, globexthdr_name, global_header_count + 1);
368 }
369
370 void
371 xheader_write (char type, char *name, struct xheader *xhdr)
372 {
373 union block *header;
374 size_t size;
375 char *p;
376
377 size = xhdr->size;
378 header = start_private_header (name, size);
379 header->header.typeflag = type;
380
381 simple_finish_header (header);
382
383 p = xhdr->buffer;
384
385 do
386 {
387 size_t len;
388
389 header = find_next_block ();
390 len = BLOCKSIZE;
391 if (len > size)
392 len = size;
393 memcpy (header->buffer, p, len);
394 if (len < BLOCKSIZE)
395 memset (header->buffer + len, 0, BLOCKSIZE - len);
396 p += len;
397 size -= len;
398 set_next_block_after (header);
399 }
400 while (size > 0);
401 xheader_destroy (xhdr);
402
403 if (type == XGLTYPE)
404 global_header_count++;
405 }
406
407 void
408 xheader_write_global (void)
409 {
410 char *name;
411 struct keyword_list *kp;
412
413 if (!keyword_global_override_list)
414 return;
415
416 extended_header_init ();
417 for (kp = keyword_global_override_list; kp; kp = kp->next)
418 code_string (kp->value, kp->pattern, &extended_header);
419 xheader_finish (&extended_header);
420 xheader_write (XGLTYPE, name = xheader_ghdr_name (),
421 &extended_header);
422 free (name);
423 }
424
425 \f
426 /* General Interface */
427
428 struct xhdr_tab
429 {
430 char const *keyword;
431 void (*coder) (struct tar_stat_info const *, char const *,
432 struct xheader *, void const *data);
433 void (*decoder) (struct tar_stat_info *, char const *, size_t);
434 bool protect;
435 };
436
437 /* This declaration must be extern, because ISO C99 section 6.9.2
438 prohibits a tentative definition that has both internal linkage and
439 incomplete type. If we made it static, we'd have to declare its
440 size which would be a maintenance pain; if we put its initializer
441 here, we'd need a boatload of forward declarations, which would be
442 even more of a pain. */
443 extern struct xhdr_tab const xhdr_tab[];
444
445 static struct xhdr_tab const *
446 locate_handler (char const *keyword)
447 {
448 struct xhdr_tab const *p;
449
450 for (p = xhdr_tab; p->keyword; p++)
451 if (strcmp (p->keyword, keyword) == 0)
452 return p;
453 return NULL;
454 }
455
456 static bool
457 xheader_protected_pattern_p (const char *pattern)
458 {
459 struct xhdr_tab const *p;
460
461 for (p = xhdr_tab; p->keyword; p++)
462 if (p->protect && fnmatch (pattern, p->keyword, 0) == 0)
463 return true;
464 return false;
465 }
466
467 static bool
468 xheader_protected_keyword_p (const char *keyword)
469 {
470 struct xhdr_tab const *p;
471
472 for (p = xhdr_tab; p->keyword; p++)
473 if (p->protect && strcmp (p->keyword, keyword) == 0)
474 return true;
475 return false;
476 }
477
478 /* Decode a single extended header record, advancing *PTR to the next record.
479 Return true on success, false otherwise. */
480 static bool
481 decode_record (char **ptr,
482 void (*handler) (void *, char const *, char const *, size_t),
483 void *data)
484 {
485 char *start = *ptr;
486 char *p = start;
487 unsigned long int len;
488 char *len_lim;
489 char const *keyword;
490 char *nextp;
491 size_t len_max = extended_header.buffer + extended_header.size - start;
492
493 while (*p == ' ' || *p == '\t')
494 p++;
495
496 if (! ISDIGIT (*p))
497 {
498 if (*p)
499 ERROR ((0, 0, _("Malformed extended header: missing length")));
500 return false;
501 }
502
503 errno = 0;
504 len = strtoul (p, &len_lim, 10);
505
506 if (len_max < len)
507 {
508 int len_len = len_lim - p;
509 ERROR ((0, 0, _("Extended header length %*s is out of range"),
510 len_len, p));
511 return false;
512 }
513
514 nextp = start + len;
515
516 for (p = len_lim; *p == ' ' || *p == '\t'; p++)
517 continue;
518 if (p == len_lim)
519 {
520 ERROR ((0, 0,
521 _("Malformed extended header: missing blank after length")));
522 return false;
523 }
524
525 keyword = p;
526 p = strchr (p, '=');
527 if (! (p && p < nextp))
528 {
529 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
530 return false;
531 }
532
533 if (nextp[-1] != '\n')
534 {
535 ERROR ((0, 0, _("Malformed extended header: missing newline")));
536 return false;
537 }
538
539 *p = nextp[-1] = '\0';
540 handler (data, keyword, p + 1, nextp - p - 2); /* '=' + trailing '\n' */
541 *p = '=';
542 nextp[-1] = '\n';
543 *ptr = nextp;
544 return true;
545 }
546
547 static void
548 run_override_list (struct keyword_list *kp, struct tar_stat_info *st)
549 {
550 for (; kp; kp = kp->next)
551 {
552 struct xhdr_tab const *t = locate_handler (kp->pattern);
553 if (t)
554 t->decoder (st, kp->value, strlen (kp->value));
555 }
556 }
557
558 static void
559 decx (void *data, char const *keyword, char const *value, size_t size)
560 {
561 struct xhdr_tab const *t;
562 struct tar_stat_info *st = data;
563
564 if (xheader_keyword_deleted_p (keyword)
565 || xheader_keyword_override_p (keyword))
566 return;
567
568 t = locate_handler (keyword);
569 if (t)
570 t->decoder (st, value, size);
571 else
572 ERROR((0, 0, _("Ignoring unknown extended header keyword `%s'"),
573 keyword));
574 }
575
576 void
577 xheader_decode (struct tar_stat_info *st)
578 {
579 run_override_list (keyword_global_override_list, st);
580 run_override_list (global_header_override_list, st);
581
582 if (extended_header.size)
583 {
584 char *p = extended_header.buffer + BLOCKSIZE;
585 while (decode_record (&p, decx, st))
586 continue;
587 }
588 run_override_list (keyword_override_list, st);
589 }
590
591 static void
592 decg (void *data, char const *keyword, char const *value,
593 size_t size __attribute__((unused)))
594 {
595 struct keyword_list **kwl = data;
596 xheader_list_append (kwl, keyword, value);
597 }
598
599 void
600 xheader_decode_global (void)
601 {
602 if (extended_header.size)
603 {
604 char *p = extended_header.buffer + BLOCKSIZE;
605
606 xheader_list_destroy (&global_header_override_list);
607 while (decode_record (&p, decg, &global_header_override_list))
608 continue;
609 }
610 }
611
612 static void
613 extended_header_init (void)
614 {
615 if (!extended_header.stk)
616 {
617 extended_header.stk = xmalloc (sizeof *extended_header.stk);
618 obstack_init (extended_header.stk);
619 }
620 }
621
622 void
623 xheader_store (char const *keyword, struct tar_stat_info const *st,
624 void const *data)
625 {
626 struct xhdr_tab const *t;
627
628 if (extended_header.buffer)
629 return;
630 t = locate_handler (keyword);
631 if (!t || !t->coder)
632 return;
633 if (xheader_keyword_deleted_p (keyword)
634 || xheader_keyword_override_p (keyword))
635 return;
636 extended_header_init ();
637 t->coder (st, keyword, &extended_header, data);
638 }
639
640 void
641 xheader_read (union block *p, size_t size)
642 {
643 size_t j = 0;
644 size_t nblocks;
645
646 free (extended_header.buffer);
647 size += BLOCKSIZE;
648 extended_header.size = size;
649 nblocks = (size + BLOCKSIZE - 1) / BLOCKSIZE;
650 extended_header.buffer = xmalloc (size + 1);
651 extended_header.buffer[size] = '\0';
652
653 do
654 {
655 size_t len = size;
656
657 if (len > BLOCKSIZE)
658 len = BLOCKSIZE;
659
660 memcpy (&extended_header.buffer[j], p->buffer, len);
661 set_next_block_after (p);
662
663 p = find_next_block ();
664
665 j += len;
666 size -= len;
667 }
668 while (size > 0);
669 }
670
671 static void
672 xheader_print_n (struct xheader *xhdr, char const *keyword,
673 char const *value, size_t vsize)
674 {
675 size_t len = strlen (keyword) + vsize + 3; /* ' ' + '=' + '\n' */
676 size_t p;
677 size_t n = 0;
678 char nbuf[UINTMAX_STRSIZE_BOUND];
679 char const *np;
680
681 do
682 {
683 p = n;
684 np = umaxtostr (len + p, nbuf);
685 n = nbuf + sizeof nbuf - 1 - np;
686 }
687 while (n != p);
688
689 x_obstack_grow (xhdr, np, n);
690 x_obstack_1grow (xhdr, ' ');
691 x_obstack_grow (xhdr, keyword, strlen (keyword));
692 x_obstack_1grow (xhdr, '=');
693 x_obstack_grow (xhdr, value, vsize);
694 x_obstack_1grow (xhdr, '\n');
695 }
696
697 static void
698 xheader_print (struct xheader *xhdr, char const *keyword, char const *value)
699 {
700 xheader_print_n (xhdr, keyword, value, strlen (value));
701 }
702
703 void
704 xheader_finish (struct xheader *xhdr)
705 {
706 struct keyword_list *kp;
707
708 for (kp = keyword_override_list; kp; kp = kp->next)
709 code_string (kp->value, kp->pattern, xhdr);
710
711 xhdr->buffer = obstack_finish (xhdr->stk);
712 }
713
714 void
715 xheader_destroy (struct xheader *xhdr)
716 {
717 if (xhdr->stk)
718 {
719 obstack_free (xhdr->stk, NULL);
720 free (xhdr->stk);
721 xhdr->stk = NULL;
722 }
723 else
724 free (xhdr->buffer);
725 xhdr->buffer = 0;
726 xhdr->size = 0;
727 }
728
729 \f
730 /* Buildable strings */
731 static uintmax_t string_length;
732
733 void
734 xheader_string_begin ()
735 {
736 string_length = 0;
737 }
738
739 void
740 xheader_string_add (char const *s)
741 {
742 if (extended_header.buffer)
743 return;
744 extended_header_init ();
745 string_length += strlen (s);
746 x_obstack_grow (&extended_header, s, strlen (s));
747 }
748
749 void
750 xheader_string_end (char const *keyword)
751 {
752 size_t len;
753 size_t p;
754 size_t n = 0;
755 char nbuf[UINTMAX_STRSIZE_BOUND];
756 char const *np;
757 char *cp;
758
759 if (extended_header.buffer)
760 return;
761 extended_header_init ();
762
763 len = strlen (keyword) + string_length + 3; /* ' ' + '=' + '\n' */
764
765 do
766 {
767 p = n;
768 np = umaxtostr (len + p, nbuf);
769 n = nbuf + sizeof nbuf - 1 - np;
770 }
771 while (n != p);
772
773 p = strlen (keyword) + n + 2;
774 x_obstack_blank (&extended_header, p);
775 x_obstack_1grow (&extended_header, '\n');
776 cp = obstack_next_free (extended_header.stk) - string_length - p - 1;
777 memmove (cp + p, cp, string_length);
778 cp = stpcpy (cp, np);
779 *cp++ = ' ';
780 cp = stpcpy (cp, keyword);
781 *cp++ = '=';
782 }
783
784 \f
785 /* Implementations */
786
787 static void
788 out_of_range_header (char const *keyword, char const *value,
789 uintmax_t minus_minval, uintmax_t maxval)
790 {
791 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
792 char maxval_buf[UINTMAX_STRSIZE_BOUND];
793 char *minval_string = umaxtostr (minus_minval, minval_buf + 1);
794 char *maxval_string = umaxtostr (maxval, maxval_buf);
795 if (minus_minval)
796 *--minval_string = '-';
797
798 /* TRANSLATORS: The first %s is the pax extended header keyword
799 (atime, gid, etc.). */
800 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
801 keyword, value, minval_string, maxval_string));
802 }
803
804 static void
805 code_string (char const *string, char const *keyword, struct xheader *xhdr)
806 {
807 char *outstr;
808 if (!utf8_convert (true, string, &outstr))
809 {
810 /* FIXME: report error */
811 outstr = xstrdup (string);
812 }
813 xheader_print (xhdr, keyword, outstr);
814 free (outstr);
815 }
816
817 static void
818 decode_string (char **string, char const *arg)
819 {
820 if (*string)
821 {
822 free (*string);
823 *string = NULL;
824 }
825 if (!utf8_convert (false, arg, string))
826 {
827 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
828 assign_string (string, arg);
829 }
830 }
831
832 static void
833 code_time (struct timespec t, char const *keyword, struct xheader *xhdr)
834 {
835 char buf[TIMESPEC_STRSIZE_BOUND];
836 xheader_print (xhdr, keyword, code_timespec (t, buf));
837 }
838
839 enum decode_time_status
840 {
841 decode_time_success,
842 decode_time_range,
843 decode_time_bad_header
844 };
845
846 static enum decode_time_status
847 _decode_time (struct timespec *ts, char const *arg, char const *keyword)
848 {
849 time_t s;
850 unsigned long int ns = 0;
851 char *p;
852 char *arg_lim;
853 bool negative = *arg == '-';
854
855 errno = 0;
856
857 if (ISDIGIT (arg[negative]))
858 {
859 if (negative)
860 {
861 intmax_t i = strtoimax (arg, &arg_lim, 10);
862 if (TYPE_SIGNED (time_t) ? i < TYPE_MINIMUM (time_t) : i < 0)
863 return decode_time_range;
864 s = i;
865 }
866 else
867 {
868 uintmax_t i = strtoumax (arg, &arg_lim, 10);
869 if (TYPE_MAXIMUM (time_t) < i)
870 return decode_time_range;
871 s = i;
872 }
873
874 p = arg_lim;
875
876 if (errno == ERANGE)
877 return decode_time_range;
878
879 if (*p == '.')
880 {
881 int digits = 0;
882 bool trailing_nonzero = false;
883
884 while (ISDIGIT (*++p))
885 if (digits < LOG10_BILLION)
886 {
887 ns = 10 * ns + (*p - '0');
888 digits++;
889 }
890 else
891 trailing_nonzero |= *p != '0';
892
893 while (digits++ < LOG10_BILLION)
894 ns *= 10;
895
896 if (negative)
897 {
898 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
899 I.e., truncate time stamps towards minus infinity while
900 converting them to internal form. */
901 ns += trailing_nonzero;
902 if (ns != 0)
903 {
904 if (s == TYPE_MINIMUM (time_t))
905 return decode_time_range;
906 s--;
907 ns = BILLION - ns;
908 }
909 }
910 }
911
912 if (! *p)
913 {
914 ts->tv_sec = s;
915 ts->tv_nsec = ns;
916 return decode_time_success;
917 }
918 }
919
920 return decode_time_bad_header;
921 }
922
923 static bool
924 decode_time (struct timespec *ts, char const *arg, char const *keyword)
925 {
926 switch (_decode_time (ts, arg, keyword))
927 {
928 case decode_time_success:
929 return true;
930 case decode_time_bad_header:
931 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
932 keyword, arg));
933 return false;
934 case decode_time_range:
935 out_of_range_header (keyword, arg, - (uintmax_t) TYPE_MINIMUM (time_t),
936 TYPE_MAXIMUM (time_t));
937 return false;
938 }
939 return true;
940 }
941
942
943
944 static void
945 code_num (uintmax_t value, char const *keyword, struct xheader *xhdr)
946 {
947 char sbuf[UINTMAX_STRSIZE_BOUND];
948 xheader_print (xhdr, keyword, umaxtostr (value, sbuf));
949 }
950
951 static bool
952 decode_num (uintmax_t *num, char const *arg, uintmax_t maxval,
953 char const *keyword)
954 {
955 uintmax_t u;
956 char *arg_lim;
957
958 if (! (ISDIGIT (*arg)
959 && (errno = 0, u = strtoumax (arg, &arg_lim, 10), !*arg_lim)))
960 {
961 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
962 keyword, arg));
963 return false;
964 }
965
966 if (! (u <= maxval && errno != ERANGE))
967 {
968 out_of_range_header (keyword, arg, 0, maxval);
969 return false;
970 }
971
972 *num = u;
973 return true;
974 }
975
976 static void
977 dummy_coder (struct tar_stat_info const *st __attribute__ ((unused)),
978 char const *keyword __attribute__ ((unused)),
979 struct xheader *xhdr __attribute__ ((unused)),
980 void const *data __attribute__ ((unused)))
981 {
982 }
983
984 static void
985 dummy_decoder (struct tar_stat_info *st __attribute__ ((unused)),
986 char const *arg __attribute__ ((unused)),
987 size_t size __attribute__((unused)))
988 {
989 }
990
991 static void
992 atime_coder (struct tar_stat_info const *st, char const *keyword,
993 struct xheader *xhdr, void const *data __attribute__ ((unused)))
994 {
995 code_time (st->atime, keyword, xhdr);
996 }
997
998 static void
999 atime_decoder (struct tar_stat_info *st, char const *arg,
1000 size_t size __attribute__((unused)))
1001 {
1002 struct timespec ts;
1003 if (decode_time (&ts, arg, "atime"))
1004 st->atime = ts;
1005 }
1006
1007 static void
1008 gid_coder (struct tar_stat_info const *st, char const *keyword,
1009 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1010 {
1011 code_num (st->stat.st_gid, keyword, xhdr);
1012 }
1013
1014 static void
1015 gid_decoder (struct tar_stat_info *st, char const *arg,
1016 size_t size __attribute__((unused)))
1017 {
1018 uintmax_t u;
1019 if (decode_num (&u, arg, TYPE_MAXIMUM (gid_t), "gid"))
1020 st->stat.st_gid = u;
1021 }
1022
1023 static void
1024 gname_coder (struct tar_stat_info const *st, char const *keyword,
1025 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1026 {
1027 code_string (st->gname, keyword, xhdr);
1028 }
1029
1030 static void
1031 gname_decoder (struct tar_stat_info *st, char const *arg,
1032 size_t size __attribute__((unused)))
1033 {
1034 decode_string (&st->gname, arg);
1035 }
1036
1037 static void
1038 linkpath_coder (struct tar_stat_info const *st, char const *keyword,
1039 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1040 {
1041 code_string (st->link_name, keyword, xhdr);
1042 }
1043
1044 static void
1045 linkpath_decoder (struct tar_stat_info *st, char const *arg,
1046 size_t size __attribute__((unused)))
1047 {
1048 decode_string (&st->link_name, arg);
1049 }
1050
1051 static void
1052 ctime_coder (struct tar_stat_info const *st, char const *keyword,
1053 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1054 {
1055 code_time (st->ctime, keyword, xhdr);
1056 }
1057
1058 static void
1059 ctime_decoder (struct tar_stat_info *st, char const *arg,
1060 size_t size __attribute__((unused)))
1061 {
1062 struct timespec ts;
1063 if (decode_time (&ts, arg, "ctime"))
1064 st->ctime = ts;
1065 }
1066
1067 static void
1068 mtime_coder (struct tar_stat_info const *st, char const *keyword,
1069 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1070 {
1071 code_time (st->mtime, keyword, xhdr);
1072 }
1073
1074 static void
1075 mtime_decoder (struct tar_stat_info *st, char const *arg,
1076 size_t size __attribute__((unused)))
1077 {
1078 struct timespec ts;
1079 if (decode_time (&ts, arg, "mtime"))
1080 st->mtime = ts;
1081 }
1082
1083 static void
1084 path_coder (struct tar_stat_info const *st, char const *keyword,
1085 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1086 {
1087 code_string (st->file_name, keyword, xhdr);
1088 }
1089
1090 static void
1091 path_decoder (struct tar_stat_info *st, char const *arg,
1092 size_t size __attribute__((unused)))
1093 {
1094 decode_string (&st->orig_file_name, arg);
1095 decode_string (&st->file_name, arg);
1096 st->had_trailing_slash = strip_trailing_slashes (st->file_name);
1097 }
1098
1099 static void
1100 size_coder (struct tar_stat_info const *st, char const *keyword,
1101 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1102 {
1103 code_num (st->stat.st_size, keyword, xhdr);
1104 }
1105
1106 static void
1107 size_decoder (struct tar_stat_info *st, char const *arg,
1108 size_t size __attribute__((unused)))
1109 {
1110 uintmax_t u;
1111 if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), "size"))
1112 st->stat.st_size = u;
1113 }
1114
1115 static void
1116 uid_coder (struct tar_stat_info const *st, char const *keyword,
1117 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1118 {
1119 code_num (st->stat.st_uid, keyword, xhdr);
1120 }
1121
1122 static void
1123 uid_decoder (struct tar_stat_info *st, char const *arg,
1124 size_t size __attribute__((unused)))
1125 {
1126 uintmax_t u;
1127 if (decode_num (&u, arg, TYPE_MAXIMUM (uid_t), "uid"))
1128 st->stat.st_uid = u;
1129 }
1130
1131 static void
1132 uname_coder (struct tar_stat_info const *st, char const *keyword,
1133 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1134 {
1135 code_string (st->uname, keyword, xhdr);
1136 }
1137
1138 static void
1139 uname_decoder (struct tar_stat_info *st, char const *arg,
1140 size_t size __attribute__((unused)))
1141 {
1142 decode_string (&st->uname, arg);
1143 }
1144
1145 static void
1146 sparse_size_coder (struct tar_stat_info const *st, char const *keyword,
1147 struct xheader *xhdr, void const *data)
1148 {
1149 size_coder (st, keyword, xhdr, data);
1150 }
1151
1152 static void
1153 sparse_size_decoder (struct tar_stat_info *st, char const *arg,
1154 size_t size __attribute__((unused)))
1155 {
1156 uintmax_t u;
1157 if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), "GNU.sparse.size"))
1158 st->stat.st_size = u;
1159 }
1160
1161 static void
1162 sparse_numblocks_coder (struct tar_stat_info const *st, char const *keyword,
1163 struct xheader *xhdr,
1164 void const *data __attribute__ ((unused)))
1165 {
1166 code_num (st->sparse_map_avail, keyword, xhdr);
1167 }
1168
1169 static void
1170 sparse_numblocks_decoder (struct tar_stat_info *st, char const *arg,
1171 size_t size __attribute__((unused)))
1172 {
1173 uintmax_t u;
1174 if (decode_num (&u, arg, SIZE_MAX, "GNU.sparse.numblocks"))
1175 {
1176 st->sparse_map_size = u;
1177 st->sparse_map = xcalloc (u, sizeof st->sparse_map[0]);
1178 st->sparse_map_avail = 0;
1179 }
1180 }
1181
1182 static void
1183 sparse_offset_coder (struct tar_stat_info const *st, char const *keyword,
1184 struct xheader *xhdr, void const *data)
1185 {
1186 size_t const *pi = data;
1187 code_num (st->sparse_map[*pi].offset, keyword, xhdr);
1188 }
1189
1190 static void
1191 sparse_offset_decoder (struct tar_stat_info *st, char const *arg,
1192 size_t size __attribute__((unused)))
1193 {
1194 uintmax_t u;
1195 if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), "GNU.sparse.offset"))
1196 {
1197 if (st->sparse_map_avail < st->sparse_map_size)
1198 st->sparse_map[st->sparse_map_avail].offset = u;
1199 else
1200 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1201 "GNU.sparse.offset", arg));
1202 }
1203 }
1204
1205 static void
1206 sparse_numbytes_coder (struct tar_stat_info const *st, char const *keyword,
1207 struct xheader *xhdr, void const *data)
1208 {
1209 size_t const *pi = data;
1210 code_num (st->sparse_map[*pi].numbytes, keyword, xhdr);
1211 }
1212
1213 static void
1214 sparse_numbytes_decoder (struct tar_stat_info *st, char const *arg,
1215 size_t size __attribute__((unused)))
1216 {
1217 uintmax_t u;
1218 if (decode_num (&u, arg, SIZE_MAX, "GNU.sparse.numbytes"))
1219 {
1220 if (st->sparse_map_avail < st->sparse_map_size)
1221 st->sparse_map[st->sparse_map_avail++].numbytes = u;
1222 else
1223 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1224 "GNU.sparse.numbytes", arg));
1225 }
1226 }
1227
1228 static void
1229 sparse_map_decoder (struct tar_stat_info *st, char const *arg,
1230 size_t size __attribute__((unused)))
1231 {
1232 int offset = 1;
1233 static char *keyword = "GNU.sparse.map";
1234
1235 st->sparse_map_avail = 0;
1236 while (1)
1237 {
1238 uintmax_t u;
1239 char *delim;
1240 struct sp_array e;
1241
1242 if (!ISDIGIT (*arg))
1243 {
1244 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1245 keyword, arg));
1246 return;
1247 }
1248
1249 errno = 0;
1250 u = strtoumax (arg, &delim, 10);
1251 if (offset)
1252 {
1253 e.offset = u;
1254 if (!(u == e.offset && errno != ERANGE))
1255 {
1256 out_of_range_header (keyword, arg, 0, TYPE_MAXIMUM (off_t));
1257 return;
1258 }
1259 }
1260 else
1261 {
1262 e.numbytes = u;
1263 if (!(u == e.numbytes && errno != ERANGE))
1264 {
1265 out_of_range_header (keyword, arg, 0, TYPE_MAXIMUM (size_t));
1266 return;
1267 }
1268 if (st->sparse_map_avail < st->sparse_map_size)
1269 st->sparse_map[st->sparse_map_avail++] = e;
1270 else
1271 {
1272 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1273 keyword, arg));
1274 return;
1275 }
1276 }
1277
1278 offset = !offset;
1279
1280 if (*delim == 0)
1281 break;
1282 else if (*delim != ',')
1283 {
1284 ERROR ((0, 0,
1285 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1286 keyword, *delim));
1287 return;
1288 }
1289
1290 arg = delim + 1;
1291 }
1292
1293 if (!offset)
1294 ERROR ((0, 0,
1295 _("Malformed extended header: invalid %s: odd number of values"),
1296 keyword));
1297 }
1298
1299 static void
1300 dumpdir_coder (struct tar_stat_info const *st, char const *keyword,
1301 struct xheader *xhdr, void const *data)
1302 {
1303 xheader_print_n (xhdr, keyword, data, dumpdir_size (data));
1304 }
1305
1306 static void
1307 dumpdir_decoder (struct tar_stat_info *st, char const *arg,
1308 size_t size)
1309 {
1310 st->dumpdir = xmalloc (size);
1311 memcpy (st->dumpdir, arg, size);
1312 }
1313
1314 static void
1315 volume_label_coder (struct tar_stat_info const *st, char const *keyword,
1316 struct xheader *xhdr, void const *data)
1317 {
1318 code_string (data, keyword, xhdr);
1319 }
1320
1321 static void
1322 volume_label_decoder (struct tar_stat_info *st, char const *arg, size_t size)
1323 {
1324 decode_string (&volume_label, arg);
1325 }
1326
1327 static void
1328 volume_size_coder (struct tar_stat_info const *st, char const *keyword,
1329 struct xheader *xhdr, void const *data)
1330 {
1331 off_t v = *(off_t*)data;
1332 code_num (v, keyword, xhdr);
1333 }
1334
1335 static void
1336 volume_size_decoder (struct tar_stat_info *st, char const *arg, size_t size)
1337 {
1338 uintmax_t u;
1339 if (decode_num (&u, arg, TYPE_MAXIMUM (uintmax_t), "GNU.volume.size"))
1340 continued_file_size = u;
1341 }
1342
1343 /* FIXME: Merge with volume_size_coder */
1344 static void
1345 volume_offset_coder (struct tar_stat_info const *st, char const *keyword,
1346 struct xheader *xhdr, void const *data)
1347 {
1348 off_t v = *(off_t*)data;
1349 code_num (v, keyword, xhdr);
1350 }
1351
1352 static void
1353 volume_offset_decoder (struct tar_stat_info *st, char const *arg, size_t size)
1354 {
1355 uintmax_t u;
1356 if (decode_num (&u, arg, TYPE_MAXIMUM (uintmax_t), "GNU.volume.offset"))
1357 continued_file_offset = u;
1358 }
1359
1360 static void
1361 volume_filename_decoder (struct tar_stat_info *st, char const *arg,
1362 size_t size)
1363 {
1364 decode_string (&continued_file_name, arg);
1365 }
1366
1367
1368 struct xhdr_tab const xhdr_tab[] = {
1369 { "atime", atime_coder, atime_decoder, false },
1370 { "comment", dummy_coder, dummy_decoder, false },
1371 { "charset", dummy_coder, dummy_decoder, false },
1372 { "ctime", ctime_coder, ctime_decoder, false },
1373 { "gid", gid_coder, gid_decoder, false },
1374 { "gname", gname_coder, gname_decoder, false },
1375 { "linkpath", linkpath_coder, linkpath_decoder, false },
1376 { "mtime", mtime_coder, mtime_decoder, false },
1377 { "path", path_coder, path_decoder, false },
1378 { "size", size_coder, size_decoder, false },
1379 { "uid", uid_coder, uid_decoder, false },
1380 { "uname", uname_coder, uname_decoder, false },
1381
1382 /* Sparse file handling */
1383 { "GNU.sparse.size", sparse_size_coder, sparse_size_decoder, true },
1384 { "GNU.sparse.numblocks", sparse_numblocks_coder, sparse_numblocks_decoder,
1385 true },
1386 /* tar 1.14 - 1.15.1 keywords. Multiplse instances of these appeared in 'x'
1387 headers, and each of them was meaningful. It confilcted with POSIX specs,
1388 which requires that "when extended header records conflict, the last one
1389 given in the header shall take precedence." */
1390 { "GNU.sparse.offset", sparse_offset_coder, sparse_offset_decoder,
1391 true },
1392 { "GNU.sparse.numbytes", sparse_numbytes_coder, sparse_numbytes_decoder,
1393 true },
1394 /* tar >=1.16 keyword, introduced to remove the above-mentioned conflict. */
1395 { "GNU.sparse.map", NULL /* Unused, see pax_dump_header() */,
1396 sparse_map_decoder, false },
1397
1398 { "GNU.dumpdir", dumpdir_coder, dumpdir_decoder,
1399 true },
1400
1401 /* Keeps the tape/volume label. May be present only in the global headers.
1402 Equivalent to GNUTYPE_VOLHDR. */
1403 { "GNU.volume.label", volume_label_coder, volume_label_decoder, true },
1404
1405 /* These may be present in a first global header of the archive.
1406 They provide the same functionality as GNUTYPE_MULTIVOL header.
1407 The GNU.volume.size keeps the real_s_sizeleft value, which is
1408 otherwise kept in the size field of a multivolume header. The
1409 GNU.volume.offset keeps the offset of the start of this volume,
1410 otherwise kept in oldgnu_header.offset. */
1411 { "GNU.volume.filename", volume_label_coder, volume_filename_decoder,
1412 true },
1413 { "GNU.volume.size", volume_size_coder, volume_size_decoder, true },
1414 { "GNU.volume.offset", volume_offset_coder, volume_offset_decoder, true },
1415
1416 { NULL, NULL, NULL, false }
1417 };
This page took 0.106129 seconds and 4 git commands to generate.