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 */
50 #include "archive_entry.h"
51 #include "archive_private.h"
52 #include "archive_rb.h"
53 #include "archive_read_private.h"
54 #include "archive_string.h"
55 #include "archive_pack_dev.h"
64 #define MTREE_HAS_DEVICE 0x0001
65 #define MTREE_HAS_FFLAGS 0x0002
66 #define MTREE_HAS_GID 0x0004
67 #define MTREE_HAS_GNAME 0x0008
68 #define MTREE_HAS_MTIME 0x0010
69 #define MTREE_HAS_NLINK 0x0020
70 #define MTREE_HAS_PERM 0x0040
71 #define MTREE_HAS_SIZE 0x0080
72 #define MTREE_HAS_TYPE 0x0100
73 #define MTREE_HAS_UID 0x0200
74 #define MTREE_HAS_UNAME 0x0400
76 #define MTREE_HAS_OPTIONAL 0x0800
77 #define MTREE_HAS_NOCHANGE 0x1000 /* FreeBSD specific */
79 #define MAX_LINE_LEN (1024 * 1024)
82 struct mtree_option *next;
87 struct archive_rb_node rbnode;
88 struct mtree_entry *next_dup;
89 struct mtree_entry *next;
90 struct mtree_option *options;
97 struct archive_string line;
103 const char *archive_format_name;
104 struct mtree_entry *entries;
105 struct mtree_entry *this_entry;
106 struct archive_rb_tree entry_rbtree;
107 struct archive_string current_dir;
108 struct archive_string contents_name;
110 struct archive_entry_linkresolver *resolver;
111 struct archive_rb_tree rbtree;
117 static int bid_keycmp(const char *, const char *, ssize_t);
118 static int cleanup(struct archive_read *);
119 static int detect_form(struct archive_read *, int *);
120 static int mtree_bid(struct archive_read *, int);
121 static int parse_file(struct archive_read *, struct archive_entry *,
122 struct mtree *, struct mtree_entry *, int *);
123 static void parse_escapes(char *, struct mtree_entry *);
124 static int parse_line(struct archive_read *, struct archive_entry *,
125 struct mtree *, struct mtree_entry *, int *);
126 static int parse_keyword(struct archive_read *, struct mtree *,
127 struct archive_entry *, struct mtree_option *, int *);
128 static int read_data(struct archive_read *a,
129 const void **buff, size_t *size, int64_t *offset);
130 static ssize_t readline(struct archive_read *, struct mtree *, char **, ssize_t);
131 static int skip(struct archive_read *a);
132 static int read_header(struct archive_read *,
133 struct archive_entry *);
134 static int64_t mtree_atol(char **, int base);
137 * There's no standard for TIME_T_MAX/TIME_T_MIN. So we compute them
138 * here. TODO: Move this to configure time, but be careful
139 * about cross-compile environments.
144 #if defined(TIME_T_MAX)
147 /* ISO C allows time_t to be a floating-point type,
148 but POSIX requires an integer type. The following
149 should work on any system that follows the POSIX
151 if (((time_t)0) < ((time_t)-1)) {
152 /* Time_t is unsigned */
155 /* Time_t is signed. */
156 /* Assume it's the same as int64_t or int32_t */
157 if (sizeof(time_t) == sizeof(int64_t)) {
158 return (time_t)INT64_MAX;
160 return (time_t)INT32_MAX;
169 #if defined(TIME_T_MIN)
172 if (((time_t)0) < ((time_t)-1)) {
173 /* Time_t is unsigned */
176 /* Time_t is signed. */
177 if (sizeof(time_t) == sizeof(int64_t)) {
178 return (time_t)INT64_MIN;
180 return (time_t)INT32_MIN;
187 archive_read_format_mtree_options(struct archive_read *a,
188 const char *key, const char *val)
192 mtree = (struct mtree *)(a->format->data);
193 if (strcmp(key, "checkfs") == 0) {
194 /* Allows to read information missing from the mtree from the file system */
195 if (val == NULL || val[0] == 0) {
203 /* Note: The "warn" return is just to inform the options
204 * supervisor that we didn't handle it. It will generate
205 * a suitable error if no one used this option. */
206 return (ARCHIVE_WARN);
210 free_options(struct mtree_option *head)
212 struct mtree_option *next;
214 for (; head != NULL; head = next) {
222 mtree_cmp_node(const struct archive_rb_node *n1,
223 const struct archive_rb_node *n2)
225 const struct mtree_entry *e1 = (const struct mtree_entry *)n1;
226 const struct mtree_entry *e2 = (const struct mtree_entry *)n2;
228 return (strcmp(e1->name, e2->name));
232 mtree_cmp_key(const struct archive_rb_node *n, const void *key)
234 const struct mtree_entry *e = (const struct mtree_entry *)n;
236 return (strcmp(e->name, key));
240 archive_read_support_format_mtree(struct archive *_a)
242 static const struct archive_rb_tree_ops rb_ops = {
243 mtree_cmp_node, mtree_cmp_key,
245 struct archive_read *a = (struct archive_read *)_a;
249 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
250 ARCHIVE_STATE_NEW, "archive_read_support_format_mtree");
252 mtree = (struct mtree *)calloc(1, sizeof(*mtree));
254 archive_set_error(&a->archive, ENOMEM,
255 "Can't allocate mtree data");
256 return (ARCHIVE_FATAL);
260 __archive_rb_tree_init(&mtree->rbtree, &rb_ops);
262 r = __archive_read_register_format(a, mtree, "mtree",
263 mtree_bid, archive_read_format_mtree_options, read_header, read_data, skip, NULL, cleanup, NULL, NULL);
271 cleanup(struct archive_read *a)
274 struct mtree_entry *p, *q;
276 mtree = (struct mtree *)(a->format->data);
282 free_options(p->options);
286 archive_string_free(&mtree->line);
287 archive_string_free(&mtree->current_dir);
288 archive_string_free(&mtree->contents_name);
289 archive_entry_linkresolver_free(mtree->resolver);
293 (a->format->data) = NULL;
298 get_line_size(const char *b, ssize_t avail, ssize_t *nlsize)
303 while (len < avail) {
305 case '\0':/* Non-ascii character or control character. */
310 if (avail-len > 1 && b[1] == '\n') {
332 * <---------------- ravail --------------------->
333 * <-- diff ------> <--- avail ----------------->
334 * <---- len ----------->
335 * | Previous lines | line being parsed nl extra |
341 next_line(struct archive_read *a,
342 const char **b, ssize_t *avail, ssize_t *ravail, ssize_t *nl)
352 len = get_line_size(*b, *avail, nl);
354 * Read bytes more while it does not reach the end of line.
356 while (*nl == 0 && len == *avail && !quit) {
357 ssize_t diff = *ravail - *avail;
358 size_t nbytes_req = (*ravail+1023) & ~1023U;
362 * Place an arbitrary limit on the line length.
363 * mtree is almost free-form input and without line length limits,
364 * it can consume a lot of memory.
366 if (len >= MAX_LINE_LEN)
369 /* Increase reading bytes if it is not enough to at least
371 if (nbytes_req < (size_t)*ravail + 160)
374 *b = __archive_read_ahead(a, nbytes_req, avail);
376 if (*ravail >= *avail)
378 /* Reading bytes reaches the end of file. */
379 *b = __archive_read_ahead(a, *avail, avail);
385 tested = len;/* Skip some bytes we already determinated. */
386 len = get_line_size(*b + len, *avail - len, nl);
394 * Compare characters with a mtree keyword.
395 * Returns the length of a mtree keyword if matched.
396 * Returns 0 if not matched.
399 bid_keycmp(const char *p, const char *key, ssize_t len)
403 while (len > 0 && *p && *key) {
411 return (0);/* Not match */
414 return (0);/* Not match */
416 /* A following character should be specified characters */
417 if (p[0] == '=' || p[0] == ' ' || p[0] == '\t' ||
418 p[0] == '\n' || p[0] == '\r' ||
419 (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r')))
421 return (0);/* Not match */
425 * Test whether the characters 'p' has is mtree keyword.
426 * Returns the length of a detected keyword.
427 * Returns 0 if any keywords were not found.
430 bid_keyword(const char *p, ssize_t len)
432 static const char * const keys_c[] = {
433 "content", "contents", "cksum", NULL
435 static const char * const keys_df[] = {
436 "device", "flags", NULL
438 static const char * const keys_g[] = {
441 static const char * const keys_il[] = {
442 "ignore", "inode", "link", NULL
444 static const char * const keys_m[] = {
445 "md5", "md5digest", "mode", NULL
447 static const char * const keys_no[] = {
448 "nlink", "nochange", "optional", NULL
450 static const char * const keys_r[] = {
451 "resdevice", "rmd160", "rmd160digest", NULL
453 static const char * const keys_s[] = {
454 "sha1", "sha1digest",
455 "sha256", "sha256digest",
456 "sha384", "sha384digest",
457 "sha512", "sha512digest",
460 static const char * const keys_t[] = {
461 "tags", "time", "type", NULL
463 static const char * const keys_u[] = {
466 const char * const *keys;
470 case 'c': keys = keys_c; break;
471 case 'd': case 'f': keys = keys_df; break;
472 case 'g': keys = keys_g; break;
473 case 'i': case 'l': keys = keys_il; break;
474 case 'm': keys = keys_m; break;
475 case 'n': case 'o': keys = keys_no; break;
476 case 'r': keys = keys_r; break;
477 case 's': keys = keys_s; break;
478 case 't': keys = keys_t; break;
479 case 'u': keys = keys_u; break;
480 default: return (0);/* Unknown key */
483 for (i = 0; keys[i] != NULL; i++) {
484 int l = bid_keycmp(p, keys[i], len);
488 return (0);/* Unknown key */
492 * Test whether there is a set of mtree keywords.
493 * Returns the number of keyword.
494 * Returns -1 if we got incorrect sequence.
495 * This function expects a set of "<space characters>keyword=value".
496 * When "unset" is specified, expects a set of "<space characters>keyword".
499 bid_keyword_list(const char *p, ssize_t len, int unset, int last_is_path)
504 while (len > 0 && *p) {
507 /* Test whether there are blank characters in the line. */
508 while (len >0 && (*p == ' ' || *p == '\t')) {
513 if (*p == '\n' || *p == '\r')
515 if (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r'))
517 if (!blank && !last_is_path) /* No blank character. */
519 if (last_is_path && len == 0)
523 l = bid_keycmp(p, "all", len);
527 /* Test whether there is a correct key in the line. */
528 l = bid_keyword(p, len);
530 return (-1);/* Unknown keyword was found. */
540 while (len > 0 && *p != ' ' && *p != '\t') {
545 /* A keyword should have a its value unless
546 * "/unset" operation. */
547 if (!unset && value == 0)
555 bid_entry(const char *p, ssize_t len, ssize_t nl, int *last_is_path)
558 static const unsigned char safe_char[256] = {
559 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
560 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
561 /* !"$%&'()*+,-./ EXCLUSION:( )(#) */
562 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
563 /* 0123456789:;<>? EXCLUSION:(=) */
564 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
565 /* @ABCDEFGHIJKLMNO */
566 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
567 /* PQRSTUVWXYZ[\]^_ */
568 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
569 /* `abcdefghijklmno */
570 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
571 /* pqrstuvwxyz{|}~ */
572 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
573 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
574 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
575 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
576 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
577 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
578 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
579 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
584 const char * const pp_end = pp + len;
588 * Skip the path-name which is quoted.
590 for (;pp < pp_end; ++pp) {
591 if (!safe_char[*(const unsigned char *)pp]) {
592 if (*pp != ' ' && *pp != '\t' && *pp != '\r'
601 /* If a path-name was not found at the first, try to check
602 * a mtree format(a.k.a form D) ``NetBSD's mtree -D'' creates,
603 * which places the path-name at the last. */
605 const char *pb = p + len - nl;
609 /* The form D accepts only a single line for an entry. */
611 pb[-1] == '\\' && (pb[-2] == ' ' || pb[-2] == '\t'))
613 if (pb-1 >= p && pb[-1] == '\\')
617 while (p <= --pb && *pb != ' ' && *pb != '\t') {
618 if (!safe_char[*(const unsigned char *)pb])
621 /* The pathname should have a slash in this
626 if (name_len == 0 || slash == 0)
628 /* If '/' is placed at the first in this field, this is not
629 * a valid filename. */
632 ll = len - nl - name_len;
637 return (bid_keyword_list(pp, ll, 0, *last_is_path));
640 #define MAX_BID_ENTRY 3
643 mtree_bid(struct archive_read *a, int best_bid)
645 const char *signature = "#mtree";
648 (void)best_bid; /* UNUSED */
650 /* Now let's look at the actual header and see if it matches. */
651 p = __archive_read_ahead(a, strlen(signature), NULL);
655 if (memcmp(p, signature, strlen(signature)) == 0)
656 return (8 * (int)strlen(signature));
659 * There is not a mtree signature. Let's try to detect mtree format.
661 return (detect_form(a, NULL));
665 detect_form(struct archive_read *a, int *is_form_d)
668 ssize_t avail, ravail;
669 ssize_t detected_bytes = 0, len, nl;
670 int entry_cnt = 0, multiline = 0;
671 int form_D = 0;/* The archive is generated by `NetBSD mtree -D'
672 * (In this source we call it `form D') . */
674 if (is_form_d != NULL)
676 p = __archive_read_ahead(a, 1, &avail);
681 len = next_line(a, &p, &avail, &ravail, &nl);
682 /* The terminal character of the line should be
683 * a new line character, '\r\n' or '\n'. */
684 if (len <= 0 || nl == 0)
687 /* Leading whitespace is never significant,
689 while (len > 0 && (*p == ' ' || *p == '\t')) {
694 /* Skip comment or empty line. */
695 if (p[0] == '#' || p[0] == '\n' || p[0] == '\r') {
701 /* A continuance line; the terminal
702 * character of previous line was '\' character. */
703 if (bid_keyword_list(p, len, 0, 0) <= 0)
706 detected_bytes += len;
707 if (p[len-nl-1] != '\\') {
708 if (multiline == 1 &&
709 ++entry_cnt >= MAX_BID_ENTRY)
718 int last_is_path, keywords;
720 keywords = bid_entry(p, len, nl, &last_is_path);
722 detected_bytes += len;
726 else if (keywords > 0)
727 /* This line is not `form D'. */
729 } else if (form_D == 1) {
730 if (!last_is_path && keywords > 0)
731 /* This this is not `form D'
732 * and We cannot accept mixed
736 if (!last_is_path && p[len-nl-1] == '\\')
737 /* This line continues. */
740 /* We've got plenty of correct lines
741 * to assume that this file is a mtree
743 if (++entry_cnt >= MAX_BID_ENTRY)
748 } else if (len > 4 && strncmp(p, "/set", 4) == 0) {
749 if (bid_keyword_list(p+4, len-4, 0, 0) <= 0)
751 /* This line continues. */
752 if (p[len-nl-1] == '\\')
754 } else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
755 if (bid_keyword_list(p+6, len-6, 1, 0) <= 0)
757 /* This line continues. */
758 if (p[len-nl-1] == '\\')
763 /* Test next line. */
767 if (entry_cnt >= MAX_BID_ENTRY || (entry_cnt > 0 && len == 0)) {
768 if (is_form_d != NULL) {
779 * The extended mtree format permits multiple lines specifying
780 * attributes for each file. For those entries, only the last line
781 * is actually used. Practically speaking, that means we have
782 * to read the entire mtree file into memory up front.
784 * The parsing is done in two steps. First, it is decided if a line
785 * changes the global defaults and if it is, processed accordingly.
786 * Otherwise, the options of the line are merged with the current
790 add_option(struct archive_read *a, struct mtree_option **global,
791 const char *value, size_t len)
793 struct mtree_option *opt;
795 if ((opt = malloc(sizeof(*opt))) == NULL) {
796 archive_set_error(&a->archive, errno, "Can't allocate memory");
797 return (ARCHIVE_FATAL);
799 if ((opt->value = malloc(len + 1)) == NULL) {
801 archive_set_error(&a->archive, errno, "Can't allocate memory");
802 return (ARCHIVE_FATAL);
804 memcpy(opt->value, value, len);
805 opt->value[len] = '\0';
812 remove_option(struct mtree_option **global, const char *value, size_t len)
814 struct mtree_option *iter, *last;
817 for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
818 if (strncmp(iter->value, value, len) == 0 &&
819 (iter->value[len] == '\0' ||
820 iter->value[len] == '='))
826 *global = iter->next;
828 last->next = iter->next;
835 process_global_set(struct archive_read *a,
836 struct mtree_option **global, const char *line)
838 const char *next, *eq;
844 next = line + strspn(line, " \t\r\n");
848 next = line + strcspn(line, " \t\r\n");
849 eq = strchr(line, '=');
855 remove_option(global, line, len);
856 r = add_option(a, global, line, next - line);
864 process_global_unset(struct archive_read *a,
865 struct mtree_option **global, const char *line)
871 if (strchr(line, '=') != NULL) {
872 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
873 "/unset shall not contain `='");
874 return ARCHIVE_FATAL;
878 next = line + strspn(line, " \t\r\n");
882 len = strcspn(line, " \t\r\n");
884 if (len == 3 && strncmp(line, "all", 3) == 0) {
885 free_options(*global);
888 remove_option(global, line, len);
896 process_add_entry(struct archive_read *a, struct mtree *mtree,
897 struct mtree_option **global, const char *line, ssize_t line_len,
898 struct mtree_entry **last_entry, int is_form_d)
900 struct mtree_entry *entry;
901 struct mtree_option *iter;
902 const char *next, *eq, *name, *end;
903 size_t name_len, len;
906 if ((entry = malloc(sizeof(*entry))) == NULL) {
907 archive_set_error(&a->archive, errno, "Can't allocate memory");
908 return (ARCHIVE_FATAL);
911 entry->options = NULL;
916 /* Add this entry to list. */
917 if (*last_entry == NULL)
918 mtree->entries = entry;
920 (*last_entry)->next = entry;
924 /* Filename is last item on line. */
925 /* Adjust line_len to trim trailing whitespace */
926 while (line_len > 0) {
927 char last_character = line[line_len - 1];
928 if (last_character == '\r'
929 || last_character == '\n'
930 || last_character == '\t'
931 || last_character == ' ') {
937 /* Name starts after the last whitespace separator */
939 for (i = 0; i < line_len; i++) {
947 name_len = line + line_len - name;
950 /* Filename is first item on line */
951 name_len = strcspn(line, " \t\r\n");
954 end = line + line_len;
956 /* name/name_len is the name within the line. */
957 /* line..end brackets the entire line except the name */
959 if ((entry->name = malloc(name_len + 1)) == NULL) {
960 archive_set_error(&a->archive, errno, "Can't allocate memory");
961 return (ARCHIVE_FATAL);
964 memcpy(entry->name, name, name_len);
965 entry->name[name_len] = '\0';
966 parse_escapes(entry->name, entry);
968 entry->next_dup = NULL;
970 if (!__archive_rb_tree_insert_node(&mtree->rbtree, &entry->rbnode)) {
971 struct mtree_entry *alt;
972 alt = (struct mtree_entry *)__archive_rb_tree_find_node(
973 &mtree->rbtree, entry->name);
974 while (alt->next_dup)
976 alt->next_dup = entry;
980 for (iter = *global; iter != NULL; iter = iter->next) {
981 r = add_option(a, &entry->options, iter->value,
982 strlen(iter->value));
988 next = line + strspn(line, " \t\r\n");
994 next = line + strcspn(line, " \t\r\n");
995 eq = strchr(line, '=');
996 if (eq == NULL || eq > next)
1001 remove_option(&entry->options, line, len);
1002 r = add_option(a, &entry->options, line, next - line);
1003 if (r != ARCHIVE_OK)
1010 read_mtree(struct archive_read *a, struct mtree *mtree)
1015 struct mtree_option *global;
1016 struct mtree_entry *last_entry;
1019 mtree->archive_format = ARCHIVE_FORMAT_MTREE;
1020 mtree->archive_format_name = "mtree";
1025 (void)detect_form(a, &is_form_d);
1027 for (counter = 1; ; ++counter) {
1028 len = readline(a, mtree, &p, 65536);
1030 mtree->this_entry = mtree->entries;
1031 free_options(global);
1032 return (ARCHIVE_OK);
1035 free_options(global);
1038 /* Leading whitespace is never significant, ignore it. */
1039 while (*p == ' ' || *p == '\t') {
1043 /* Skip content lines and blank lines. */
1046 if (*p == '\r' || *p == '\n' || *p == '\0')
1049 r = process_add_entry(a, mtree, &global, p, len,
1050 &last_entry, is_form_d);
1051 } else if (len > 4 && strncmp(p, "/set", 4) == 0) {
1052 if (p[4] != ' ' && p[4] != '\t')
1054 r = process_global_set(a, &global, p);
1055 } else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
1056 if (p[6] != ' ' && p[6] != '\t')
1058 r = process_global_unset(a, &global, p);
1062 if (r != ARCHIVE_OK) {
1063 free_options(global);
1068 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1069 "Can't parse line %ju", counter);
1070 free_options(global);
1071 return (ARCHIVE_FATAL);
1075 * Read in the entire mtree file into memory on the first request.
1076 * Then use the next unused file to satisfy each header request.
1079 read_header(struct archive_read *a, struct archive_entry *entry)
1081 struct mtree *mtree;
1085 mtree = (struct mtree *)(a->format->data);
1087 if (mtree->fd >= 0) {
1092 if (mtree->entries == NULL) {
1093 mtree->resolver = archive_entry_linkresolver_new();
1094 if (mtree->resolver == NULL)
1095 return ARCHIVE_FATAL;
1096 archive_entry_linkresolver_set_strategy(mtree->resolver,
1097 ARCHIVE_FORMAT_MTREE);
1098 r = read_mtree(a, mtree);
1099 if (r != ARCHIVE_OK)
1103 a->archive.archive_format = mtree->archive_format;
1104 a->archive.archive_format_name = mtree->archive_format_name;
1107 if (mtree->this_entry == NULL)
1108 return (ARCHIVE_EOF);
1109 if (strcmp(mtree->this_entry->name, "..") == 0) {
1110 mtree->this_entry->used = 1;
1111 if (archive_strlen(&mtree->current_dir) > 0) {
1112 /* Roll back current path. */
1113 p = mtree->current_dir.s
1114 + mtree->current_dir.length - 1;
1115 while (p >= mtree->current_dir.s && *p != '/')
1117 if (p >= mtree->current_dir.s)
1119 mtree->current_dir.length
1120 = p - mtree->current_dir.s + 1;
1123 if (!mtree->this_entry->used) {
1125 r = parse_file(a, entry, mtree, mtree->this_entry,
1130 mtree->this_entry = mtree->this_entry->next;
1135 * A single file can have multiple lines contribute specifications.
1136 * Parse as many lines as necessary, then pull additional information
1137 * from a backing file on disk as necessary.
1140 parse_file(struct archive_read *a, struct archive_entry *entry,
1141 struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
1144 struct stat st_storage, *st;
1145 struct mtree_entry *mp;
1146 struct archive_entry *sparse_entry;
1147 int r = ARCHIVE_OK, r1, parsed_kws;
1151 /* Initialize reasonable defaults. */
1152 archive_entry_set_filetype(entry, AE_IFREG);
1153 archive_entry_set_size(entry, 0);
1154 archive_string_empty(&mtree->contents_name);
1156 /* Parse options from this line. */
1158 r = parse_line(a, entry, mtree, mentry, &parsed_kws);
1161 archive_entry_copy_pathname(entry, mentry->name);
1163 * "Full" entries are allowed to have multiple lines
1164 * and those lines aren't required to be adjacent. We
1165 * don't support multiple lines for "relative" entries
1166 * nor do we make any attempt to merge data from
1167 * separate "relative" and "full" entries. (Merging
1168 * "relative" and "full" entries would require dealing
1169 * with pathname canonicalization, which is a very
1172 mp = (struct mtree_entry *)__archive_rb_tree_find_node(
1173 &mtree->rbtree, mentry->name);
1174 for (; mp; mp = mp->next_dup) {
1175 if (mp->full && !mp->used) {
1176 /* Later lines override earlier ones. */
1178 r1 = parse_line(a, entry, mtree, mp, &parsed_kws);
1185 * Relative entries require us to construct
1186 * the full path and possibly update the
1187 * current directory.
1189 size_t n = archive_strlen(&mtree->current_dir);
1191 archive_strcat(&mtree->current_dir, "/");
1192 archive_strcat(&mtree->current_dir, mentry->name);
1193 archive_entry_copy_pathname(entry, mtree->current_dir.s);
1194 if (archive_entry_filetype(entry) != AE_IFDIR)
1195 mtree->current_dir.length = n;
1198 if (mtree->checkfs) {
1200 * Try to open and stat the file to get the real size
1201 * and other file info. It would be nice to avoid
1202 * this here so that getting a listing of an mtree
1203 * wouldn't require opening every referenced contents
1204 * file. But then we wouldn't know the actual
1205 * contents size, so I don't see a really viable way
1206 * around this. (Also, we may want to someday pull
1207 * other unspecified info from the contents file on
1211 if (archive_strlen(&mtree->contents_name) > 0)
1212 path = mtree->contents_name.s;
1214 path = archive_entry_pathname(entry);
1216 if (archive_entry_filetype(entry) == AE_IFREG ||
1217 archive_entry_filetype(entry) == AE_IFDIR) {
1218 mtree->fd = open(path, O_RDONLY | O_BINARY | O_CLOEXEC);
1219 __archive_ensure_cloexec_flag(mtree->fd);
1220 if (mtree->fd == -1 &&
1222 archive_strlen(&mtree->contents_name) > 0)) {
1223 archive_set_error(&a->archive, errno,
1224 "Can't open %s", path);
1230 if (mtree->fd >= 0) {
1231 if (fstat(mtree->fd, st) == -1) {
1232 archive_set_error(&a->archive, errno,
1233 "Could not fstat %s", path);
1235 /* If we can't stat it, don't keep it open. */
1240 } else if (lstat(path, st) == -1) {
1245 * Check for a mismatch between the type in the specification
1246 * and the type of the contents object on disk.
1249 if (((st->st_mode & S_IFMT) == S_IFREG &&
1250 archive_entry_filetype(entry) == AE_IFREG)
1252 ||((st->st_mode & S_IFMT) == S_IFLNK &&
1253 archive_entry_filetype(entry) == AE_IFLNK)
1256 ||((st->st_mode & S_IFSOCK) == S_IFSOCK &&
1257 archive_entry_filetype(entry) == AE_IFSOCK)
1260 ||((st->st_mode & S_IFMT) == S_IFCHR &&
1261 archive_entry_filetype(entry) == AE_IFCHR)
1264 ||((st->st_mode & S_IFMT) == S_IFBLK &&
1265 archive_entry_filetype(entry) == AE_IFBLK)
1267 ||((st->st_mode & S_IFMT) == S_IFDIR &&
1268 archive_entry_filetype(entry) == AE_IFDIR)
1270 ||((st->st_mode & S_IFMT) == S_IFIFO &&
1271 archive_entry_filetype(entry) == AE_IFIFO)
1276 /* Types don't match; bail out gracefully. */
1280 if (parsed_kws & MTREE_HAS_OPTIONAL) {
1281 /* It's not an error for an optional
1282 * entry to not match disk. */
1284 } else if (r == ARCHIVE_OK) {
1285 archive_set_error(&a->archive,
1287 "mtree specification has different"
1289 archive_entry_pathname(entry));
1297 * If there is a contents file on disk, pick some of the
1298 * metadata from that file. For most of these, we only
1299 * set it from the contents if it wasn't already parsed
1300 * from the specification.
1303 if (((parsed_kws & MTREE_HAS_DEVICE) == 0 ||
1304 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) &&
1305 (archive_entry_filetype(entry) == AE_IFCHR ||
1306 archive_entry_filetype(entry) == AE_IFBLK))
1307 archive_entry_set_rdev(entry, st->st_rdev);
1308 if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME))
1310 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1311 archive_entry_set_gid(entry, st->st_gid);
1312 if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME))
1314 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1315 archive_entry_set_uid(entry, st->st_uid);
1316 if ((parsed_kws & MTREE_HAS_MTIME) == 0 ||
1317 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) {
1318 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1319 archive_entry_set_mtime(entry, st->st_mtime,
1320 st->st_mtimespec.tv_nsec);
1321 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1322 archive_entry_set_mtime(entry, st->st_mtime,
1323 st->st_mtim.tv_nsec);
1324 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1325 archive_entry_set_mtime(entry, st->st_mtime,
1327 #elif HAVE_STRUCT_STAT_ST_UMTIME
1328 archive_entry_set_mtime(entry, st->st_mtime,
1329 st->st_umtime*1000);
1330 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
1331 archive_entry_set_mtime(entry, st->st_mtime,
1332 st->st_mtime_usec*1000);
1334 archive_entry_set_mtime(entry, st->st_mtime, 0);
1337 if ((parsed_kws & MTREE_HAS_NLINK) == 0 ||
1338 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1339 archive_entry_set_nlink(entry, st->st_nlink);
1340 if ((parsed_kws & MTREE_HAS_PERM) == 0 ||
1341 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1342 archive_entry_set_perm(entry, st->st_mode);
1343 if ((parsed_kws & MTREE_HAS_SIZE) == 0 ||
1344 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1345 archive_entry_set_size(entry, st->st_size);
1346 archive_entry_set_ino(entry, st->st_ino);
1347 archive_entry_set_dev(entry, st->st_dev);
1349 archive_entry_linkify(mtree->resolver, &entry,
1351 } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
1353 * Couldn't open the entry, stat it or the on-disk type
1354 * didn't match. If this entry is optional, just
1355 * ignore it and read the next header entry.
1362 mtree->cur_size = archive_entry_size(entry);
1369 * Each line contains a sequence of keywords.
1372 parse_line(struct archive_read *a, struct archive_entry *entry,
1373 struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
1375 struct mtree_option *iter;
1376 int r = ARCHIVE_OK, r1;
1378 for (iter = mp->options; iter != NULL; iter = iter->next) {
1379 r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
1383 if (r == ARCHIVE_OK && (*parsed_kws & MTREE_HAS_TYPE) == 0) {
1384 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1385 "Missing type keyword in mtree specification");
1386 return (ARCHIVE_WARN);
1392 * Device entries have one of the following forms:
1394 * - format,major,minor[,subdevice]
1395 * When parsing succeeded, `pdev' will contain the appropriate dev_t value.
1398 /* strsep() is not in C90, but strcspn() is. */
1399 /* Taken from http://unixpapa.com/incnote/string.html */
1401 la_strsep(char **sp, const char *sep)
1404 if (sp == NULL || *sp == NULL || **sp == '\0')
1407 p = s + strcspn(s, sep);
1415 parse_device(dev_t *pdev, struct archive *a, char *val)
1417 #define MAX_PACK_ARGS 3
1418 unsigned long numbers[MAX_PACK_ARGS];
1423 const char *error = NULL;
1425 memset(pdev, 0, sizeof(*pdev));
1426 if ((dev = strchr(val, ',')) != NULL) {
1428 * Device's major/minor are given in a specified format.
1429 * Decode and pack it accordingly.
1432 if ((pack = pack_find(val)) == NULL) {
1433 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1434 "Unknown format `%s'", val);
1435 return ARCHIVE_WARN;
1438 while ((p = la_strsep(&dev, ",")) != NULL) {
1440 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1442 return ARCHIVE_WARN;
1444 if (argc >= MAX_PACK_ARGS) {
1445 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1446 "Too many arguments");
1447 return ARCHIVE_WARN;
1449 numbers[argc++] = (unsigned long)mtree_atol(&p, 0);
1452 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1453 "Not enough arguments");
1454 return ARCHIVE_WARN;
1456 result = (*pack)(argc, numbers, &error);
1457 if (error != NULL) {
1458 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1460 return ARCHIVE_WARN;
1463 /* file system raw value. */
1464 result = (dev_t)mtree_atol(&val, 0);
1468 #undef MAX_PACK_ARGS
1472 * Parse a single keyword and its value.
1475 parse_keyword(struct archive_read *a, struct mtree *mtree,
1476 struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws)
1483 return (ARCHIVE_OK);
1485 if (strcmp(key, "nochange") == 0) {
1486 *parsed_kws |= MTREE_HAS_NOCHANGE;
1487 return (ARCHIVE_OK);
1489 if (strcmp(key, "optional") == 0) {
1490 *parsed_kws |= MTREE_HAS_OPTIONAL;
1491 return (ARCHIVE_OK);
1493 if (strcmp(key, "ignore") == 0) {
1495 * The mtree processing is not recursive, so
1496 * recursion will only happen for explicitly listed
1499 return (ARCHIVE_OK);
1502 val = strchr(key, '=');
1504 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1505 "Malformed attribute \"%s\" (%d)", key, key[0]);
1506 return (ARCHIVE_WARN);
1514 if (strcmp(key, "content") == 0
1515 || strcmp(key, "contents") == 0) {
1516 parse_escapes(val, NULL);
1517 archive_strcpy(&mtree->contents_name, val);
1520 if (strcmp(key, "cksum") == 0)
1524 if (strcmp(key, "device") == 0) {
1525 /* stat(2) st_rdev field, e.g. the major/minor IDs
1526 * of a char/block special file */
1530 *parsed_kws |= MTREE_HAS_DEVICE;
1531 r = parse_device(&dev, &a->archive, val);
1532 if (r == ARCHIVE_OK)
1533 archive_entry_set_rdev(entry, dev);
1538 if (strcmp(key, "flags") == 0) {
1539 *parsed_kws |= MTREE_HAS_FFLAGS;
1540 archive_entry_copy_fflags_text(entry, val);
1545 if (strcmp(key, "gid") == 0) {
1546 *parsed_kws |= MTREE_HAS_GID;
1547 archive_entry_set_gid(entry, mtree_atol(&val, 10));
1550 if (strcmp(key, "gname") == 0) {
1551 *parsed_kws |= MTREE_HAS_GNAME;
1552 archive_entry_copy_gname(entry, val);
1557 if (strcmp(key, "inode") == 0) {
1558 archive_entry_set_ino(entry, mtree_atol(&val, 10));
1563 if (strcmp(key, "link") == 0) {
1564 archive_entry_copy_symlink(entry, val);
1569 if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
1571 if (strcmp(key, "mode") == 0) {
1572 if (val[0] >= '0' && val[0] <= '7') {
1573 *parsed_kws |= MTREE_HAS_PERM;
1574 archive_entry_set_perm(entry,
1575 (mode_t)mtree_atol(&val, 8));
1577 archive_set_error(&a->archive,
1578 ARCHIVE_ERRNO_FILE_FORMAT,
1579 "Symbolic or non-octal mode \"%s\" unsupported", val);
1580 return ARCHIVE_WARN;
1586 if (strcmp(key, "nlink") == 0) {
1587 *parsed_kws |= MTREE_HAS_NLINK;
1588 archive_entry_set_nlink(entry,
1589 (unsigned int)mtree_atol(&val, 10));
1594 if (strcmp(key, "resdevice") == 0) {
1595 /* stat(2) st_dev field, e.g. the device ID where the
1600 r = parse_device(&dev, &a->archive, val);
1601 if (r == ARCHIVE_OK)
1602 archive_entry_set_dev(entry, dev);
1605 if (strcmp(key, "rmd160") == 0 ||
1606 strcmp(key, "rmd160digest") == 0)
1610 if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
1612 if (strcmp(key, "sha256") == 0 ||
1613 strcmp(key, "sha256digest") == 0)
1615 if (strcmp(key, "sha384") == 0 ||
1616 strcmp(key, "sha384digest") == 0)
1618 if (strcmp(key, "sha512") == 0 ||
1619 strcmp(key, "sha512digest") == 0)
1621 if (strcmp(key, "size") == 0) {
1622 archive_entry_set_size(entry, mtree_atol(&val, 10));
1627 if (strcmp(key, "tags") == 0) {
1629 * Comma delimited list of tags.
1630 * Ignore the tags for now, but the interface
1631 * should be extended to allow inclusion/exclusion.
1635 if (strcmp(key, "time") == 0) {
1637 int64_t my_time_t_max = get_time_t_max();
1638 int64_t my_time_t_min = get_time_t_min();
1641 *parsed_kws |= MTREE_HAS_MTIME;
1642 m = mtree_atol(&val, 10);
1643 /* Replicate an old mtree bug:
1644 * 123456789.1 represents 123456789
1645 * seconds and 1 nanosecond. */
1648 ns = (long)mtree_atol(&val, 10);
1651 else if (ns > 999999999)
1654 if (m > my_time_t_max)
1656 else if (m < my_time_t_min)
1658 archive_entry_set_mtime(entry, (time_t)m, ns);
1661 if (strcmp(key, "type") == 0) {
1664 if (strcmp(val, "block") == 0) {
1665 archive_entry_set_filetype(entry, AE_IFBLK);
1670 if (strcmp(val, "char") == 0) {
1671 archive_entry_set_filetype(entry,
1677 if (strcmp(val, "dir") == 0) {
1678 archive_entry_set_filetype(entry,
1684 if (strcmp(val, "fifo") == 0) {
1685 archive_entry_set_filetype(entry,
1689 if (strcmp(val, "file") == 0) {
1690 archive_entry_set_filetype(entry,
1696 if (strcmp(val, "link") == 0) {
1697 archive_entry_set_filetype(entry,
1703 archive_set_error(&a->archive,
1704 ARCHIVE_ERRNO_FILE_FORMAT,
1705 "Unrecognized file type \"%s\"; "
1706 "assuming \"file\"", val);
1707 archive_entry_set_filetype(entry, AE_IFREG);
1708 return (ARCHIVE_WARN);
1710 *parsed_kws |= MTREE_HAS_TYPE;
1715 if (strcmp(key, "uid") == 0) {
1716 *parsed_kws |= MTREE_HAS_UID;
1717 archive_entry_set_uid(entry, mtree_atol(&val, 10));
1720 if (strcmp(key, "uname") == 0) {
1721 *parsed_kws |= MTREE_HAS_UNAME;
1722 archive_entry_copy_uname(entry, val);
1727 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1728 "Unrecognized key %s=%s", key, val);
1729 return (ARCHIVE_WARN);
1731 return (ARCHIVE_OK);
1735 read_data(struct archive_read *a, const void **buff, size_t *size,
1738 size_t bytes_to_read;
1740 struct mtree *mtree;
1742 mtree = (struct mtree *)(a->format->data);
1743 if (mtree->fd < 0) {
1747 return (ARCHIVE_EOF);
1749 if (mtree->buff == NULL) {
1750 mtree->buffsize = 64 * 1024;
1751 mtree->buff = malloc(mtree->buffsize);
1752 if (mtree->buff == NULL) {
1753 archive_set_error(&a->archive, ENOMEM,
1754 "Can't allocate memory");
1755 return (ARCHIVE_FATAL);
1759 *buff = mtree->buff;
1760 *offset = mtree->offset;
1761 if ((int64_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1762 bytes_to_read = (size_t)(mtree->cur_size - mtree->offset);
1764 bytes_to_read = mtree->buffsize;
1765 bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1766 if (bytes_read < 0) {
1767 archive_set_error(&a->archive, errno, "Can't read");
1768 return (ARCHIVE_WARN);
1770 if (bytes_read == 0) {
1772 return (ARCHIVE_EOF);
1774 mtree->offset += bytes_read;
1776 return (ARCHIVE_OK);
1779 /* Skip does nothing except possibly close the contents file. */
1781 skip(struct archive_read *a)
1783 struct mtree *mtree;
1785 mtree = (struct mtree *)(a->format->data);
1786 if (mtree->fd >= 0) {
1790 return (ARCHIVE_OK);
1794 * Since parsing backslash sequences always makes strings shorter,
1795 * we can always do this conversion in-place.
1798 parse_escapes(char *src, struct mtree_entry *mentry)
1803 if (mentry != NULL && strcmp(src, ".") == 0)
1806 while (*src != '\0') {
1808 if (c == '/' && mentry != NULL)
1813 if (src[1] < '0' || src[1] > '7') {
1822 if (src[1] >= '0' && src[1] <= '7' &&
1823 src[2] >= '0' && src[2] <= '7') {
1824 c = (src[0] - '0') << 6;
1825 c |= (src[1] - '0') << 3;
1826 c |= (src[2] - '0');
1873 /* Parse a hex digit. */
1877 if (c >= '0' && c <= '9')
1879 else if (c >= 'a' && c <= 'f')
1881 else if (c >= 'A' && c <= 'F')
1888 * Note that this implementation does not (and should not!) obey
1889 * locale settings; you cannot simply substitute strtol here, since
1890 * it does obey locale.
1893 mtree_atol(char **p, int base)
1896 int digit, last_digit_limit;
1901 else if ((*p)[1] == 'x' || (*p)[1] == 'X') {
1910 limit = INT64_MIN / base;
1911 last_digit_limit = INT64_MIN % base;
1915 digit = parsedigit(**p);
1916 while (digit >= 0 && digit < base) {
1917 if (l < limit || (l == limit && digit > last_digit_limit))
1919 l = (l * base) - digit;
1920 digit = parsedigit(*++(*p));
1924 limit = INT64_MAX / base;
1925 last_digit_limit = INT64_MAX % 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));
1940 * Returns length of line (including trailing newline)
1941 * or negative on error. 'start' argument is updated to
1942 * point to first character of line.
1945 readline(struct archive_read *a, struct mtree *mtree, char **start,
1949 ssize_t total_size = 0;
1950 ssize_t find_off = 0;
1955 /* Accumulate line in a line buffer. */
1957 /* Read some more. */
1958 t = __archive_read_ahead(a, 1, &bytes_read);
1962 return (ARCHIVE_FATAL);
1963 nl = memchr(t, '\n', bytes_read);
1964 /* If we found '\n', trim the read to end exactly there. */
1966 bytes_read = ((const char *)nl) - ((const char *)t) + 1;
1968 if (total_size + bytes_read + 1 > limit) {
1969 archive_set_error(&a->archive,
1970 ARCHIVE_ERRNO_FILE_FORMAT,
1972 return (ARCHIVE_FATAL);
1974 if (archive_string_ensure(&mtree->line,
1975 total_size + bytes_read + 1) == NULL) {
1976 archive_set_error(&a->archive, ENOMEM,
1977 "Can't allocate working buffer");
1978 return (ARCHIVE_FATAL);
1980 /* Append new bytes to string. */
1981 memcpy(mtree->line.s + total_size, t, bytes_read);
1982 __archive_read_consume(a, bytes_read);
1983 total_size += bytes_read;
1984 mtree->line.s[total_size] = '\0';
1986 for (u = mtree->line.s + find_off; *u; ++u) {
1988 /* Ends with unescaped newline. */
1989 *start = mtree->line.s;
1991 } else if (u[0] == '#') {
1992 /* Ends with comment sequence #...\n */
1994 /* But we've not found the \n yet */
1997 } else if (u[0] == '\\') {
1999 /* Trim escaped newline. */
2001 mtree->line.s[total_size] = '\0';
2003 } else if (u[1] != '\0') {
2004 /* Skip the two-char escape sequence */
2009 find_off = u - mtree->line.s;