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_private.h"
55 #include "archive_rb.h"
56 #include "archive_read_private.h"
57 #include "archive_string.h"
58 #include "archive_pack_dev.h"
67 #define MTREE_HAS_DEVICE 0x0001
68 #define MTREE_HAS_FFLAGS 0x0002
69 #define MTREE_HAS_GID 0x0004
70 #define MTREE_HAS_GNAME 0x0008
71 #define MTREE_HAS_MTIME 0x0010
72 #define MTREE_HAS_NLINK 0x0020
73 #define MTREE_HAS_PERM 0x0040
74 #define MTREE_HAS_SIZE 0x0080
75 #define MTREE_HAS_TYPE 0x0100
76 #define MTREE_HAS_UID 0x0200
77 #define MTREE_HAS_UNAME 0x0400
79 #define MTREE_HAS_OPTIONAL 0x0800
80 #define MTREE_HAS_NOCHANGE 0x1000 /* FreeBSD specific */
82 #define MAX_LINE_LEN (1024 * 1024)
85 struct mtree_option *next;
90 struct archive_rb_node rbnode;
91 struct mtree_entry *next_dup;
92 struct mtree_entry *next;
93 struct mtree_option *options;
100 struct archive_string line;
106 const char *archive_format_name;
107 struct mtree_entry *entries;
108 struct mtree_entry *this_entry;
109 struct archive_rb_tree entry_rbtree;
110 struct archive_string current_dir;
111 struct archive_string contents_name;
113 struct archive_entry_linkresolver *resolver;
114 struct archive_rb_tree rbtree;
120 static int bid_keycmp(const char *, const char *, ssize_t);
121 static int cleanup(struct archive_read *);
122 static int detect_form(struct archive_read *, int *);
123 static int mtree_bid(struct archive_read *, int);
124 static int parse_file(struct archive_read *, struct archive_entry *,
125 struct mtree *, struct mtree_entry *, int *);
126 static void parse_escapes(char *, struct mtree_entry *);
127 static int parse_line(struct archive_read *, struct archive_entry *,
128 struct mtree *, struct mtree_entry *, int *);
129 static int parse_keyword(struct archive_read *, struct mtree *,
130 struct archive_entry *, struct mtree_option *, int *);
131 static int read_data(struct archive_read *a,
132 const void **buff, size_t *size, int64_t *offset);
133 static ssize_t readline(struct archive_read *, struct mtree *, char **, ssize_t);
134 static int skip(struct archive_read *a);
135 static int read_header(struct archive_read *,
136 struct archive_entry *);
137 static int64_t mtree_atol(char **, int base);
140 * There's no standard for TIME_T_MAX/TIME_T_MIN. So we compute them
141 * here. TODO: Move this to configure time, but be careful
142 * about cross-compile environments.
147 #if defined(TIME_T_MAX)
150 /* ISO C allows time_t to be a floating-point type,
151 but POSIX requires an integer type. The following
152 should work on any system that follows the POSIX
154 if (((time_t)0) < ((time_t)-1)) {
155 /* Time_t is unsigned */
158 /* Time_t is signed. */
159 /* Assume it's the same as int64_t or int32_t */
160 if (sizeof(time_t) == sizeof(int64_t)) {
161 return (time_t)INT64_MAX;
163 return (time_t)INT32_MAX;
172 #if defined(TIME_T_MIN)
175 if (((time_t)0) < ((time_t)-1)) {
176 /* Time_t is unsigned */
179 /* Time_t is signed. */
180 if (sizeof(time_t) == sizeof(int64_t)) {
181 return (time_t)INT64_MIN;
183 return (time_t)INT32_MIN;
190 archive_read_format_mtree_options(struct archive_read *a,
191 const char *key, const char *val)
195 mtree = (struct mtree *)(a->format->data);
196 if (strcmp(key, "checkfs") == 0) {
197 /* Allows to read information missing from the mtree from the file system */
198 if (val == NULL || val[0] == 0) {
206 /* Note: The "warn" return is just to inform the options
207 * supervisor that we didn't handle it. It will generate
208 * a suitable error if no one used this option. */
209 return (ARCHIVE_WARN);
213 free_options(struct mtree_option *head)
215 struct mtree_option *next;
217 for (; head != NULL; head = next) {
225 mtree_cmp_node(const struct archive_rb_node *n1,
226 const struct archive_rb_node *n2)
228 const struct mtree_entry *e1 = (const struct mtree_entry *)n1;
229 const struct mtree_entry *e2 = (const struct mtree_entry *)n2;
231 return (strcmp(e1->name, e2->name));
235 mtree_cmp_key(const struct archive_rb_node *n, const void *key)
237 const struct mtree_entry *e = (const struct mtree_entry *)n;
239 return (strcmp(e->name, key));
243 archive_read_support_format_mtree(struct archive *_a)
245 static const struct archive_rb_tree_ops rb_ops = {
246 mtree_cmp_node, mtree_cmp_key,
248 struct archive_read *a = (struct archive_read *)_a;
252 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
253 ARCHIVE_STATE_NEW, "archive_read_support_format_mtree");
255 mtree = (struct mtree *)calloc(1, sizeof(*mtree));
257 archive_set_error(&a->archive, ENOMEM,
258 "Can't allocate mtree data");
259 return (ARCHIVE_FATAL);
264 __archive_rb_tree_init(&mtree->rbtree, &rb_ops);
266 r = __archive_read_register_format(a, mtree, "mtree",
267 mtree_bid, archive_read_format_mtree_options, read_header, read_data, skip, NULL, cleanup, NULL, NULL);
275 cleanup(struct archive_read *a)
278 struct mtree_entry *p, *q;
280 mtree = (struct mtree *)(a->format->data);
286 free_options(p->options);
290 archive_string_free(&mtree->line);
291 archive_string_free(&mtree->current_dir);
292 archive_string_free(&mtree->contents_name);
293 archive_entry_linkresolver_free(mtree->resolver);
297 (a->format->data) = NULL;
302 get_line_size(const char *b, ssize_t avail, ssize_t *nlsize)
307 while (len < avail) {
309 case '\0':/* Non-ascii character or control character. */
314 if (avail-len > 1 && b[1] == '\n') {
336 * <---------------- ravail --------------------->
337 * <-- diff ------> <--- avail ----------------->
338 * <---- len ----------->
339 * | Previous lines | line being parsed nl extra |
345 next_line(struct archive_read *a,
346 const char **b, ssize_t *avail, ssize_t *ravail, ssize_t *nl)
356 len = get_line_size(*b, *avail, nl);
358 * Read bytes more while it does not reach the end of line.
360 while (*nl == 0 && len == *avail && !quit) {
361 ssize_t diff = *ravail - *avail;
362 size_t nbytes_req = (*ravail+1023) & ~1023U;
366 * Place an arbitrary limit on the line length.
367 * mtree is almost free-form input and without line length limits,
368 * it can consume a lot of memory.
370 if (len >= MAX_LINE_LEN)
373 /* Increase reading bytes if it is not enough to at least
375 if (nbytes_req < (size_t)*ravail + 160)
378 *b = __archive_read_ahead(a, nbytes_req, avail);
380 if (*ravail >= *avail)
382 /* Reading bytes reaches the end of file. */
383 *b = __archive_read_ahead(a, *avail, avail);
389 tested = len;/* Skip some bytes we already determinated. */
390 len = get_line_size(*b + len, *avail - len, nl);
398 * Compare characters with a mtree keyword.
399 * Returns the length of a mtree keyword if matched.
400 * Returns 0 if not matched.
403 bid_keycmp(const char *p, const char *key, ssize_t len)
407 while (len > 0 && *p && *key) {
415 return (0);/* Not match */
418 return (0);/* Not match */
420 /* A following character should be specified characters */
421 if (p[0] == '=' || p[0] == ' ' || p[0] == '\t' ||
422 p[0] == '\n' || p[0] == '\r' ||
423 (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r')))
425 return (0);/* Not match */
429 * Test whether the characters 'p' has is mtree keyword.
430 * Returns the length of a detected keyword.
431 * Returns 0 if any keywords were not found.
434 bid_keyword(const char *p, ssize_t len)
436 static const char * const keys_c[] = {
437 "content", "contents", "cksum", NULL
439 static const char * const keys_df[] = {
440 "device", "flags", NULL
442 static const char * const keys_g[] = {
445 static const char * const keys_il[] = {
446 "ignore", "inode", "link", NULL
448 static const char * const keys_m[] = {
449 "md5", "md5digest", "mode", NULL
451 static const char * const keys_no[] = {
452 "nlink", "nochange", "optional", NULL
454 static const char * const keys_r[] = {
455 "resdevice", "rmd160", "rmd160digest", NULL
457 static const char * const keys_s[] = {
458 "sha1", "sha1digest",
459 "sha256", "sha256digest",
460 "sha384", "sha384digest",
461 "sha512", "sha512digest",
464 static const char * const keys_t[] = {
465 "tags", "time", "type", NULL
467 static const char * const keys_u[] = {
470 const char * const *keys;
474 case 'c': keys = keys_c; break;
475 case 'd': case 'f': keys = keys_df; break;
476 case 'g': keys = keys_g; break;
477 case 'i': case 'l': keys = keys_il; break;
478 case 'm': keys = keys_m; break;
479 case 'n': case 'o': keys = keys_no; break;
480 case 'r': keys = keys_r; break;
481 case 's': keys = keys_s; break;
482 case 't': keys = keys_t; break;
483 case 'u': keys = keys_u; break;
484 default: return (0);/* Unknown key */
487 for (i = 0; keys[i] != NULL; i++) {
488 int l = bid_keycmp(p, keys[i], len);
492 return (0);/* Unknown key */
496 * Test whether there is a set of mtree keywords.
497 * Returns the number of keyword.
498 * Returns -1 if we got incorrect sequence.
499 * This function expects a set of "<space characters>keyword=value".
500 * When "unset" is specified, expects a set of "<space characters>keyword".
503 bid_keyword_list(const char *p, ssize_t len, int unset, int last_is_path)
508 while (len > 0 && *p) {
511 /* Test whether there are blank characters in the line. */
512 while (len >0 && (*p == ' ' || *p == '\t')) {
517 if (*p == '\n' || *p == '\r')
519 if (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r'))
521 if (!blank && !last_is_path) /* No blank character. */
523 if (last_is_path && len == 0)
527 l = bid_keycmp(p, "all", len);
531 /* Test whether there is a correct key in the line. */
532 l = bid_keyword(p, len);
534 return (-1);/* Unknown keyword was found. */
544 while (len > 0 && *p != ' ' && *p != '\t') {
549 /* A keyword should have a its value unless
550 * "/unset" operation. */
551 if (!unset && value == 0)
559 bid_entry(const char *p, ssize_t len, ssize_t nl, int *last_is_path)
562 static const unsigned char safe_char[256] = {
563 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
564 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
565 /* !"$%&'()*+,-./ EXCLUSION:( )(#) */
566 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
567 /* 0123456789:;<>? EXCLUSION:(=) */
568 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
569 /* @ABCDEFGHIJKLMNO */
570 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
571 /* PQRSTUVWXYZ[\]^_ */
572 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
573 /* `abcdefghijklmno */
574 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
575 /* pqrstuvwxyz{|}~ */
576 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
577 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
578 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
579 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
582 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
583 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
588 const char * const pp_end = pp + len;
592 * Skip the path-name which is quoted.
594 for (;pp < pp_end; ++pp) {
595 if (!safe_char[*(const unsigned char *)pp]) {
596 if (*pp != ' ' && *pp != '\t' && *pp != '\r'
605 /* If a path-name was not found at the first, try to check
606 * a mtree format(a.k.a form D) ``NetBSD's mtree -D'' creates,
607 * which places the path-name at the last. */
609 const char *pb = p + len - nl;
613 /* The form D accepts only a single line for an entry. */
615 pb[-1] == '\\' && (pb[-2] == ' ' || pb[-2] == '\t'))
617 if (pb-1 >= p && pb[-1] == '\\')
621 while (p <= --pb && *pb != ' ' && *pb != '\t') {
622 if (!safe_char[*(const unsigned char *)pb])
625 /* The pathname should have a slash in this
630 if (name_len == 0 || slash == 0)
632 /* If '/' is placed at the first in this field, this is not
633 * a valid filename. */
636 ll = len - nl - name_len;
641 return (bid_keyword_list(pp, ll, 0, *last_is_path));
644 #define MAX_BID_ENTRY 3
647 mtree_bid(struct archive_read *a, int best_bid)
649 const char *signature = "#mtree";
652 (void)best_bid; /* UNUSED */
654 /* Now let's look at the actual header and see if it matches. */
655 p = __archive_read_ahead(a, strlen(signature), NULL);
659 if (memcmp(p, signature, strlen(signature)) == 0)
660 return (8 * (int)strlen(signature));
663 * There is not a mtree signature. Let's try to detect mtree format.
665 return (detect_form(a, NULL));
669 detect_form(struct archive_read *a, int *is_form_d)
672 ssize_t avail, ravail;
673 ssize_t detected_bytes = 0, len, nl;
674 int entry_cnt = 0, multiline = 0;
675 int form_D = 0;/* The archive is generated by `NetBSD mtree -D'
676 * (In this source we call it `form D') . */
678 if (is_form_d != NULL)
680 p = __archive_read_ahead(a, 1, &avail);
685 len = next_line(a, &p, &avail, &ravail, &nl);
686 /* The terminal character of the line should be
687 * a new line character, '\r\n' or '\n'. */
688 if (len <= 0 || nl == 0)
691 /* Leading whitespace is never significant,
693 while (len > 0 && (*p == ' ' || *p == '\t')) {
698 /* Skip comment or empty line. */
699 if (p[0] == '#' || p[0] == '\n' || p[0] == '\r') {
705 /* A continuance line; the terminal
706 * character of previous line was '\' character. */
707 if (bid_keyword_list(p, len, 0, 0) <= 0)
710 detected_bytes += len;
711 if (p[len-nl-1] != '\\') {
712 if (multiline == 1 &&
713 ++entry_cnt >= MAX_BID_ENTRY)
722 int last_is_path, keywords;
724 keywords = bid_entry(p, len, nl, &last_is_path);
726 detected_bytes += len;
730 else if (keywords > 0)
731 /* This line is not `form D'. */
733 } else if (form_D == 1) {
734 if (!last_is_path && keywords > 0)
735 /* This this is not `form D'
736 * and We cannot accept mixed
740 if (!last_is_path && p[len-nl-1] == '\\')
741 /* This line continues. */
744 /* We've got plenty of correct lines
745 * to assume that this file is a mtree
747 if (++entry_cnt >= MAX_BID_ENTRY)
752 } else if (len > 4 && strncmp(p, "/set", 4) == 0) {
753 if (bid_keyword_list(p+4, len-4, 0, 0) <= 0)
755 /* This line continues. */
756 if (p[len-nl-1] == '\\')
758 } else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
759 if (bid_keyword_list(p+6, len-6, 1, 0) <= 0)
761 /* This line continues. */
762 if (p[len-nl-1] == '\\')
767 /* Test next line. */
771 if (entry_cnt >= MAX_BID_ENTRY || (entry_cnt > 0 && len == 0)) {
772 if (is_form_d != NULL) {
783 * The extended mtree format permits multiple lines specifying
784 * attributes for each file. For those entries, only the last line
785 * is actually used. Practically speaking, that means we have
786 * to read the entire mtree file into memory up front.
788 * The parsing is done in two steps. First, it is decided if a line
789 * changes the global defaults and if it is, processed accordingly.
790 * Otherwise, the options of the line are merged with the current
794 add_option(struct archive_read *a, struct mtree_option **global,
795 const char *value, size_t len)
797 struct mtree_option *opt;
799 if ((opt = malloc(sizeof(*opt))) == NULL) {
800 archive_set_error(&a->archive, errno, "Can't allocate memory");
801 return (ARCHIVE_FATAL);
803 if ((opt->value = malloc(len + 1)) == NULL) {
805 archive_set_error(&a->archive, errno, "Can't allocate memory");
806 return (ARCHIVE_FATAL);
808 memcpy(opt->value, value, len);
809 opt->value[len] = '\0';
816 remove_option(struct mtree_option **global, const char *value, size_t len)
818 struct mtree_option *iter, *last;
821 for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
822 if (strncmp(iter->value, value, len) == 0 &&
823 (iter->value[len] == '\0' ||
824 iter->value[len] == '='))
830 *global = iter->next;
832 last->next = iter->next;
839 process_global_set(struct archive_read *a,
840 struct mtree_option **global, const char *line)
842 const char *next, *eq;
848 next = line + strspn(line, " \t\r\n");
852 next = line + strcspn(line, " \t\r\n");
853 eq = strchr(line, '=');
859 remove_option(global, line, len);
860 r = add_option(a, global, line, next - line);
868 process_global_unset(struct archive_read *a,
869 struct mtree_option **global, const char *line)
875 if (strchr(line, '=') != NULL) {
876 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
877 "/unset shall not contain `='");
878 return ARCHIVE_FATAL;
882 next = line + strspn(line, " \t\r\n");
886 len = strcspn(line, " \t\r\n");
888 if (len == 3 && strncmp(line, "all", 3) == 0) {
889 free_options(*global);
892 remove_option(global, line, len);
900 process_add_entry(struct archive_read *a, struct mtree *mtree,
901 struct mtree_option **global, const char *line, ssize_t line_len,
902 struct mtree_entry **last_entry, int is_form_d)
904 struct mtree_entry *entry;
905 struct mtree_option *iter;
906 const char *next, *eq, *name, *end;
907 size_t name_len, len;
910 if ((entry = malloc(sizeof(*entry))) == NULL) {
911 archive_set_error(&a->archive, errno, "Can't allocate memory");
912 return (ARCHIVE_FATAL);
915 entry->options = NULL;
920 /* Add this entry to list. */
921 if (*last_entry == NULL)
922 mtree->entries = entry;
924 (*last_entry)->next = entry;
928 /* Filename is last item on line. */
929 /* Adjust line_len to trim trailing whitespace */
930 while (line_len > 0) {
931 char last_character = line[line_len - 1];
932 if (last_character == '\r'
933 || last_character == '\n'
934 || last_character == '\t'
935 || last_character == ' ') {
941 /* Name starts after the last whitespace separator */
943 for (i = 0; i < line_len; i++) {
951 name_len = line + line_len - name;
954 /* Filename is first item on line */
955 name_len = strcspn(line, " \t\r\n");
958 end = line + line_len;
960 /* name/name_len is the name within the line. */
961 /* line..end brackets the entire line except the name */
963 if ((entry->name = malloc(name_len + 1)) == NULL) {
964 archive_set_error(&a->archive, errno, "Can't allocate memory");
965 return (ARCHIVE_FATAL);
968 memcpy(entry->name, name, name_len);
969 entry->name[name_len] = '\0';
970 parse_escapes(entry->name, entry);
972 entry->next_dup = NULL;
974 if (!__archive_rb_tree_insert_node(&mtree->rbtree, &entry->rbnode)) {
975 struct mtree_entry *alt;
976 alt = (struct mtree_entry *)__archive_rb_tree_find_node(
977 &mtree->rbtree, entry->name);
978 while (alt->next_dup)
980 alt->next_dup = entry;
984 for (iter = *global; iter != NULL; iter = iter->next) {
985 r = add_option(a, &entry->options, iter->value,
986 strlen(iter->value));
992 next = line + strspn(line, " \t\r\n");
998 next = line + strcspn(line, " \t\r\n");
999 eq = strchr(line, '=');
1000 if (eq == NULL || eq > next)
1005 remove_option(&entry->options, line, len);
1006 r = add_option(a, &entry->options, line, next - line);
1007 if (r != ARCHIVE_OK)
1014 read_mtree(struct archive_read *a, struct mtree *mtree)
1019 struct mtree_option *global;
1020 struct mtree_entry *last_entry;
1023 mtree->archive_format = ARCHIVE_FORMAT_MTREE;
1024 mtree->archive_format_name = "mtree";
1029 (void)detect_form(a, &is_form_d);
1031 for (counter = 1; ; ++counter) {
1033 len = readline(a, mtree, &p, 65536);
1035 mtree->this_entry = mtree->entries;
1036 free_options(global);
1037 return (ARCHIVE_OK);
1040 free_options(global);
1043 /* Leading whitespace is never significant, ignore it. */
1044 while (*p == ' ' || *p == '\t') {
1048 /* Skip content lines and blank lines. */
1051 if (*p == '\r' || *p == '\n' || *p == '\0')
1053 /* Non-printable characters are not allowed */
1054 for (s = p;s < p + len - 1; s++) {
1060 if (r != ARCHIVE_OK)
1063 r = process_add_entry(a, mtree, &global, p, len,
1064 &last_entry, is_form_d);
1065 } else if (len > 4 && strncmp(p, "/set", 4) == 0) {
1066 if (p[4] != ' ' && p[4] != '\t')
1068 r = process_global_set(a, &global, p);
1069 } else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
1070 if (p[6] != ' ' && p[6] != '\t')
1072 r = process_global_unset(a, &global, p);
1076 if (r != ARCHIVE_OK) {
1077 free_options(global);
1082 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1083 "Can't parse line %ju", counter);
1084 free_options(global);
1085 return (ARCHIVE_FATAL);
1089 * Read in the entire mtree file into memory on the first request.
1090 * Then use the next unused file to satisfy each header request.
1093 read_header(struct archive_read *a, struct archive_entry *entry)
1095 struct mtree *mtree;
1099 mtree = (struct mtree *)(a->format->data);
1101 if (mtree->fd >= 0) {
1106 if (mtree->entries == NULL) {
1107 mtree->resolver = archive_entry_linkresolver_new();
1108 if (mtree->resolver == NULL)
1109 return ARCHIVE_FATAL;
1110 archive_entry_linkresolver_set_strategy(mtree->resolver,
1111 ARCHIVE_FORMAT_MTREE);
1112 r = read_mtree(a, mtree);
1113 if (r != ARCHIVE_OK)
1117 a->archive.archive_format = mtree->archive_format;
1118 a->archive.archive_format_name = mtree->archive_format_name;
1121 if (mtree->this_entry == NULL)
1122 return (ARCHIVE_EOF);
1123 if (strcmp(mtree->this_entry->name, "..") == 0) {
1124 mtree->this_entry->used = 1;
1125 if (archive_strlen(&mtree->current_dir) > 0) {
1126 /* Roll back current path. */
1127 p = mtree->current_dir.s
1128 + mtree->current_dir.length - 1;
1129 while (p >= mtree->current_dir.s && *p != '/')
1131 if (p >= mtree->current_dir.s)
1133 mtree->current_dir.length
1134 = p - mtree->current_dir.s + 1;
1137 if (!mtree->this_entry->used) {
1139 r = parse_file(a, entry, mtree, mtree->this_entry,
1144 mtree->this_entry = mtree->this_entry->next;
1149 * A single file can have multiple lines contribute specifications.
1150 * Parse as many lines as necessary, then pull additional information
1151 * from a backing file on disk as necessary.
1154 parse_file(struct archive_read *a, struct archive_entry *entry,
1155 struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
1158 struct stat st_storage, *st;
1159 struct mtree_entry *mp;
1160 struct archive_entry *sparse_entry;
1161 int r = ARCHIVE_OK, r1, parsed_kws;
1165 /* Initialize reasonable defaults. */
1166 archive_entry_set_filetype(entry, AE_IFREG);
1167 archive_entry_set_size(entry, 0);
1168 archive_string_empty(&mtree->contents_name);
1170 /* Parse options from this line. */
1172 r = parse_line(a, entry, mtree, mentry, &parsed_kws);
1175 archive_entry_copy_pathname(entry, mentry->name);
1177 * "Full" entries are allowed to have multiple lines
1178 * and those lines aren't required to be adjacent. We
1179 * don't support multiple lines for "relative" entries
1180 * nor do we make any attempt to merge data from
1181 * separate "relative" and "full" entries. (Merging
1182 * "relative" and "full" entries would require dealing
1183 * with pathname canonicalization, which is a very
1186 mp = (struct mtree_entry *)__archive_rb_tree_find_node(
1187 &mtree->rbtree, mentry->name);
1188 for (; mp; mp = mp->next_dup) {
1189 if (mp->full && !mp->used) {
1190 /* Later lines override earlier ones. */
1192 r1 = parse_line(a, entry, mtree, mp, &parsed_kws);
1199 * Relative entries require us to construct
1200 * the full path and possibly update the
1201 * current directory.
1203 size_t n = archive_strlen(&mtree->current_dir);
1205 archive_strcat(&mtree->current_dir, "/");
1206 archive_strcat(&mtree->current_dir, mentry->name);
1207 archive_entry_copy_pathname(entry, mtree->current_dir.s);
1208 if (archive_entry_filetype(entry) != AE_IFDIR)
1209 mtree->current_dir.length = n;
1212 if (mtree->checkfs) {
1214 * Try to open and stat the file to get the real size
1215 * and other file info. It would be nice to avoid
1216 * this here so that getting a listing of an mtree
1217 * wouldn't require opening every referenced contents
1218 * file. But then we wouldn't know the actual
1219 * contents size, so I don't see a really viable way
1220 * around this. (Also, we may want to someday pull
1221 * other unspecified info from the contents file on
1225 if (archive_strlen(&mtree->contents_name) > 0)
1226 path = mtree->contents_name.s;
1228 path = archive_entry_pathname(entry);
1230 if (archive_entry_filetype(entry) == AE_IFREG ||
1231 archive_entry_filetype(entry) == AE_IFDIR) {
1232 mtree->fd = open(path, O_RDONLY | O_BINARY | O_CLOEXEC);
1233 __archive_ensure_cloexec_flag(mtree->fd);
1234 if (mtree->fd == -1 &&
1236 archive_strlen(&mtree->contents_name) > 0)) {
1237 archive_set_error(&a->archive, errno,
1238 "Can't open %s", path);
1244 if (mtree->fd >= 0) {
1245 if (fstat(mtree->fd, st) == -1) {
1246 archive_set_error(&a->archive, errno,
1247 "Could not fstat %s", path);
1249 /* If we can't stat it, don't keep it open. */
1254 } else if (lstat(path, st) == -1) {
1259 * Check for a mismatch between the type in the specification
1260 * and the type of the contents object on disk.
1263 if (((st->st_mode & S_IFMT) == S_IFREG &&
1264 archive_entry_filetype(entry) == AE_IFREG)
1266 ||((st->st_mode & S_IFMT) == S_IFLNK &&
1267 archive_entry_filetype(entry) == AE_IFLNK)
1270 ||((st->st_mode & S_IFSOCK) == S_IFSOCK &&
1271 archive_entry_filetype(entry) == AE_IFSOCK)
1274 ||((st->st_mode & S_IFMT) == S_IFCHR &&
1275 archive_entry_filetype(entry) == AE_IFCHR)
1278 ||((st->st_mode & S_IFMT) == S_IFBLK &&
1279 archive_entry_filetype(entry) == AE_IFBLK)
1281 ||((st->st_mode & S_IFMT) == S_IFDIR &&
1282 archive_entry_filetype(entry) == AE_IFDIR)
1284 ||((st->st_mode & S_IFMT) == S_IFIFO &&
1285 archive_entry_filetype(entry) == AE_IFIFO)
1290 /* Types don't match; bail out gracefully. */
1294 if (parsed_kws & MTREE_HAS_OPTIONAL) {
1295 /* It's not an error for an optional
1296 * entry to not match disk. */
1298 } else if (r == ARCHIVE_OK) {
1299 archive_set_error(&a->archive,
1301 "mtree specification has different"
1303 archive_entry_pathname(entry));
1311 * If there is a contents file on disk, pick some of the
1312 * metadata from that file. For most of these, we only
1313 * set it from the contents if it wasn't already parsed
1314 * from the specification.
1317 if (((parsed_kws & MTREE_HAS_DEVICE) == 0 ||
1318 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) &&
1319 (archive_entry_filetype(entry) == AE_IFCHR ||
1320 archive_entry_filetype(entry) == AE_IFBLK))
1321 archive_entry_set_rdev(entry, st->st_rdev);
1322 if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME))
1324 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1325 archive_entry_set_gid(entry, st->st_gid);
1326 if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME))
1328 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1329 archive_entry_set_uid(entry, st->st_uid);
1330 if ((parsed_kws & MTREE_HAS_MTIME) == 0 ||
1331 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) {
1332 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1333 archive_entry_set_mtime(entry, st->st_mtime,
1334 st->st_mtimespec.tv_nsec);
1335 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1336 archive_entry_set_mtime(entry, st->st_mtime,
1337 st->st_mtim.tv_nsec);
1338 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1339 archive_entry_set_mtime(entry, st->st_mtime,
1341 #elif HAVE_STRUCT_STAT_ST_UMTIME
1342 archive_entry_set_mtime(entry, st->st_mtime,
1343 st->st_umtime*1000);
1344 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
1345 archive_entry_set_mtime(entry, st->st_mtime,
1346 st->st_mtime_usec*1000);
1348 archive_entry_set_mtime(entry, st->st_mtime, 0);
1351 if ((parsed_kws & MTREE_HAS_NLINK) == 0 ||
1352 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1353 archive_entry_set_nlink(entry, st->st_nlink);
1354 if ((parsed_kws & MTREE_HAS_PERM) == 0 ||
1355 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1356 archive_entry_set_perm(entry, st->st_mode);
1357 if ((parsed_kws & MTREE_HAS_SIZE) == 0 ||
1358 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1359 archive_entry_set_size(entry, st->st_size);
1360 archive_entry_set_ino(entry, st->st_ino);
1361 archive_entry_set_dev(entry, st->st_dev);
1363 archive_entry_linkify(mtree->resolver, &entry,
1365 } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
1367 * Couldn't open the entry, stat it or the on-disk type
1368 * didn't match. If this entry is optional, just
1369 * ignore it and read the next header entry.
1376 mtree->cur_size = archive_entry_size(entry);
1383 * Each line contains a sequence of keywords.
1386 parse_line(struct archive_read *a, struct archive_entry *entry,
1387 struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
1389 struct mtree_option *iter;
1390 int r = ARCHIVE_OK, r1;
1392 for (iter = mp->options; iter != NULL; iter = iter->next) {
1393 r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
1397 if (r == ARCHIVE_OK && (*parsed_kws & MTREE_HAS_TYPE) == 0) {
1398 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1399 "Missing type keyword in mtree specification");
1400 return (ARCHIVE_WARN);
1406 * Device entries have one of the following forms:
1408 * - format,major,minor[,subdevice]
1409 * When parsing succeeded, `pdev' will contain the appropriate dev_t value.
1412 /* strsep() is not in C90, but strcspn() is. */
1413 /* Taken from http://unixpapa.com/incnote/string.html */
1415 la_strsep(char **sp, const char *sep)
1418 if (sp == NULL || *sp == NULL || **sp == '\0')
1421 p = s + strcspn(s, sep);
1429 parse_device(dev_t *pdev, struct archive *a, char *val)
1431 #define MAX_PACK_ARGS 3
1432 unsigned long numbers[MAX_PACK_ARGS];
1437 const char *error = NULL;
1439 memset(pdev, 0, sizeof(*pdev));
1440 if ((dev = strchr(val, ',')) != NULL) {
1442 * Device's major/minor are given in a specified format.
1443 * Decode and pack it accordingly.
1446 if ((pack = pack_find(val)) == NULL) {
1447 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1448 "Unknown format `%s'", val);
1449 return ARCHIVE_WARN;
1452 while ((p = la_strsep(&dev, ",")) != NULL) {
1454 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1456 return ARCHIVE_WARN;
1458 if (argc >= MAX_PACK_ARGS) {
1459 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1460 "Too many arguments");
1461 return ARCHIVE_WARN;
1463 numbers[argc++] = (unsigned long)mtree_atol(&p, 0);
1466 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1467 "Not enough arguments");
1468 return ARCHIVE_WARN;
1470 result = (*pack)(argc, numbers, &error);
1471 if (error != NULL) {
1472 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1474 return ARCHIVE_WARN;
1477 /* file system raw value. */
1478 result = (dev_t)mtree_atol(&val, 0);
1482 #undef MAX_PACK_ARGS
1486 * Parse a single keyword and its value.
1489 parse_keyword(struct archive_read *a, struct mtree *mtree,
1490 struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws)
1497 return (ARCHIVE_OK);
1499 if (strcmp(key, "nochange") == 0) {
1500 *parsed_kws |= MTREE_HAS_NOCHANGE;
1501 return (ARCHIVE_OK);
1503 if (strcmp(key, "optional") == 0) {
1504 *parsed_kws |= MTREE_HAS_OPTIONAL;
1505 return (ARCHIVE_OK);
1507 if (strcmp(key, "ignore") == 0) {
1509 * The mtree processing is not recursive, so
1510 * recursion will only happen for explicitly listed
1513 return (ARCHIVE_OK);
1516 val = strchr(key, '=');
1518 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1519 "Malformed attribute \"%s\" (%d)", key, key[0]);
1520 return (ARCHIVE_WARN);
1528 if (strcmp(key, "content") == 0
1529 || strcmp(key, "contents") == 0) {
1530 parse_escapes(val, NULL);
1531 archive_strcpy(&mtree->contents_name, val);
1534 if (strcmp(key, "cksum") == 0)
1538 if (strcmp(key, "device") == 0) {
1539 /* stat(2) st_rdev field, e.g. the major/minor IDs
1540 * of a char/block special file */
1544 *parsed_kws |= MTREE_HAS_DEVICE;
1545 r = parse_device(&dev, &a->archive, val);
1546 if (r == ARCHIVE_OK)
1547 archive_entry_set_rdev(entry, dev);
1552 if (strcmp(key, "flags") == 0) {
1553 *parsed_kws |= MTREE_HAS_FFLAGS;
1554 archive_entry_copy_fflags_text(entry, val);
1559 if (strcmp(key, "gid") == 0) {
1560 *parsed_kws |= MTREE_HAS_GID;
1561 archive_entry_set_gid(entry, mtree_atol(&val, 10));
1564 if (strcmp(key, "gname") == 0) {
1565 *parsed_kws |= MTREE_HAS_GNAME;
1566 archive_entry_copy_gname(entry, val);
1571 if (strcmp(key, "inode") == 0) {
1572 archive_entry_set_ino(entry, mtree_atol(&val, 10));
1577 if (strcmp(key, "link") == 0) {
1578 archive_entry_copy_symlink(entry, val);
1583 if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
1585 if (strcmp(key, "mode") == 0) {
1586 if (val[0] >= '0' && val[0] <= '7') {
1587 *parsed_kws |= MTREE_HAS_PERM;
1588 archive_entry_set_perm(entry,
1589 (mode_t)mtree_atol(&val, 8));
1591 archive_set_error(&a->archive,
1592 ARCHIVE_ERRNO_FILE_FORMAT,
1593 "Symbolic or non-octal mode \"%s\" unsupported", val);
1594 return ARCHIVE_WARN;
1600 if (strcmp(key, "nlink") == 0) {
1601 *parsed_kws |= MTREE_HAS_NLINK;
1602 archive_entry_set_nlink(entry,
1603 (unsigned int)mtree_atol(&val, 10));
1608 if (strcmp(key, "resdevice") == 0) {
1609 /* stat(2) st_dev field, e.g. the device ID where the
1614 r = parse_device(&dev, &a->archive, val);
1615 if (r == ARCHIVE_OK)
1616 archive_entry_set_dev(entry, dev);
1619 if (strcmp(key, "rmd160") == 0 ||
1620 strcmp(key, "rmd160digest") == 0)
1624 if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
1626 if (strcmp(key, "sha256") == 0 ||
1627 strcmp(key, "sha256digest") == 0)
1629 if (strcmp(key, "sha384") == 0 ||
1630 strcmp(key, "sha384digest") == 0)
1632 if (strcmp(key, "sha512") == 0 ||
1633 strcmp(key, "sha512digest") == 0)
1635 if (strcmp(key, "size") == 0) {
1636 archive_entry_set_size(entry, mtree_atol(&val, 10));
1641 if (strcmp(key, "tags") == 0) {
1643 * Comma delimited list of tags.
1644 * Ignore the tags for now, but the interface
1645 * should be extended to allow inclusion/exclusion.
1649 if (strcmp(key, "time") == 0) {
1651 int64_t my_time_t_max = get_time_t_max();
1652 int64_t my_time_t_min = get_time_t_min();
1655 *parsed_kws |= MTREE_HAS_MTIME;
1656 m = mtree_atol(&val, 10);
1657 /* Replicate an old mtree bug:
1658 * 123456789.1 represents 123456789
1659 * seconds and 1 nanosecond. */
1662 ns = (long)mtree_atol(&val, 10);
1665 else if (ns > 999999999)
1668 if (m > my_time_t_max)
1670 else if (m < my_time_t_min)
1672 archive_entry_set_mtime(entry, (time_t)m, ns);
1675 if (strcmp(key, "type") == 0) {
1678 if (strcmp(val, "block") == 0) {
1679 archive_entry_set_filetype(entry, AE_IFBLK);
1684 if (strcmp(val, "char") == 0) {
1685 archive_entry_set_filetype(entry,
1691 if (strcmp(val, "dir") == 0) {
1692 archive_entry_set_filetype(entry,
1698 if (strcmp(val, "fifo") == 0) {
1699 archive_entry_set_filetype(entry,
1703 if (strcmp(val, "file") == 0) {
1704 archive_entry_set_filetype(entry,
1710 if (strcmp(val, "link") == 0) {
1711 archive_entry_set_filetype(entry,
1717 archive_set_error(&a->archive,
1718 ARCHIVE_ERRNO_FILE_FORMAT,
1719 "Unrecognized file type \"%s\"; "
1720 "assuming \"file\"", val);
1721 archive_entry_set_filetype(entry, AE_IFREG);
1722 return (ARCHIVE_WARN);
1724 *parsed_kws |= MTREE_HAS_TYPE;
1729 if (strcmp(key, "uid") == 0) {
1730 *parsed_kws |= MTREE_HAS_UID;
1731 archive_entry_set_uid(entry, mtree_atol(&val, 10));
1734 if (strcmp(key, "uname") == 0) {
1735 *parsed_kws |= MTREE_HAS_UNAME;
1736 archive_entry_copy_uname(entry, val);
1741 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1742 "Unrecognized key %s=%s", key, val);
1743 return (ARCHIVE_WARN);
1745 return (ARCHIVE_OK);
1749 read_data(struct archive_read *a, const void **buff, size_t *size,
1752 size_t bytes_to_read;
1754 struct mtree *mtree;
1756 mtree = (struct mtree *)(a->format->data);
1757 if (mtree->fd < 0) {
1761 return (ARCHIVE_EOF);
1763 if (mtree->buff == NULL) {
1764 mtree->buffsize = 64 * 1024;
1765 mtree->buff = malloc(mtree->buffsize);
1766 if (mtree->buff == NULL) {
1767 archive_set_error(&a->archive, ENOMEM,
1768 "Can't allocate memory");
1769 return (ARCHIVE_FATAL);
1773 *buff = mtree->buff;
1774 *offset = mtree->offset;
1775 if ((int64_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1776 bytes_to_read = (size_t)(mtree->cur_size - mtree->offset);
1778 bytes_to_read = mtree->buffsize;
1779 bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1780 if (bytes_read < 0) {
1781 archive_set_error(&a->archive, errno, "Can't read");
1782 return (ARCHIVE_WARN);
1784 if (bytes_read == 0) {
1786 return (ARCHIVE_EOF);
1788 mtree->offset += bytes_read;
1790 return (ARCHIVE_OK);
1793 /* Skip does nothing except possibly close the contents file. */
1795 skip(struct archive_read *a)
1797 struct mtree *mtree;
1799 mtree = (struct mtree *)(a->format->data);
1800 if (mtree->fd >= 0) {
1804 return (ARCHIVE_OK);
1808 * Since parsing backslash sequences always makes strings shorter,
1809 * we can always do this conversion in-place.
1812 parse_escapes(char *src, struct mtree_entry *mentry)
1817 if (mentry != NULL && strcmp(src, ".") == 0)
1820 while (*src != '\0') {
1822 if (c == '/' && mentry != NULL)
1827 if (src[1] < '0' || src[1] > '7') {
1836 if (src[1] >= '0' && src[1] <= '7' &&
1837 src[2] >= '0' && src[2] <= '7') {
1838 c = (src[0] - '0') << 6;
1839 c |= (src[1] - '0') << 3;
1840 c |= (src[2] - '0');
1887 /* Parse a hex digit. */
1891 if (c >= '0' && c <= '9')
1893 else if (c >= 'a' && c <= 'f')
1895 else if (c >= 'A' && c <= 'F')
1902 * Note that this implementation does not (and should not!) obey
1903 * locale settings; you cannot simply substitute strtol here, since
1904 * it does obey locale.
1907 mtree_atol(char **p, int base)
1910 int digit, last_digit_limit;
1915 else if ((*p)[1] == 'x' || (*p)[1] == 'X') {
1924 limit = INT64_MIN / base;
1925 last_digit_limit = INT64_MIN % base;
1929 digit = parsedigit(**p);
1930 while (digit >= 0 && digit < base) {
1931 if (l < limit || (l == limit && digit > last_digit_limit))
1933 l = (l * base) - digit;
1934 digit = parsedigit(*++(*p));
1938 limit = INT64_MAX / base;
1939 last_digit_limit = INT64_MAX % base;
1942 digit = parsedigit(**p);
1943 while (digit >= 0 && digit < base) {
1944 if (l > limit || (l == limit && digit > last_digit_limit))
1946 l = (l * base) + digit;
1947 digit = parsedigit(*++(*p));
1954 * Returns length of line (including trailing newline)
1955 * or negative on error. 'start' argument is updated to
1956 * point to first character of line.
1959 readline(struct archive_read *a, struct mtree *mtree, char **start,
1963 ssize_t total_size = 0;
1964 ssize_t find_off = 0;
1969 /* Accumulate line in a line buffer. */
1971 /* Read some more. */
1972 t = __archive_read_ahead(a, 1, &bytes_read);
1976 return (ARCHIVE_FATAL);
1977 nl = memchr(t, '\n', bytes_read);
1978 /* If we found '\n', trim the read to end exactly there. */
1980 bytes_read = ((const char *)nl) - ((const char *)t) + 1;
1982 if (total_size + bytes_read + 1 > limit) {
1983 archive_set_error(&a->archive,
1984 ARCHIVE_ERRNO_FILE_FORMAT,
1986 return (ARCHIVE_FATAL);
1988 if (archive_string_ensure(&mtree->line,
1989 total_size + bytes_read + 1) == NULL) {
1990 archive_set_error(&a->archive, ENOMEM,
1991 "Can't allocate working buffer");
1992 return (ARCHIVE_FATAL);
1994 /* Append new bytes to string. */
1995 memcpy(mtree->line.s + total_size, t, bytes_read);
1996 __archive_read_consume(a, bytes_read);
1997 total_size += bytes_read;
1998 mtree->line.s[total_size] = '\0';
2000 for (u = mtree->line.s + find_off; *u; ++u) {
2002 /* Ends with unescaped newline. */
2003 *start = mtree->line.s;
2005 } else if (u[0] == '#') {
2006 /* Ends with comment sequence #...\n */
2008 /* But we've not found the \n yet */
2011 } else if (u[0] == '\\') {
2013 /* Trim escaped newline. */
2015 mtree->line.s[total_size] = '\0';
2017 } else if (u[1] != '\0') {
2018 /* Skip the two-char escape sequence */
2023 find_off = u - mtree->line.s;