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$");
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);
263 __archive_rb_tree_init(&mtree->rbtree, &rb_ops);
265 r = __archive_read_register_format(a, mtree, "mtree",
266 mtree_bid, archive_read_format_mtree_options, read_header, read_data, skip, NULL, cleanup, NULL, NULL);
274 cleanup(struct archive_read *a)
277 struct mtree_entry *p, *q;
279 mtree = (struct mtree *)(a->format->data);
285 free_options(p->options);
289 archive_string_free(&mtree->line);
290 archive_string_free(&mtree->current_dir);
291 archive_string_free(&mtree->contents_name);
292 archive_entry_linkresolver_free(mtree->resolver);
296 (a->format->data) = NULL;
301 get_line_size(const char *b, ssize_t avail, ssize_t *nlsize)
306 while (len < avail) {
308 case '\0':/* Non-ascii character or control character. */
313 if (avail-len > 1 && b[1] == '\n') {
335 * <---------------- ravail --------------------->
336 * <-- diff ------> <--- avail ----------------->
337 * <---- len ----------->
338 * | Previous lines | line being parsed nl extra |
344 next_line(struct archive_read *a,
345 const char **b, ssize_t *avail, ssize_t *ravail, ssize_t *nl)
355 len = get_line_size(*b, *avail, nl);
357 * Read bytes more while it does not reach the end of line.
359 while (*nl == 0 && len == *avail && !quit) {
360 ssize_t diff = *ravail - *avail;
361 size_t nbytes_req = (*ravail+1023) & ~1023U;
365 * Place an arbitrary limit on the line length.
366 * mtree is almost free-form input and without line length limits,
367 * it can consume a lot of memory.
369 if (len >= MAX_LINE_LEN)
372 /* Increase reading bytes if it is not enough to at least
374 if (nbytes_req < (size_t)*ravail + 160)
377 *b = __archive_read_ahead(a, nbytes_req, avail);
379 if (*ravail >= *avail)
381 /* Reading bytes reaches the end of file. */
382 *b = __archive_read_ahead(a, *avail, avail);
388 tested = len;/* Skip some bytes we already determinated. */
389 len = get_line_size(*b + len, *avail - len, nl);
397 * Compare characters with a mtree keyword.
398 * Returns the length of a mtree keyword if matched.
399 * Returns 0 if not matched.
402 bid_keycmp(const char *p, const char *key, ssize_t len)
406 while (len > 0 && *p && *key) {
414 return (0);/* Not match */
417 return (0);/* Not match */
419 /* A following character should be specified characters */
420 if (p[0] == '=' || p[0] == ' ' || p[0] == '\t' ||
421 p[0] == '\n' || p[0] == '\r' ||
422 (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r')))
424 return (0);/* Not match */
428 * Test whether the characters 'p' has is mtree keyword.
429 * Returns the length of a detected keyword.
430 * Returns 0 if any keywords were not found.
433 bid_keyword(const char *p, ssize_t len)
435 static const char * const keys_c[] = {
436 "content", "contents", "cksum", NULL
438 static const char * const keys_df[] = {
439 "device", "flags", NULL
441 static const char * const keys_g[] = {
444 static const char * const keys_il[] = {
445 "ignore", "inode", "link", NULL
447 static const char * const keys_m[] = {
448 "md5", "md5digest", "mode", NULL
450 static const char * const keys_no[] = {
451 "nlink", "nochange", "optional", NULL
453 static const char * const keys_r[] = {
454 "resdevice", "rmd160", "rmd160digest", NULL
456 static const char * const keys_s[] = {
457 "sha1", "sha1digest",
458 "sha256", "sha256digest",
459 "sha384", "sha384digest",
460 "sha512", "sha512digest",
463 static const char * const keys_t[] = {
464 "tags", "time", "type", NULL
466 static const char * const keys_u[] = {
469 const char * const *keys;
473 case 'c': keys = keys_c; break;
474 case 'd': case 'f': keys = keys_df; break;
475 case 'g': keys = keys_g; break;
476 case 'i': case 'l': keys = keys_il; break;
477 case 'm': keys = keys_m; break;
478 case 'n': case 'o': keys = keys_no; break;
479 case 'r': keys = keys_r; break;
480 case 's': keys = keys_s; break;
481 case 't': keys = keys_t; break;
482 case 'u': keys = keys_u; break;
483 default: return (0);/* Unknown key */
486 for (i = 0; keys[i] != NULL; i++) {
487 int l = bid_keycmp(p, keys[i], len);
491 return (0);/* Unknown key */
495 * Test whether there is a set of mtree keywords.
496 * Returns the number of keyword.
497 * Returns -1 if we got incorrect sequence.
498 * This function expects a set of "<space characters>keyword=value".
499 * When "unset" is specified, expects a set of "<space characters>keyword".
502 bid_keyword_list(const char *p, ssize_t len, int unset, int last_is_path)
507 while (len > 0 && *p) {
510 /* Test whether there are blank characters in the line. */
511 while (len >0 && (*p == ' ' || *p == '\t')) {
516 if (*p == '\n' || *p == '\r')
518 if (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r'))
520 if (!blank && !last_is_path) /* No blank character. */
522 if (last_is_path && len == 0)
526 l = bid_keycmp(p, "all", len);
530 /* Test whether there is a correct key in the line. */
531 l = bid_keyword(p, len);
533 return (-1);/* Unknown keyword was found. */
543 while (len > 0 && *p != ' ' && *p != '\t') {
548 /* A keyword should have a its value unless
549 * "/unset" operation. */
550 if (!unset && value == 0)
558 bid_entry(const char *p, ssize_t len, ssize_t nl, int *last_is_path)
561 static const unsigned char safe_char[256] = {
562 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
563 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
564 /* !"$%&'()*+,-./ EXCLUSION:( )(#) */
565 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
566 /* 0123456789:;<>? EXCLUSION:(=) */
567 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
568 /* @ABCDEFGHIJKLMNO */
569 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
570 /* PQRSTUVWXYZ[\]^_ */
571 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
572 /* `abcdefghijklmno */
573 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
574 /* pqrstuvwxyz{|}~ */
575 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
576 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
577 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
578 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
579 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
582 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
583 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
587 const char * const pp_end = pp + len;
591 * Skip the path-name which is quoted.
593 for (;pp < pp_end; ++pp) {
594 if (!safe_char[*(const unsigned char *)pp]) {
595 if (*pp != ' ' && *pp != '\t' && *pp != '\r'
604 /* If a path-name was not found at the first, try to check
605 * a mtree format(a.k.a form D) ``NetBSD's mtree -D'' creates,
606 * which places the path-name at the last. */
608 const char *pb = p + len - nl;
612 /* The form D accepts only a single line for an entry. */
614 pb[-1] == '\\' && (pb[-2] == ' ' || pb[-2] == '\t'))
616 if (pb-1 >= p && pb[-1] == '\\')
620 while (p <= --pb && *pb != ' ' && *pb != '\t') {
621 if (!safe_char[*(const unsigned char *)pb])
624 /* The pathname should have a slash in this
629 if (name_len == 0 || slash == 0)
631 /* If '/' is placed at the first in this field, this is not
632 * a valid filename. */
635 ll = len - nl - name_len;
640 return (bid_keyword_list(pp, ll, 0, *last_is_path));
643 #define MAX_BID_ENTRY 3
646 mtree_bid(struct archive_read *a, int best_bid)
648 const char *signature = "#mtree";
651 (void)best_bid; /* UNUSED */
653 /* Now let's look at the actual header and see if it matches. */
654 p = __archive_read_ahead(a, strlen(signature), NULL);
658 if (memcmp(p, signature, strlen(signature)) == 0)
659 return (8 * (int)strlen(signature));
662 * There is not a mtree signature. Let's try to detect mtree format.
664 return (detect_form(a, NULL));
668 detect_form(struct archive_read *a, int *is_form_d)
671 ssize_t avail, ravail;
672 ssize_t detected_bytes = 0, len, nl;
673 int entry_cnt = 0, multiline = 0;
674 int form_D = 0;/* The archive is generated by `NetBSD mtree -D'
675 * (In this source we call it `form D') . */
677 if (is_form_d != NULL)
679 p = __archive_read_ahead(a, 1, &avail);
684 len = next_line(a, &p, &avail, &ravail, &nl);
685 /* The terminal character of the line should be
686 * a new line character, '\r\n' or '\n'. */
687 if (len <= 0 || nl == 0)
690 /* Leading whitespace is never significant,
692 while (len > 0 && (*p == ' ' || *p == '\t')) {
697 /* Skip comment or empty line. */
698 if (p[0] == '#' || p[0] == '\n' || p[0] == '\r') {
704 /* A continuance line; the terminal
705 * character of previous line was '\' character. */
706 if (bid_keyword_list(p, len, 0, 0) <= 0)
709 detected_bytes += len;
710 if (p[len-nl-1] != '\\') {
711 if (multiline == 1 &&
712 ++entry_cnt >= MAX_BID_ENTRY)
721 int last_is_path, keywords;
723 keywords = bid_entry(p, len, nl, &last_is_path);
725 detected_bytes += len;
729 else if (keywords > 0)
730 /* This line is not `form D'. */
732 } else if (form_D == 1) {
733 if (!last_is_path && keywords > 0)
734 /* This this is not `form D'
735 * and We cannot accept mixed
739 if (!last_is_path && p[len-nl-1] == '\\')
740 /* This line continues. */
743 /* We've got plenty of correct lines
744 * to assume that this file is a mtree
746 if (++entry_cnt >= MAX_BID_ENTRY)
751 } else if (len > 4 && strncmp(p, "/set", 4) == 0) {
752 if (bid_keyword_list(p+4, len-4, 0, 0) <= 0)
754 /* This line continues. */
755 if (p[len-nl-1] == '\\')
757 } else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
758 if (bid_keyword_list(p+6, len-6, 1, 0) <= 0)
760 /* This line continues. */
761 if (p[len-nl-1] == '\\')
766 /* Test next line. */
770 if (entry_cnt >= MAX_BID_ENTRY || (entry_cnt > 0 && len == 0)) {
771 if (is_form_d != NULL) {
782 * The extended mtree format permits multiple lines specifying
783 * attributes for each file. For those entries, only the last line
784 * is actually used. Practically speaking, that means we have
785 * to read the entire mtree file into memory up front.
787 * The parsing is done in two steps. First, it is decided if a line
788 * changes the global defaults and if it is, processed accordingly.
789 * Otherwise, the options of the line are merged with the current
793 add_option(struct archive_read *a, struct mtree_option **global,
794 const char *value, size_t len)
796 struct mtree_option *opt;
798 if ((opt = malloc(sizeof(*opt))) == NULL) {
799 archive_set_error(&a->archive, errno, "Can't allocate memory");
800 return (ARCHIVE_FATAL);
802 if ((opt->value = malloc(len + 1)) == NULL) {
804 archive_set_error(&a->archive, errno, "Can't allocate memory");
805 return (ARCHIVE_FATAL);
807 memcpy(opt->value, value, len);
808 opt->value[len] = '\0';
815 remove_option(struct mtree_option **global, const char *value, size_t len)
817 struct mtree_option *iter, *last;
820 for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
821 if (strncmp(iter->value, value, len) == 0 &&
822 (iter->value[len] == '\0' ||
823 iter->value[len] == '='))
829 *global = iter->next;
831 last->next = iter->next;
838 process_global_set(struct archive_read *a,
839 struct mtree_option **global, const char *line)
841 const char *next, *eq;
847 next = line + strspn(line, " \t\r\n");
851 next = line + strcspn(line, " \t\r\n");
852 eq = strchr(line, '=');
858 remove_option(global, line, len);
859 r = add_option(a, global, line, next - line);
867 process_global_unset(struct archive_read *a,
868 struct mtree_option **global, const char *line)
874 if (strchr(line, '=') != NULL) {
875 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
876 "/unset shall not contain `='");
877 return ARCHIVE_FATAL;
881 next = line + strspn(line, " \t\r\n");
885 len = strcspn(line, " \t\r\n");
887 if (len == 3 && strncmp(line, "all", 3) == 0) {
888 free_options(*global);
891 remove_option(global, line, len);
899 process_add_entry(struct archive_read *a, struct mtree *mtree,
900 struct mtree_option **global, const char *line, ssize_t line_len,
901 struct mtree_entry **last_entry, int is_form_d)
903 struct mtree_entry *entry;
904 struct mtree_option *iter;
905 const char *next, *eq, *name, *end;
906 size_t name_len, len;
909 if ((entry = malloc(sizeof(*entry))) == NULL) {
910 archive_set_error(&a->archive, errno, "Can't allocate memory");
911 return (ARCHIVE_FATAL);
914 entry->options = NULL;
919 /* Add this entry to list. */
920 if (*last_entry == NULL)
921 mtree->entries = entry;
923 (*last_entry)->next = entry;
927 /* Filename is last item on line. */
928 /* Adjust line_len to trim trailing whitespace */
929 while (line_len > 0) {
930 char last_character = line[line_len - 1];
931 if (last_character == '\r'
932 || last_character == '\n'
933 || last_character == '\t'
934 || last_character == ' ') {
940 /* Name starts after the last whitespace separator */
942 for (i = 0; i < line_len; i++) {
950 name_len = line + line_len - name;
953 /* Filename is first item on line */
954 name_len = strcspn(line, " \t\r\n");
957 end = line + line_len;
959 /* name/name_len is the name within the line. */
960 /* line..end brackets the entire line except the name */
962 if ((entry->name = malloc(name_len + 1)) == NULL) {
963 archive_set_error(&a->archive, errno, "Can't allocate memory");
964 return (ARCHIVE_FATAL);
967 memcpy(entry->name, name, name_len);
968 entry->name[name_len] = '\0';
969 parse_escapes(entry->name, entry);
971 entry->next_dup = NULL;
973 if (!__archive_rb_tree_insert_node(&mtree->rbtree, &entry->rbnode)) {
974 struct mtree_entry *alt;
975 alt = (struct mtree_entry *)__archive_rb_tree_find_node(
976 &mtree->rbtree, entry->name);
977 while (alt->next_dup)
979 alt->next_dup = entry;
983 for (iter = *global; iter != NULL; iter = iter->next) {
984 r = add_option(a, &entry->options, iter->value,
985 strlen(iter->value));
991 next = line + strspn(line, " \t\r\n");
997 next = line + strcspn(line, " \t\r\n");
998 eq = strchr(line, '=');
999 if (eq == NULL || eq > next)
1004 remove_option(&entry->options, line, len);
1005 r = add_option(a, &entry->options, line, next - line);
1006 if (r != ARCHIVE_OK)
1013 read_mtree(struct archive_read *a, struct mtree *mtree)
1018 struct mtree_option *global;
1019 struct mtree_entry *last_entry;
1022 mtree->archive_format = ARCHIVE_FORMAT_MTREE;
1023 mtree->archive_format_name = "mtree";
1028 (void)detect_form(a, &is_form_d);
1030 for (counter = 1; ; ++counter) {
1032 len = readline(a, mtree, &p, 65536);
1034 mtree->this_entry = mtree->entries;
1035 free_options(global);
1036 return (ARCHIVE_OK);
1039 free_options(global);
1042 /* Leading whitespace is never significant, ignore it. */
1043 while (*p == ' ' || *p == '\t') {
1047 /* Skip content lines and blank lines. */
1050 if (*p == '\r' || *p == '\n' || *p == '\0')
1052 /* Non-printable characters are not allowed */
1053 for (s = p;s < p + len - 1; s++) {
1059 if (r != ARCHIVE_OK)
1062 r = process_add_entry(a, mtree, &global, p, len,
1063 &last_entry, is_form_d);
1064 } else if (len > 4 && strncmp(p, "/set", 4) == 0) {
1065 if (p[4] != ' ' && p[4] != '\t')
1067 r = process_global_set(a, &global, p);
1068 } else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
1069 if (p[6] != ' ' && p[6] != '\t')
1071 r = process_global_unset(a, &global, p);
1075 if (r != ARCHIVE_OK) {
1076 free_options(global);
1081 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1082 "Can't parse line %ju", counter);
1083 free_options(global);
1084 return (ARCHIVE_FATAL);
1088 * Read in the entire mtree file into memory on the first request.
1089 * Then use the next unused file to satisfy each header request.
1092 read_header(struct archive_read *a, struct archive_entry *entry)
1094 struct mtree *mtree;
1098 mtree = (struct mtree *)(a->format->data);
1100 if (mtree->fd >= 0) {
1105 if (mtree->entries == NULL) {
1106 mtree->resolver = archive_entry_linkresolver_new();
1107 if (mtree->resolver == NULL)
1108 return ARCHIVE_FATAL;
1109 archive_entry_linkresolver_set_strategy(mtree->resolver,
1110 ARCHIVE_FORMAT_MTREE);
1111 r = read_mtree(a, mtree);
1112 if (r != ARCHIVE_OK)
1116 a->archive.archive_format = mtree->archive_format;
1117 a->archive.archive_format_name = mtree->archive_format_name;
1120 if (mtree->this_entry == NULL)
1121 return (ARCHIVE_EOF);
1122 if (strcmp(mtree->this_entry->name, "..") == 0) {
1123 mtree->this_entry->used = 1;
1124 if (archive_strlen(&mtree->current_dir) > 0) {
1125 /* Roll back current path. */
1126 p = mtree->current_dir.s
1127 + mtree->current_dir.length - 1;
1128 while (p >= mtree->current_dir.s && *p != '/')
1130 if (p >= mtree->current_dir.s)
1132 mtree->current_dir.length
1133 = p - mtree->current_dir.s + 1;
1136 if (!mtree->this_entry->used) {
1138 r = parse_file(a, entry, mtree, mtree->this_entry,
1143 mtree->this_entry = mtree->this_entry->next;
1148 * A single file can have multiple lines contribute specifications.
1149 * Parse as many lines as necessary, then pull additional information
1150 * from a backing file on disk as necessary.
1153 parse_file(struct archive_read *a, struct archive_entry *entry,
1154 struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
1157 struct stat st_storage, *st;
1158 struct mtree_entry *mp;
1159 struct archive_entry *sparse_entry;
1160 int r = ARCHIVE_OK, r1, parsed_kws;
1164 /* Initialize reasonable defaults. */
1165 archive_entry_set_filetype(entry, AE_IFREG);
1166 archive_entry_set_size(entry, 0);
1167 archive_string_empty(&mtree->contents_name);
1169 /* Parse options from this line. */
1171 r = parse_line(a, entry, mtree, mentry, &parsed_kws);
1174 archive_entry_copy_pathname(entry, mentry->name);
1176 * "Full" entries are allowed to have multiple lines
1177 * and those lines aren't required to be adjacent. We
1178 * don't support multiple lines for "relative" entries
1179 * nor do we make any attempt to merge data from
1180 * separate "relative" and "full" entries. (Merging
1181 * "relative" and "full" entries would require dealing
1182 * with pathname canonicalization, which is a very
1185 mp = (struct mtree_entry *)__archive_rb_tree_find_node(
1186 &mtree->rbtree, mentry->name);
1187 for (; mp; mp = mp->next_dup) {
1188 if (mp->full && !mp->used) {
1189 /* Later lines override earlier ones. */
1191 r1 = parse_line(a, entry, mtree, mp, &parsed_kws);
1198 * Relative entries require us to construct
1199 * the full path and possibly update the
1200 * current directory.
1202 size_t n = archive_strlen(&mtree->current_dir);
1204 archive_strcat(&mtree->current_dir, "/");
1205 archive_strcat(&mtree->current_dir, mentry->name);
1206 archive_entry_copy_pathname(entry, mtree->current_dir.s);
1207 if (archive_entry_filetype(entry) != AE_IFDIR)
1208 mtree->current_dir.length = n;
1211 if (mtree->checkfs) {
1213 * Try to open and stat the file to get the real size
1214 * and other file info. It would be nice to avoid
1215 * this here so that getting a listing of an mtree
1216 * wouldn't require opening every referenced contents
1217 * file. But then we wouldn't know the actual
1218 * contents size, so I don't see a really viable way
1219 * around this. (Also, we may want to someday pull
1220 * other unspecified info from the contents file on
1224 if (archive_strlen(&mtree->contents_name) > 0)
1225 path = mtree->contents_name.s;
1227 path = archive_entry_pathname(entry);
1229 if (archive_entry_filetype(entry) == AE_IFREG ||
1230 archive_entry_filetype(entry) == AE_IFDIR) {
1231 mtree->fd = open(path, O_RDONLY | O_BINARY | O_CLOEXEC);
1232 __archive_ensure_cloexec_flag(mtree->fd);
1233 if (mtree->fd == -1 &&
1235 archive_strlen(&mtree->contents_name) > 0)) {
1236 archive_set_error(&a->archive, errno,
1237 "Can't open %s", path);
1243 if (mtree->fd >= 0) {
1244 if (fstat(mtree->fd, st) == -1) {
1245 archive_set_error(&a->archive, errno,
1246 "Could not fstat %s", path);
1248 /* If we can't stat it, don't keep it open. */
1253 } else if (lstat(path, st) == -1) {
1258 * Check for a mismatch between the type in the specification
1259 * and the type of the contents object on disk.
1262 if (((st->st_mode & S_IFMT) == S_IFREG &&
1263 archive_entry_filetype(entry) == AE_IFREG)
1265 ||((st->st_mode & S_IFMT) == S_IFLNK &&
1266 archive_entry_filetype(entry) == AE_IFLNK)
1269 ||((st->st_mode & S_IFSOCK) == S_IFSOCK &&
1270 archive_entry_filetype(entry) == AE_IFSOCK)
1273 ||((st->st_mode & S_IFMT) == S_IFCHR &&
1274 archive_entry_filetype(entry) == AE_IFCHR)
1277 ||((st->st_mode & S_IFMT) == S_IFBLK &&
1278 archive_entry_filetype(entry) == AE_IFBLK)
1280 ||((st->st_mode & S_IFMT) == S_IFDIR &&
1281 archive_entry_filetype(entry) == AE_IFDIR)
1283 ||((st->st_mode & S_IFMT) == S_IFIFO &&
1284 archive_entry_filetype(entry) == AE_IFIFO)
1289 /* Types don't match; bail out gracefully. */
1293 if (parsed_kws & MTREE_HAS_OPTIONAL) {
1294 /* It's not an error for an optional
1295 * entry to not match disk. */
1297 } else if (r == ARCHIVE_OK) {
1298 archive_set_error(&a->archive,
1300 "mtree specification has different"
1302 archive_entry_pathname(entry));
1310 * If there is a contents file on disk, pick some of the
1311 * metadata from that file. For most of these, we only
1312 * set it from the contents if it wasn't already parsed
1313 * from the specification.
1316 if (((parsed_kws & MTREE_HAS_DEVICE) == 0 ||
1317 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) &&
1318 (archive_entry_filetype(entry) == AE_IFCHR ||
1319 archive_entry_filetype(entry) == AE_IFBLK))
1320 archive_entry_set_rdev(entry, st->st_rdev);
1321 if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME))
1323 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1324 archive_entry_set_gid(entry, st->st_gid);
1325 if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME))
1327 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1328 archive_entry_set_uid(entry, st->st_uid);
1329 if ((parsed_kws & MTREE_HAS_MTIME) == 0 ||
1330 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) {
1331 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1332 archive_entry_set_mtime(entry, st->st_mtime,
1333 st->st_mtimespec.tv_nsec);
1334 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1335 archive_entry_set_mtime(entry, st->st_mtime,
1336 st->st_mtim.tv_nsec);
1337 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1338 archive_entry_set_mtime(entry, st->st_mtime,
1340 #elif HAVE_STRUCT_STAT_ST_UMTIME
1341 archive_entry_set_mtime(entry, st->st_mtime,
1342 st->st_umtime*1000);
1343 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
1344 archive_entry_set_mtime(entry, st->st_mtime,
1345 st->st_mtime_usec*1000);
1347 archive_entry_set_mtime(entry, st->st_mtime, 0);
1350 if ((parsed_kws & MTREE_HAS_NLINK) == 0 ||
1351 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1352 archive_entry_set_nlink(entry, st->st_nlink);
1353 if ((parsed_kws & MTREE_HAS_PERM) == 0 ||
1354 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1355 archive_entry_set_perm(entry, st->st_mode);
1356 if ((parsed_kws & MTREE_HAS_SIZE) == 0 ||
1357 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1358 archive_entry_set_size(entry, st->st_size);
1359 archive_entry_set_ino(entry, st->st_ino);
1360 archive_entry_set_dev(entry, st->st_dev);
1362 archive_entry_linkify(mtree->resolver, &entry,
1364 } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
1366 * Couldn't open the entry, stat it or the on-disk type
1367 * didn't match. If this entry is optional, just
1368 * ignore it and read the next header entry.
1375 mtree->cur_size = archive_entry_size(entry);
1382 * Each line contains a sequence of keywords.
1385 parse_line(struct archive_read *a, struct archive_entry *entry,
1386 struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
1388 struct mtree_option *iter;
1389 int r = ARCHIVE_OK, r1;
1391 for (iter = mp->options; iter != NULL; iter = iter->next) {
1392 r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
1396 if (r == ARCHIVE_OK && (*parsed_kws & MTREE_HAS_TYPE) == 0) {
1397 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1398 "Missing type keyword in mtree specification");
1399 return (ARCHIVE_WARN);
1405 * Device entries have one of the following forms:
1407 * - format,major,minor[,subdevice]
1408 * When parsing succeeded, `pdev' will contain the appropriate dev_t value.
1411 /* strsep() is not in C90, but strcspn() is. */
1412 /* Taken from http://unixpapa.com/incnote/string.html */
1414 la_strsep(char **sp, const char *sep)
1417 if (sp == NULL || *sp == NULL || **sp == '\0')
1420 p = s + strcspn(s, sep);
1428 parse_device(dev_t *pdev, struct archive *a, char *val)
1430 #define MAX_PACK_ARGS 3
1431 unsigned long numbers[MAX_PACK_ARGS];
1436 const char *error = NULL;
1438 memset(pdev, 0, sizeof(*pdev));
1439 if ((dev = strchr(val, ',')) != NULL) {
1441 * Device's major/minor are given in a specified format.
1442 * Decode and pack it accordingly.
1445 if ((pack = pack_find(val)) == NULL) {
1446 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1447 "Unknown format `%s'", val);
1448 return ARCHIVE_WARN;
1451 while ((p = la_strsep(&dev, ",")) != NULL) {
1453 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1455 return ARCHIVE_WARN;
1457 if (argc >= MAX_PACK_ARGS) {
1458 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1459 "Too many arguments");
1460 return ARCHIVE_WARN;
1462 numbers[argc++] = (unsigned long)mtree_atol(&p, 0);
1465 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1466 "Not enough arguments");
1467 return ARCHIVE_WARN;
1469 result = (*pack)(argc, numbers, &error);
1470 if (error != NULL) {
1471 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1473 return ARCHIVE_WARN;
1476 /* file system raw value. */
1477 result = (dev_t)mtree_atol(&val, 0);
1481 #undef MAX_PACK_ARGS
1485 * Parse a single keyword and its value.
1488 parse_keyword(struct archive_read *a, struct mtree *mtree,
1489 struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws)
1496 return (ARCHIVE_OK);
1498 if (strcmp(key, "nochange") == 0) {
1499 *parsed_kws |= MTREE_HAS_NOCHANGE;
1500 return (ARCHIVE_OK);
1502 if (strcmp(key, "optional") == 0) {
1503 *parsed_kws |= MTREE_HAS_OPTIONAL;
1504 return (ARCHIVE_OK);
1506 if (strcmp(key, "ignore") == 0) {
1508 * The mtree processing is not recursive, so
1509 * recursion will only happen for explicitly listed
1512 return (ARCHIVE_OK);
1515 val = strchr(key, '=');
1517 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1518 "Malformed attribute \"%s\" (%d)", key, key[0]);
1519 return (ARCHIVE_WARN);
1527 if (strcmp(key, "content") == 0
1528 || strcmp(key, "contents") == 0) {
1529 parse_escapes(val, NULL);
1530 archive_strcpy(&mtree->contents_name, val);
1533 if (strcmp(key, "cksum") == 0)
1537 if (strcmp(key, "device") == 0) {
1538 /* stat(2) st_rdev field, e.g. the major/minor IDs
1539 * of a char/block special file */
1543 *parsed_kws |= MTREE_HAS_DEVICE;
1544 r = parse_device(&dev, &a->archive, val);
1545 if (r == ARCHIVE_OK)
1546 archive_entry_set_rdev(entry, dev);
1551 if (strcmp(key, "flags") == 0) {
1552 *parsed_kws |= MTREE_HAS_FFLAGS;
1553 archive_entry_copy_fflags_text(entry, val);
1558 if (strcmp(key, "gid") == 0) {
1559 *parsed_kws |= MTREE_HAS_GID;
1560 archive_entry_set_gid(entry, mtree_atol(&val, 10));
1563 if (strcmp(key, "gname") == 0) {
1564 *parsed_kws |= MTREE_HAS_GNAME;
1565 archive_entry_copy_gname(entry, val);
1570 if (strcmp(key, "inode") == 0) {
1571 archive_entry_set_ino(entry, mtree_atol(&val, 10));
1576 if (strcmp(key, "link") == 0) {
1577 archive_entry_copy_symlink(entry, val);
1582 if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
1584 if (strcmp(key, "mode") == 0) {
1585 if (val[0] >= '0' && val[0] <= '7') {
1586 *parsed_kws |= MTREE_HAS_PERM;
1587 archive_entry_set_perm(entry,
1588 (mode_t)mtree_atol(&val, 8));
1590 archive_set_error(&a->archive,
1591 ARCHIVE_ERRNO_FILE_FORMAT,
1592 "Symbolic or non-octal mode \"%s\" unsupported", val);
1593 return ARCHIVE_WARN;
1599 if (strcmp(key, "nlink") == 0) {
1600 *parsed_kws |= MTREE_HAS_NLINK;
1601 archive_entry_set_nlink(entry,
1602 (unsigned int)mtree_atol(&val, 10));
1607 if (strcmp(key, "resdevice") == 0) {
1608 /* stat(2) st_dev field, e.g. the device ID where the
1613 r = parse_device(&dev, &a->archive, val);
1614 if (r == ARCHIVE_OK)
1615 archive_entry_set_dev(entry, dev);
1618 if (strcmp(key, "rmd160") == 0 ||
1619 strcmp(key, "rmd160digest") == 0)
1623 if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
1625 if (strcmp(key, "sha256") == 0 ||
1626 strcmp(key, "sha256digest") == 0)
1628 if (strcmp(key, "sha384") == 0 ||
1629 strcmp(key, "sha384digest") == 0)
1631 if (strcmp(key, "sha512") == 0 ||
1632 strcmp(key, "sha512digest") == 0)
1634 if (strcmp(key, "size") == 0) {
1635 archive_entry_set_size(entry, mtree_atol(&val, 10));
1640 if (strcmp(key, "tags") == 0) {
1642 * Comma delimited list of tags.
1643 * Ignore the tags for now, but the interface
1644 * should be extended to allow inclusion/exclusion.
1648 if (strcmp(key, "time") == 0) {
1650 int64_t my_time_t_max = get_time_t_max();
1651 int64_t my_time_t_min = get_time_t_min();
1654 *parsed_kws |= MTREE_HAS_MTIME;
1655 m = mtree_atol(&val, 10);
1656 /* Replicate an old mtree bug:
1657 * 123456789.1 represents 123456789
1658 * seconds and 1 nanosecond. */
1661 ns = (long)mtree_atol(&val, 10);
1664 else if (ns > 999999999)
1667 if (m > my_time_t_max)
1669 else if (m < my_time_t_min)
1671 archive_entry_set_mtime(entry, (time_t)m, ns);
1674 if (strcmp(key, "type") == 0) {
1677 if (strcmp(val, "block") == 0) {
1678 archive_entry_set_filetype(entry, AE_IFBLK);
1683 if (strcmp(val, "char") == 0) {
1684 archive_entry_set_filetype(entry,
1690 if (strcmp(val, "dir") == 0) {
1691 archive_entry_set_filetype(entry,
1697 if (strcmp(val, "fifo") == 0) {
1698 archive_entry_set_filetype(entry,
1702 if (strcmp(val, "file") == 0) {
1703 archive_entry_set_filetype(entry,
1709 if (strcmp(val, "link") == 0) {
1710 archive_entry_set_filetype(entry,
1716 archive_set_error(&a->archive,
1717 ARCHIVE_ERRNO_FILE_FORMAT,
1718 "Unrecognized file type \"%s\"; "
1719 "assuming \"file\"", val);
1720 archive_entry_set_filetype(entry, AE_IFREG);
1721 return (ARCHIVE_WARN);
1723 *parsed_kws |= MTREE_HAS_TYPE;
1728 if (strcmp(key, "uid") == 0) {
1729 *parsed_kws |= MTREE_HAS_UID;
1730 archive_entry_set_uid(entry, mtree_atol(&val, 10));
1733 if (strcmp(key, "uname") == 0) {
1734 *parsed_kws |= MTREE_HAS_UNAME;
1735 archive_entry_copy_uname(entry, val);
1740 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1741 "Unrecognized key %s=%s", key, val);
1742 return (ARCHIVE_WARN);
1744 return (ARCHIVE_OK);
1748 read_data(struct archive_read *a, const void **buff, size_t *size,
1751 size_t bytes_to_read;
1753 struct mtree *mtree;
1755 mtree = (struct mtree *)(a->format->data);
1756 if (mtree->fd < 0) {
1760 return (ARCHIVE_EOF);
1762 if (mtree->buff == NULL) {
1763 mtree->buffsize = 64 * 1024;
1764 mtree->buff = malloc(mtree->buffsize);
1765 if (mtree->buff == NULL) {
1766 archive_set_error(&a->archive, ENOMEM,
1767 "Can't allocate memory");
1768 return (ARCHIVE_FATAL);
1772 *buff = mtree->buff;
1773 *offset = mtree->offset;
1774 if ((int64_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1775 bytes_to_read = (size_t)(mtree->cur_size - mtree->offset);
1777 bytes_to_read = mtree->buffsize;
1778 bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1779 if (bytes_read < 0) {
1780 archive_set_error(&a->archive, errno, "Can't read");
1781 return (ARCHIVE_WARN);
1783 if (bytes_read == 0) {
1785 return (ARCHIVE_EOF);
1787 mtree->offset += bytes_read;
1789 return (ARCHIVE_OK);
1792 /* Skip does nothing except possibly close the contents file. */
1794 skip(struct archive_read *a)
1796 struct mtree *mtree;
1798 mtree = (struct mtree *)(a->format->data);
1799 if (mtree->fd >= 0) {
1803 return (ARCHIVE_OK);
1807 * Since parsing backslash sequences always makes strings shorter,
1808 * we can always do this conversion in-place.
1811 parse_escapes(char *src, struct mtree_entry *mentry)
1816 if (mentry != NULL && strcmp(src, ".") == 0)
1819 while (*src != '\0') {
1821 if (c == '/' && mentry != NULL)
1826 if (src[1] < '0' || src[1] > '7') {
1835 if (src[1] >= '0' && src[1] <= '7' &&
1836 src[2] >= '0' && src[2] <= '7') {
1837 c = (src[0] - '0') << 6;
1838 c |= (src[1] - '0') << 3;
1839 c |= (src[2] - '0');
1886 /* Parse a hex digit. */
1890 if (c >= '0' && c <= '9')
1892 else if (c >= 'a' && c <= 'f')
1894 else if (c >= 'A' && c <= 'F')
1901 * Note that this implementation does not (and should not!) obey
1902 * locale settings; you cannot simply substitute strtol here, since
1903 * it does obey locale.
1906 mtree_atol(char **p, int base)
1909 int digit, last_digit_limit;
1914 else if ((*p)[1] == 'x' || (*p)[1] == 'X') {
1923 limit = INT64_MIN / base;
1924 last_digit_limit = INT64_MIN % base;
1928 digit = parsedigit(**p);
1929 while (digit >= 0 && digit < base) {
1930 if (l < limit || (l == limit && digit > last_digit_limit))
1932 l = (l * base) - digit;
1933 digit = parsedigit(*++(*p));
1937 limit = INT64_MAX / base;
1938 last_digit_limit = INT64_MAX % base;
1941 digit = parsedigit(**p);
1942 while (digit >= 0 && digit < base) {
1943 if (l > limit || (l == limit && digit > last_digit_limit))
1945 l = (l * base) + digit;
1946 digit = parsedigit(*++(*p));
1953 * Returns length of line (including trailing newline)
1954 * or negative on error. 'start' argument is updated to
1955 * point to first character of line.
1958 readline(struct archive_read *a, struct mtree *mtree, char **start,
1962 ssize_t total_size = 0;
1963 ssize_t find_off = 0;
1968 /* Accumulate line in a line buffer. */
1970 /* Read some more. */
1971 t = __archive_read_ahead(a, 1, &bytes_read);
1975 return (ARCHIVE_FATAL);
1976 nl = memchr(t, '\n', bytes_read);
1977 /* If we found '\n', trim the read to end exactly there. */
1979 bytes_read = ((const char *)nl) - ((const char *)t) + 1;
1981 if (total_size + bytes_read + 1 > limit) {
1982 archive_set_error(&a->archive,
1983 ARCHIVE_ERRNO_FILE_FORMAT,
1985 return (ARCHIVE_FATAL);
1987 if (archive_string_ensure(&mtree->line,
1988 total_size + bytes_read + 1) == NULL) {
1989 archive_set_error(&a->archive, ENOMEM,
1990 "Can't allocate working buffer");
1991 return (ARCHIVE_FATAL);
1993 /* Append new bytes to string. */
1994 memcpy(mtree->line.s + total_size, t, bytes_read);
1995 __archive_read_consume(a, bytes_read);
1996 total_size += bytes_read;
1997 mtree->line.s[total_size] = '\0';
1999 for (u = mtree->line.s + find_off; *u; ++u) {
2001 /* Ends with unescaped newline. */
2002 *start = mtree->line.s;
2004 } else if (u[0] == '#') {
2005 /* Ends with comment sequence #...\n */
2007 /* But we've not found the \n yet */
2010 } else if (u[0] == '\\') {
2012 /* Trim escaped newline. */
2014 mtree->line.s[total_size] = '\0';
2016 } else if (u[1] != '\0') {
2017 /* Skip the two-char escape sequence */
2022 find_off = u - mtree->line.s;