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