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