2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2008 Joerg Sonnenberger
4 * Copyright (c) 2011 Michihiro NAKAJIMA
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
31 #ifdef HAVE_SYS_STAT_H
41 /* #include <stdint.h> */ /* See archive_platform.h */
50 #include "archive_entry.h"
51 #include "archive_private.h"
52 #include "archive_read_private.h"
53 #include "archive_string.h"
59 #define MTREE_HAS_DEVICE 0x0001
60 #define MTREE_HAS_FFLAGS 0x0002
61 #define MTREE_HAS_GID 0x0004
62 #define MTREE_HAS_GNAME 0x0008
63 #define MTREE_HAS_MTIME 0x0010
64 #define MTREE_HAS_NLINK 0x0020
65 #define MTREE_HAS_PERM 0x0040
66 #define MTREE_HAS_SIZE 0x0080
67 #define MTREE_HAS_TYPE 0x0100
68 #define MTREE_HAS_UID 0x0200
69 #define MTREE_HAS_UNAME 0x0400
71 #define MTREE_HAS_OPTIONAL 0x0800
74 struct mtree_option *next;
79 struct mtree_entry *next;
80 struct mtree_option *options;
87 struct archive_string line;
93 const char *archive_format_name;
94 struct mtree_entry *entries;
95 struct mtree_entry *this_entry;
96 struct archive_string current_dir;
97 struct archive_string contents_name;
99 struct archive_entry_linkresolver *resolver;
104 static int bid_keycmp(const char *, const char *, ssize_t);
105 static int cleanup(struct archive_read *);
106 static int mtree_bid(struct archive_read *, int);
107 static int parse_file(struct archive_read *, struct archive_entry *,
108 struct mtree *, struct mtree_entry *, int *);
109 static void parse_escapes(char *, struct mtree_entry *);
110 static int parse_line(struct archive_read *, struct archive_entry *,
111 struct mtree *, struct mtree_entry *, int *);
112 static int parse_keyword(struct archive_read *, struct mtree *,
113 struct archive_entry *, struct mtree_option *, int *);
114 static int read_data(struct archive_read *a,
115 const void **buff, size_t *size, int64_t *offset);
116 static ssize_t readline(struct archive_read *, struct mtree *, char **, ssize_t);
117 static int skip(struct archive_read *a);
118 static int read_header(struct archive_read *,
119 struct archive_entry *);
120 static int64_t mtree_atol10(char **);
121 static int64_t mtree_atol8(char **);
122 static int64_t mtree_atol(char **);
125 * There's no standard for TIME_T_MAX/TIME_T_MIN. So we compute them
126 * here. TODO: Move this to configure time, but be careful
127 * about cross-compile environments.
132 #if defined(TIME_T_MAX)
151 #if defined(TIME_T_MIN)
154 /* 't' will hold the minimum value, which will be zero (if
155 * time_t is unsigned) or -2^n (if time_t is signed). */
172 free_options(struct mtree_option *head)
174 struct mtree_option *next;
176 for (; head != NULL; head = next) {
184 archive_read_support_format_mtree(struct archive *_a)
186 struct archive_read *a = (struct archive_read *)_a;
190 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
191 ARCHIVE_STATE_NEW, "archive_read_support_format_mtree");
193 mtree = (struct mtree *)malloc(sizeof(*mtree));
195 archive_set_error(&a->archive, ENOMEM,
196 "Can't allocate mtree data");
197 return (ARCHIVE_FATAL);
199 memset(mtree, 0, sizeof(*mtree));
202 r = __archive_read_register_format(a, mtree, "mtree",
203 mtree_bid, NULL, read_header, read_data, skip, cleanup);
211 cleanup(struct archive_read *a)
214 struct mtree_entry *p, *q;
216 mtree = (struct mtree *)(a->format->data);
222 free_options(p->options);
226 archive_string_free(&mtree->line);
227 archive_string_free(&mtree->current_dir);
228 archive_string_free(&mtree->contents_name);
229 archive_entry_linkresolver_free(mtree->resolver);
233 (a->format->data) = NULL;
238 get_line_size(const char *b, ssize_t avail, ssize_t *nlsize)
243 while (len < avail) {
245 case '\0':/* Non-ascii character or control character. */
250 if (avail-len > 1 && b[1] == '\n') {
272 next_line(struct archive_read *a,
273 const char **b, ssize_t *avail, ssize_t *ravail, ssize_t *nl)
283 len = get_line_size(*b, *avail, nl);
285 * Read bytes more while it does not reach the end of line.
287 while (*nl == 0 && len == *avail && !quit) {
288 ssize_t diff = *ravail - *avail;
289 size_t nbytes_req = (*ravail+1023) & ~1023U;
292 /* Increase reading bytes if it is not enough to at least
294 if (nbytes_req < (size_t)*ravail + 160)
297 *b = __archive_read_ahead(a, nbytes_req, avail);
299 if (*ravail >= *avail)
301 /* Reading bytes reaches the end of file. */
302 *b = __archive_read_ahead(a, *avail, avail);
308 tested = len;/* Skip some bytes we already determinated. */
309 len = get_line_size(*b, *avail, nl);
317 * Compare characters with a mtree keyword.
318 * Returns the length of a mtree keyword if matched.
319 * Returns 0 if not matched.
322 bid_keycmp(const char *p, const char *key, ssize_t len)
326 while (len > 0 && *p && *key) {
334 return (0);/* Not match */
337 return (0);/* Not match */
339 /* A following character should be specified characters */
340 if (p[0] == '=' || p[0] == ' ' || p[0] == '\t' ||
341 p[0] == '\n' || p[0] == '\r' ||
342 (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r')))
344 return (0);/* Not match */
348 * Test whether the characters 'p' has is mtree keyword.
349 * Returns the length of a detected keyword.
350 * Returns 0 if any keywords were not found.
353 bid_keyword(const char *p, ssize_t len)
355 static const char *keys_c[] = {
356 "content", "contents", "cksum", NULL
358 static const char *keys_df[] = {
359 "device", "flags", NULL
361 static const char *keys_g[] = {
364 static const char *keys_il[] = {
365 "ignore", "link", NULL
367 static const char *keys_m[] = {
368 "md5", "md5digest", "mode", NULL
370 static const char *keys_no[] = {
371 "nlink", "optional", NULL
373 static const char *keys_r[] = {
374 "rmd160", "rmd160digest", NULL
376 static const char *keys_s[] = {
377 "sha1", "sha1digest",
378 "sha256", "sha256digest",
379 "sha384", "sha384digest",
380 "sha512", "sha512digest",
383 static const char *keys_t[] = {
384 "tags", "time", "type", NULL
386 static const char *keys_u[] = {
393 case 'c': keys = keys_c; break;
394 case 'd': case 'f': keys = keys_df; break;
395 case 'g': keys = keys_g; break;
396 case 'i': case 'l': keys = keys_il; break;
397 case 'm': keys = keys_m; break;
398 case 'n': case 'o': keys = keys_no; break;
399 case 'r': keys = keys_r; break;
400 case 's': keys = keys_s; break;
401 case 't': keys = keys_t; break;
402 case 'u': keys = keys_u; break;
403 default: return (0);/* Unknown key */
406 for (i = 0; keys[i] != NULL; i++) {
407 int l = bid_keycmp(p, keys[i], len);
411 return (0);/* Unknown key */
415 * Test whether there is a set of mtree keywords.
416 * Returns the number of keyword.
417 * Returns -1 if we got incorrect sequence.
418 * This function expects a set of "<space characters>keyword=value".
419 * When "unset" is specified, expects a set of "<space characters>keyword".
422 bid_keyword_list(const char *p, ssize_t len, int unset)
427 while (len > 0 && *p) {
430 /* Test whether there are blank characters in the line. */
431 while (len >0 && (*p == ' ' || *p == '\t')) {
436 if (*p == '\n' || *p == '\r')
438 if (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r'))
440 if (!blank) /* No blank character. */
444 l = bid_keycmp(p, "all", len);
448 /* Test whether there is a correct key in the line. */
449 l = bid_keyword(p, len);
451 return (-1);/* Unknown keyword was found. */
461 while (len > 0 && *p != ' ' && *p != '\t') {
466 /* A keyword should have a its value unless
467 * "/unset" operation. */
468 if (!unset && value == 0)
476 bid_entry(const char *p, ssize_t len)
479 static const unsigned char safe_char[256] = {
480 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
481 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
482 /* !"$%&'()*+,-./ EXCLUSION:( )(#) */
483 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
484 /* 0123456789:;<>? EXCLUSION:(=) */
485 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
486 /* @ABCDEFGHIJKLMNO */
487 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
488 /* PQRSTUVWXYZ[\]^_ */
489 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
490 /* `abcdefghijklmno */
491 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
492 /* pqrstuvwxyz{|}~ */
493 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
494 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
495 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
496 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
497 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
498 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
499 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
500 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
501 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
505 * Skip the path-name which is quoted.
507 while (len > 0 && *p != ' ' && *p != '\t') {
508 if (!safe_char[*(const unsigned char *)p])
514 /* If a path-name was not found, returns error. */
518 return (bid_keyword_list(p, len, 0));
521 #define MAX_BID_ENTRY 3
524 mtree_bid(struct archive_read *a, int best_bid)
526 const char *signature = "#mtree";
528 ssize_t avail, ravail;
530 int detected_bytes = 0, entry_cnt = 0, multiline = 0;
532 (void)best_bid; /* UNUSED */
534 /* Now let's look at the actual header and see if it matches. */
535 p = __archive_read_ahead(a, strlen(signature), &avail);
539 if (memcmp(p, signature, strlen(signature)) == 0)
540 return (8 * (int)strlen(signature));
543 * There is not a mtree signature. Let's try to detect mtree format.
547 len = next_line(a, &p, &avail, &ravail, &nl);
548 /* The terminal character of the line should be
549 * a new line character, '\r\n' or '\n'. */
550 if (len <= 0 || nl == 0)
553 /* Leading whitespace is never significant,
555 while (len > 0 && (*p == ' ' || *p == '\t')) {
560 /* Skip comment or empty line. */
561 if (p[0] == '#' || p[0] == '\n' || p[0] == '\r') {
567 /* A continuance line; the terminal
568 * character of previous line was '\' character. */
569 if (bid_keyword_list(p, len, 0) <= 0)
572 detected_bytes += len;
573 if (p[len-nl-1] != '\\') {
574 if (multiline == 1 &&
575 ++entry_cnt >= MAX_BID_ENTRY)
584 if (bid_entry(p, len) >= 0) {
585 detected_bytes += len;
586 if (p[len-nl-1] == '\\')
587 /* This line continues. */
590 /* We've got plenty of correct lines
591 * to assume that this file is a mtree
593 if (++entry_cnt >= MAX_BID_ENTRY)
598 } else if (strncmp(p, "/set", 4) == 0) {
599 if (bid_keyword_list(p+4, len-4, 0) <= 0)
601 /* This line continues. */
602 if (p[len-nl-1] == '\\')
604 } else if (strncmp(p, "/unset", 6) == 0) {
605 if (bid_keyword_list(p+6, len-6, 1) <= 0)
607 /* This line continues. */
608 if (p[len-nl-1] == '\\')
613 /* Test next line. */
617 if (entry_cnt >= MAX_BID_ENTRY || (entry_cnt > 0 && len == 0))
624 * The extended mtree format permits multiple lines specifying
625 * attributes for each file. For those entries, only the last line
626 * is actually used. Practically speaking, that means we have
627 * to read the entire mtree file into memory up front.
629 * The parsing is done in two steps. First, it is decided if a line
630 * changes the global defaults and if it is, processed accordingly.
631 * Otherwise, the options of the line are merged with the current
635 add_option(struct archive_read *a, struct mtree_option **global,
636 const char *value, size_t len)
638 struct mtree_option *opt;
640 if ((opt = malloc(sizeof(*opt))) == NULL) {
641 archive_set_error(&a->archive, errno, "Can't allocate memory");
642 return (ARCHIVE_FATAL);
644 if ((opt->value = malloc(len + 1)) == NULL) {
646 archive_set_error(&a->archive, errno, "Can't allocate memory");
647 return (ARCHIVE_FATAL);
649 memcpy(opt->value, value, len);
650 opt->value[len] = '\0';
657 remove_option(struct mtree_option **global, const char *value, size_t len)
659 struct mtree_option *iter, *last;
662 for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
663 if (strncmp(iter->value, value, len) == 0 &&
664 (iter->value[len] == '\0' ||
665 iter->value[len] == '='))
671 *global = iter->next;
673 last->next = iter->next;
680 process_global_set(struct archive_read *a,
681 struct mtree_option **global, const char *line)
683 const char *next, *eq;
689 next = line + strspn(line, " \t\r\n");
693 next = line + strcspn(line, " \t\r\n");
694 eq = strchr(line, '=');
700 remove_option(global, line, len);
701 r = add_option(a, global, line, next - line);
709 process_global_unset(struct archive_read *a,
710 struct mtree_option **global, const char *line)
716 if (strchr(line, '=') != NULL) {
717 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
718 "/unset shall not contain `='");
719 return ARCHIVE_FATAL;
723 next = line + strspn(line, " \t\r\n");
727 len = strcspn(line, " \t\r\n");
729 if (len == 3 && strncmp(line, "all", 3) == 0) {
730 free_options(*global);
733 remove_option(global, line, len);
741 process_add_entry(struct archive_read *a, struct mtree *mtree,
742 struct mtree_option **global, const char *line,
743 struct mtree_entry **last_entry)
745 struct mtree_entry *entry;
746 struct mtree_option *iter;
747 const char *next, *eq;
751 if ((entry = malloc(sizeof(*entry))) == NULL) {
752 archive_set_error(&a->archive, errno, "Can't allocate memory");
753 return (ARCHIVE_FATAL);
756 entry->options = NULL;
761 /* Add this entry to list. */
762 if (*last_entry == NULL)
763 mtree->entries = entry;
765 (*last_entry)->next = entry;
768 len = strcspn(line, " \t\r\n");
769 if ((entry->name = malloc(len + 1)) == NULL) {
770 archive_set_error(&a->archive, errno, "Can't allocate memory");
771 return (ARCHIVE_FATAL);
774 memcpy(entry->name, line, len);
775 entry->name[len] = '\0';
776 parse_escapes(entry->name, entry);
779 for (iter = *global; iter != NULL; iter = iter->next) {
780 r = add_option(a, &entry->options, iter->value,
781 strlen(iter->value));
787 next = line + strspn(line, " \t\r\n");
791 next = line + strcspn(line, " \t\r\n");
792 eq = strchr(line, '=');
793 if (eq == NULL || eq > next)
798 remove_option(&entry->options, line, len);
799 r = add_option(a, &entry->options, line, next - line);
807 read_mtree(struct archive_read *a, struct mtree *mtree)
812 struct mtree_option *global;
813 struct mtree_entry *last_entry;
816 mtree->archive_format = ARCHIVE_FORMAT_MTREE;
817 mtree->archive_format_name = "mtree";
822 for (counter = 1; ; ++counter) {
823 len = readline(a, mtree, &p, 65536);
825 mtree->this_entry = mtree->entries;
826 free_options(global);
830 free_options(global);
833 /* Leading whitespace is never significant, ignore it. */
834 while (*p == ' ' || *p == '\t') {
838 /* Skip content lines and blank lines. */
841 if (*p == '\r' || *p == '\n' || *p == '\0')
844 r = process_add_entry(a, mtree, &global, p,
846 } else if (strncmp(p, "/set", 4) == 0) {
847 if (p[4] != ' ' && p[4] != '\t')
849 r = process_global_set(a, &global, p);
850 } else if (strncmp(p, "/unset", 6) == 0) {
851 if (p[6] != ' ' && p[6] != '\t')
853 r = process_global_unset(a, &global, p);
857 if (r != ARCHIVE_OK) {
858 free_options(global);
863 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
864 "Can't parse line %ju", counter);
865 free_options(global);
866 return (ARCHIVE_FATAL);
870 * Read in the entire mtree file into memory on the first request.
871 * Then use the next unused file to satisfy each header request.
874 read_header(struct archive_read *a, struct archive_entry *entry)
880 mtree = (struct mtree *)(a->format->data);
882 if (mtree->fd >= 0) {
887 if (mtree->entries == NULL) {
888 mtree->resolver = archive_entry_linkresolver_new();
889 if (mtree->resolver == NULL)
890 return ARCHIVE_FATAL;
891 archive_entry_linkresolver_set_strategy(mtree->resolver,
892 ARCHIVE_FORMAT_MTREE);
893 r = read_mtree(a, mtree);
898 a->archive.archive_format = mtree->archive_format;
899 a->archive.archive_format_name = mtree->archive_format_name;
902 if (mtree->this_entry == NULL)
903 return (ARCHIVE_EOF);
904 if (strcmp(mtree->this_entry->name, "..") == 0) {
905 mtree->this_entry->used = 1;
906 if (archive_strlen(&mtree->current_dir) > 0) {
907 /* Roll back current path. */
908 p = mtree->current_dir.s
909 + mtree->current_dir.length - 1;
910 while (p >= mtree->current_dir.s && *p != '/')
912 if (p >= mtree->current_dir.s)
914 mtree->current_dir.length
915 = p - mtree->current_dir.s + 1;
918 if (!mtree->this_entry->used) {
920 r = parse_file(a, entry, mtree, mtree->this_entry, &use_next);
924 mtree->this_entry = mtree->this_entry->next;
929 * A single file can have multiple lines contribute specifications.
930 * Parse as many lines as necessary, then pull additional information
931 * from a backing file on disk as necessary.
934 parse_file(struct archive_read *a, struct archive_entry *entry,
935 struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
938 struct stat st_storage, *st;
939 struct mtree_entry *mp;
940 struct archive_entry *sparse_entry;
941 int r = ARCHIVE_OK, r1, parsed_kws;
945 /* Initialize reasonable defaults. */
946 archive_entry_set_filetype(entry, AE_IFREG);
947 archive_entry_set_size(entry, 0);
948 archive_string_empty(&mtree->contents_name);
950 /* Parse options from this line. */
952 r = parse_line(a, entry, mtree, mentry, &parsed_kws);
955 archive_entry_copy_pathname(entry, mentry->name);
957 * "Full" entries are allowed to have multiple lines
958 * and those lines aren't required to be adjacent. We
959 * don't support multiple lines for "relative" entries
960 * nor do we make any attempt to merge data from
961 * separate "relative" and "full" entries. (Merging
962 * "relative" and "full" entries would require dealing
963 * with pathname canonicalization, which is a very
966 for (mp = mentry->next; mp != NULL; mp = mp->next) {
967 if (mp->full && !mp->used
968 && strcmp(mentry->name, mp->name) == 0) {
969 /* Later lines override earlier ones. */
971 r1 = parse_line(a, entry, mtree, mp,
979 * Relative entries require us to construct
980 * the full path and possibly update the
983 size_t n = archive_strlen(&mtree->current_dir);
985 archive_strcat(&mtree->current_dir, "/");
986 archive_strcat(&mtree->current_dir, mentry->name);
987 archive_entry_copy_pathname(entry, mtree->current_dir.s);
988 if (archive_entry_filetype(entry) != AE_IFDIR)
989 mtree->current_dir.length = n;
993 * Try to open and stat the file to get the real size
994 * and other file info. It would be nice to avoid
995 * this here so that getting a listing of an mtree
996 * wouldn't require opening every referenced contents
997 * file. But then we wouldn't know the actual
998 * contents size, so I don't see a really viable way
999 * around this. (Also, we may want to someday pull
1000 * other unspecified info from the contents file on
1004 if (archive_strlen(&mtree->contents_name) > 0)
1005 path = mtree->contents_name.s;
1007 path = archive_entry_pathname(entry);
1009 if (archive_entry_filetype(entry) == AE_IFREG ||
1010 archive_entry_filetype(entry) == AE_IFDIR) {
1011 mtree->fd = open(path, O_RDONLY | O_BINARY);
1012 if (mtree->fd == -1 &&
1014 archive_strlen(&mtree->contents_name) > 0)) {
1015 archive_set_error(&a->archive, errno,
1016 "Can't open %s", path);
1022 if (mtree->fd >= 0) {
1023 if (fstat(mtree->fd, st) == -1) {
1024 archive_set_error(&a->archive, errno,
1025 "Could not fstat %s", path);
1027 /* If we can't stat it, don't keep it open. */
1032 } else if (lstat(path, st) == -1) {
1037 * Check for a mismatch between the type in the specification and
1038 * the type of the contents object on disk.
1042 ((st->st_mode & S_IFMT) == S_IFREG &&
1043 archive_entry_filetype(entry) == AE_IFREG)
1045 || ((st->st_mode & S_IFMT) == S_IFLNK &&
1046 archive_entry_filetype(entry) == AE_IFLNK)
1049 || ((st->st_mode & S_IFSOCK) == S_IFSOCK &&
1050 archive_entry_filetype(entry) == AE_IFSOCK)
1053 || ((st->st_mode & S_IFMT) == S_IFCHR &&
1054 archive_entry_filetype(entry) == AE_IFCHR)
1057 || ((st->st_mode & S_IFMT) == S_IFBLK &&
1058 archive_entry_filetype(entry) == AE_IFBLK)
1060 || ((st->st_mode & S_IFMT) == S_IFDIR &&
1061 archive_entry_filetype(entry) == AE_IFDIR)
1063 || ((st->st_mode & S_IFMT) == S_IFIFO &&
1064 archive_entry_filetype(entry) == AE_IFIFO)
1069 /* Types don't match; bail out gracefully. */
1073 if (parsed_kws & MTREE_HAS_OPTIONAL) {
1074 /* It's not an error for an optional entry
1075 to not match disk. */
1077 } else if (r == ARCHIVE_OK) {
1078 archive_set_error(&a->archive,
1080 "mtree specification has different type for %s",
1081 archive_entry_pathname(entry));
1089 * If there is a contents file on disk, pick some of the metadata
1090 * from that file. For most of these, we only set it from the contents
1091 * if it wasn't already parsed from the specification.
1094 if ((parsed_kws & MTREE_HAS_DEVICE) == 0 &&
1095 (archive_entry_filetype(entry) == AE_IFCHR ||
1096 archive_entry_filetype(entry) == AE_IFBLK))
1097 archive_entry_set_rdev(entry, st->st_rdev);
1098 if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME)) == 0)
1099 archive_entry_set_gid(entry, st->st_gid);
1100 if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME)) == 0)
1101 archive_entry_set_uid(entry, st->st_uid);
1102 if ((parsed_kws & MTREE_HAS_MTIME) == 0) {
1103 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1104 archive_entry_set_mtime(entry, st->st_mtime,
1105 st->st_mtimespec.tv_nsec);
1106 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1107 archive_entry_set_mtime(entry, st->st_mtime,
1108 st->st_mtim.tv_nsec);
1109 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1110 archive_entry_set_mtime(entry, st->st_mtime,
1112 #elif HAVE_STRUCT_STAT_ST_UMTIME
1113 archive_entry_set_mtime(entry, st->st_mtime,
1114 st->st_umtime*1000);
1115 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
1116 archive_entry_set_mtime(entry, st->st_mtime,
1117 st->st_mtime_usec*1000);
1119 archive_entry_set_mtime(entry, st->st_mtime, 0);
1122 if ((parsed_kws & MTREE_HAS_NLINK) == 0)
1123 archive_entry_set_nlink(entry, st->st_nlink);
1124 if ((parsed_kws & MTREE_HAS_PERM) == 0)
1125 archive_entry_set_perm(entry, st->st_mode);
1126 if ((parsed_kws & MTREE_HAS_SIZE) == 0)
1127 archive_entry_set_size(entry, st->st_size);
1128 archive_entry_set_ino(entry, st->st_ino);
1129 archive_entry_set_dev(entry, st->st_dev);
1131 archive_entry_linkify(mtree->resolver, &entry, &sparse_entry);
1132 } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
1134 * Couldn't open the entry, stat it or the on-disk type
1135 * didn't match. If this entry is optional, just ignore it
1136 * and read the next header entry.
1142 mtree->cur_size = archive_entry_size(entry);
1149 * Each line contains a sequence of keywords.
1152 parse_line(struct archive_read *a, struct archive_entry *entry,
1153 struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
1155 struct mtree_option *iter;
1156 int r = ARCHIVE_OK, r1;
1158 for (iter = mp->options; iter != NULL; iter = iter->next) {
1159 r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
1163 if (r == ARCHIVE_OK && (*parsed_kws & MTREE_HAS_TYPE) == 0) {
1164 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1165 "Missing type keyword in mtree specification");
1166 return (ARCHIVE_WARN);
1172 * Device entries have one of the following forms:
1174 * format,major,minor[,subdevice]
1176 * Just use major and minor, no translation etc is done
1180 parse_device(struct archive *a, struct archive_entry *entry, char *val)
1182 char *comma1, *comma2;
1184 comma1 = strchr(val, ',');
1185 if (comma1 == NULL) {
1186 archive_entry_set_dev(entry, mtree_atol10(&val));
1187 return (ARCHIVE_OK);
1190 comma2 = strchr(comma1, ',');
1191 if (comma2 == NULL) {
1192 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1193 "Malformed device attribute");
1194 return (ARCHIVE_WARN);
1197 archive_entry_set_rdevmajor(entry, mtree_atol(&comma1));
1198 archive_entry_set_rdevminor(entry, mtree_atol(&comma2));
1199 return (ARCHIVE_OK);
1203 * Parse a single keyword and its value.
1206 parse_keyword(struct archive_read *a, struct mtree *mtree,
1207 struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws)
1214 return (ARCHIVE_OK);
1216 if (strcmp(key, "optional") == 0) {
1217 *parsed_kws |= MTREE_HAS_OPTIONAL;
1218 return (ARCHIVE_OK);
1220 if (strcmp(key, "ignore") == 0) {
1222 * The mtree processing is not recursive, so
1223 * recursion will only happen for explicitly listed
1226 return (ARCHIVE_OK);
1229 val = strchr(key, '=');
1231 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1232 "Malformed attribute \"%s\" (%d)", key, key[0]);
1233 return (ARCHIVE_WARN);
1241 if (strcmp(key, "content") == 0
1242 || strcmp(key, "contents") == 0) {
1243 parse_escapes(val, NULL);
1244 archive_strcpy(&mtree->contents_name, val);
1247 if (strcmp(key, "cksum") == 0)
1250 if (strcmp(key, "device") == 0) {
1251 *parsed_kws |= MTREE_HAS_DEVICE;
1252 return parse_device(&a->archive, entry, val);
1255 if (strcmp(key, "flags") == 0) {
1256 *parsed_kws |= MTREE_HAS_FFLAGS;
1257 archive_entry_copy_fflags_text(entry, val);
1261 if (strcmp(key, "gid") == 0) {
1262 *parsed_kws |= MTREE_HAS_GID;
1263 archive_entry_set_gid(entry, mtree_atol10(&val));
1266 if (strcmp(key, "gname") == 0) {
1267 *parsed_kws |= MTREE_HAS_GNAME;
1268 archive_entry_copy_gname(entry, val);
1272 if (strcmp(key, "link") == 0) {
1273 archive_entry_copy_symlink(entry, val);
1277 if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
1279 if (strcmp(key, "mode") == 0) {
1280 if (val[0] >= '0' && val[0] <= '9') {
1281 *parsed_kws |= MTREE_HAS_PERM;
1282 archive_entry_set_perm(entry,
1285 archive_set_error(&a->archive,
1286 ARCHIVE_ERRNO_FILE_FORMAT,
1287 "Symbolic mode \"%s\" unsupported", val);
1288 return ARCHIVE_WARN;
1293 if (strcmp(key, "nlink") == 0) {
1294 *parsed_kws |= MTREE_HAS_NLINK;
1295 archive_entry_set_nlink(entry, mtree_atol10(&val));
1299 if (strcmp(key, "rmd160") == 0 ||
1300 strcmp(key, "rmd160digest") == 0)
1303 if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
1305 if (strcmp(key, "sha256") == 0 ||
1306 strcmp(key, "sha256digest") == 0)
1308 if (strcmp(key, "sha384") == 0 ||
1309 strcmp(key, "sha384digest") == 0)
1311 if (strcmp(key, "sha512") == 0 ||
1312 strcmp(key, "sha512digest") == 0)
1314 if (strcmp(key, "size") == 0) {
1315 archive_entry_set_size(entry, mtree_atol10(&val));
1319 if (strcmp(key, "tags") == 0) {
1321 * Comma delimited list of tags.
1322 * Ignore the tags for now, but the interface
1323 * should be extended to allow inclusion/exclusion.
1327 if (strcmp(key, "time") == 0) {
1329 int64_t my_time_t_max = get_time_t_max();
1330 int64_t my_time_t_min = get_time_t_min();
1333 *parsed_kws |= MTREE_HAS_MTIME;
1334 m = mtree_atol10(&val);
1335 /* Replicate an old mtree bug:
1336 * 123456789.1 represents 123456789
1337 * seconds and 1 nanosecond. */
1340 ns = (long)mtree_atol10(&val);
1343 if (m > my_time_t_max)
1345 else if (m < my_time_t_min)
1347 archive_entry_set_mtime(entry, (time_t)m, ns);
1350 if (strcmp(key, "type") == 0) {
1353 if (strcmp(val, "block") == 0) {
1354 archive_entry_set_filetype(entry, AE_IFBLK);
1358 if (strcmp(val, "char") == 0) {
1359 archive_entry_set_filetype(entry, AE_IFCHR);
1363 if (strcmp(val, "dir") == 0) {
1364 archive_entry_set_filetype(entry, AE_IFDIR);
1368 if (strcmp(val, "fifo") == 0) {
1369 archive_entry_set_filetype(entry, AE_IFIFO);
1372 if (strcmp(val, "file") == 0) {
1373 archive_entry_set_filetype(entry, AE_IFREG);
1377 if (strcmp(val, "link") == 0) {
1378 archive_entry_set_filetype(entry, AE_IFLNK);
1382 archive_set_error(&a->archive,
1383 ARCHIVE_ERRNO_FILE_FORMAT,
1384 "Unrecognized file type \"%s\"; assuming \"file\"", val);
1385 archive_entry_set_filetype(entry, AE_IFREG);
1386 return (ARCHIVE_WARN);
1388 *parsed_kws |= MTREE_HAS_TYPE;
1392 if (strcmp(key, "uid") == 0) {
1393 *parsed_kws |= MTREE_HAS_UID;
1394 archive_entry_set_uid(entry, mtree_atol10(&val));
1397 if (strcmp(key, "uname") == 0) {
1398 *parsed_kws |= MTREE_HAS_UNAME;
1399 archive_entry_copy_uname(entry, val);
1403 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1404 "Unrecognized key %s=%s", key, val);
1405 return (ARCHIVE_WARN);
1407 return (ARCHIVE_OK);
1411 read_data(struct archive_read *a, const void **buff, size_t *size, int64_t *offset)
1413 size_t bytes_to_read;
1415 struct mtree *mtree;
1417 mtree = (struct mtree *)(a->format->data);
1418 if (mtree->fd < 0) {
1422 return (ARCHIVE_EOF);
1424 if (mtree->buff == NULL) {
1425 mtree->buffsize = 64 * 1024;
1426 mtree->buff = malloc(mtree->buffsize);
1427 if (mtree->buff == NULL) {
1428 archive_set_error(&a->archive, ENOMEM,
1429 "Can't allocate memory");
1430 return (ARCHIVE_FATAL);
1434 *buff = mtree->buff;
1435 *offset = mtree->offset;
1436 if ((int64_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1437 bytes_to_read = mtree->cur_size - mtree->offset;
1439 bytes_to_read = mtree->buffsize;
1440 bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1441 if (bytes_read < 0) {
1442 archive_set_error(&a->archive, errno, "Can't read");
1443 return (ARCHIVE_WARN);
1445 if (bytes_read == 0) {
1447 return (ARCHIVE_EOF);
1449 mtree->offset += bytes_read;
1451 return (ARCHIVE_OK);
1454 /* Skip does nothing except possibly close the contents file. */
1456 skip(struct archive_read *a)
1458 struct mtree *mtree;
1460 mtree = (struct mtree *)(a->format->data);
1461 if (mtree->fd >= 0) {
1465 return (ARCHIVE_OK);
1469 * Since parsing backslash sequences always makes strings shorter,
1470 * we can always do this conversion in-place.
1473 parse_escapes(char *src, struct mtree_entry *mentry)
1478 if (mentry != NULL && strcmp(src, ".") == 0)
1481 while (*src != '\0') {
1483 if (c == '/' && mentry != NULL)
1488 if (src[1] < '0' || src[1] > '7') {
1497 if (src[1] >= '0' && src[1] <= '7' &&
1498 src[2] >= '0' && src[2] <= '7') {
1499 c = (src[0] - '0') << 6;
1500 c |= (src[1] - '0') << 3;
1501 c |= (src[2] - '0');
1545 * Note that this implementation does not (and should not!) obey
1546 * locale settings; you cannot simply substitute strtol here, since
1547 * it does obey locale.
1550 mtree_atol8(char **p)
1552 int64_t l, limit, last_digit_limit;
1556 limit = INT64_MAX / base;
1557 last_digit_limit = INT64_MAX % base;
1561 while (digit >= 0 && digit < base) {
1562 if (l>limit || (l == limit && digit > last_digit_limit)) {
1563 l = INT64_MAX; /* Truncate on overflow. */
1566 l = (l * base) + digit;
1567 digit = *++(*p) - '0';
1573 * Note that this implementation does not (and should not!) obey
1574 * locale settings; you cannot simply substitute strtol here, since
1575 * it does obey locale.
1578 mtree_atol10(char **p)
1580 int64_t l, limit, last_digit_limit;
1581 int base, digit, sign;
1587 limit = ((uint64_t)(INT64_MAX) + 1) / base;
1588 last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
1592 limit = INT64_MAX / base;
1593 last_digit_limit = INT64_MAX % base;
1598 while (digit >= 0 && digit < base) {
1599 if (l > limit || (l == limit && digit > last_digit_limit))
1600 return (sign < 0) ? INT64_MIN : INT64_MAX;
1601 l = (l * base) + digit;
1602 digit = *++(*p) - '0';
1604 return (sign < 0) ? -l : l;
1607 /* Parse a hex digit. */
1611 if (c >= '0' && c <= '9')
1613 else if (c >= 'a' && c <= 'f')
1615 else if (c >= 'A' && c <= 'F')
1622 * Note that this implementation does not (and should not!) obey
1623 * locale settings; you cannot simply substitute strtol here, since
1624 * it does obey locale.
1627 mtree_atol16(char **p)
1629 int64_t l, limit, last_digit_limit;
1630 int base, digit, sign;
1636 limit = ((uint64_t)(INT64_MAX) + 1) / base;
1637 last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
1641 limit = INT64_MAX / base;
1642 last_digit_limit = INT64_MAX % base;
1646 digit = parsehex(**p);
1647 while (digit >= 0 && digit < base) {
1648 if (l > limit || (l == limit && digit > last_digit_limit))
1649 return (sign < 0) ? INT64_MIN : INT64_MAX;
1650 l = (l * base) + digit;
1651 digit = parsehex(*++(*p));
1653 return (sign < 0) ? -l : l;
1657 mtree_atol(char **p)
1660 return mtree_atol10(p);
1661 if ((*p)[1] == 'x' || (*p)[1] == 'X') {
1663 return mtree_atol16(p);
1665 return mtree_atol8(p);
1669 * Returns length of line (including trailing newline)
1670 * or negative on error. 'start' argument is updated to
1671 * point to first character of line.
1674 readline(struct archive_read *a, struct mtree *mtree, char **start, ssize_t limit)
1677 ssize_t total_size = 0;
1678 ssize_t find_off = 0;
1684 /* Accumulate line in a line buffer. */
1686 /* Read some more. */
1687 t = __archive_read_ahead(a, 1, &bytes_read);
1691 return (ARCHIVE_FATAL);
1692 s = t; /* Start of line? */
1693 p = memchr(t, '\n', bytes_read);
1694 /* If we found '\n', trim the read. */
1696 bytes_read = 1 + ((const char *)p) - s;
1698 if (total_size + bytes_read + 1 > limit) {
1699 archive_set_error(&a->archive,
1700 ARCHIVE_ERRNO_FILE_FORMAT,
1702 return (ARCHIVE_FATAL);
1704 if (archive_string_ensure(&mtree->line,
1705 total_size + bytes_read + 1) == NULL) {
1706 archive_set_error(&a->archive, ENOMEM,
1707 "Can't allocate working buffer");
1708 return (ARCHIVE_FATAL);
1710 memcpy(mtree->line.s + total_size, t, bytes_read);
1711 __archive_read_consume(a, bytes_read);
1712 total_size += bytes_read;
1713 /* Null terminate. */
1714 mtree->line.s[total_size] = '\0';
1715 /* If we found an unescaped '\n', clean up and return. */
1716 for (u = mtree->line.s + find_off; *u; ++u) {
1718 *start = mtree->line.s;
1724 *start = mtree->line.s;
1735 total_size - (u - mtree->line.s) + 1);
1743 find_off = u - mtree->line.s;