2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2008 Joerg Sonnenberger
4 * Copyright (c) 2011-2012 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: head/lib/libarchive/archive_read_support_format_mtree.c 201165 2009-12-29 05:52:13Z kientzle $");
31 #ifdef HAVE_SYS_STAT_H
41 /* #include <stdint.h> */ /* See archive_platform.h */
53 #include "archive_entry.h"
54 #include "archive_entry_private.h"
55 #include "archive_private.h"
56 #include "archive_rb.h"
57 #include "archive_read_private.h"
58 #include "archive_string.h"
59 #include "archive_pack_dev.h"
68 #define MTREE_HAS_DEVICE 0x0001
69 #define MTREE_HAS_FFLAGS 0x0002
70 #define MTREE_HAS_GID 0x0004
71 #define MTREE_HAS_GNAME 0x0008
72 #define MTREE_HAS_MTIME 0x0010
73 #define MTREE_HAS_NLINK 0x0020
74 #define MTREE_HAS_PERM 0x0040
75 #define MTREE_HAS_SIZE 0x0080
76 #define MTREE_HAS_TYPE 0x0100
77 #define MTREE_HAS_UID 0x0200
78 #define MTREE_HAS_UNAME 0x0400
80 #define MTREE_HAS_OPTIONAL 0x0800
81 #define MTREE_HAS_NOCHANGE 0x1000 /* FreeBSD specific */
83 #define MAX_LINE_LEN (1024 * 1024)
86 struct mtree_option *next;
91 struct archive_rb_node rbnode;
92 struct mtree_entry *next_dup;
93 struct mtree_entry *next;
94 struct mtree_option *options;
101 struct archive_string line;
107 const char *archive_format_name;
108 struct mtree_entry *entries;
109 struct mtree_entry *this_entry;
110 struct archive_rb_tree entry_rbtree;
111 struct archive_string current_dir;
112 struct archive_string contents_name;
114 struct archive_entry_linkresolver *resolver;
115 struct archive_rb_tree rbtree;
121 static int bid_keycmp(const char *, const char *, ssize_t);
122 static int cleanup(struct archive_read *);
123 static int detect_form(struct archive_read *, int *);
124 static int mtree_bid(struct archive_read *, int);
125 static int parse_file(struct archive_read *, struct archive_entry *,
126 struct mtree *, struct mtree_entry *, int *);
127 static void parse_escapes(char *, struct mtree_entry *);
128 static int parse_line(struct archive_read *, struct archive_entry *,
129 struct mtree *, struct mtree_entry *, int *);
130 static int parse_keyword(struct archive_read *, struct mtree *,
131 struct archive_entry *, struct mtree_option *, int *);
132 static int read_data(struct archive_read *a,
133 const void **buff, size_t *size, int64_t *offset);
134 static ssize_t readline(struct archive_read *, struct mtree *, char **, ssize_t);
135 static int skip(struct archive_read *a);
136 static int read_header(struct archive_read *,
137 struct archive_entry *);
138 static int64_t mtree_atol(char **, int base);
140 static size_t mtree_strnlen(const char *, size_t);
144 * There's no standard for TIME_T_MAX/TIME_T_MIN. So we compute them
145 * here. TODO: Move this to configure time, but be careful
146 * about cross-compile environments.
151 #if defined(TIME_T_MAX)
154 /* ISO C allows time_t to be a floating-point type,
155 but POSIX requires an integer type. The following
156 should work on any system that follows the POSIX
158 if (((time_t)0) < ((time_t)-1)) {
159 /* Time_t is unsigned */
162 /* Time_t is signed. */
163 /* Assume it's the same as int64_t or int32_t */
164 if (sizeof(time_t) == sizeof(int64_t)) {
165 return (time_t)INT64_MAX;
167 return (time_t)INT32_MAX;
176 #if defined(TIME_T_MIN)
179 if (((time_t)0) < ((time_t)-1)) {
180 /* Time_t is unsigned */
183 /* Time_t is signed. */
184 if (sizeof(time_t) == sizeof(int64_t)) {
185 return (time_t)INT64_MIN;
187 return (time_t)INT32_MIN;
194 #define mtree_strnlen(a,b) strnlen(a,b)
197 mtree_strnlen(const char *p, size_t maxlen)
201 for (i = 0; i <= maxlen; i++) {
206 return (-1);/* invalid */
212 archive_read_format_mtree_options(struct archive_read *a,
213 const char *key, const char *val)
217 mtree = (struct mtree *)(a->format->data);
218 if (strcmp(key, "checkfs") == 0) {
219 /* Allows to read information missing from the mtree from the file system */
220 if (val == NULL || val[0] == 0) {
228 /* Note: The "warn" return is just to inform the options
229 * supervisor that we didn't handle it. It will generate
230 * a suitable error if no one used this option. */
231 return (ARCHIVE_WARN);
235 free_options(struct mtree_option *head)
237 struct mtree_option *next;
239 for (; head != NULL; head = next) {
247 mtree_cmp_node(const struct archive_rb_node *n1,
248 const struct archive_rb_node *n2)
250 const struct mtree_entry *e1 = (const struct mtree_entry *)n1;
251 const struct mtree_entry *e2 = (const struct mtree_entry *)n2;
253 return (strcmp(e1->name, e2->name));
257 mtree_cmp_key(const struct archive_rb_node *n, const void *key)
259 const struct mtree_entry *e = (const struct mtree_entry *)n;
261 return (strcmp(e->name, key));
265 archive_read_support_format_mtree(struct archive *_a)
267 static const struct archive_rb_tree_ops rb_ops = {
268 mtree_cmp_node, mtree_cmp_key,
270 struct archive_read *a = (struct archive_read *)_a;
274 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
275 ARCHIVE_STATE_NEW, "archive_read_support_format_mtree");
277 mtree = (struct mtree *)calloc(1, sizeof(*mtree));
279 archive_set_error(&a->archive, ENOMEM,
280 "Can't allocate mtree data");
281 return (ARCHIVE_FATAL);
286 __archive_rb_tree_init(&mtree->rbtree, &rb_ops);
288 r = __archive_read_register_format(a, mtree, "mtree",
289 mtree_bid, archive_read_format_mtree_options, read_header, read_data, skip, NULL, cleanup, NULL, NULL);
297 cleanup(struct archive_read *a)
300 struct mtree_entry *p, *q;
302 mtree = (struct mtree *)(a->format->data);
308 free_options(p->options);
312 archive_string_free(&mtree->line);
313 archive_string_free(&mtree->current_dir);
314 archive_string_free(&mtree->contents_name);
315 archive_entry_linkresolver_free(mtree->resolver);
319 (a->format->data) = NULL;
324 get_line_size(const char *b, ssize_t avail, ssize_t *nlsize)
329 while (len < avail) {
331 case '\0':/* Non-ascii character or control character. */
336 if (avail-len > 1 && b[1] == '\n') {
358 * <---------------- ravail --------------------->
359 * <-- diff ------> <--- avail ----------------->
360 * <---- len ----------->
361 * | Previous lines | line being parsed nl extra |
367 next_line(struct archive_read *a,
368 const char **b, ssize_t *avail, ssize_t *ravail, ssize_t *nl)
378 len = get_line_size(*b, *avail, nl);
380 * Read bytes more while it does not reach the end of line.
382 while (*nl == 0 && len == *avail && !quit) {
383 ssize_t diff = *ravail - *avail;
384 size_t nbytes_req = (*ravail+1023) & ~1023U;
388 * Place an arbitrary limit on the line length.
389 * mtree is almost free-form input and without line length limits,
390 * it can consume a lot of memory.
392 if (len >= MAX_LINE_LEN)
395 /* Increase reading bytes if it is not enough to at least
397 if (nbytes_req < (size_t)*ravail + 160)
400 *b = __archive_read_ahead(a, nbytes_req, avail);
402 if (*ravail >= *avail)
404 /* Reading bytes reaches the end of file. */
405 *b = __archive_read_ahead(a, *avail, avail);
411 tested = len;/* Skip some bytes we already determined. */
412 len = get_line_size(*b + len, *avail - len, nl);
420 * Compare characters with a mtree keyword.
421 * Returns the length of a mtree keyword if matched.
422 * Returns 0 if not matched.
425 bid_keycmp(const char *p, const char *key, ssize_t len)
429 while (len > 0 && *p && *key) {
437 return (0);/* Not match */
440 return (0);/* Not match */
442 /* A following character should be specified characters */
443 if (p[0] == '=' || p[0] == ' ' || p[0] == '\t' ||
444 p[0] == '\n' || p[0] == '\r' ||
445 (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r')))
447 return (0);/* Not match */
451 * Test whether the characters 'p' has is mtree keyword.
452 * Returns the length of a detected keyword.
453 * Returns 0 if any keywords were not found.
456 bid_keyword(const char *p, ssize_t len)
458 static const char * const keys_c[] = {
459 "content", "contents", "cksum", NULL
461 static const char * const keys_df[] = {
462 "device", "flags", NULL
464 static const char * const keys_g[] = {
467 static const char * const keys_il[] = {
468 "ignore", "inode", "link", NULL
470 static const char * const keys_m[] = {
471 "md5", "md5digest", "mode", NULL
473 static const char * const keys_no[] = {
474 "nlink", "nochange", "optional", NULL
476 static const char * const keys_r[] = {
477 "resdevice", "rmd160", "rmd160digest", NULL
479 static const char * const keys_s[] = {
480 "sha1", "sha1digest",
481 "sha256", "sha256digest",
482 "sha384", "sha384digest",
483 "sha512", "sha512digest",
486 static const char * const keys_t[] = {
487 "tags", "time", "type", NULL
489 static const char * const keys_u[] = {
492 const char * const *keys;
496 case 'c': keys = keys_c; break;
497 case 'd': case 'f': keys = keys_df; break;
498 case 'g': keys = keys_g; break;
499 case 'i': case 'l': keys = keys_il; break;
500 case 'm': keys = keys_m; break;
501 case 'n': case 'o': keys = keys_no; break;
502 case 'r': keys = keys_r; break;
503 case 's': keys = keys_s; break;
504 case 't': keys = keys_t; break;
505 case 'u': keys = keys_u; break;
506 default: return (0);/* Unknown key */
509 for (i = 0; keys[i] != NULL; i++) {
510 int l = bid_keycmp(p, keys[i], len);
514 return (0);/* Unknown key */
518 * Test whether there is a set of mtree keywords.
519 * Returns the number of keyword.
520 * Returns -1 if we got incorrect sequence.
521 * This function expects a set of "<space characters>keyword=value".
522 * When "unset" is specified, expects a set of "<space characters>keyword".
525 bid_keyword_list(const char *p, ssize_t len, int unset, int last_is_path)
530 while (len > 0 && *p) {
533 /* Test whether there are blank characters in the line. */
534 while (len >0 && (*p == ' ' || *p == '\t')) {
539 if (*p == '\n' || *p == '\r')
541 if (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r'))
543 if (!blank && !last_is_path) /* No blank character. */
545 if (last_is_path && len == 0)
549 l = bid_keycmp(p, "all", len);
553 /* Test whether there is a correct key in the line. */
554 l = bid_keyword(p, len);
556 return (-1);/* Unknown keyword was found. */
566 while (len > 0 && *p != ' ' && *p != '\t') {
571 /* A keyword should have a its value unless
572 * "/unset" operation. */
573 if (!unset && value == 0)
581 bid_entry(const char *p, ssize_t len, ssize_t nl, int *last_is_path)
584 static const unsigned char safe_char[256] = {
585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
587 /* !"$%&'()*+,-./ EXCLUSION:( )(#) */
588 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
589 /* 0123456789:;<>? EXCLUSION:(=) */
590 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
591 /* @ABCDEFGHIJKLMNO */
592 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
593 /* PQRSTUVWXYZ[\]^_ */
594 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
595 /* `abcdefghijklmno */
596 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
597 /* pqrstuvwxyz{|}~ */
598 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
599 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
600 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
601 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
602 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
603 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
604 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
605 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
606 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
610 const char * const pp_end = pp + len;
614 * Skip the path-name which is quoted.
616 for (;pp < pp_end; ++pp) {
617 if (!safe_char[*(const unsigned char *)pp]) {
618 if (*pp != ' ' && *pp != '\t' && *pp != '\r'
627 /* If a path-name was not found at the first, try to check
628 * a mtree format(a.k.a form D) ``NetBSD's mtree -D'' creates,
629 * which places the path-name at the last. */
631 const char *pb = p + len - nl;
635 /* The form D accepts only a single line for an entry. */
637 pb[-1] == '\\' && (pb[-2] == ' ' || pb[-2] == '\t'))
639 if (pb-1 >= p && pb[-1] == '\\')
643 while (p <= --pb && *pb != ' ' && *pb != '\t') {
644 if (!safe_char[*(const unsigned char *)pb])
647 /* The pathname should have a slash in this
652 if (name_len == 0 || slash == 0)
654 /* If '/' is placed at the first in this field, this is not
655 * a valid filename. */
658 ll = len - nl - name_len;
663 return (bid_keyword_list(pp, ll, 0, *last_is_path));
666 #define MAX_BID_ENTRY 3
669 mtree_bid(struct archive_read *a, int best_bid)
671 const char *signature = "#mtree";
674 (void)best_bid; /* UNUSED */
676 /* Now let's look at the actual header and see if it matches. */
677 p = __archive_read_ahead(a, strlen(signature), NULL);
681 if (memcmp(p, signature, strlen(signature)) == 0)
682 return (8 * (int)strlen(signature));
685 * There is not a mtree signature. Let's try to detect mtree format.
687 return (detect_form(a, NULL));
691 detect_form(struct archive_read *a, int *is_form_d)
694 ssize_t avail, ravail;
696 int entry_cnt = 0, multiline = 0;
697 int form_D = 0;/* The archive is generated by `NetBSD mtree -D'
698 * (In this source we call it `form D') . */
700 if (is_form_d != NULL)
702 p = __archive_read_ahead(a, 1, &avail);
707 len = next_line(a, &p, &avail, &ravail, &nl);
708 /* The terminal character of the line should be
709 * a new line character, '\r\n' or '\n'. */
710 if (len <= 0 || nl == 0)
713 /* Leading whitespace is never significant,
715 while (len > 0 && (*p == ' ' || *p == '\t')) {
720 /* Skip comment or empty line. */
721 if (p[0] == '#' || p[0] == '\n' || p[0] == '\r') {
727 /* A continuance line; the terminal
728 * character of previous line was '\' character. */
729 if (bid_keyword_list(p, len, 0, 0) <= 0)
731 if (p[len-nl-1] != '\\') {
732 if (multiline == 1 &&
733 ++entry_cnt >= MAX_BID_ENTRY)
742 int last_is_path, keywords;
744 keywords = bid_entry(p, len, nl, &last_is_path);
749 else if (keywords > 0)
750 /* This line is not `form D'. */
752 } else if (form_D == 1) {
753 if (!last_is_path && keywords > 0)
754 /* This this is not `form D'
755 * and We cannot accept mixed
759 if (!last_is_path && p[len-nl-1] == '\\')
760 /* This line continues. */
763 /* We've got plenty of correct lines
764 * to assume that this file is a mtree
766 if (++entry_cnt >= MAX_BID_ENTRY)
771 } else if (len > 4 && strncmp(p, "/set", 4) == 0) {
772 if (bid_keyword_list(p+4, len-4, 0, 0) <= 0)
774 /* This line continues. */
775 if (p[len-nl-1] == '\\')
777 } else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
778 if (bid_keyword_list(p+6, len-6, 1, 0) <= 0)
780 /* This line continues. */
781 if (p[len-nl-1] == '\\')
786 /* Test next line. */
790 if (entry_cnt >= MAX_BID_ENTRY || (entry_cnt > 0 && len == 0)) {
791 if (is_form_d != NULL) {
802 * The extended mtree format permits multiple lines specifying
803 * attributes for each file. For those entries, only the last line
804 * is actually used. Practically speaking, that means we have
805 * to read the entire mtree file into memory up front.
807 * The parsing is done in two steps. First, it is decided if a line
808 * changes the global defaults and if it is, processed accordingly.
809 * Otherwise, the options of the line are merged with the current
813 add_option(struct archive_read *a, struct mtree_option **global,
814 const char *value, size_t len)
816 struct mtree_option *opt;
818 if ((opt = malloc(sizeof(*opt))) == NULL) {
819 archive_set_error(&a->archive, errno, "Can't allocate memory");
820 return (ARCHIVE_FATAL);
822 if ((opt->value = malloc(len + 1)) == NULL) {
824 archive_set_error(&a->archive, errno, "Can't allocate memory");
825 return (ARCHIVE_FATAL);
827 memcpy(opt->value, value, len);
828 opt->value[len] = '\0';
835 remove_option(struct mtree_option **global, const char *value, size_t len)
837 struct mtree_option *iter, *last;
840 for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
841 if (strncmp(iter->value, value, len) == 0 &&
842 (iter->value[len] == '\0' ||
843 iter->value[len] == '='))
849 *global = iter->next;
851 last->next = iter->next;
858 process_global_set(struct archive_read *a,
859 struct mtree_option **global, const char *line)
861 const char *next, *eq;
867 next = line + strspn(line, " \t\r\n");
871 next = line + strcspn(line, " \t\r\n");
872 eq = strchr(line, '=');
878 remove_option(global, line, len);
879 r = add_option(a, global, line, next - line);
887 process_global_unset(struct archive_read *a,
888 struct mtree_option **global, const char *line)
894 if (strchr(line, '=') != NULL) {
895 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
896 "/unset shall not contain `='");
897 return ARCHIVE_FATAL;
901 next = line + strspn(line, " \t\r\n");
905 len = strcspn(line, " \t\r\n");
907 if (len == 3 && strncmp(line, "all", 3) == 0) {
908 free_options(*global);
911 remove_option(global, line, len);
919 process_add_entry(struct archive_read *a, struct mtree *mtree,
920 struct mtree_option **global, const char *line, ssize_t line_len,
921 struct mtree_entry **last_entry, int is_form_d)
923 struct mtree_entry *entry;
924 struct mtree_option *iter;
925 const char *next, *eq, *name, *end;
926 size_t name_len, len;
929 if ((entry = malloc(sizeof(*entry))) == NULL) {
930 archive_set_error(&a->archive, errno, "Can't allocate memory");
931 return (ARCHIVE_FATAL);
934 entry->options = NULL;
939 /* Add this entry to list. */
940 if (*last_entry == NULL)
941 mtree->entries = entry;
943 (*last_entry)->next = entry;
947 /* Filename is last item on line. */
948 /* Adjust line_len to trim trailing whitespace */
949 while (line_len > 0) {
950 char last_character = line[line_len - 1];
951 if (last_character == '\r'
952 || last_character == '\n'
953 || last_character == '\t'
954 || last_character == ' ') {
960 /* Name starts after the last whitespace separator */
962 for (i = 0; i < line_len; i++) {
970 name_len = line + line_len - name;
973 /* Filename is first item on line */
974 name_len = strcspn(line, " \t\r\n");
977 end = line + line_len;
979 /* name/name_len is the name within the line. */
980 /* line..end brackets the entire line except the name */
982 if ((entry->name = malloc(name_len + 1)) == NULL) {
983 archive_set_error(&a->archive, errno, "Can't allocate memory");
984 return (ARCHIVE_FATAL);
987 memcpy(entry->name, name, name_len);
988 entry->name[name_len] = '\0';
989 parse_escapes(entry->name, entry);
991 entry->next_dup = NULL;
993 if (!__archive_rb_tree_insert_node(&mtree->rbtree, &entry->rbnode)) {
994 struct mtree_entry *alt;
995 alt = (struct mtree_entry *)__archive_rb_tree_find_node(
996 &mtree->rbtree, entry->name);
997 while (alt->next_dup)
999 alt->next_dup = entry;
1003 for (iter = *global; iter != NULL; iter = iter->next) {
1004 r = add_option(a, &entry->options, iter->value,
1005 strlen(iter->value));
1006 if (r != ARCHIVE_OK)
1011 next = line + strspn(line, " \t\r\n");
1013 return (ARCHIVE_OK);
1015 return (ARCHIVE_OK);
1017 next = line + strcspn(line, " \t\r\n");
1018 eq = strchr(line, '=');
1019 if (eq == NULL || eq > next)
1024 remove_option(&entry->options, line, len);
1025 r = add_option(a, &entry->options, line, next - line);
1026 if (r != ARCHIVE_OK)
1033 read_mtree(struct archive_read *a, struct mtree *mtree)
1038 struct mtree_option *global;
1039 struct mtree_entry *last_entry;
1042 mtree->archive_format = ARCHIVE_FORMAT_MTREE;
1043 mtree->archive_format_name = "mtree";
1048 (void)detect_form(a, &is_form_d);
1050 for (counter = 1; ; ++counter) {
1052 len = readline(a, mtree, &p, 65536);
1054 mtree->this_entry = mtree->entries;
1055 free_options(global);
1056 return (ARCHIVE_OK);
1059 free_options(global);
1062 /* Leading whitespace is never significant, ignore it. */
1063 while (*p == ' ' || *p == '\t') {
1067 /* Skip content lines and blank lines. */
1070 if (*p == '\r' || *p == '\n' || *p == '\0')
1072 /* Non-printable characters are not allowed */
1073 for (s = p;s < p + len - 1; s++) {
1074 if (!isprint((unsigned char)*s)) {
1079 if (r != ARCHIVE_OK)
1082 r = process_add_entry(a, mtree, &global, p, len,
1083 &last_entry, is_form_d);
1084 } else if (len > 4 && strncmp(p, "/set", 4) == 0) {
1085 if (p[4] != ' ' && p[4] != '\t')
1087 r = process_global_set(a, &global, p);
1088 } else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
1089 if (p[6] != ' ' && p[6] != '\t')
1091 r = process_global_unset(a, &global, p);
1095 if (r != ARCHIVE_OK) {
1096 free_options(global);
1101 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1102 "Can't parse line %ju", counter);
1103 free_options(global);
1104 return (ARCHIVE_FATAL);
1108 * Read in the entire mtree file into memory on the first request.
1109 * Then use the next unused file to satisfy each header request.
1112 read_header(struct archive_read *a, struct archive_entry *entry)
1114 struct mtree *mtree;
1118 mtree = (struct mtree *)(a->format->data);
1120 if (mtree->fd >= 0) {
1125 if (mtree->entries == NULL) {
1126 mtree->resolver = archive_entry_linkresolver_new();
1127 if (mtree->resolver == NULL)
1128 return ARCHIVE_FATAL;
1129 archive_entry_linkresolver_set_strategy(mtree->resolver,
1130 ARCHIVE_FORMAT_MTREE);
1131 r = read_mtree(a, mtree);
1132 if (r != ARCHIVE_OK)
1136 a->archive.archive_format = mtree->archive_format;
1137 a->archive.archive_format_name = mtree->archive_format_name;
1140 if (mtree->this_entry == NULL)
1141 return (ARCHIVE_EOF);
1142 if (strcmp(mtree->this_entry->name, "..") == 0) {
1143 mtree->this_entry->used = 1;
1144 if (archive_strlen(&mtree->current_dir) > 0) {
1145 /* Roll back current path. */
1146 p = mtree->current_dir.s
1147 + mtree->current_dir.length - 1;
1148 while (p >= mtree->current_dir.s && *p != '/')
1150 if (p >= mtree->current_dir.s)
1152 mtree->current_dir.length
1153 = p - mtree->current_dir.s + 1;
1156 if (!mtree->this_entry->used) {
1158 r = parse_file(a, entry, mtree, mtree->this_entry,
1163 mtree->this_entry = mtree->this_entry->next;
1168 * A single file can have multiple lines contribute specifications.
1169 * Parse as many lines as necessary, then pull additional information
1170 * from a backing file on disk as necessary.
1173 parse_file(struct archive_read *a, struct archive_entry *entry,
1174 struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
1177 struct stat st_storage, *st;
1178 struct mtree_entry *mp;
1179 struct archive_entry *sparse_entry;
1180 int r = ARCHIVE_OK, r1, parsed_kws;
1184 /* Initialize reasonable defaults. */
1185 archive_entry_set_filetype(entry, AE_IFREG);
1186 archive_entry_set_size(entry, 0);
1187 archive_string_empty(&mtree->contents_name);
1189 /* Parse options from this line. */
1191 r = parse_line(a, entry, mtree, mentry, &parsed_kws);
1194 archive_entry_copy_pathname(entry, mentry->name);
1196 * "Full" entries are allowed to have multiple lines
1197 * and those lines aren't required to be adjacent. We
1198 * don't support multiple lines for "relative" entries
1199 * nor do we make any attempt to merge data from
1200 * separate "relative" and "full" entries. (Merging
1201 * "relative" and "full" entries would require dealing
1202 * with pathname canonicalization, which is a very
1205 mp = (struct mtree_entry *)__archive_rb_tree_find_node(
1206 &mtree->rbtree, mentry->name);
1207 for (; mp; mp = mp->next_dup) {
1208 if (mp->full && !mp->used) {
1209 /* Later lines override earlier ones. */
1211 r1 = parse_line(a, entry, mtree, mp, &parsed_kws);
1218 * Relative entries require us to construct
1219 * the full path and possibly update the
1220 * current directory.
1222 size_t n = archive_strlen(&mtree->current_dir);
1224 archive_strcat(&mtree->current_dir, "/");
1225 archive_strcat(&mtree->current_dir, mentry->name);
1226 archive_entry_copy_pathname(entry, mtree->current_dir.s);
1227 if (archive_entry_filetype(entry) != AE_IFDIR)
1228 mtree->current_dir.length = n;
1231 if (mtree->checkfs) {
1233 * Try to open and stat the file to get the real size
1234 * and other file info. It would be nice to avoid
1235 * this here so that getting a listing of an mtree
1236 * wouldn't require opening every referenced contents
1237 * file. But then we wouldn't know the actual
1238 * contents size, so I don't see a really viable way
1239 * around this. (Also, we may want to someday pull
1240 * other unspecified info from the contents file on
1244 if (archive_strlen(&mtree->contents_name) > 0)
1245 path = mtree->contents_name.s;
1247 path = archive_entry_pathname(entry);
1249 if (archive_entry_filetype(entry) == AE_IFREG ||
1250 archive_entry_filetype(entry) == AE_IFDIR) {
1251 mtree->fd = open(path, O_RDONLY | O_BINARY | O_CLOEXEC);
1252 __archive_ensure_cloexec_flag(mtree->fd);
1253 if (mtree->fd == -1 &&
1255 archive_strlen(&mtree->contents_name) > 0)) {
1256 archive_set_error(&a->archive, errno,
1257 "Can't open %s", path);
1263 if (mtree->fd >= 0) {
1264 if (fstat(mtree->fd, st) == -1) {
1265 archive_set_error(&a->archive, errno,
1266 "Could not fstat %s", path);
1268 /* If we can't stat it, don't keep it open. */
1273 } else if (lstat(path, st) == -1) {
1278 * Check for a mismatch between the type in the specification
1279 * and the type of the contents object on disk.
1282 if (((st->st_mode & S_IFMT) == S_IFREG &&
1283 archive_entry_filetype(entry) == AE_IFREG)
1285 ||((st->st_mode & S_IFMT) == S_IFLNK &&
1286 archive_entry_filetype(entry) == AE_IFLNK)
1289 ||((st->st_mode & S_IFSOCK) == S_IFSOCK &&
1290 archive_entry_filetype(entry) == AE_IFSOCK)
1293 ||((st->st_mode & S_IFMT) == S_IFCHR &&
1294 archive_entry_filetype(entry) == AE_IFCHR)
1297 ||((st->st_mode & S_IFMT) == S_IFBLK &&
1298 archive_entry_filetype(entry) == AE_IFBLK)
1300 ||((st->st_mode & S_IFMT) == S_IFDIR &&
1301 archive_entry_filetype(entry) == AE_IFDIR)
1303 ||((st->st_mode & S_IFMT) == S_IFIFO &&
1304 archive_entry_filetype(entry) == AE_IFIFO)
1309 /* Types don't match; bail out gracefully. */
1313 if (parsed_kws & MTREE_HAS_OPTIONAL) {
1314 /* It's not an error for an optional
1315 * entry to not match disk. */
1317 } else if (r == ARCHIVE_OK) {
1318 archive_set_error(&a->archive,
1320 "mtree specification has different"
1322 archive_entry_pathname(entry));
1330 * If there is a contents file on disk, pick some of the
1331 * metadata from that file. For most of these, we only
1332 * set it from the contents if it wasn't already parsed
1333 * from the specification.
1336 if (((parsed_kws & MTREE_HAS_DEVICE) == 0 ||
1337 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) &&
1338 (archive_entry_filetype(entry) == AE_IFCHR ||
1339 archive_entry_filetype(entry) == AE_IFBLK))
1340 archive_entry_set_rdev(entry, st->st_rdev);
1341 if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME))
1343 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1344 archive_entry_set_gid(entry, st->st_gid);
1345 if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME))
1347 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1348 archive_entry_set_uid(entry, st->st_uid);
1349 if ((parsed_kws & MTREE_HAS_MTIME) == 0 ||
1350 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) {
1351 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1352 archive_entry_set_mtime(entry, st->st_mtime,
1353 st->st_mtimespec.tv_nsec);
1354 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1355 archive_entry_set_mtime(entry, st->st_mtime,
1356 st->st_mtim.tv_nsec);
1357 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1358 archive_entry_set_mtime(entry, st->st_mtime,
1360 #elif HAVE_STRUCT_STAT_ST_UMTIME
1361 archive_entry_set_mtime(entry, st->st_mtime,
1362 st->st_umtime*1000);
1363 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
1364 archive_entry_set_mtime(entry, st->st_mtime,
1365 st->st_mtime_usec*1000);
1367 archive_entry_set_mtime(entry, st->st_mtime, 0);
1370 if ((parsed_kws & MTREE_HAS_NLINK) == 0 ||
1371 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1372 archive_entry_set_nlink(entry, st->st_nlink);
1373 if ((parsed_kws & MTREE_HAS_PERM) == 0 ||
1374 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1375 archive_entry_set_perm(entry, st->st_mode);
1376 if ((parsed_kws & MTREE_HAS_SIZE) == 0 ||
1377 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1378 archive_entry_set_size(entry, st->st_size);
1379 archive_entry_set_ino(entry, st->st_ino);
1380 archive_entry_set_dev(entry, st->st_dev);
1382 archive_entry_linkify(mtree->resolver, &entry,
1384 } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
1386 * Couldn't open the entry, stat it or the on-disk type
1387 * didn't match. If this entry is optional, just
1388 * ignore it and read the next header entry.
1395 mtree->cur_size = archive_entry_size(entry);
1402 * Each line contains a sequence of keywords.
1405 parse_line(struct archive_read *a, struct archive_entry *entry,
1406 struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
1408 struct mtree_option *iter;
1409 int r = ARCHIVE_OK, r1;
1411 for (iter = mp->options; iter != NULL; iter = iter->next) {
1412 r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
1416 if (r == ARCHIVE_OK && (*parsed_kws & MTREE_HAS_TYPE) == 0) {
1417 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1418 "Missing type keyword in mtree specification");
1419 return (ARCHIVE_WARN);
1425 * Device entries have one of the following forms:
1427 * - format,major,minor[,subdevice]
1428 * When parsing succeeded, `pdev' will contain the appropriate dev_t value.
1431 /* strsep() is not in C90, but strcspn() is. */
1432 /* Taken from http://unixpapa.com/incnote/string.html */
1434 la_strsep(char **sp, const char *sep)
1437 if (sp == NULL || *sp == NULL || **sp == '\0')
1440 p = s + strcspn(s, sep);
1448 parse_device(dev_t *pdev, struct archive *a, char *val)
1450 #define MAX_PACK_ARGS 3
1451 unsigned long numbers[MAX_PACK_ARGS];
1456 const char *error = NULL;
1458 memset(pdev, 0, sizeof(*pdev));
1459 if ((dev = strchr(val, ',')) != NULL) {
1461 * Device's major/minor are given in a specified format.
1462 * Decode and pack it accordingly.
1465 if ((pack = pack_find(val)) == NULL) {
1466 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1467 "Unknown format `%s'", val);
1468 return ARCHIVE_WARN;
1471 while ((p = la_strsep(&dev, ",")) != NULL) {
1473 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1475 return ARCHIVE_WARN;
1477 if (argc >= MAX_PACK_ARGS) {
1478 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1479 "Too many arguments");
1480 return ARCHIVE_WARN;
1482 numbers[argc++] = (unsigned long)mtree_atol(&p, 0);
1485 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1486 "Not enough arguments");
1487 return ARCHIVE_WARN;
1489 result = (*pack)(argc, numbers, &error);
1490 if (error != NULL) {
1491 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1493 return ARCHIVE_WARN;
1496 /* file system raw value. */
1497 result = (dev_t)mtree_atol(&val, 0);
1501 #undef MAX_PACK_ARGS
1505 parse_hex_nibble(char c)
1507 if (c >= '0' && c <= '9')
1509 if (c >= 'a' && c <= 'f')
1510 return 10 + c - 'a';
1512 /* XXX: Is uppercase something we should support? */
1513 if (c >= 'A' && c <= 'F')
1514 return 10 + c - 'A';
1521 parse_digest(struct archive_read *a, struct archive_entry *entry,
1522 const char *digest, int type)
1524 unsigned char digest_buf[64];
1529 case ARCHIVE_ENTRY_DIGEST_MD5:
1530 len = sizeof(entry->digest.md5);
1532 case ARCHIVE_ENTRY_DIGEST_RMD160:
1533 len = sizeof(entry->digest.rmd160);
1535 case ARCHIVE_ENTRY_DIGEST_SHA1:
1536 len = sizeof(entry->digest.sha1);
1538 case ARCHIVE_ENTRY_DIGEST_SHA256:
1539 len = sizeof(entry->digest.sha256);
1541 case ARCHIVE_ENTRY_DIGEST_SHA384:
1542 len = sizeof(entry->digest.sha384);
1544 case ARCHIVE_ENTRY_DIGEST_SHA512:
1545 len = sizeof(entry->digest.sha512);
1548 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1549 "Internal error: Unknown digest type");
1550 return ARCHIVE_FATAL;
1553 if (len > sizeof(digest_buf)) {
1554 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1555 "Internal error: Digest storage too large");
1556 return ARCHIVE_FATAL;
1561 if (mtree_strnlen(digest, len+1) != len) {
1562 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1563 "incorrect digest length, ignoring");
1564 return ARCHIVE_WARN;
1567 for (i = 0, j = 0; i < len; i += 2, j++) {
1568 high = parse_hex_nibble(digest[i]);
1569 low = parse_hex_nibble(digest[i+1]);
1570 if (high == -1 || low == -1) {
1571 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1572 "invalid digest data, ignoring");
1573 return ARCHIVE_WARN;
1576 digest_buf[j] = high << 4 | low;
1579 return archive_entry_set_digest(entry, type, digest_buf);
1583 * Parse a single keyword and its value.
1586 parse_keyword(struct archive_read *a, struct mtree *mtree,
1587 struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws)
1594 return (ARCHIVE_OK);
1596 if (strcmp(key, "nochange") == 0) {
1597 *parsed_kws |= MTREE_HAS_NOCHANGE;
1598 return (ARCHIVE_OK);
1600 if (strcmp(key, "optional") == 0) {
1601 *parsed_kws |= MTREE_HAS_OPTIONAL;
1602 return (ARCHIVE_OK);
1604 if (strcmp(key, "ignore") == 0) {
1606 * The mtree processing is not recursive, so
1607 * recursion will only happen for explicitly listed
1610 return (ARCHIVE_OK);
1613 val = strchr(key, '=');
1615 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1616 "Malformed attribute \"%s\" (%d)", key, key[0]);
1617 return (ARCHIVE_WARN);
1625 if (strcmp(key, "content") == 0
1626 || strcmp(key, "contents") == 0) {
1627 parse_escapes(val, NULL);
1628 archive_strcpy(&mtree->contents_name, val);
1629 return (ARCHIVE_OK);
1631 if (strcmp(key, "cksum") == 0)
1632 return (ARCHIVE_OK);
1635 if (strcmp(key, "device") == 0) {
1636 /* stat(2) st_rdev field, e.g. the major/minor IDs
1637 * of a char/block special file */
1641 *parsed_kws |= MTREE_HAS_DEVICE;
1642 r = parse_device(&dev, &a->archive, val);
1643 if (r == ARCHIVE_OK)
1644 archive_entry_set_rdev(entry, dev);
1649 if (strcmp(key, "flags") == 0) {
1650 *parsed_kws |= MTREE_HAS_FFLAGS;
1651 archive_entry_copy_fflags_text(entry, val);
1652 return (ARCHIVE_OK);
1656 if (strcmp(key, "gid") == 0) {
1657 *parsed_kws |= MTREE_HAS_GID;
1658 archive_entry_set_gid(entry, mtree_atol(&val, 10));
1659 return (ARCHIVE_OK);
1661 if (strcmp(key, "gname") == 0) {
1662 *parsed_kws |= MTREE_HAS_GNAME;
1663 archive_entry_copy_gname(entry, val);
1664 return (ARCHIVE_OK);
1668 if (strcmp(key, "inode") == 0) {
1669 archive_entry_set_ino(entry, mtree_atol(&val, 10));
1670 return (ARCHIVE_OK);
1674 if (strcmp(key, "link") == 0) {
1675 parse_escapes(val, NULL);
1676 archive_entry_copy_symlink(entry, val);
1677 return (ARCHIVE_OK);
1681 if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0) {
1682 return parse_digest(a, entry, val,
1683 ARCHIVE_ENTRY_DIGEST_MD5);
1685 if (strcmp(key, "mode") == 0) {
1686 if (val[0] < '0' || val[0] > '7') {
1687 archive_set_error(&a->archive,
1688 ARCHIVE_ERRNO_FILE_FORMAT,
1689 "Symbolic or non-octal mode \"%s\" unsupported", val);
1690 return (ARCHIVE_WARN);
1692 *parsed_kws |= MTREE_HAS_PERM;
1693 archive_entry_set_perm(entry, (mode_t)mtree_atol(&val, 8));
1694 return (ARCHIVE_OK);
1698 if (strcmp(key, "nlink") == 0) {
1699 *parsed_kws |= MTREE_HAS_NLINK;
1700 archive_entry_set_nlink(entry,
1701 (unsigned int)mtree_atol(&val, 10));
1702 return (ARCHIVE_OK);
1706 if (strcmp(key, "resdevice") == 0) {
1707 /* stat(2) st_dev field, e.g. the device ID where the
1712 r = parse_device(&dev, &a->archive, val);
1713 if (r == ARCHIVE_OK)
1714 archive_entry_set_dev(entry, dev);
1717 if (strcmp(key, "rmd160") == 0 ||
1718 strcmp(key, "rmd160digest") == 0) {
1719 return parse_digest(a, entry, val,
1720 ARCHIVE_ENTRY_DIGEST_RMD160);
1724 if (strcmp(key, "sha1") == 0 ||
1725 strcmp(key, "sha1digest") == 0) {
1726 return parse_digest(a, entry, val,
1727 ARCHIVE_ENTRY_DIGEST_SHA1);
1729 if (strcmp(key, "sha256") == 0 ||
1730 strcmp(key, "sha256digest") == 0) {
1731 return parse_digest(a, entry, val,
1732 ARCHIVE_ENTRY_DIGEST_SHA256);
1734 if (strcmp(key, "sha384") == 0 ||
1735 strcmp(key, "sha384digest") == 0) {
1736 return parse_digest(a, entry, val,
1737 ARCHIVE_ENTRY_DIGEST_SHA384);
1739 if (strcmp(key, "sha512") == 0 ||
1740 strcmp(key, "sha512digest") == 0) {
1741 return parse_digest(a, entry, val,
1742 ARCHIVE_ENTRY_DIGEST_SHA512);
1744 if (strcmp(key, "size") == 0) {
1745 archive_entry_set_size(entry, mtree_atol(&val, 10));
1746 return (ARCHIVE_OK);
1750 if (strcmp(key, "tags") == 0) {
1752 * Comma delimited list of tags.
1753 * Ignore the tags for now, but the interface
1754 * should be extended to allow inclusion/exclusion.
1756 return (ARCHIVE_OK);
1758 if (strcmp(key, "time") == 0) {
1760 int64_t my_time_t_max = get_time_t_max();
1761 int64_t my_time_t_min = get_time_t_min();
1764 *parsed_kws |= MTREE_HAS_MTIME;
1765 m = mtree_atol(&val, 10);
1766 /* Replicate an old mtree bug:
1767 * 123456789.1 represents 123456789
1768 * seconds and 1 nanosecond. */
1771 ns = (long)mtree_atol(&val, 10);
1774 else if (ns > 999999999)
1777 if (m > my_time_t_max)
1779 else if (m < my_time_t_min)
1781 archive_entry_set_mtime(entry, (time_t)m, ns);
1782 return (ARCHIVE_OK);
1784 if (strcmp(key, "type") == 0) {
1787 if (strcmp(val, "block") == 0) {
1788 *parsed_kws |= MTREE_HAS_TYPE;
1789 archive_entry_set_filetype(entry,
1791 return (ARCHIVE_OK);
1795 if (strcmp(val, "char") == 0) {
1796 *parsed_kws |= MTREE_HAS_TYPE;
1797 archive_entry_set_filetype(entry,
1799 return (ARCHIVE_OK);
1803 if (strcmp(val, "dir") == 0) {
1804 *parsed_kws |= MTREE_HAS_TYPE;
1805 archive_entry_set_filetype(entry,
1807 return (ARCHIVE_OK);
1811 if (strcmp(val, "fifo") == 0) {
1812 *parsed_kws |= MTREE_HAS_TYPE;
1813 archive_entry_set_filetype(entry,
1815 return (ARCHIVE_OK);
1817 if (strcmp(val, "file") == 0) {
1818 *parsed_kws |= MTREE_HAS_TYPE;
1819 archive_entry_set_filetype(entry,
1821 return (ARCHIVE_OK);
1825 if (strcmp(val, "link") == 0) {
1826 *parsed_kws |= MTREE_HAS_TYPE;
1827 archive_entry_set_filetype(entry,
1829 return (ARCHIVE_OK);
1835 archive_set_error(&a->archive,
1836 ARCHIVE_ERRNO_FILE_FORMAT,
1837 "Unrecognized file type \"%s\"; "
1838 "assuming \"file\"", val);
1839 archive_entry_set_filetype(entry, AE_IFREG);
1840 return (ARCHIVE_WARN);
1844 if (strcmp(key, "uid") == 0) {
1845 *parsed_kws |= MTREE_HAS_UID;
1846 archive_entry_set_uid(entry, mtree_atol(&val, 10));
1847 return (ARCHIVE_OK);
1849 if (strcmp(key, "uname") == 0) {
1850 *parsed_kws |= MTREE_HAS_UNAME;
1851 archive_entry_copy_uname(entry, val);
1852 return (ARCHIVE_OK);
1858 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1859 "Unrecognized key %s=%s", key, val);
1860 return (ARCHIVE_WARN);
1864 read_data(struct archive_read *a, const void **buff, size_t *size,
1867 size_t bytes_to_read;
1869 struct mtree *mtree;
1871 mtree = (struct mtree *)(a->format->data);
1872 if (mtree->fd < 0) {
1876 return (ARCHIVE_EOF);
1878 if (mtree->buff == NULL) {
1879 mtree->buffsize = 64 * 1024;
1880 mtree->buff = malloc(mtree->buffsize);
1881 if (mtree->buff == NULL) {
1882 archive_set_error(&a->archive, ENOMEM,
1883 "Can't allocate memory");
1884 return (ARCHIVE_FATAL);
1888 *buff = mtree->buff;
1889 *offset = mtree->offset;
1890 if ((int64_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1891 bytes_to_read = (size_t)(mtree->cur_size - mtree->offset);
1893 bytes_to_read = mtree->buffsize;
1894 bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1895 if (bytes_read < 0) {
1896 archive_set_error(&a->archive, errno, "Can't read");
1897 return (ARCHIVE_WARN);
1899 if (bytes_read == 0) {
1901 return (ARCHIVE_EOF);
1903 mtree->offset += bytes_read;
1905 return (ARCHIVE_OK);
1908 /* Skip does nothing except possibly close the contents file. */
1910 skip(struct archive_read *a)
1912 struct mtree *mtree;
1914 mtree = (struct mtree *)(a->format->data);
1915 if (mtree->fd >= 0) {
1919 return (ARCHIVE_OK);
1923 * Since parsing backslash sequences always makes strings shorter,
1924 * we can always do this conversion in-place.
1927 parse_escapes(char *src, struct mtree_entry *mentry)
1932 if (mentry != NULL && strcmp(src, ".") == 0)
1935 while (*src != '\0') {
1937 if (c == '/' && mentry != NULL)
1942 if (src[1] < '0' || src[1] > '7') {
1951 if (src[1] >= '0' && src[1] <= '7' &&
1952 src[2] >= '0' && src[2] <= '7') {
1953 c = (src[0] - '0') << 6;
1954 c |= (src[1] - '0') << 3;
1955 c |= (src[2] - '0');
2002 /* Parse a hex digit. */
2006 if (c >= '0' && c <= '9')
2008 else if (c >= 'a' && c <= 'f')
2010 else if (c >= 'A' && c <= 'F')
2017 * Note that this implementation does not (and should not!) obey
2018 * locale settings; you cannot simply substitute strtol here, since
2019 * it does obey locale.
2022 mtree_atol(char **p, int base)
2025 int digit, last_digit_limit;
2030 else if ((*p)[1] == 'x' || (*p)[1] == 'X') {
2039 limit = INT64_MIN / base;
2040 last_digit_limit = -(INT64_MIN % base);
2044 digit = parsedigit(**p);
2045 while (digit >= 0 && digit < base) {
2046 if (l < limit || (l == limit && digit >= last_digit_limit))
2048 l = (l * base) - digit;
2049 digit = parsedigit(*++(*p));
2053 limit = INT64_MAX / base;
2054 last_digit_limit = INT64_MAX % base;
2057 digit = parsedigit(**p);
2058 while (digit >= 0 && digit < base) {
2059 if (l > limit || (l == limit && digit > last_digit_limit))
2061 l = (l * base) + digit;
2062 digit = parsedigit(*++(*p));
2069 * Returns length of line (including trailing newline)
2070 * or negative on error. 'start' argument is updated to
2071 * point to first character of line.
2074 readline(struct archive_read *a, struct mtree *mtree, char **start,
2078 ssize_t total_size = 0;
2079 ssize_t find_off = 0;
2084 /* Accumulate line in a line buffer. */
2086 /* Read some more. */
2087 t = __archive_read_ahead(a, 1, &bytes_read);
2091 return (ARCHIVE_FATAL);
2092 nl = memchr(t, '\n', bytes_read);
2093 /* If we found '\n', trim the read to end exactly there. */
2095 bytes_read = ((const char *)nl) - ((const char *)t) + 1;
2097 if (total_size + bytes_read + 1 > limit) {
2098 archive_set_error(&a->archive,
2099 ARCHIVE_ERRNO_FILE_FORMAT,
2101 return (ARCHIVE_FATAL);
2103 if (archive_string_ensure(&mtree->line,
2104 total_size + bytes_read + 1) == NULL) {
2105 archive_set_error(&a->archive, ENOMEM,
2106 "Can't allocate working buffer");
2107 return (ARCHIVE_FATAL);
2109 /* Append new bytes to string. */
2110 memcpy(mtree->line.s + total_size, t, bytes_read);
2111 __archive_read_consume(a, bytes_read);
2112 total_size += bytes_read;
2113 mtree->line.s[total_size] = '\0';
2115 for (u = mtree->line.s + find_off; *u; ++u) {
2117 /* Ends with unescaped newline. */
2118 *start = mtree->line.s;
2120 } else if (u[0] == '#') {
2121 /* Ends with comment sequence #...\n */
2123 /* But we've not found the \n yet */
2126 } else if (u[0] == '\\') {
2128 /* Trim escaped newline. */
2130 mtree->line.s[total_size] = '\0';
2132 } else if (u[1] != '\0') {
2133 /* Skip the two-char escape sequence */
2138 find_off = u - mtree->line.s;