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