]> Dogcows Code - chaz/tar/blob - src/xheader.c
Use keywords from the global headers.
[chaz/tar] / src / xheader.c
1 /* POSIX extended headers for tar.
2
3 Copyright (C) 2003, 2004 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 #include "system.h"
20
21 #include <hash.h>
22 #include <quotearg.h>
23 #include <xstrtol.h>
24
25 #include "common.h"
26
27 #define obstack_chunk_alloc xmalloc
28 #define obstack_chunk_free free
29 #include <obstack.h>
30
31 bool xheader_protected_pattern_p (const char *pattern);
32 bool xheader_protected_keyword_p (const char *keyword);
33
34 /* Used by xheader_finish() */
35 static void code_string (char const *string, char const *keyword,
36 struct xheader *xhdr);
37 static void extended_header_init ();
38
39 /* Number of global headers written so far. */
40 static size_t global_header_count;
41 /* FIXME: Possibly it should be reset after changing the volume.
42 POSIX %n specification says that it is expanded to the sequence
43 number of current global header in *the* archive. However, for
44 multi-volume archives this will yield duplicate header names
45 in different volumes, which I'd like to avoid. The best way
46 to solve this would be to use per-archive header count as required
47 by POSIX *and* set globexthdr.name to, say,
48 $TMPDIR/GlobalHead.%p.$NUMVOLUME.%n.
49
50 However it should wait until buffer.c is finally rewritten */
51
52 \f
53 /* Keyword options */
54
55 struct keyword_list
56 {
57 struct keyword_list *next;
58 char *pattern;
59 char *value;
60 };
61
62
63 /* List of keyword patterns set by delete= option */
64 static struct keyword_list *keyword_pattern_list;
65
66 /* List of keyword/value pairs set by `keyword=value' option */
67 static struct keyword_list *keyword_global_override_list;
68
69 /* List of keyword/value pairs set by `keyword:=value' option */
70 static struct keyword_list *keyword_override_list;
71
72 /* List of keyword/value pairs decoded from the last 'g' type header */
73 static struct keyword_list *global_header_override_list;
74
75 /* Template for the name field of an 'x' type header */
76 static char *exthdr_name;
77
78 /* Template for the name field of a 'g' type header */
79 static char *globexthdr_name;
80
81 bool
82 xheader_keyword_deleted_p (const char *kw)
83 {
84 struct keyword_list *kp;
85
86 for (kp = keyword_pattern_list; kp; kp = kp->next)
87 if (fnmatch (kp->pattern, kw, 0) == 0)
88 return true;
89 return false;
90 }
91
92 bool
93 xheader_keyword_override_p (const char *keyword)
94 {
95 struct keyword_list *kp;
96
97 for (kp = keyword_override_list; kp; kp = kp->next)
98 if (strcmp (kp->pattern, keyword) == 0)
99 return true;
100 return false;
101 }
102
103 void
104 xheader_list_append (struct keyword_list **root, char const *kw,
105 char const *value)
106 {
107 struct keyword_list *kp = xmalloc (sizeof *kp);
108 kp->pattern = xstrdup (kw);
109 kp->value = value ? xstrdup (value) : NULL;
110 kp->next = *root;
111 *root = kp;
112 }
113
114 void
115 xheader_list_destroy (struct keyword_list **root)
116 {
117 if (root)
118 {
119 struct keyword_list *kw = *root;
120 while (kw)
121 {
122 struct keyword_list *next = kw->next;
123 free (kw->pattern);
124 free (kw->value);
125 free (kw);
126 kw = next;
127 }
128 *root = NULL;
129 }
130 }
131
132
133 void
134 xheader_set_single_keyword (char *kw)
135 {
136 USAGE_ERROR ((0, 0, _("Keyword %s is unknown or not yet imlemented"), kw));
137 }
138
139 void
140 xheader_set_keyword_equal (char *kw, char *eq)
141 {
142 bool global = true;
143 char *p = eq;
144
145 if (eq[-1] == ':')
146 {
147 p--;
148 global = false;
149 }
150
151 while (p > kw && isspace (*p))
152 p--;
153
154 *p = 0;
155
156 for (p = eq + 1; *p && isspace (*p); p++)
157 ;
158
159 if (strcmp (kw, "delete") == 0)
160 {
161 if (xheader_protected_pattern_p (p))
162 USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), p));
163 xheader_list_append (&keyword_pattern_list, p, NULL);
164 }
165 else if (strcmp (kw, "exthdr.name") == 0)
166 assign_string (&exthdr_name, p);
167 else if (strcmp (kw, "globexthdr.name") == 0)
168 assign_string (&globexthdr_name, p);
169 else
170 {
171 if (xheader_protected_keyword_p (kw))
172 USAGE_ERROR ((0, 0, _("Keyword %s cannot be overridden"), kw));
173 if (global)
174 xheader_list_append (&keyword_global_override_list, kw, p);
175 else
176 xheader_list_append (&keyword_override_list, kw, p);
177 }
178 }
179
180 void
181 xheader_set_option (char *string)
182 {
183 char *token;
184 for (token = strtok (string, ","); token; token = strtok (NULL, ","))
185 {
186 char *p = strchr (token, '=');
187 if (!p)
188 xheader_set_single_keyword (token);
189 else
190 xheader_set_keyword_equal (token, p);
191 }
192 }
193
194 /*
195 string Includes: Replaced By:
196 %d The directory name of the file,
197 equivalent to the result of the
198 dirname utility on the translated
199 pathname.
200 %f The filename of the file, equivalent
201 to the result of the basename
202 utility on the translated pathname.
203 %p The process ID of the pax process.
204 %% A '%' character. */
205
206 static char *
207 xheader_format_name (struct tar_stat_info *st, const char *fmt, bool allow_n)
208 {
209 char *buf;
210 size_t len = strlen (fmt);
211 char *q;
212 const char *p;
213 char *dirname = NULL;
214 char *basename = NULL;
215 char pidbuf[64];
216 char nbuf[64];
217
218 for (p = fmt; *p && (p = strchr (p, '%')); )
219 {
220 switch (p[1])
221 {
222 case '%':
223 len--;
224 break;
225
226 case 'd':
227 if (st)
228 {
229 dirname = safer_name_suffix (dir_name (st->orig_file_name),
230 false);
231 len += strlen (dirname) - 1;
232 }
233 break;
234
235 case 'f':
236 if (st)
237 {
238 basename = base_name (st->orig_file_name);
239 len += strlen (basename) - 1;
240 }
241 break;
242
243 case 'p':
244 snprintf (pidbuf, sizeof pidbuf, "%lu",
245 (unsigned long) getpid ());
246 len += strlen (pidbuf) - 1;
247 break;
248
249 case 'n':
250 if (allow_n)
251 {
252 snprintf (nbuf, sizeof nbuf, "%lu",
253 (unsigned long) global_header_count + 1);
254 len += strlen (nbuf) - 1;
255 }
256 break;
257 }
258 p++;
259 }
260
261 buf = xmalloc (len + 1);
262 for (q = buf, p = fmt; *p; )
263 {
264 if (*p == '%')
265 {
266 switch (p[1])
267 {
268 case '%':
269 *q++ = *p++;
270 p++;
271 break;
272
273 case 'd':
274 if (dirname)
275 q = stpcpy (q, dirname);
276 p += 2;
277 break;
278
279 case 'f':
280 if (basename)
281 q = stpcpy (q, basename);
282 p += 2;
283 break;
284
285 case 'p':
286 q = stpcpy (q, pidbuf);
287 p += 2;
288 break;
289
290 case 'n':
291 if (allow_n)
292 {
293 q = stpcpy (q, nbuf);
294 p += 2;
295 }
296 /* else fall through */
297
298 default:
299 *q++ = *p++;
300 if (*p)
301 *q++ = *p++;
302 }
303 }
304 else
305 *q++ = *p++;
306 }
307
308 /* Do not allow it to end in a slash */
309 while (q > buf && ISSLASH (q[-1]))
310 q--;
311 *q = 0;
312 return buf;
313 }
314
315 char *
316 xheader_xhdr_name (struct tar_stat_info *st)
317 {
318 if (!exthdr_name)
319 assign_string (&exthdr_name, "%d/PaxHeaders.%p/%f");
320 return xheader_format_name (st, exthdr_name, false);
321 }
322
323 #define GLOBAL_HEADER_TEMPLATE "/GlobalHead.%p.%n"
324
325 char *
326 xheader_ghdr_name ()
327 {
328 if (!globexthdr_name)
329 {
330 size_t len;
331 const char *tmp = getenv ("TMPDIR");
332 if (!tmp)
333 tmp = "/tmp";
334 len = strlen (tmp) + sizeof (GLOBAL_HEADER_TEMPLATE); /* Includes nul */
335 globexthdr_name = xmalloc (len);
336 strcpy(globexthdr_name, tmp);
337 strcat(globexthdr_name, GLOBAL_HEADER_TEMPLATE);
338 }
339
340 return xheader_format_name (NULL, globexthdr_name, true);
341 }
342
343 void
344 xheader_write (char type, char *name, struct xheader *xhdr)
345 {
346 union block *header;
347 size_t size;
348 char *p;
349
350 size = xhdr->size;
351 header = start_private_header (name, size);
352 header->header.typeflag = type;
353
354 simple_finish_header (header);
355
356 p = xhdr->buffer;
357
358 do
359 {
360 size_t len;
361
362 header = find_next_block ();
363 len = BLOCKSIZE;
364 if (len > size)
365 len = size;
366 memcpy (header->buffer, p, len);
367 if (len < BLOCKSIZE)
368 memset (header->buffer + len, 0, BLOCKSIZE - len);
369 p += len;
370 size -= len;
371 set_next_block_after (header);
372 }
373 while (size > 0);
374 xheader_destroy (xhdr);
375 }
376
377 void
378 xheader_write_global ()
379 {
380 char *name;
381 struct keyword_list *kp;
382
383 if (!keyword_global_override_list)
384 return;
385
386 extended_header_init ();
387 for (kp = keyword_global_override_list; kp; kp = kp->next)
388 code_string (kp->value, kp->pattern, &extended_header);
389 xheader_finish (&extended_header);
390 xheader_write (XGLTYPE, name = xheader_ghdr_name (),
391 &extended_header);
392 free (name);
393 global_header_count++;
394 }
395
396 \f
397 /* General Interface */
398
399 struct xhdr_tab
400 {
401 char const *keyword;
402 void (*coder) (struct tar_stat_info const *, char const *,
403 struct xheader *, void *data);
404 void (*decoder) (struct tar_stat_info *, char const *);
405 bool protect;
406 };
407
408 /* This declaration must be extern, because ISO C99 section 6.9.2
409 prohibits a tentative definition that has both internal linkage and
410 incomplete type. If we made it static, we'd have to declare its
411 size which would be a maintenance pain; if we put its initializer
412 here, we'd need a boatload of forward declarations, which would be
413 even more of a pain. */
414 extern struct xhdr_tab const xhdr_tab[];
415
416 static struct xhdr_tab const *
417 locate_handler (char const *keyword)
418 {
419 struct xhdr_tab const *p;
420
421 for (p = xhdr_tab; p->keyword; p++)
422 if (strcmp (p->keyword, keyword) == 0)
423 return p;
424 return NULL;
425 }
426
427 bool
428 xheader_protected_pattern_p (const char *pattern)
429 {
430 struct xhdr_tab const *p;
431
432 for (p = xhdr_tab; p->keyword; p++)
433 if (p->protect && fnmatch (pattern, p->keyword, 0) == 0)
434 return true;
435 return false;
436 }
437
438 bool
439 xheader_protected_keyword_p (const char *keyword)
440 {
441 struct xhdr_tab const *p;
442
443 for (p = xhdr_tab; p->keyword; p++)
444 if (p->protect && strcmp (p->keyword, keyword) == 0)
445 return true;
446 return false;
447 }
448
449 /* Decodes a single extended header record. Advances P to the next
450 record.
451 Returns true on success, false otherwise. */
452 static bool
453 decode_record (char **p,
454 void (*handler) (void *, char const *, char const *),
455 void *data)
456 {
457 size_t len;
458 char const *keyword;
459 char *start = *p;
460 char endc;
461
462 if (**p == 0)
463 return false;
464
465 len = strtoul (*p, p, 10);
466 if (**p != ' ')
467 {
468 ERROR ((0, 0,
469 _("Malformed extended header: missing whitespace after the length")));
470 return false;
471 }
472
473 keyword = ++*p;
474 for (;*p < start + len; ++*p)
475 if (**p == '=')
476 break;
477
478 if (**p != '=')
479 {
480 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
481 return false;
482 }
483
484 **p = 0;
485
486 endc = start[len-1];
487 start[len-1] = 0;
488
489 handler (data, keyword, *p + 1);
490
491 start[len-1] = endc;
492 **p = '=';
493 *p = &start[len];
494 return true;
495 }
496
497 static void
498 run_override_list (struct keyword_list *kp, struct tar_stat_info *st)
499 {
500 for (; kp; kp = kp->next)
501 {
502 struct xhdr_tab const *t = locate_handler (kp->pattern);
503 if (t)
504 t->decoder (st, kp->value);
505 }
506 }
507
508 static void
509 decx (void *data, char const *keyword, char const *value)
510 {
511 struct xhdr_tab const *t;
512 struct tar_stat_info *st = data;
513
514 if (xheader_keyword_deleted_p (keyword)
515 || xheader_keyword_override_p (keyword))
516 return;
517
518 t = locate_handler (keyword);
519 if (t)
520 t->decoder (st, value);
521 }
522
523 void
524 xheader_decode (struct tar_stat_info *st)
525 {
526 run_override_list (keyword_global_override_list, st);
527 run_override_list (global_header_override_list, st);
528
529 if (extended_header.size)
530 {
531 char *p = extended_header.buffer + BLOCKSIZE;
532 char *endp = &extended_header.buffer[extended_header.size-1];
533
534 while (p < endp)
535 if (!decode_record (&p, decx, st))
536 break;
537 }
538 run_override_list (keyword_override_list, st);
539 }
540
541 static void
542 decg (void *data, char const *keyword, char const *value)
543 {
544 struct keyword_list **kwl = data;
545 xheader_list_append (kwl, keyword, value);
546 }
547
548 void
549 xheader_decode_global ()
550 {
551 if (extended_header.size)
552 {
553 char *p = extended_header.buffer + BLOCKSIZE;
554 char *endp = &extended_header.buffer[extended_header.size-1];
555
556 xheader_list_destroy (&global_header_override_list);
557 while (p < endp)
558 if (!decode_record (&p, decg, &global_header_override_list))
559 break;
560 }
561 }
562
563 static void
564 extended_header_init ()
565 {
566 if (!extended_header.stk)
567 {
568 extended_header.stk = xmalloc (sizeof *extended_header.stk);
569 obstack_init (extended_header.stk);
570 }
571 }
572
573 void
574 xheader_store (char const *keyword, struct tar_stat_info const *st, void *data)
575 {
576 struct xhdr_tab const *t;
577 char *value;
578
579 if (extended_header.buffer)
580 return;
581 t = locate_handler (keyword);
582 if (!t)
583 return;
584 if (xheader_keyword_deleted_p (keyword)
585 || xheader_keyword_override_p (keyword))
586 return;
587 extended_header_init ();
588 t->coder (st, keyword, &extended_header, data);
589 }
590
591 void
592 xheader_read (union block *p, size_t size)
593 {
594 size_t j = 0;
595 size_t nblocks;
596
597 free (extended_header.buffer);
598 size += BLOCKSIZE;
599 extended_header.size = size;
600 nblocks = (size + BLOCKSIZE - 1) / BLOCKSIZE;
601 extended_header.buffer = xmalloc (size + 1);
602
603 do
604 {
605 size_t len = size;
606
607 if (len > BLOCKSIZE)
608 len = BLOCKSIZE;
609
610 memcpy (&extended_header.buffer[j], p->buffer, len);
611 set_next_block_after (p);
612
613 p = find_next_block ();
614
615 j += len;
616 size -= len;
617 }
618 while (size > 0);
619 }
620
621 static size_t
622 format_uintmax (uintmax_t val, char *buf, size_t s)
623 {
624 if (!buf)
625 {
626 s = 0;
627 do
628 s++;
629 while ((val /= 10) != 0);
630 }
631 else
632 {
633 char *p = buf + s - 1;
634
635 do
636 {
637 *p-- = val % 10 + '0';
638 }
639 while ((val /= 10) != 0);
640
641 while (p >= buf)
642 *p-- = '0';
643 }
644 return s;
645 }
646
647 static void
648 xheader_print (struct xheader *xhdr, char const *keyword, char const *value)
649 {
650 size_t len = strlen (keyword) + strlen (value) + 3; /* ' ' + '=' + '\n' */
651 size_t p, n = 0;
652 char nbuf[100];
653
654 do
655 {
656 p = n;
657 n = format_uintmax (len + p, NULL, 0);
658 }
659 while (n != p);
660
661 format_uintmax (len + n, nbuf, n);
662 obstack_grow (xhdr->stk, nbuf, n);
663 obstack_1grow (xhdr->stk, ' ');
664 obstack_grow (xhdr->stk, keyword, strlen (keyword));
665 obstack_1grow (xhdr->stk, '=');
666 obstack_grow (xhdr->stk, value, strlen (value));
667 obstack_1grow (xhdr->stk, '\n');
668 }
669
670 void
671 xheader_finish (struct xheader *xhdr)
672 {
673 struct keyword_list *kp;
674
675 for (kp = keyword_override_list; kp; kp = kp->next)
676 code_string (kp->value, kp->pattern, xhdr);
677
678 obstack_1grow (xhdr->stk, 0);
679 xhdr->buffer = obstack_finish (xhdr->stk);
680 xhdr->size = strlen (xhdr->buffer);
681 }
682
683 void
684 xheader_destroy (struct xheader *xhdr)
685 {
686 if (xhdr->stk)
687 {
688 obstack_free (xhdr->stk, NULL);
689 free (xhdr->stk);
690 xhdr->stk = NULL;
691 }
692 else
693 free (xhdr->buffer);
694 xhdr->buffer = 0;
695 xhdr->size = 0;
696 }
697
698 \f
699 /* Implementations */
700 static void
701 code_string (char const *string, char const *keyword, struct xheader *xhdr)
702 {
703 char *outstr;
704 if (!utf8_convert (true, string, &outstr))
705 {
706 /* FIXME: report error */
707 outstr = xstrdup (string);
708 }
709 xheader_print (xhdr, keyword, outstr);
710 free (outstr);
711 }
712
713 static void
714 decode_string (char **string, char const *arg)
715 {
716 if (*string)
717 {
718 free (*string);
719 *string = NULL;
720 }
721 if (!utf8_convert (false, arg, string))
722 {
723 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
724 assign_string (string, arg);
725 }
726 }
727
728 static void
729 code_time (time_t t, unsigned long nano,
730 char const *keyword, struct xheader *xhdr)
731 {
732 char sbuf[200];
733 size_t s = format_uintmax (t, NULL, 0);
734 if (s + 11 >= sizeof sbuf)
735 return;
736 format_uintmax (t, sbuf, s);
737 sbuf[s++] = '.';
738 s += format_uintmax (nano, sbuf + s, 9);
739 sbuf[s] = 0;
740 xheader_print (xhdr, keyword, sbuf);
741 }
742
743 static void
744 decode_time (char const *arg, time_t *secs, unsigned long *nsecs)
745 {
746 uintmax_t u;
747 char *p;
748 if (xstrtoumax (arg, &p, 10, &u, "") == LONGINT_OK)
749 {
750 *secs = u;
751 if (*p == '.' && xstrtoumax (p+1, NULL, 10, &u, "") == LONGINT_OK)
752 *nsecs = u;
753 }
754 }
755
756 static void
757 code_num (uintmax_t value, char const *keyword, struct xheader *xhdr)
758 {
759 char sbuf[100];
760 size_t s = format_uintmax (value, NULL, 0);
761 format_uintmax (value, sbuf, s);
762 sbuf[s] = 0;
763 xheader_print (xhdr, keyword, sbuf);
764 }
765
766 static void
767 dummy_coder (struct tar_stat_info const *st, char const *keyword,
768 struct xheader *xhdr, void *data)
769 {
770 }
771
772 static void
773 dummy_decoder (struct tar_stat_info *st, char const *arg)
774 {
775 }
776
777 static void
778 atime_coder (struct tar_stat_info const *st, char const *keyword,
779 struct xheader *xhdr, void *data)
780 {
781 code_time (st->stat.st_atime, st->atime_nsec, keyword, xhdr);
782 }
783
784 static void
785 atime_decoder (struct tar_stat_info *st, char const *arg)
786 {
787 decode_time (arg, &st->stat.st_atime, &st->atime_nsec);
788 }
789
790 static void
791 gid_coder (struct tar_stat_info const *st, char const *keyword,
792 struct xheader *xhdr, void *data)
793 {
794 code_num (st->stat.st_gid, keyword, xhdr);
795 }
796
797 static void
798 gid_decoder (struct tar_stat_info *st, char const *arg)
799 {
800 uintmax_t u;
801 if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
802 st->stat.st_gid = u;
803 }
804
805 static void
806 gname_coder (struct tar_stat_info const *st, char const *keyword,
807 struct xheader *xhdr, void *data)
808 {
809 code_string (st->gname, keyword, xhdr);
810 }
811
812 static void
813 gname_decoder (struct tar_stat_info *st, char const *arg)
814 {
815 decode_string (&st->gname, arg);
816 }
817
818 static void
819 linkpath_coder (struct tar_stat_info const *st, char const *keyword,
820 struct xheader *xhdr, void *data)
821 {
822 code_string (st->link_name, keyword, xhdr);
823 }
824
825 static void
826 linkpath_decoder (struct tar_stat_info *st, char const *arg)
827 {
828 decode_string (&st->link_name, arg);
829 }
830
831 static void
832 ctime_coder (struct tar_stat_info const *st, char const *keyword,
833 struct xheader *xhdr, void *data)
834 {
835 code_time (st->stat.st_ctime, st->ctime_nsec, keyword, xhdr);
836 }
837
838 static void
839 ctime_decoder (struct tar_stat_info *st, char const *arg)
840 {
841 decode_time (arg, &st->stat.st_ctime, &st->ctime_nsec);
842 }
843
844 static void
845 mtime_coder (struct tar_stat_info const *st, char const *keyword,
846 struct xheader *xhdr, void *data)
847 {
848 code_time (st->stat.st_mtime, st->mtime_nsec, keyword, xhdr);
849 }
850
851 static void
852 mtime_decoder (struct tar_stat_info *st, char const *arg)
853 {
854 decode_time (arg, &st->stat.st_mtime, &st->mtime_nsec);
855 }
856
857 static void
858 path_coder (struct tar_stat_info const *st, char const *keyword,
859 struct xheader *xhdr, void *data)
860 {
861 code_string (st->file_name, keyword, xhdr);
862 }
863
864 static void
865 path_decoder (struct tar_stat_info *st, char const *arg)
866 {
867 decode_string (&st->orig_file_name, arg);
868 decode_string (&st->file_name, arg);
869 st->had_trailing_slash = strip_trailing_slashes (st->file_name);
870 }
871
872 static void
873 size_coder (struct tar_stat_info const *st, char const *keyword,
874 struct xheader *xhdr, void *data)
875 {
876 code_num (st->stat.st_size, keyword, xhdr);
877 }
878
879 static void
880 size_decoder (struct tar_stat_info *st, char const *arg)
881 {
882 uintmax_t u;
883 if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
884 st->stat.st_size = u;
885 }
886
887 static void
888 uid_coder (struct tar_stat_info const *st, char const *keyword,
889 struct xheader *xhdr, void *data)
890 {
891 code_num (st->stat.st_uid, keyword, xhdr);
892 }
893
894 static void
895 uid_decoder (struct tar_stat_info *st, char const *arg)
896 {
897 uintmax_t u;
898 if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
899 st->stat.st_uid = u;
900 }
901
902 static void
903 uname_coder (struct tar_stat_info const *st, char const *keyword,
904 struct xheader *xhdr, void *data)
905 {
906 code_string (st->uname, keyword, xhdr);
907 }
908
909 static void
910 uname_decoder (struct tar_stat_info *st, char const *arg)
911 {
912 decode_string (&st->uname, arg);
913 }
914
915 static void
916 sparse_size_coder (struct tar_stat_info const *st, char const *keyword,
917 struct xheader *xhdr, void *data)
918 {
919 size_coder (st, keyword, xhdr, data);
920 }
921
922 static void
923 sparse_size_decoder (struct tar_stat_info *st, char const *arg)
924 {
925 uintmax_t u;
926 if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
927 st->archive_file_size = u;
928 }
929
930 static void
931 sparse_numblocks_coder (struct tar_stat_info const *st, char const *keyword,
932 struct xheader *xhdr, void *data)
933 {
934 code_num (st->sparse_map_avail, keyword, xhdr);
935 }
936
937 static void
938 sparse_numblocks_decoder (struct tar_stat_info *st, char const *arg)
939 {
940 uintmax_t u;
941 if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
942 {
943 st->sparse_map_size = u;
944 st->sparse_map = calloc(st->sparse_map_size, sizeof(st->sparse_map[0]));
945 st->sparse_map_avail = 0;
946 }
947 }
948
949 static void
950 sparse_offset_coder (struct tar_stat_info const *st, char const *keyword,
951 struct xheader *xhdr, void *data)
952 {
953 size_t i = *(size_t*)data;
954 code_num (st->sparse_map[i].offset, keyword, xhdr);
955 }
956
957 static void
958 sparse_offset_decoder (struct tar_stat_info *st, char const *arg)
959 {
960 uintmax_t u;
961 if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
962 st->sparse_map[st->sparse_map_avail].offset = u;
963 }
964
965 static void
966 sparse_numbytes_coder (struct tar_stat_info const *st, char const *keyword,
967 struct xheader *xhdr, void *data)
968 {
969 size_t i = *(size_t*)data;
970 code_num (st->sparse_map[i].numbytes, keyword, xhdr);
971 }
972
973 static void
974 sparse_numbytes_decoder (struct tar_stat_info *st, char const *arg)
975 {
976 uintmax_t u;
977 if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
978 {
979 if (st->sparse_map_avail == st->sparse_map_size)
980 {
981 size_t newsize = st->sparse_map_size *= 2;
982 st->sparse_map = xrealloc (st->sparse_map,
983 st->sparse_map_size
984 * sizeof st->sparse_map[0]);
985 }
986 st->sparse_map[st->sparse_map_avail++].numbytes = u;
987 }
988 }
989
990 struct xhdr_tab const xhdr_tab[] = {
991 { "atime", atime_coder, atime_decoder },
992 { "comment", dummy_coder, dummy_decoder },
993 { "charset", dummy_coder, dummy_decoder },
994 { "ctime", ctime_coder, ctime_decoder },
995 { "gid", gid_coder, gid_decoder },
996 { "gname", gname_coder, gname_decoder },
997 { "linkpath", linkpath_coder, linkpath_decoder},
998 { "mtime", mtime_coder, mtime_decoder },
999 { "path", path_coder, path_decoder },
1000 { "size", size_coder, size_decoder },
1001 { "uid", uid_coder, uid_decoder },
1002 { "uname", uname_coder, uname_decoder },
1003
1004 /* Sparse file handling */
1005 { "GNU.sparse.size", sparse_size_coder, sparse_size_decoder, true },
1006 { "GNU.sparse.numblocks", sparse_numblocks_coder, sparse_numblocks_decoder,
1007 true },
1008 { "GNU.sparse.offset", sparse_offset_coder, sparse_offset_decoder,
1009 true },
1010 { "GNU.sparse.numbytes", sparse_numbytes_coder, sparse_numbytes_decoder,
1011 true },
1012
1013 #if 0 /* GNU private keywords (not yet implemented) */
1014
1015 /* The next directory entry actually contains the names of files
1016 that were in the directory at the time the dump was made.
1017 Supersedes GNUTYPE_DUMPDIR header type. */
1018 { "GNU.dump.name", dump_name_coder, dump_name_decoder },
1019 { "GNU.dump.status", dump_status_coder, dump_status_decoder },
1020
1021 /* Keeps the tape/volume header. May be present only in the global headers.
1022 Equivalent to GNUTYPE_VOLHDR. */
1023 { "GNU.volume.header", volume_header_coder, volume_header_decoder },
1024
1025 /* These may be present in a first global header of the archive.
1026 They provide the same functionality as GNUTYPE_MULTIVOL header.
1027 The GNU.volume.size keeps the real_s_sizeleft value, which is
1028 otherwise kept in the size field of a multivolume header. The
1029 GNU.volume.offset keeps the offset of the start of this volume,
1030 otherwise kept in oldgnu_header.offset. */
1031 { "GNU.volume.size", volume_size_coder, volume_size_decoder },
1032 { "GNU.volume.offset", volume_offset_coder, volume_offset_decoder },
1033 #endif
1034
1035 { NULL, NULL, NULL }
1036 };
This page took 0.079694 seconds and 5 git commands to generate.