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 */
50 #include "archive_entry.h"
51 #include "archive_private.h"
52 #include "archive_read_private.h"
53 #include "archive_string.h"
54 #include "archive_pack_dev.h"
63 #define MTREE_HAS_DEVICE 0x0001
64 #define MTREE_HAS_FFLAGS 0x0002
65 #define MTREE_HAS_GID 0x0004
66 #define MTREE_HAS_GNAME 0x0008
67 #define MTREE_HAS_MTIME 0x0010
68 #define MTREE_HAS_NLINK 0x0020
69 #define MTREE_HAS_PERM 0x0040
70 #define MTREE_HAS_SIZE 0x0080
71 #define MTREE_HAS_TYPE 0x0100
72 #define MTREE_HAS_UID 0x0200
73 #define MTREE_HAS_UNAME 0x0400
75 #define MTREE_HAS_OPTIONAL 0x0800
76 #define MTREE_HAS_NOCHANGE 0x1000 /* FreeBSD specific */
79 struct mtree_option *next;
84 struct mtree_entry *next;
85 struct mtree_option *options;
92 struct archive_string line;
98 const char *archive_format_name;
99 struct mtree_entry *entries;
100 struct mtree_entry *this_entry;
101 struct archive_string current_dir;
102 struct archive_string contents_name;
104 struct archive_entry_linkresolver *resolver;
110 static int bid_keycmp(const char *, const char *, ssize_t);
111 static int cleanup(struct archive_read *);
112 static int detect_form(struct archive_read *, int *);
113 static int mtree_bid(struct archive_read *, int);
114 static int parse_file(struct archive_read *, struct archive_entry *,
115 struct mtree *, struct mtree_entry *, int *);
116 static void parse_escapes(char *, struct mtree_entry *);
117 static int parse_line(struct archive_read *, struct archive_entry *,
118 struct mtree *, struct mtree_entry *, int *);
119 static int parse_keyword(struct archive_read *, struct mtree *,
120 struct archive_entry *, struct mtree_option *, int *);
121 static int read_data(struct archive_read *a,
122 const void **buff, size_t *size, int64_t *offset);
123 static ssize_t readline(struct archive_read *, struct mtree *, char **, ssize_t);
124 static int skip(struct archive_read *a);
125 static int read_header(struct archive_read *,
126 struct archive_entry *);
127 static int64_t mtree_atol10(char **);
128 static int64_t mtree_atol8(char **);
129 static int64_t mtree_atol(char **);
132 * There's no standard for TIME_T_MAX/TIME_T_MIN. So we compute them
133 * here. TODO: Move this to configure time, but be careful
134 * about cross-compile environments.
139 #if defined(TIME_T_MAX)
142 /* ISO C allows time_t to be a floating-point type,
143 but POSIX requires an integer type. The following
144 should work on any system that follows the POSIX
146 if (((time_t)0) < ((time_t)-1)) {
147 /* Time_t is unsigned */
150 /* Time_t is signed. */
151 /* Assume it's the same as int64_t or int32_t */
152 if (sizeof(time_t) == sizeof(int64_t)) {
153 return (time_t)INT64_MAX;
155 return (time_t)INT32_MAX;
164 #if defined(TIME_T_MIN)
167 if (((time_t)0) < ((time_t)-1)) {
168 /* Time_t is unsigned */
171 /* Time_t is signed. */
172 if (sizeof(time_t) == sizeof(int64_t)) {
173 return (time_t)INT64_MIN;
175 return (time_t)INT32_MIN;
182 archive_read_format_mtree_options(struct archive_read *a,
183 const char *key, const char *val)
187 mtree = (struct mtree *)(a->format->data);
188 if (strcmp(key, "checkfs") == 0) {
189 /* Allows to read information missing from the mtree from the file system */
190 if (val == NULL || val[0] == 0) {
198 /* Note: The "warn" return is just to inform the options
199 * supervisor that we didn't handle it. It will generate
200 * a suitable error if no one used this option. */
201 return (ARCHIVE_WARN);
205 free_options(struct mtree_option *head)
207 struct mtree_option *next;
209 for (; head != NULL; head = next) {
217 archive_read_support_format_mtree(struct archive *_a)
219 struct archive_read *a = (struct archive_read *)_a;
223 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
224 ARCHIVE_STATE_NEW, "archive_read_support_format_mtree");
226 mtree = (struct mtree *)malloc(sizeof(*mtree));
228 archive_set_error(&a->archive, ENOMEM,
229 "Can't allocate mtree data");
230 return (ARCHIVE_FATAL);
232 memset(mtree, 0, sizeof(*mtree));
235 r = __archive_read_register_format(a, mtree, "mtree",
236 mtree_bid, archive_read_format_mtree_options, read_header, read_data, skip, NULL, cleanup, NULL, NULL);
244 cleanup(struct archive_read *a)
247 struct mtree_entry *p, *q;
249 mtree = (struct mtree *)(a->format->data);
255 free_options(p->options);
259 archive_string_free(&mtree->line);
260 archive_string_free(&mtree->current_dir);
261 archive_string_free(&mtree->contents_name);
262 archive_entry_linkresolver_free(mtree->resolver);
266 (a->format->data) = NULL;
271 get_line_size(const char *b, ssize_t avail, ssize_t *nlsize)
276 while (len < avail) {
278 case '\0':/* Non-ascii character or control character. */
283 if (avail-len > 1 && b[1] == '\n') {
305 * <---------------- ravail --------------------->
306 * <-- diff ------> <--- avail ----------------->
307 * <---- len ----------->
308 * | Previous lines | line being parsed nl extra |
314 next_line(struct archive_read *a,
315 const char **b, ssize_t *avail, ssize_t *ravail, ssize_t *nl)
325 len = get_line_size(*b, *avail, nl);
327 * Read bytes more while it does not reach the end of line.
329 while (*nl == 0 && len == *avail && !quit) {
330 ssize_t diff = *ravail - *avail;
331 size_t nbytes_req = (*ravail+1023) & ~1023U;
334 /* Increase reading bytes if it is not enough to at least
336 if (nbytes_req < (size_t)*ravail + 160)
339 *b = __archive_read_ahead(a, nbytes_req, avail);
341 if (*ravail >= *avail)
343 /* Reading bytes reaches the end of file. */
344 *b = __archive_read_ahead(a, *avail, avail);
350 tested = len;/* Skip some bytes we already determinated. */
351 len = get_line_size(*b + len, *avail - len, nl);
359 * Compare characters with a mtree keyword.
360 * Returns the length of a mtree keyword if matched.
361 * Returns 0 if not matched.
364 bid_keycmp(const char *p, const char *key, ssize_t len)
368 while (len > 0 && *p && *key) {
376 return (0);/* Not match */
379 return (0);/* Not match */
381 /* A following character should be specified characters */
382 if (p[0] == '=' || p[0] == ' ' || p[0] == '\t' ||
383 p[0] == '\n' || p[0] == '\r' ||
384 (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r')))
386 return (0);/* Not match */
390 * Test whether the characters 'p' has is mtree keyword.
391 * Returns the length of a detected keyword.
392 * Returns 0 if any keywords were not found.
395 bid_keyword(const char *p, ssize_t len)
397 static const char *keys_c[] = {
398 "content", "contents", "cksum", NULL
400 static const char *keys_df[] = {
401 "device", "flags", NULL
403 static const char *keys_g[] = {
406 static const char *keys_il[] = {
407 "ignore", "inode", "link", NULL
409 static const char *keys_m[] = {
410 "md5", "md5digest", "mode", NULL
412 static const char *keys_no[] = {
413 "nlink", "nochange", "optional", NULL
415 static const char *keys_r[] = {
416 "resdevice", "rmd160", "rmd160digest", NULL
418 static const char *keys_s[] = {
419 "sha1", "sha1digest",
420 "sha256", "sha256digest",
421 "sha384", "sha384digest",
422 "sha512", "sha512digest",
425 static const char *keys_t[] = {
426 "tags", "time", "type", NULL
428 static const char *keys_u[] = {
435 case 'c': keys = keys_c; break;
436 case 'd': case 'f': keys = keys_df; break;
437 case 'g': keys = keys_g; break;
438 case 'i': case 'l': keys = keys_il; break;
439 case 'm': keys = keys_m; break;
440 case 'n': case 'o': keys = keys_no; break;
441 case 'r': keys = keys_r; break;
442 case 's': keys = keys_s; break;
443 case 't': keys = keys_t; break;
444 case 'u': keys = keys_u; break;
445 default: return (0);/* Unknown key */
448 for (i = 0; keys[i] != NULL; i++) {
449 int l = bid_keycmp(p, keys[i], len);
453 return (0);/* Unknown key */
457 * Test whether there is a set of mtree keywords.
458 * Returns the number of keyword.
459 * Returns -1 if we got incorrect sequence.
460 * This function expects a set of "<space characters>keyword=value".
461 * When "unset" is specified, expects a set of "<space characters>keyword".
464 bid_keyword_list(const char *p, ssize_t len, int unset, int last_is_path)
469 while (len > 0 && *p) {
472 /* Test whether there are blank characters in the line. */
473 while (len >0 && (*p == ' ' || *p == '\t')) {
478 if (*p == '\n' || *p == '\r')
480 if (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r'))
482 if (!blank && !last_is_path) /* No blank character. */
484 if (last_is_path && len == 0)
488 l = bid_keycmp(p, "all", len);
492 /* Test whether there is a correct key in the line. */
493 l = bid_keyword(p, len);
495 return (-1);/* Unknown keyword was found. */
505 while (len > 0 && *p != ' ' && *p != '\t') {
510 /* A keyword should have a its value unless
511 * "/unset" operation. */
512 if (!unset && value == 0)
520 bid_entry(const char *p, ssize_t len, ssize_t nl, int *last_is_path)
523 static const unsigned char safe_char[256] = {
524 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
525 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
526 /* !"$%&'()*+,-./ EXCLUSION:( )(#) */
527 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
528 /* 0123456789:;<>? EXCLUSION:(=) */
529 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
530 /* @ABCDEFGHIJKLMNO */
531 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
532 /* PQRSTUVWXYZ[\]^_ */
533 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
534 /* `abcdefghijklmno */
535 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
536 /* pqrstuvwxyz{|}~ */
537 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
538 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
539 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
540 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
541 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
542 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
543 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
544 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
545 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
549 const char * const pp_end = pp + len;
553 * Skip the path-name which is quoted.
555 for (;pp < pp_end; ++pp) {
556 if (!safe_char[*(const unsigned char *)pp]) {
557 if (*pp != ' ' && *pp != '\t' && *pp != '\r'
566 /* If a path-name was not found at the first, try to check
567 * a mtree format(a.k.a form D) ``NetBSD's mtree -D'' creates,
568 * which places the path-name at the last. */
570 const char *pb = p + len - nl;
574 /* The form D accepts only a single line for an entry. */
576 pb[-1] == '\\' && (pb[-2] == ' ' || pb[-2] == '\t'))
578 if (pb-1 >= p && pb[-1] == '\\')
582 while (p <= --pb && *pb != ' ' && *pb != '\t') {
583 if (!safe_char[*(const unsigned char *)pb])
586 /* The pathname should have a slash in this
591 if (name_len == 0 || slash == 0)
593 /* If '/' is placed at the first in this field, this is not
594 * a valid filename. */
597 ll = len - nl - name_len;
602 return (bid_keyword_list(pp, ll, 0, *last_is_path));
605 #define MAX_BID_ENTRY 3
608 mtree_bid(struct archive_read *a, int best_bid)
610 const char *signature = "#mtree";
613 (void)best_bid; /* UNUSED */
615 /* Now let's look at the actual header and see if it matches. */
616 p = __archive_read_ahead(a, strlen(signature), NULL);
620 if (memcmp(p, signature, strlen(signature)) == 0)
621 return (8 * (int)strlen(signature));
624 * There is not a mtree signature. Let's try to detect mtree format.
626 return (detect_form(a, NULL));
630 detect_form(struct archive_read *a, int *is_form_d)
633 ssize_t avail, ravail;
634 ssize_t detected_bytes = 0, len, nl;
635 int entry_cnt = 0, multiline = 0;
636 int form_D = 0;/* The archive is generated by `NetBSD mtree -D'
637 * (In this source we call it `form D') . */
639 if (is_form_d != NULL)
641 p = __archive_read_ahead(a, 1, &avail);
646 len = next_line(a, &p, &avail, &ravail, &nl);
647 /* The terminal character of the line should be
648 * a new line character, '\r\n' or '\n'. */
649 if (len <= 0 || nl == 0)
652 /* Leading whitespace is never significant,
654 while (len > 0 && (*p == ' ' || *p == '\t')) {
659 /* Skip comment or empty line. */
660 if (p[0] == '#' || p[0] == '\n' || p[0] == '\r') {
666 /* A continuance line; the terminal
667 * character of previous line was '\' character. */
668 if (bid_keyword_list(p, len, 0, 0) <= 0)
671 detected_bytes += len;
672 if (p[len-nl-1] != '\\') {
673 if (multiline == 1 &&
674 ++entry_cnt >= MAX_BID_ENTRY)
683 int last_is_path, keywords;
685 keywords = bid_entry(p, len, nl, &last_is_path);
687 detected_bytes += len;
691 else if (keywords > 0)
692 /* This line is not `form D'. */
694 } else if (form_D == 1) {
695 if (!last_is_path && keywords > 0)
696 /* This this is not `form D'
697 * and We cannot accept mixed
701 if (!last_is_path && p[len-nl-1] == '\\')
702 /* This line continues. */
705 /* We've got plenty of correct lines
706 * to assume that this file is a mtree
708 if (++entry_cnt >= MAX_BID_ENTRY)
713 } else if (strncmp(p, "/set", 4) == 0) {
714 if (bid_keyword_list(p+4, len-4, 0, 0) <= 0)
716 /* This line continues. */
717 if (p[len-nl-1] == '\\')
719 } else if (strncmp(p, "/unset", 6) == 0) {
720 if (bid_keyword_list(p+6, len-6, 1, 0) <= 0)
722 /* This line continues. */
723 if (p[len-nl-1] == '\\')
728 /* Test next line. */
732 if (entry_cnt >= MAX_BID_ENTRY || (entry_cnt > 0 && len == 0)) {
733 if (is_form_d != NULL) {
744 * The extended mtree format permits multiple lines specifying
745 * attributes for each file. For those entries, only the last line
746 * is actually used. Practically speaking, that means we have
747 * to read the entire mtree file into memory up front.
749 * The parsing is done in two steps. First, it is decided if a line
750 * changes the global defaults and if it is, processed accordingly.
751 * Otherwise, the options of the line are merged with the current
755 add_option(struct archive_read *a, struct mtree_option **global,
756 const char *value, size_t len)
758 struct mtree_option *opt;
760 if ((opt = malloc(sizeof(*opt))) == NULL) {
761 archive_set_error(&a->archive, errno, "Can't allocate memory");
762 return (ARCHIVE_FATAL);
764 if ((opt->value = malloc(len + 1)) == NULL) {
766 archive_set_error(&a->archive, errno, "Can't allocate memory");
767 return (ARCHIVE_FATAL);
769 memcpy(opt->value, value, len);
770 opt->value[len] = '\0';
777 remove_option(struct mtree_option **global, const char *value, size_t len)
779 struct mtree_option *iter, *last;
782 for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
783 if (strncmp(iter->value, value, len) == 0 &&
784 (iter->value[len] == '\0' ||
785 iter->value[len] == '='))
791 *global = iter->next;
793 last->next = iter->next;
800 process_global_set(struct archive_read *a,
801 struct mtree_option **global, const char *line)
803 const char *next, *eq;
809 next = line + strspn(line, " \t\r\n");
813 next = line + strcspn(line, " \t\r\n");
814 eq = strchr(line, '=');
820 remove_option(global, line, len);
821 r = add_option(a, global, line, next - line);
829 process_global_unset(struct archive_read *a,
830 struct mtree_option **global, const char *line)
836 if (strchr(line, '=') != NULL) {
837 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
838 "/unset shall not contain `='");
839 return ARCHIVE_FATAL;
843 next = line + strspn(line, " \t\r\n");
847 len = strcspn(line, " \t\r\n");
849 if (len == 3 && strncmp(line, "all", 3) == 0) {
850 free_options(*global);
853 remove_option(global, line, len);
861 process_add_entry(struct archive_read *a, struct mtree *mtree,
862 struct mtree_option **global, const char *line, ssize_t line_len,
863 struct mtree_entry **last_entry, int is_form_d)
865 struct mtree_entry *entry;
866 struct mtree_option *iter;
867 const char *next, *eq, *name, *end;
868 size_t name_len, len;
871 if ((entry = malloc(sizeof(*entry))) == NULL) {
872 archive_set_error(&a->archive, errno, "Can't allocate memory");
873 return (ARCHIVE_FATAL);
876 entry->options = NULL;
881 /* Add this entry to list. */
882 if (*last_entry == NULL)
883 mtree->entries = entry;
885 (*last_entry)->next = entry;
889 /* Filename is last item on line. */
890 /* Adjust line_len to trim trailing whitespace */
891 while (line_len > 0) {
892 char last_character = line[line_len - 1];
893 if (last_character == '\r'
894 || last_character == '\n'
895 || last_character == '\t'
896 || last_character == ' ') {
902 /* Name starts after the last whitespace separator */
904 for (i = 0; i < line_len; i++) {
912 name_len = line + line_len - name;
915 /* Filename is first item on line */
916 name_len = strcspn(line, " \t\r\n");
919 end = line + line_len;
921 /* name/name_len is the name within the line. */
922 /* line..end brackets the entire line except the name */
924 if ((entry->name = malloc(name_len + 1)) == NULL) {
925 archive_set_error(&a->archive, errno, "Can't allocate memory");
926 return (ARCHIVE_FATAL);
929 memcpy(entry->name, name, name_len);
930 entry->name[name_len] = '\0';
931 parse_escapes(entry->name, entry);
933 for (iter = *global; iter != NULL; iter = iter->next) {
934 r = add_option(a, &entry->options, iter->value,
935 strlen(iter->value));
941 next = line + strspn(line, " \t\r\n");
947 next = line + strcspn(line, " \t\r\n");
948 eq = strchr(line, '=');
949 if (eq == NULL || eq > next)
954 remove_option(&entry->options, line, len);
955 r = add_option(a, &entry->options, line, next - line);
963 read_mtree(struct archive_read *a, struct mtree *mtree)
968 struct mtree_option *global;
969 struct mtree_entry *last_entry;
972 mtree->archive_format = ARCHIVE_FORMAT_MTREE;
973 mtree->archive_format_name = "mtree";
978 (void)detect_form(a, &is_form_d);
980 for (counter = 1; ; ++counter) {
981 len = readline(a, mtree, &p, 65536);
983 mtree->this_entry = mtree->entries;
984 free_options(global);
988 free_options(global);
991 /* Leading whitespace is never significant, ignore it. */
992 while (*p == ' ' || *p == '\t') {
996 /* Skip content lines and blank lines. */
999 if (*p == '\r' || *p == '\n' || *p == '\0')
1002 r = process_add_entry(a, mtree, &global, p, len,
1003 &last_entry, is_form_d);
1004 } else if (strncmp(p, "/set", 4) == 0) {
1005 if (p[4] != ' ' && p[4] != '\t')
1007 r = process_global_set(a, &global, p);
1008 } else if (strncmp(p, "/unset", 6) == 0) {
1009 if (p[6] != ' ' && p[6] != '\t')
1011 r = process_global_unset(a, &global, p);
1015 if (r != ARCHIVE_OK) {
1016 free_options(global);
1021 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1022 "Can't parse line %ju", counter);
1023 free_options(global);
1024 return (ARCHIVE_FATAL);
1028 * Read in the entire mtree file into memory on the first request.
1029 * Then use the next unused file to satisfy each header request.
1032 read_header(struct archive_read *a, struct archive_entry *entry)
1034 struct mtree *mtree;
1038 mtree = (struct mtree *)(a->format->data);
1040 if (mtree->fd >= 0) {
1045 if (mtree->entries == NULL) {
1046 mtree->resolver = archive_entry_linkresolver_new();
1047 if (mtree->resolver == NULL)
1048 return ARCHIVE_FATAL;
1049 archive_entry_linkresolver_set_strategy(mtree->resolver,
1050 ARCHIVE_FORMAT_MTREE);
1051 r = read_mtree(a, mtree);
1052 if (r != ARCHIVE_OK)
1056 a->archive.archive_format = mtree->archive_format;
1057 a->archive.archive_format_name = mtree->archive_format_name;
1060 if (mtree->this_entry == NULL)
1061 return (ARCHIVE_EOF);
1062 if (strcmp(mtree->this_entry->name, "..") == 0) {
1063 mtree->this_entry->used = 1;
1064 if (archive_strlen(&mtree->current_dir) > 0) {
1065 /* Roll back current path. */
1066 p = mtree->current_dir.s
1067 + mtree->current_dir.length - 1;
1068 while (p >= mtree->current_dir.s && *p != '/')
1070 if (p >= mtree->current_dir.s)
1072 mtree->current_dir.length
1073 = p - mtree->current_dir.s + 1;
1076 if (!mtree->this_entry->used) {
1078 r = parse_file(a, entry, mtree, mtree->this_entry,
1083 mtree->this_entry = mtree->this_entry->next;
1088 * A single file can have multiple lines contribute specifications.
1089 * Parse as many lines as necessary, then pull additional information
1090 * from a backing file on disk as necessary.
1093 parse_file(struct archive_read *a, struct archive_entry *entry,
1094 struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
1097 struct stat st_storage, *st;
1098 struct mtree_entry *mp;
1099 struct archive_entry *sparse_entry;
1100 int r = ARCHIVE_OK, r1, parsed_kws;
1104 /* Initialize reasonable defaults. */
1105 archive_entry_set_filetype(entry, AE_IFREG);
1106 archive_entry_set_size(entry, 0);
1107 archive_string_empty(&mtree->contents_name);
1109 /* Parse options from this line. */
1111 r = parse_line(a, entry, mtree, mentry, &parsed_kws);
1114 archive_entry_copy_pathname(entry, mentry->name);
1116 * "Full" entries are allowed to have multiple lines
1117 * and those lines aren't required to be adjacent. We
1118 * don't support multiple lines for "relative" entries
1119 * nor do we make any attempt to merge data from
1120 * separate "relative" and "full" entries. (Merging
1121 * "relative" and "full" entries would require dealing
1122 * with pathname canonicalization, which is a very
1125 for (mp = mentry->next; mp != NULL; mp = mp->next) {
1126 if (mp->full && !mp->used
1127 && strcmp(mentry->name, mp->name) == 0) {
1128 /* Later lines override earlier ones. */
1130 r1 = parse_line(a, entry, mtree, mp,
1138 * Relative entries require us to construct
1139 * the full path and possibly update the
1140 * current directory.
1142 size_t n = archive_strlen(&mtree->current_dir);
1144 archive_strcat(&mtree->current_dir, "/");
1145 archive_strcat(&mtree->current_dir, mentry->name);
1146 archive_entry_copy_pathname(entry, mtree->current_dir.s);
1147 if (archive_entry_filetype(entry) != AE_IFDIR)
1148 mtree->current_dir.length = n;
1151 if (mtree->checkfs) {
1153 * Try to open and stat the file to get the real size
1154 * and other file info. It would be nice to avoid
1155 * this here so that getting a listing of an mtree
1156 * wouldn't require opening every referenced contents
1157 * file. But then we wouldn't know the actual
1158 * contents size, so I don't see a really viable way
1159 * around this. (Also, we may want to someday pull
1160 * other unspecified info from the contents file on
1164 if (archive_strlen(&mtree->contents_name) > 0)
1165 path = mtree->contents_name.s;
1167 path = archive_entry_pathname(entry);
1169 if (archive_entry_filetype(entry) == AE_IFREG ||
1170 archive_entry_filetype(entry) == AE_IFDIR) {
1171 mtree->fd = open(path, O_RDONLY | O_BINARY | O_CLOEXEC);
1172 __archive_ensure_cloexec_flag(mtree->fd);
1173 if (mtree->fd == -1 &&
1175 archive_strlen(&mtree->contents_name) > 0)) {
1176 archive_set_error(&a->archive, errno,
1177 "Can't open %s", path);
1183 if (mtree->fd >= 0) {
1184 if (fstat(mtree->fd, st) == -1) {
1185 archive_set_error(&a->archive, errno,
1186 "Could not fstat %s", path);
1188 /* If we can't stat it, don't keep it open. */
1193 } else if (lstat(path, st) == -1) {
1198 * Check for a mismatch between the type in the specification
1199 * and the type of the contents object on disk.
1202 if (((st->st_mode & S_IFMT) == S_IFREG &&
1203 archive_entry_filetype(entry) == AE_IFREG)
1205 ||((st->st_mode & S_IFMT) == S_IFLNK &&
1206 archive_entry_filetype(entry) == AE_IFLNK)
1209 ||((st->st_mode & S_IFSOCK) == S_IFSOCK &&
1210 archive_entry_filetype(entry) == AE_IFSOCK)
1213 ||((st->st_mode & S_IFMT) == S_IFCHR &&
1214 archive_entry_filetype(entry) == AE_IFCHR)
1217 ||((st->st_mode & S_IFMT) == S_IFBLK &&
1218 archive_entry_filetype(entry) == AE_IFBLK)
1220 ||((st->st_mode & S_IFMT) == S_IFDIR &&
1221 archive_entry_filetype(entry) == AE_IFDIR)
1223 ||((st->st_mode & S_IFMT) == S_IFIFO &&
1224 archive_entry_filetype(entry) == AE_IFIFO)
1229 /* Types don't match; bail out gracefully. */
1233 if (parsed_kws & MTREE_HAS_OPTIONAL) {
1234 /* It's not an error for an optional
1235 * entry to not match disk. */
1237 } else if (r == ARCHIVE_OK) {
1238 archive_set_error(&a->archive,
1240 "mtree specification has different"
1242 archive_entry_pathname(entry));
1250 * If there is a contents file on disk, pick some of the
1251 * metadata from that file. For most of these, we only
1252 * set it from the contents if it wasn't already parsed
1253 * from the specification.
1256 if (((parsed_kws & MTREE_HAS_DEVICE) == 0 ||
1257 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) &&
1258 (archive_entry_filetype(entry) == AE_IFCHR ||
1259 archive_entry_filetype(entry) == AE_IFBLK))
1260 archive_entry_set_rdev(entry, st->st_rdev);
1261 if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME))
1263 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1264 archive_entry_set_gid(entry, st->st_gid);
1265 if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME))
1267 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1268 archive_entry_set_uid(entry, st->st_uid);
1269 if ((parsed_kws & MTREE_HAS_MTIME) == 0 ||
1270 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) {
1271 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1272 archive_entry_set_mtime(entry, st->st_mtime,
1273 st->st_mtimespec.tv_nsec);
1274 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1275 archive_entry_set_mtime(entry, st->st_mtime,
1276 st->st_mtim.tv_nsec);
1277 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1278 archive_entry_set_mtime(entry, st->st_mtime,
1280 #elif HAVE_STRUCT_STAT_ST_UMTIME
1281 archive_entry_set_mtime(entry, st->st_mtime,
1282 st->st_umtime*1000);
1283 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
1284 archive_entry_set_mtime(entry, st->st_mtime,
1285 st->st_mtime_usec*1000);
1287 archive_entry_set_mtime(entry, st->st_mtime, 0);
1290 if ((parsed_kws & MTREE_HAS_NLINK) == 0 ||
1291 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1292 archive_entry_set_nlink(entry, st->st_nlink);
1293 if ((parsed_kws & MTREE_HAS_PERM) == 0 ||
1294 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1295 archive_entry_set_perm(entry, st->st_mode);
1296 if ((parsed_kws & MTREE_HAS_SIZE) == 0 ||
1297 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1298 archive_entry_set_size(entry, st->st_size);
1299 archive_entry_set_ino(entry, st->st_ino);
1300 archive_entry_set_dev(entry, st->st_dev);
1302 archive_entry_linkify(mtree->resolver, &entry,
1304 } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
1306 * Couldn't open the entry, stat it or the on-disk type
1307 * didn't match. If this entry is optional, just
1308 * ignore it and read the next header entry.
1315 mtree->cur_size = archive_entry_size(entry);
1322 * Each line contains a sequence of keywords.
1325 parse_line(struct archive_read *a, struct archive_entry *entry,
1326 struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
1328 struct mtree_option *iter;
1329 int r = ARCHIVE_OK, r1;
1331 for (iter = mp->options; iter != NULL; iter = iter->next) {
1332 r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
1336 if (r == ARCHIVE_OK && (*parsed_kws & MTREE_HAS_TYPE) == 0) {
1337 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1338 "Missing type keyword in mtree specification");
1339 return (ARCHIVE_WARN);
1345 * Device entries have one of the following forms:
1347 * - format,major,minor[,subdevice]
1348 * When parsing succeeded, `pdev' will contain the appropriate dev_t value.
1351 /* strsep() is not in C90, but strcspn() is. */
1352 /* Taken from http://unixpapa.com/incnote/string.html */
1354 la_strsep(char **sp, const char *sep)
1357 if (sp == NULL || *sp == NULL || **sp == '\0')
1360 p = s + strcspn(s, sep);
1368 parse_device(dev_t *pdev, struct archive *a, char *val)
1370 #define MAX_PACK_ARGS 3
1371 unsigned long numbers[MAX_PACK_ARGS];
1376 const char *error = NULL;
1378 memset(pdev, 0, sizeof(*pdev));
1379 if ((dev = strchr(val, ',')) != NULL) {
1381 * Device's major/minor are given in a specified format.
1382 * Decode and pack it accordingly.
1385 if ((pack = pack_find(val)) == NULL) {
1386 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1387 "Unknown format `%s'", val);
1388 return ARCHIVE_WARN;
1391 while ((p = la_strsep(&dev, ",")) != NULL) {
1393 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1395 return ARCHIVE_WARN;
1397 if (argc >= MAX_PACK_ARGS) {
1398 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1399 "Too many arguments");
1400 return ARCHIVE_WARN;
1402 numbers[argc++] = (unsigned long)mtree_atol(&p);
1405 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1406 "Not enough arguments");
1407 return ARCHIVE_WARN;
1409 result = (*pack)(argc, numbers, &error);
1410 if (error != NULL) {
1411 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1413 return ARCHIVE_WARN;
1416 /* file system raw value. */
1417 result = (dev_t)mtree_atol(&val);
1421 #undef MAX_PACK_ARGS
1425 * Parse a single keyword and its value.
1428 parse_keyword(struct archive_read *a, struct mtree *mtree,
1429 struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws)
1436 return (ARCHIVE_OK);
1438 if (strcmp(key, "nochange") == 0) {
1439 *parsed_kws |= MTREE_HAS_NOCHANGE;
1440 return (ARCHIVE_OK);
1442 if (strcmp(key, "optional") == 0) {
1443 *parsed_kws |= MTREE_HAS_OPTIONAL;
1444 return (ARCHIVE_OK);
1446 if (strcmp(key, "ignore") == 0) {
1448 * The mtree processing is not recursive, so
1449 * recursion will only happen for explicitly listed
1452 return (ARCHIVE_OK);
1455 val = strchr(key, '=');
1457 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1458 "Malformed attribute \"%s\" (%d)", key, key[0]);
1459 return (ARCHIVE_WARN);
1467 if (strcmp(key, "content") == 0
1468 || strcmp(key, "contents") == 0) {
1469 parse_escapes(val, NULL);
1470 archive_strcpy(&mtree->contents_name, val);
1473 if (strcmp(key, "cksum") == 0)
1476 if (strcmp(key, "device") == 0) {
1477 /* stat(2) st_rdev field, e.g. the major/minor IDs
1478 * of a char/block special file */
1482 *parsed_kws |= MTREE_HAS_DEVICE;
1483 r = parse_device(&dev, &a->archive, val);
1484 if (r == ARCHIVE_OK)
1485 archive_entry_set_rdev(entry, dev);
1489 if (strcmp(key, "flags") == 0) {
1490 *parsed_kws |= MTREE_HAS_FFLAGS;
1491 archive_entry_copy_fflags_text(entry, val);
1495 if (strcmp(key, "gid") == 0) {
1496 *parsed_kws |= MTREE_HAS_GID;
1497 archive_entry_set_gid(entry, mtree_atol10(&val));
1500 if (strcmp(key, "gname") == 0) {
1501 *parsed_kws |= MTREE_HAS_GNAME;
1502 archive_entry_copy_gname(entry, val);
1506 if (strcmp(key, "inode") == 0) {
1507 archive_entry_set_ino(entry, mtree_atol10(&val));
1511 if (strcmp(key, "link") == 0) {
1512 archive_entry_copy_symlink(entry, val);
1516 if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
1518 if (strcmp(key, "mode") == 0) {
1519 if (val[0] >= '0' && val[0] <= '9') {
1520 *parsed_kws |= MTREE_HAS_PERM;
1521 archive_entry_set_perm(entry,
1522 (mode_t)mtree_atol8(&val));
1524 archive_set_error(&a->archive,
1525 ARCHIVE_ERRNO_FILE_FORMAT,
1526 "Symbolic mode \"%s\" unsupported", val);
1527 return ARCHIVE_WARN;
1532 if (strcmp(key, "nlink") == 0) {
1533 *parsed_kws |= MTREE_HAS_NLINK;
1534 archive_entry_set_nlink(entry,
1535 (unsigned int)mtree_atol10(&val));
1539 if (strcmp(key, "resdevice") == 0) {
1540 /* stat(2) st_dev field, e.g. the device ID where the
1545 r = parse_device(&dev, &a->archive, val);
1546 if (r == ARCHIVE_OK)
1547 archive_entry_set_dev(entry, dev);
1550 if (strcmp(key, "rmd160") == 0 ||
1551 strcmp(key, "rmd160digest") == 0)
1554 if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
1556 if (strcmp(key, "sha256") == 0 ||
1557 strcmp(key, "sha256digest") == 0)
1559 if (strcmp(key, "sha384") == 0 ||
1560 strcmp(key, "sha384digest") == 0)
1562 if (strcmp(key, "sha512") == 0 ||
1563 strcmp(key, "sha512digest") == 0)
1565 if (strcmp(key, "size") == 0) {
1566 archive_entry_set_size(entry, mtree_atol10(&val));
1570 if (strcmp(key, "tags") == 0) {
1572 * Comma delimited list of tags.
1573 * Ignore the tags for now, but the interface
1574 * should be extended to allow inclusion/exclusion.
1578 if (strcmp(key, "time") == 0) {
1580 int64_t my_time_t_max = get_time_t_max();
1581 int64_t my_time_t_min = get_time_t_min();
1584 *parsed_kws |= MTREE_HAS_MTIME;
1585 m = mtree_atol10(&val);
1586 /* Replicate an old mtree bug:
1587 * 123456789.1 represents 123456789
1588 * seconds and 1 nanosecond. */
1591 ns = (long)mtree_atol10(&val);
1594 if (m > my_time_t_max)
1596 else if (m < my_time_t_min)
1598 archive_entry_set_mtime(entry, (time_t)m, ns);
1601 if (strcmp(key, "type") == 0) {
1604 if (strcmp(val, "block") == 0) {
1605 archive_entry_set_filetype(entry, AE_IFBLK);
1609 if (strcmp(val, "char") == 0) {
1610 archive_entry_set_filetype(entry,
1615 if (strcmp(val, "dir") == 0) {
1616 archive_entry_set_filetype(entry,
1621 if (strcmp(val, "fifo") == 0) {
1622 archive_entry_set_filetype(entry,
1626 if (strcmp(val, "file") == 0) {
1627 archive_entry_set_filetype(entry,
1632 if (strcmp(val, "link") == 0) {
1633 archive_entry_set_filetype(entry,
1638 archive_set_error(&a->archive,
1639 ARCHIVE_ERRNO_FILE_FORMAT,
1640 "Unrecognized file type \"%s\"; "
1641 "assuming \"file\"", val);
1642 archive_entry_set_filetype(entry, AE_IFREG);
1643 return (ARCHIVE_WARN);
1645 *parsed_kws |= MTREE_HAS_TYPE;
1649 if (strcmp(key, "uid") == 0) {
1650 *parsed_kws |= MTREE_HAS_UID;
1651 archive_entry_set_uid(entry, mtree_atol10(&val));
1654 if (strcmp(key, "uname") == 0) {
1655 *parsed_kws |= MTREE_HAS_UNAME;
1656 archive_entry_copy_uname(entry, val);
1660 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1661 "Unrecognized key %s=%s", key, val);
1662 return (ARCHIVE_WARN);
1664 return (ARCHIVE_OK);
1668 read_data(struct archive_read *a, const void **buff, size_t *size,
1671 size_t bytes_to_read;
1673 struct mtree *mtree;
1675 mtree = (struct mtree *)(a->format->data);
1676 if (mtree->fd < 0) {
1680 return (ARCHIVE_EOF);
1682 if (mtree->buff == NULL) {
1683 mtree->buffsize = 64 * 1024;
1684 mtree->buff = malloc(mtree->buffsize);
1685 if (mtree->buff == NULL) {
1686 archive_set_error(&a->archive, ENOMEM,
1687 "Can't allocate memory");
1688 return (ARCHIVE_FATAL);
1692 *buff = mtree->buff;
1693 *offset = mtree->offset;
1694 if ((int64_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1695 bytes_to_read = (size_t)(mtree->cur_size - mtree->offset);
1697 bytes_to_read = mtree->buffsize;
1698 bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1699 if (bytes_read < 0) {
1700 archive_set_error(&a->archive, errno, "Can't read");
1701 return (ARCHIVE_WARN);
1703 if (bytes_read == 0) {
1705 return (ARCHIVE_EOF);
1707 mtree->offset += bytes_read;
1709 return (ARCHIVE_OK);
1712 /* Skip does nothing except possibly close the contents file. */
1714 skip(struct archive_read *a)
1716 struct mtree *mtree;
1718 mtree = (struct mtree *)(a->format->data);
1719 if (mtree->fd >= 0) {
1723 return (ARCHIVE_OK);
1727 * Since parsing backslash sequences always makes strings shorter,
1728 * we can always do this conversion in-place.
1731 parse_escapes(char *src, struct mtree_entry *mentry)
1736 if (mentry != NULL && strcmp(src, ".") == 0)
1739 while (*src != '\0') {
1741 if (c == '/' && mentry != NULL)
1746 if (src[1] < '0' || src[1] > '7') {
1755 if (src[1] >= '0' && src[1] <= '7' &&
1756 src[2] >= '0' && src[2] <= '7') {
1757 c = (src[0] - '0') << 6;
1758 c |= (src[1] - '0') << 3;
1759 c |= (src[2] - '0');
1807 * Note that this implementation does not (and should not!) obey
1808 * locale settings; you cannot simply substitute strtol here, since
1809 * it does obey locale.
1812 mtree_atol8(char **p)
1814 int64_t l, limit, last_digit_limit;
1818 limit = INT64_MAX / base;
1819 last_digit_limit = INT64_MAX % base;
1823 while (digit >= 0 && digit < base) {
1824 if (l>limit || (l == limit && digit > last_digit_limit)) {
1825 l = INT64_MAX; /* Truncate on overflow. */
1828 l = (l * base) + digit;
1829 digit = *++(*p) - '0';
1835 * Note that this implementation does not (and should not!) obey
1836 * locale settings; you cannot simply substitute strtol here, since
1837 * it does obey locale.
1840 mtree_atol10(char **p)
1842 int64_t l, limit, last_digit_limit;
1843 int base, digit, sign;
1849 limit = ((uint64_t)(INT64_MAX) + 1) / base;
1850 last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
1854 limit = INT64_MAX / base;
1855 last_digit_limit = INT64_MAX % base;
1860 while (digit >= 0 && digit < base) {
1861 if (l > limit || (l == limit && digit > last_digit_limit))
1862 return (sign < 0) ? INT64_MIN : INT64_MAX;
1863 l = (l * base) + digit;
1864 digit = *++(*p) - '0';
1866 return (sign < 0) ? -l : l;
1869 /* Parse a hex digit. */
1873 if (c >= '0' && c <= '9')
1875 else if (c >= 'a' && c <= 'f')
1877 else if (c >= 'A' && c <= 'F')
1884 * Note that this implementation does not (and should not!) obey
1885 * locale settings; you cannot simply substitute strtol here, since
1886 * it does obey locale.
1889 mtree_atol16(char **p)
1891 int64_t l, limit, last_digit_limit;
1892 int base, digit, sign;
1898 limit = ((uint64_t)(INT64_MAX) + 1) / base;
1899 last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
1903 limit = INT64_MAX / base;
1904 last_digit_limit = INT64_MAX % base;
1908 digit = parsehex(**p);
1909 while (digit >= 0 && digit < base) {
1910 if (l > limit || (l == limit && digit > last_digit_limit))
1911 return (sign < 0) ? INT64_MIN : INT64_MAX;
1912 l = (l * base) + digit;
1913 digit = parsehex(*++(*p));
1915 return (sign < 0) ? -l : l;
1919 mtree_atol(char **p)
1922 return mtree_atol10(p);
1923 if ((*p)[1] == 'x' || (*p)[1] == 'X') {
1925 return mtree_atol16(p);
1927 return mtree_atol8(p);
1931 * Returns length of line (including trailing newline)
1932 * or negative on error. 'start' argument is updated to
1933 * point to first character of line.
1936 readline(struct archive_read *a, struct mtree *mtree, char **start,
1940 ssize_t total_size = 0;
1941 ssize_t find_off = 0;
1946 /* Accumulate line in a line buffer. */
1948 /* Read some more. */
1949 t = __archive_read_ahead(a, 1, &bytes_read);
1953 return (ARCHIVE_FATAL);
1954 nl = memchr(t, '\n', bytes_read);
1955 /* If we found '\n', trim the read to end exactly there. */
1957 bytes_read = ((const char *)nl) - ((const char *)t) + 1;
1959 if (total_size + bytes_read + 1 > limit) {
1960 archive_set_error(&a->archive,
1961 ARCHIVE_ERRNO_FILE_FORMAT,
1963 return (ARCHIVE_FATAL);
1965 if (archive_string_ensure(&mtree->line,
1966 total_size + bytes_read + 1) == NULL) {
1967 archive_set_error(&a->archive, ENOMEM,
1968 "Can't allocate working buffer");
1969 return (ARCHIVE_FATAL);
1971 /* Append new bytes to string. */
1972 memcpy(mtree->line.s + total_size, t, bytes_read);
1973 __archive_read_consume(a, bytes_read);
1974 total_size += bytes_read;
1975 mtree->line.s[total_size] = '\0';
1977 for (u = mtree->line.s + find_off; *u; ++u) {
1979 /* Ends with unescaped newline. */
1980 *start = mtree->line.s;
1982 } else if (u[0] == '#') {
1983 /* Ends with comment sequence #...\n */
1985 /* But we've not found the \n yet */
1988 } else if (u[0] == '\\') {
1990 /* Trim escaped newline. */
1992 mtree->line.s[total_size] = '\0';
1994 } else if (u[1] != '\0') {
1995 /* Skip the two-char escape sequence */
2000 find_off = u - mtree->line.s;