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