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