]> Dogcows Code - chaz/tar/blob - src/xheader.c
5676dcabfb5ee11c933b2e73260eb4b7d47be841
[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 = last_component (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 *, 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 uintmax_t u;
488 size_t len;
489 char *len_lim;
490 char const *keyword;
491 char *nextp;
492 size_t len_max = extended_header.buffer + extended_header.size - start;
493
494 while (*p == ' ' || *p == '\t')
495 p++;
496
497 if (! ISDIGIT (*p))
498 {
499 if (*p)
500 ERROR ((0, 0, _("Malformed extended header: missing length")));
501 return false;
502 }
503
504 errno = 0;
505 len = u = strtoumax (p, &len_lim, 10);
506 if (len != u || errno == ERANGE)
507 {
508 ERROR ((0, 0, _("Extended header length is out of allowed range")));
509 return false;
510 }
511
512 if (len_max < len)
513 {
514 int len_len = len_lim - p;
515 ERROR ((0, 0, _("Extended header length %*s is out of range"),
516 len_len, p));
517 return false;
518 }
519
520 nextp = start + len;
521
522 for (p = len_lim; *p == ' ' || *p == '\t'; p++)
523 continue;
524 if (p == len_lim)
525 {
526 ERROR ((0, 0,
527 _("Malformed extended header: missing blank after length")));
528 return false;
529 }
530
531 keyword = p;
532 p = strchr (p, '=');
533 if (! (p && p < nextp))
534 {
535 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
536 return false;
537 }
538
539 if (nextp[-1] != '\n')
540 {
541 ERROR ((0, 0, _("Malformed extended header: missing newline")));
542 return false;
543 }
544
545 *p = nextp[-1] = '\0';
546 handler (data, keyword, p + 1, nextp - p - 2); /* '=' + trailing '\n' */
547 *p = '=';
548 nextp[-1] = '\n';
549 *ptr = nextp;
550 return true;
551 }
552
553 static void
554 run_override_list (struct keyword_list *kp, struct tar_stat_info *st)
555 {
556 for (; kp; kp = kp->next)
557 {
558 struct xhdr_tab const *t = locate_handler (kp->pattern);
559 if (t)
560 t->decoder (st, t->keyword, kp->value, strlen (kp->value));
561 }
562 }
563
564 static void
565 decx (void *data, char const *keyword, char const *value, size_t size)
566 {
567 struct xhdr_tab const *t;
568 struct tar_stat_info *st = data;
569
570 if (xheader_keyword_deleted_p (keyword)
571 || xheader_keyword_override_p (keyword))
572 return;
573
574 t = locate_handler (keyword);
575 if (t)
576 t->decoder (st, keyword, value, size);
577 else
578 ERROR((0, 0, _("Ignoring unknown extended header keyword `%s'"),
579 keyword));
580 }
581
582 void
583 xheader_decode (struct tar_stat_info *st)
584 {
585 run_override_list (keyword_global_override_list, st);
586 run_override_list (global_header_override_list, st);
587
588 if (extended_header.size)
589 {
590 char *p = extended_header.buffer + BLOCKSIZE;
591 while (decode_record (&p, decx, st))
592 continue;
593 }
594 run_override_list (keyword_override_list, st);
595 }
596
597 static void
598 decg (void *data, char const *keyword, char const *value,
599 size_t size __attribute__((unused)))
600 {
601 struct keyword_list **kwl = data;
602 xheader_list_append (kwl, keyword, value);
603 }
604
605 void
606 xheader_decode_global (void)
607 {
608 if (extended_header.size)
609 {
610 char *p = extended_header.buffer + BLOCKSIZE;
611
612 xheader_list_destroy (&global_header_override_list);
613 while (decode_record (&p, decg, &global_header_override_list))
614 continue;
615 }
616 }
617
618 static void
619 extended_header_init (void)
620 {
621 if (!extended_header.stk)
622 {
623 extended_header.stk = xmalloc (sizeof *extended_header.stk);
624 obstack_init (extended_header.stk);
625 }
626 }
627
628 void
629 xheader_store (char const *keyword, struct tar_stat_info const *st,
630 void const *data)
631 {
632 struct xhdr_tab const *t;
633
634 if (extended_header.buffer)
635 return;
636 t = locate_handler (keyword);
637 if (!t || !t->coder)
638 return;
639 if (xheader_keyword_deleted_p (keyword)
640 || xheader_keyword_override_p (keyword))
641 return;
642 extended_header_init ();
643 t->coder (st, keyword, &extended_header, data);
644 }
645
646 void
647 xheader_read (union block *p, size_t size)
648 {
649 size_t j = 0;
650 size_t nblocks;
651
652 free (extended_header.buffer);
653 size += BLOCKSIZE;
654 extended_header.size = size;
655 nblocks = (size + BLOCKSIZE - 1) / BLOCKSIZE;
656 extended_header.buffer = xmalloc (size + 1);
657 extended_header.buffer[size] = '\0';
658
659 do
660 {
661 size_t len = size;
662
663 if (len > BLOCKSIZE)
664 len = BLOCKSIZE;
665
666 memcpy (&extended_header.buffer[j], p->buffer, len);
667 set_next_block_after (p);
668
669 p = find_next_block ();
670
671 j += len;
672 size -= len;
673 }
674 while (size > 0);
675 }
676
677 static void
678 xheader_print_n (struct xheader *xhdr, char const *keyword,
679 char const *value, size_t vsize)
680 {
681 size_t len = strlen (keyword) + vsize + 3; /* ' ' + '=' + '\n' */
682 size_t p;
683 size_t n = 0;
684 char nbuf[UINTMAX_STRSIZE_BOUND];
685 char const *np;
686
687 do
688 {
689 p = n;
690 np = umaxtostr (len + p, nbuf);
691 n = nbuf + sizeof nbuf - 1 - np;
692 }
693 while (n != p);
694
695 x_obstack_grow (xhdr, np, n);
696 x_obstack_1grow (xhdr, ' ');
697 x_obstack_grow (xhdr, keyword, strlen (keyword));
698 x_obstack_1grow (xhdr, '=');
699 x_obstack_grow (xhdr, value, vsize);
700 x_obstack_1grow (xhdr, '\n');
701 }
702
703 static void
704 xheader_print (struct xheader *xhdr, char const *keyword, char const *value)
705 {
706 xheader_print_n (xhdr, keyword, value, strlen (value));
707 }
708
709 void
710 xheader_finish (struct xheader *xhdr)
711 {
712 struct keyword_list *kp;
713
714 for (kp = keyword_override_list; kp; kp = kp->next)
715 code_string (kp->value, kp->pattern, xhdr);
716
717 xhdr->buffer = obstack_finish (xhdr->stk);
718 }
719
720 void
721 xheader_destroy (struct xheader *xhdr)
722 {
723 if (xhdr->stk)
724 {
725 obstack_free (xhdr->stk, NULL);
726 free (xhdr->stk);
727 xhdr->stk = NULL;
728 }
729 else
730 free (xhdr->buffer);
731 xhdr->buffer = 0;
732 xhdr->size = 0;
733 }
734
735 \f
736 /* Buildable strings */
737 static uintmax_t string_length;
738
739 void
740 xheader_string_begin ()
741 {
742 string_length = 0;
743 }
744
745 void
746 xheader_string_add (char const *s)
747 {
748 if (extended_header.buffer)
749 return;
750 extended_header_init ();
751 string_length += strlen (s);
752 x_obstack_grow (&extended_header, s, strlen (s));
753 }
754
755 bool
756 xheader_string_end (char const *keyword)
757 {
758 uintmax_t len;
759 uintmax_t p;
760 uintmax_t n = 0;
761 size_t size;
762 char nbuf[UINTMAX_STRSIZE_BOUND];
763 char const *np;
764 char *cp;
765
766 if (extended_header.buffer)
767 return false;
768 extended_header_init ();
769
770 len = strlen (keyword) + string_length + 3; /* ' ' + '=' + '\n' */
771
772 do
773 {
774 p = n;
775 np = umaxtostr (len + p, nbuf);
776 n = nbuf + sizeof nbuf - 1 - np;
777 }
778 while (n != p);
779
780 p = strlen (keyword) + n + 2;
781 size = p;
782 if (size != p)
783 {
784 ERROR ((0, 0,
785 _("Generated keyword/value pair is too long (keyword=%s, length=%s)"),
786 keyword, nbuf));
787 obstack_free (extended_header.stk, obstack_finish (extended_header.stk));
788 return false;
789 }
790 x_obstack_blank (&extended_header, p);
791 x_obstack_1grow (&extended_header, '\n');
792 cp = obstack_next_free (extended_header.stk) - string_length - p - 1;
793 memmove (cp + p, cp, string_length);
794 cp = stpcpy (cp, np);
795 *cp++ = ' ';
796 cp = stpcpy (cp, keyword);
797 *cp++ = '=';
798 return true;
799 }
800
801 \f
802 /* Implementations */
803
804 static void
805 out_of_range_header (char const *keyword, char const *value,
806 uintmax_t minus_minval, uintmax_t maxval)
807 {
808 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
809 char maxval_buf[UINTMAX_STRSIZE_BOUND];
810 char *minval_string = umaxtostr (minus_minval, minval_buf + 1);
811 char *maxval_string = umaxtostr (maxval, maxval_buf);
812 if (minus_minval)
813 *--minval_string = '-';
814
815 /* TRANSLATORS: The first %s is the pax extended header keyword
816 (atime, gid, etc.). */
817 ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
818 keyword, value, minval_string, maxval_string));
819 }
820
821 static void
822 code_string (char const *string, char const *keyword, struct xheader *xhdr)
823 {
824 char *outstr;
825 if (!utf8_convert (true, string, &outstr))
826 {
827 /* FIXME: report error */
828 outstr = xstrdup (string);
829 }
830 xheader_print (xhdr, keyword, outstr);
831 free (outstr);
832 }
833
834 static void
835 decode_string (char **string, char const *arg)
836 {
837 if (*string)
838 {
839 free (*string);
840 *string = NULL;
841 }
842 if (!utf8_convert (false, arg, string))
843 {
844 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
845 assign_string (string, arg);
846 }
847 }
848
849 static void
850 code_time (struct timespec t, char const *keyword, struct xheader *xhdr)
851 {
852 char buf[TIMESPEC_STRSIZE_BOUND];
853 xheader_print (xhdr, keyword, code_timespec (t, buf));
854 }
855
856 enum decode_time_status
857 {
858 decode_time_success,
859 decode_time_range,
860 decode_time_bad_header
861 };
862
863 static enum decode_time_status
864 _decode_time (struct timespec *ts, char const *arg, char const *keyword)
865 {
866 time_t s;
867 unsigned long int ns = 0;
868 char *p;
869 char *arg_lim;
870 bool negative = *arg == '-';
871
872 errno = 0;
873
874 if (ISDIGIT (arg[negative]))
875 {
876 if (negative)
877 {
878 intmax_t i = strtoimax (arg, &arg_lim, 10);
879 if (TYPE_SIGNED (time_t) ? i < TYPE_MINIMUM (time_t) : i < 0)
880 return decode_time_range;
881 s = i;
882 }
883 else
884 {
885 uintmax_t i = strtoumax (arg, &arg_lim, 10);
886 if (TYPE_MAXIMUM (time_t) < i)
887 return decode_time_range;
888 s = i;
889 }
890
891 p = arg_lim;
892
893 if (errno == ERANGE)
894 return decode_time_range;
895
896 if (*p == '.')
897 {
898 int digits = 0;
899 bool trailing_nonzero = false;
900
901 while (ISDIGIT (*++p))
902 if (digits < LOG10_BILLION)
903 {
904 ns = 10 * ns + (*p - '0');
905 digits++;
906 }
907 else
908 trailing_nonzero |= *p != '0';
909
910 while (digits++ < LOG10_BILLION)
911 ns *= 10;
912
913 if (negative)
914 {
915 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
916 I.e., truncate time stamps towards minus infinity while
917 converting them to internal form. */
918 ns += trailing_nonzero;
919 if (ns != 0)
920 {
921 if (s == TYPE_MINIMUM (time_t))
922 return decode_time_range;
923 s--;
924 ns = BILLION - ns;
925 }
926 }
927 }
928
929 if (! *p)
930 {
931 ts->tv_sec = s;
932 ts->tv_nsec = ns;
933 return decode_time_success;
934 }
935 }
936
937 return decode_time_bad_header;
938 }
939
940 static bool
941 decode_time (struct timespec *ts, char const *arg, char const *keyword)
942 {
943 switch (_decode_time (ts, arg, keyword))
944 {
945 case decode_time_success:
946 return true;
947 case decode_time_bad_header:
948 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
949 keyword, arg));
950 return false;
951 case decode_time_range:
952 out_of_range_header (keyword, arg, - (uintmax_t) TYPE_MINIMUM (time_t),
953 TYPE_MAXIMUM (time_t));
954 return false;
955 }
956 return true;
957 }
958
959
960
961 static void
962 code_num (uintmax_t value, char const *keyword, struct xheader *xhdr)
963 {
964 char sbuf[UINTMAX_STRSIZE_BOUND];
965 xheader_print (xhdr, keyword, umaxtostr (value, sbuf));
966 }
967
968 static bool
969 decode_num (uintmax_t *num, char const *arg, uintmax_t maxval,
970 char const *keyword)
971 {
972 uintmax_t u;
973 char *arg_lim;
974
975 if (! (ISDIGIT (*arg)
976 && (errno = 0, u = strtoumax (arg, &arg_lim, 10), !*arg_lim)))
977 {
978 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
979 keyword, arg));
980 return false;
981 }
982
983 if (! (u <= maxval && errno != ERANGE))
984 {
985 out_of_range_header (keyword, arg, 0, maxval);
986 return false;
987 }
988
989 *num = u;
990 return true;
991 }
992
993 static void
994 dummy_coder (struct tar_stat_info const *st __attribute__ ((unused)),
995 char const *keyword __attribute__ ((unused)),
996 struct xheader *xhdr __attribute__ ((unused)),
997 void const *data __attribute__ ((unused)))
998 {
999 }
1000
1001 static void
1002 dummy_decoder (struct tar_stat_info *st __attribute__ ((unused)),
1003 char const *keyword __attribute__ ((unused)),
1004 char const *arg __attribute__ ((unused)),
1005 size_t size __attribute__((unused)))
1006 {
1007 }
1008
1009 static void
1010 atime_coder (struct tar_stat_info const *st, char const *keyword,
1011 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1012 {
1013 code_time (st->atime, keyword, xhdr);
1014 }
1015
1016 static void
1017 atime_decoder (struct tar_stat_info *st,
1018 char const *keyword,
1019 char const *arg,
1020 size_t size __attribute__((unused)))
1021 {
1022 struct timespec ts;
1023 if (decode_time (&ts, arg, keyword))
1024 st->atime = ts;
1025 }
1026
1027 static void
1028 gid_coder (struct tar_stat_info const *st, char const *keyword,
1029 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1030 {
1031 code_num (st->stat.st_gid, keyword, xhdr);
1032 }
1033
1034 static void
1035 gid_decoder (struct tar_stat_info *st,
1036 char const *keyword,
1037 char const *arg,
1038 size_t size __attribute__((unused)))
1039 {
1040 uintmax_t u;
1041 if (decode_num (&u, arg, TYPE_MAXIMUM (gid_t), keyword))
1042 st->stat.st_gid = u;
1043 }
1044
1045 static void
1046 gname_coder (struct tar_stat_info const *st, char const *keyword,
1047 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1048 {
1049 code_string (st->gname, keyword, xhdr);
1050 }
1051
1052 static void
1053 gname_decoder (struct tar_stat_info *st,
1054 char const *keyword __attribute__((unused)),
1055 char const *arg,
1056 size_t size __attribute__((unused)))
1057 {
1058 decode_string (&st->gname, arg);
1059 }
1060
1061 static void
1062 linkpath_coder (struct tar_stat_info const *st, char const *keyword,
1063 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1064 {
1065 code_string (st->link_name, keyword, xhdr);
1066 }
1067
1068 static void
1069 linkpath_decoder (struct tar_stat_info *st,
1070 char const *keyword __attribute__((unused)),
1071 char const *arg,
1072 size_t size __attribute__((unused)))
1073 {
1074 decode_string (&st->link_name, arg);
1075 }
1076
1077 static void
1078 ctime_coder (struct tar_stat_info const *st, char const *keyword,
1079 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1080 {
1081 code_time (st->ctime, keyword, xhdr);
1082 }
1083
1084 static void
1085 ctime_decoder (struct tar_stat_info *st,
1086 char const *keyword,
1087 char const *arg,
1088 size_t size __attribute__((unused)))
1089 {
1090 struct timespec ts;
1091 if (decode_time (&ts, arg, keyword))
1092 st->ctime = ts;
1093 }
1094
1095 static void
1096 mtime_coder (struct tar_stat_info const *st, char const *keyword,
1097 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1098 {
1099 code_time (st->mtime, keyword, xhdr);
1100 }
1101
1102 static void
1103 mtime_decoder (struct tar_stat_info *st,
1104 char const *keyword,
1105 char const *arg,
1106 size_t size __attribute__((unused)))
1107 {
1108 struct timespec ts;
1109 if (decode_time (&ts, arg, keyword))
1110 st->mtime = ts;
1111 }
1112
1113 static void
1114 path_coder (struct tar_stat_info const *st, char const *keyword,
1115 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1116 {
1117 code_string (st->file_name, keyword, xhdr);
1118 }
1119
1120 static void
1121 path_decoder (struct tar_stat_info *st,
1122 char const *keyword __attribute__((unused)),
1123 char const *arg,
1124 size_t size __attribute__((unused)))
1125 {
1126 decode_string (&st->orig_file_name, arg);
1127 decode_string (&st->file_name, arg);
1128 st->had_trailing_slash = strip_trailing_slashes (st->file_name);
1129 }
1130
1131 static void
1132 size_coder (struct tar_stat_info const *st, char const *keyword,
1133 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1134 {
1135 code_num (st->stat.st_size, keyword, xhdr);
1136 }
1137
1138 static void
1139 size_decoder (struct tar_stat_info *st,
1140 char const *keyword,
1141 char const *arg,
1142 size_t size __attribute__((unused)))
1143 {
1144 uintmax_t u;
1145 if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), keyword))
1146 st->stat.st_size = u;
1147 }
1148
1149 static void
1150 uid_coder (struct tar_stat_info const *st, char const *keyword,
1151 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1152 {
1153 code_num (st->stat.st_uid, keyword, xhdr);
1154 }
1155
1156 static void
1157 uid_decoder (struct tar_stat_info *st,
1158 char const *keyword,
1159 char const *arg,
1160 size_t size __attribute__((unused)))
1161 {
1162 uintmax_t u;
1163 if (decode_num (&u, arg, TYPE_MAXIMUM (uid_t), keyword))
1164 st->stat.st_uid = u;
1165 }
1166
1167 static void
1168 uname_coder (struct tar_stat_info const *st, char const *keyword,
1169 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1170 {
1171 code_string (st->uname, keyword, xhdr);
1172 }
1173
1174 static void
1175 uname_decoder (struct tar_stat_info *st,
1176 char const *keyword __attribute__((unused)),
1177 char const *arg,
1178 size_t size __attribute__((unused)))
1179 {
1180 decode_string (&st->uname, arg);
1181 }
1182
1183 static void
1184 sparse_size_coder (struct tar_stat_info const *st, char const *keyword,
1185 struct xheader *xhdr, void const *data)
1186 {
1187 size_coder (st, keyword, xhdr, data);
1188 }
1189
1190 static void
1191 sparse_size_decoder (struct tar_stat_info *st,
1192 char const *keyword,
1193 char const *arg,
1194 size_t size __attribute__((unused)))
1195 {
1196 uintmax_t u;
1197 if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), keyword))
1198 st->stat.st_size = u;
1199 }
1200
1201 static void
1202 sparse_numblocks_coder (struct tar_stat_info const *st, char const *keyword,
1203 struct xheader *xhdr,
1204 void const *data __attribute__ ((unused)))
1205 {
1206 code_num (st->sparse_map_avail, keyword, xhdr);
1207 }
1208
1209 static void
1210 sparse_numblocks_decoder (struct tar_stat_info *st,
1211 char const *keyword,
1212 char const *arg,
1213 size_t size __attribute__((unused)))
1214 {
1215 uintmax_t u;
1216 if (decode_num (&u, arg, SIZE_MAX, keyword))
1217 {
1218 st->sparse_map_size = u;
1219 st->sparse_map = xcalloc (u, sizeof st->sparse_map[0]);
1220 st->sparse_map_avail = 0;
1221 }
1222 }
1223
1224 static void
1225 sparse_offset_coder (struct tar_stat_info const *st, char const *keyword,
1226 struct xheader *xhdr, void const *data)
1227 {
1228 size_t const *pi = data;
1229 code_num (st->sparse_map[*pi].offset, keyword, xhdr);
1230 }
1231
1232 static void
1233 sparse_offset_decoder (struct tar_stat_info *st,
1234 char const *keyword,
1235 char const *arg,
1236 size_t size __attribute__((unused)))
1237 {
1238 uintmax_t u;
1239 if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), keyword))
1240 {
1241 if (st->sparse_map_avail < st->sparse_map_size)
1242 st->sparse_map[st->sparse_map_avail].offset = u;
1243 else
1244 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1245 "GNU.sparse.offset", arg));
1246 }
1247 }
1248
1249 static void
1250 sparse_numbytes_coder (struct tar_stat_info const *st, char const *keyword,
1251 struct xheader *xhdr, void const *data)
1252 {
1253 size_t const *pi = data;
1254 code_num (st->sparse_map[*pi].numbytes, keyword, xhdr);
1255 }
1256
1257 static void
1258 sparse_numbytes_decoder (struct tar_stat_info *st,
1259 char const *keyword,
1260 char const *arg,
1261 size_t size __attribute__((unused)))
1262 {
1263 uintmax_t u;
1264 if (decode_num (&u, arg, SIZE_MAX, keyword))
1265 {
1266 if (st->sparse_map_avail < st->sparse_map_size)
1267 st->sparse_map[st->sparse_map_avail++].numbytes = u;
1268 else
1269 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1270 keyword, arg));
1271 }
1272 }
1273
1274 static void
1275 sparse_map_decoder (struct tar_stat_info *st,
1276 char const *keyword,
1277 char const *arg,
1278 size_t size __attribute__((unused)))
1279 {
1280 int offset = 1;
1281
1282 st->sparse_map_avail = 0;
1283 while (1)
1284 {
1285 uintmax_t u;
1286 char *delim;
1287 struct sp_array e;
1288
1289 if (!ISDIGIT (*arg))
1290 {
1291 ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
1292 keyword, arg));
1293 return;
1294 }
1295
1296 errno = 0;
1297 u = strtoumax (arg, &delim, 10);
1298 if (offset)
1299 {
1300 e.offset = u;
1301 if (!(u == e.offset && errno != ERANGE))
1302 {
1303 out_of_range_header (keyword, arg, 0, TYPE_MAXIMUM (off_t));
1304 return;
1305 }
1306 }
1307 else
1308 {
1309 e.numbytes = u;
1310 if (!(u == e.numbytes && errno != ERANGE))
1311 {
1312 out_of_range_header (keyword, arg, 0, TYPE_MAXIMUM (size_t));
1313 return;
1314 }
1315 if (st->sparse_map_avail < st->sparse_map_size)
1316 st->sparse_map[st->sparse_map_avail++] = e;
1317 else
1318 {
1319 ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
1320 keyword, arg));
1321 return;
1322 }
1323 }
1324
1325 offset = !offset;
1326
1327 if (*delim == 0)
1328 break;
1329 else if (*delim != ',')
1330 {
1331 ERROR ((0, 0,
1332 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1333 keyword, *delim));
1334 return;
1335 }
1336
1337 arg = delim + 1;
1338 }
1339
1340 if (!offset)
1341 ERROR ((0, 0,
1342 _("Malformed extended header: invalid %s: odd number of values"),
1343 keyword));
1344 }
1345
1346 static void
1347 dumpdir_coder (struct tar_stat_info const *st, char const *keyword,
1348 struct xheader *xhdr, void const *data)
1349 {
1350 xheader_print_n (xhdr, keyword, data, dumpdir_size (data));
1351 }
1352
1353 static void
1354 dumpdir_decoder (struct tar_stat_info *st,
1355 char const *keyword __attribute__((unused)),
1356 char const *arg,
1357 size_t size)
1358 {
1359 st->dumpdir = xmalloc (size);
1360 memcpy (st->dumpdir, arg, size);
1361 }
1362
1363 static void
1364 volume_label_coder (struct tar_stat_info const *st, char const *keyword,
1365 struct xheader *xhdr, void const *data)
1366 {
1367 code_string (data, keyword, xhdr);
1368 }
1369
1370 static void
1371 volume_label_decoder (struct tar_stat_info *st,
1372 char const *keyword __attribute__((unused)),
1373 char const *arg,
1374 size_t size __attribute__((unused)))
1375 {
1376 decode_string (&volume_label, arg);
1377 }
1378
1379 static void
1380 volume_size_coder (struct tar_stat_info const *st, char const *keyword,
1381 struct xheader *xhdr, void const *data)
1382 {
1383 off_t v = *(off_t*)data;
1384 code_num (v, keyword, xhdr);
1385 }
1386
1387 static void
1388 volume_size_decoder (struct tar_stat_info *st,
1389 char const *keyword,
1390 char const *arg, size_t size)
1391 {
1392 uintmax_t u;
1393 if (decode_num (&u, arg, TYPE_MAXIMUM (uintmax_t), keyword))
1394 continued_file_size = u;
1395 }
1396
1397 /* FIXME: Merge with volume_size_coder */
1398 static void
1399 volume_offset_coder (struct tar_stat_info const *st, char const *keyword,
1400 struct xheader *xhdr, void const *data)
1401 {
1402 off_t v = *(off_t*)data;
1403 code_num (v, keyword, xhdr);
1404 }
1405
1406 static void
1407 volume_offset_decoder (struct tar_stat_info *st,
1408 char const *keyword,
1409 char const *arg, size_t size)
1410 {
1411 uintmax_t u;
1412 if (decode_num (&u, arg, TYPE_MAXIMUM (uintmax_t), keyword))
1413 continued_file_offset = u;
1414 }
1415
1416 static void
1417 volume_filename_decoder (struct tar_stat_info *st,
1418 char const *keyword __attribute__((unused)),
1419 char const *arg,
1420 size_t size __attribute__((unused)))
1421 {
1422 decode_string (&continued_file_name, arg);
1423 }
1424
1425 static void
1426 sparse_major_coder (struct tar_stat_info const *st, char const *keyword,
1427 struct xheader *xhdr, void const *data)
1428 {
1429 code_num (st->sparse_major, keyword, xhdr);
1430 }
1431
1432 static void
1433 sparse_major_decoder (struct tar_stat_info *st,
1434 char const *keyword,
1435 char const *arg,
1436 size_t size)
1437 {
1438 uintmax_t u;
1439 if (decode_num (&u, arg, TYPE_MAXIMUM (unsigned), keyword))
1440 st->sparse_major = u;
1441 }
1442
1443 static void
1444 sparse_minor_coder (struct tar_stat_info const *st, char const *keyword,
1445 struct xheader *xhdr, void const *data)
1446 {
1447 code_num (st->sparse_minor, keyword, xhdr);
1448 }
1449
1450 static void
1451 sparse_minor_decoder (struct tar_stat_info *st,
1452 char const *keyword,
1453 char const *arg,
1454 size_t size)
1455 {
1456 uintmax_t u;
1457 if (decode_num (&u, arg, TYPE_MAXIMUM (unsigned), keyword))
1458 st->sparse_minor = u;
1459 }
1460
1461 struct xhdr_tab const xhdr_tab[] = {
1462 { "atime", atime_coder, atime_decoder, false },
1463 { "comment", dummy_coder, dummy_decoder, false },
1464 { "charset", dummy_coder, dummy_decoder, false },
1465 { "ctime", ctime_coder, ctime_decoder, false },
1466 { "gid", gid_coder, gid_decoder, false },
1467 { "gname", gname_coder, gname_decoder, false },
1468 { "linkpath", linkpath_coder, linkpath_decoder, false },
1469 { "mtime", mtime_coder, mtime_decoder, false },
1470 { "path", path_coder, path_decoder, false },
1471 { "size", size_coder, size_decoder, false },
1472 { "uid", uid_coder, uid_decoder, false },
1473 { "uname", uname_coder, uname_decoder, false },
1474
1475 /* Sparse file handling */
1476 { "GNU.sparse.name", path_coder, path_decoder,
1477 true },
1478 { "GNU.sparse.major", sparse_major_coder, sparse_major_decoder,
1479 true },
1480 { "GNU.sparse.minor", sparse_minor_coder, sparse_minor_decoder,
1481 true },
1482 { "GNU.sparse.realsize", sparse_size_coder, sparse_size_decoder,
1483 true },
1484 { "GNU.sparse.numblocks", sparse_numblocks_coder, sparse_numblocks_decoder,
1485 true },
1486
1487 /* tar 1.14 - 1.15.90 keywords. */
1488 { "GNU.sparse.size", sparse_size_coder, sparse_size_decoder, true },
1489 /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
1490 headers, and each of them was meaningful. It confilcted with POSIX specs,
1491 which requires that "when extended header records conflict, the last one
1492 given in the header shall take precedence." */
1493 { "GNU.sparse.offset", sparse_offset_coder, sparse_offset_decoder,
1494 true },
1495 { "GNU.sparse.numbytes", sparse_numbytes_coder, sparse_numbytes_decoder,
1496 true },
1497 /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
1498 { "GNU.sparse.map", NULL /* Unused, see pax_dump_header() */,
1499 sparse_map_decoder, false },
1500
1501 { "GNU.dumpdir", dumpdir_coder, dumpdir_decoder,
1502 true },
1503
1504 /* Keeps the tape/volume label. May be present only in the global headers.
1505 Equivalent to GNUTYPE_VOLHDR. */
1506 { "GNU.volume.label", volume_label_coder, volume_label_decoder, true },
1507
1508 /* These may be present in a first global header of the archive.
1509 They provide the same functionality as GNUTYPE_MULTIVOL header.
1510 The GNU.volume.size keeps the real_s_sizeleft value, which is
1511 otherwise kept in the size field of a multivolume header. The
1512 GNU.volume.offset keeps the offset of the start of this volume,
1513 otherwise kept in oldgnu_header.offset. */
1514 { "GNU.volume.filename", volume_label_coder, volume_filename_decoder,
1515 true },
1516 { "GNU.volume.size", volume_size_coder, volume_size_decoder, true },
1517 { "GNU.volume.offset", volume_offset_coder, volume_offset_decoder, true },
1518
1519 { NULL, NULL, NULL, false }
1520 };
This page took 0.107736 seconds and 4 git commands to generate.