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