2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2008 Joerg Sonnenberger
4 * Copyright (c) 2011 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_read_private.h"
53 #include "archive_string.h"
59 #define MTREE_HAS_DEVICE 0x0001
60 #define MTREE_HAS_FFLAGS 0x0002
61 #define MTREE_HAS_GID 0x0004
62 #define MTREE_HAS_GNAME 0x0008
63 #define MTREE_HAS_MTIME 0x0010
64 #define MTREE_HAS_NLINK 0x0020
65 #define MTREE_HAS_PERM 0x0040
66 #define MTREE_HAS_SIZE 0x0080
67 #define MTREE_HAS_TYPE 0x0100
68 #define MTREE_HAS_UID 0x0200
69 #define MTREE_HAS_UNAME 0x0400
71 #define MTREE_HAS_OPTIONAL 0x0800
74 struct mtree_option *next;
79 struct mtree_entry *next;
80 struct mtree_option *options;
87 struct archive_string line;
93 const char *archive_format_name;
94 struct mtree_entry *entries;
95 struct mtree_entry *this_entry;
96 struct archive_string current_dir;
97 struct archive_string contents_name;
99 struct archive_entry_linkresolver *resolver;
104 static int cleanup(struct archive_read *);
105 static int mtree_bid(struct archive_read *, int);
106 static int parse_file(struct archive_read *, struct archive_entry *,
107 struct mtree *, struct mtree_entry *, int *);
108 static void parse_escapes(char *, struct mtree_entry *);
109 static int parse_line(struct archive_read *, struct archive_entry *,
110 struct mtree *, struct mtree_entry *, int *);
111 static int parse_keyword(struct archive_read *, struct mtree *,
112 struct archive_entry *, struct mtree_option *, int *);
113 static int read_data(struct archive_read *a,
114 const void **buff, size_t *size, int64_t *offset);
115 static ssize_t readline(struct archive_read *, struct mtree *, char **, ssize_t);
116 static int skip(struct archive_read *a);
117 static int read_header(struct archive_read *,
118 struct archive_entry *);
119 static int64_t mtree_atol10(char **);
120 static int64_t mtree_atol8(char **);
121 static int64_t mtree_atol(char **);
124 * There's no standard for TIME_T_MAX/TIME_T_MIN. So we compute them
125 * here. TODO: Move this to configure time, but be careful
126 * about cross-compile environments.
131 #if defined(TIME_T_MAX)
150 #if defined(TIME_T_MIN)
153 /* 't' will hold the minimum value, which will be zero (if
154 * time_t is unsigned) or -2^n (if time_t is signed). */
171 free_options(struct mtree_option *head)
173 struct mtree_option *next;
175 for (; head != NULL; head = next) {
183 archive_read_support_format_mtree(struct archive *_a)
185 struct archive_read *a = (struct archive_read *)_a;
189 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
190 ARCHIVE_STATE_NEW, "archive_read_support_format_mtree");
192 mtree = (struct mtree *)malloc(sizeof(*mtree));
194 archive_set_error(&a->archive, ENOMEM,
195 "Can't allocate mtree data");
196 return (ARCHIVE_FATAL);
198 memset(mtree, 0, sizeof(*mtree));
201 r = __archive_read_register_format(a, mtree, "mtree",
202 mtree_bid, NULL, read_header, read_data, skip, cleanup);
210 cleanup(struct archive_read *a)
213 struct mtree_entry *p, *q;
215 mtree = (struct mtree *)(a->format->data);
221 free_options(p->options);
225 archive_string_free(&mtree->line);
226 archive_string_free(&mtree->current_dir);
227 archive_string_free(&mtree->contents_name);
228 archive_entry_linkresolver_free(mtree->resolver);
232 (a->format->data) = NULL;
237 get_line_size(const char *b, ssize_t avail, ssize_t *nlsize)
242 while (len < avail) {
244 case '\0':/* Non-ascii character or control character. */
249 if (avail-len > 1 && b[1] == '\n') {
271 next_line(struct archive_read *a,
272 const char **b, ssize_t *avail, ssize_t *ravail, ssize_t *nl)
282 len = get_line_size(*b, *avail, nl);
284 * Read bytes more while it does not reach the end of line.
286 while (*nl == 0 && len == *avail && !quit) {
287 ssize_t diff = *ravail - *avail;
288 size_t nbytes_req = (*ravail+1023) & ~1023U;
291 /* Increase reading bytes if it is not enough to at least
293 if (nbytes_req < (size_t)*ravail + 160)
296 *b = __archive_read_ahead(a, nbytes_req, avail);
298 if (*ravail >= *avail)
300 /* Reading bytes reaches the end of file. */
301 *b = __archive_read_ahead(a, *avail, avail);
307 tested = len;/* Skip some bytes we already determinated. */
308 len = get_line_size(*b, *avail, nl);
316 * Compare characters with a mtree keyword.
317 * Returns the length of a mtree keyword if matched.
318 * Returns 0 if not matched.
321 bid_keycmp(const char *p, const char *key, ssize_t len)
325 while (len > 0 && *p && *key) {
333 return (0);/* Not match */
336 return (0);/* Not match */
338 /* A following character should be specified characters */
339 if (p[0] == '=' || p[0] == ' ' || p[0] == '\t' ||
340 p[0] == '\n' || p[0] == '\r' ||
341 (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r')))
343 return (0);/* Not match */
347 * Test whether the characters 'p' has is mtree keyword.
348 * Returns the length of a detected keyword.
349 * Returns 0 if any keywords were not found.
352 bid_keyword(const char *p, ssize_t len)
354 static const char *keys_c[] = {
355 "content", "contents", "cksum", NULL
357 static const char *keys_df[] = {
358 "device", "flags", NULL
360 static const char *keys_g[] = {
363 static const char *keys_il[] = {
364 "ignore", "link", NULL
366 static const char *keys_m[] = {
367 "md5", "md5digest", "mode", NULL
369 static const char *keys_no[] = {
370 "nlink", "optional", NULL
372 static const char *keys_r[] = {
373 "rmd160", "rmd160digest", NULL
375 static const char *keys_s[] = {
376 "sha1", "sha1digest",
377 "sha256", "sha256digest",
378 "sha384", "sha384digest",
379 "sha512", "sha512digest",
382 static const char *keys_t[] = {
383 "tags", "time", "type", NULL
385 static const char *keys_u[] = {
392 case 'c': keys = keys_c; break;
393 case 'd': case 'f': keys = keys_df; break;
394 case 'g': keys = keys_g; break;
395 case 'i': case 'l': keys = keys_il; break;
396 case 'm': keys = keys_m; break;
397 case 'n': case 'o': keys = keys_no; break;
398 case 'r': keys = keys_r; break;
399 case 's': keys = keys_s; break;
400 case 't': keys = keys_t; break;
401 case 'u': keys = keys_u; break;
402 default: return (0);/* Unknown key */
405 for (i = 0; keys[i] != NULL; i++) {
406 int l = bid_keycmp(p, keys[i], len);
410 return (0);/* Unknown key */
414 * Test whether there is a set of mtree keywords.
415 * Returns the number of keyword.
416 * Returns -1 if we got incorrect sequence.
417 * This function expects a set of "<space characters>keyword=value".
418 * When "unset" is specified, expects a set of "<space characters>keyword".
421 bid_keyword_list(const char *p, ssize_t len, int unset)
426 while (len > 0 && *p) {
429 /* Test whether there are blank characters in the line. */
430 while (len >0 && (*p == ' ' || *p == '\t')) {
435 if (*p == '\n' || *p == '\r')
437 if (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r'))
439 if (!blank) /* No blank character. */
443 l = bid_keycmp(p, "all", len);
447 /* Test whether there is a correct key in the line. */
448 l = bid_keyword(p, len);
450 return (-1);/* Unknown keyword was found. */
460 while (len > 0 && *p != ' ' && *p != '\t') {
465 /* A keyword should have a its value unless
466 * "/unset" operation. */
467 if (!unset && value == 0)
475 bid_entry(const char *p, ssize_t len)
478 static const unsigned char safe_char[256] = {
479 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
480 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
481 /* !"$%&'()*+,-./ EXCLUSION:( )(#) */
482 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
483 /* 0123456789:;<>? EXCLUSION:(=) */
484 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
485 /* @ABCDEFGHIJKLMNO */
486 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
487 /* PQRSTUVWXYZ[\]^_ */
488 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
489 /* `abcdefghijklmno */
490 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
491 /* pqrstuvwxyz{|}~ */
492 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
493 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
494 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
495 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
496 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
497 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
498 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
499 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
500 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
504 * Skip the path-name which is quoted.
506 while (len > 0 && *p != ' ' && *p != '\t') {
507 if (!safe_char[*(const unsigned char *)p])
513 /* If a path-name was not found, returns error. */
517 return (bid_keyword_list(p, len, 0));
520 #define MAX_BID_ENTRY 3
523 mtree_bid(struct archive_read *a, int best_bid)
525 const char *signature = "#mtree";
527 ssize_t avail, ravail;
529 int detected_bytes = 0, entry_cnt = 0, multiline = 0;
531 (void)best_bid; /* UNUSED */
533 /* Now let's look at the actual header and see if it matches. */
534 p = __archive_read_ahead(a, strlen(signature), &avail);
538 if (memcmp(p, signature, strlen(signature)) == 0)
539 return (8 * (int)strlen(signature));
542 * There is not a mtree signature. Let's try to detect mtree format.
546 len = next_line(a, &p, &avail, &ravail, &nl);
547 /* The terminal character of the line should be
548 * a new line character, '\r\n' or '\n'. */
549 if (len <= 0 || nl == 0)
552 /* Leading whitespace is never significant,
554 while (len > 0 && (*p == ' ' || *p == '\t')) {
559 /* Skip comment or empty line. */
560 if (p[0] == '#' || p[0] == '\n' || p[0] == '\r') {
566 /* A continuance line; the terminal
567 * character of previous line was '\' character. */
568 if (bid_keyword_list(p, len, 0) <= 0)
571 detected_bytes += len;
572 if (p[len-nl-1] != '\\') {
573 if (multiline == 1 &&
574 ++entry_cnt >= MAX_BID_ENTRY)
583 if (bid_entry(p, len) >= 0) {
584 detected_bytes += len;
585 if (p[len-nl-1] == '\\')
586 /* This line continues. */
589 /* We've got plenty of correct lines
590 * to assume that this file is a mtree
592 if (++entry_cnt >= MAX_BID_ENTRY)
597 } else if (strncmp(p, "/set", 4) == 0) {
598 if (bid_keyword_list(p+4, len-4, 0) <= 0)
600 /* This line continues. */
601 if (p[len-nl-1] == '\\')
603 } else if (strncmp(p, "/unset", 6) == 0) {
604 if (bid_keyword_list(p+6, len-6, 1) <= 0)
606 /* This line continues. */
607 if (p[len-nl-1] == '\\')
612 /* Test next line. */
616 if (entry_cnt >= MAX_BID_ENTRY || (entry_cnt > 0 && len == 0))
623 * The extended mtree format permits multiple lines specifying
624 * attributes for each file. For those entries, only the last line
625 * is actually used. Practically speaking, that means we have
626 * to read the entire mtree file into memory up front.
628 * The parsing is done in two steps. First, it is decided if a line
629 * changes the global defaults and if it is, processed accordingly.
630 * Otherwise, the options of the line are merged with the current
634 add_option(struct archive_read *a, struct mtree_option **global,
635 const char *value, size_t len)
637 struct mtree_option *opt;
639 if ((opt = malloc(sizeof(*opt))) == NULL) {
640 archive_set_error(&a->archive, errno, "Can't allocate memory");
641 return (ARCHIVE_FATAL);
643 if ((opt->value = malloc(len + 1)) == NULL) {
645 archive_set_error(&a->archive, errno, "Can't allocate memory");
646 return (ARCHIVE_FATAL);
648 memcpy(opt->value, value, len);
649 opt->value[len] = '\0';
656 remove_option(struct mtree_option **global, const char *value, size_t len)
658 struct mtree_option *iter, *last;
661 for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
662 if (strncmp(iter->value, value, len) == 0 &&
663 (iter->value[len] == '\0' ||
664 iter->value[len] == '='))
670 *global = iter->next;
672 last->next = iter->next;
679 process_global_set(struct archive_read *a,
680 struct mtree_option **global, const char *line)
682 const char *next, *eq;
688 next = line + strspn(line, " \t\r\n");
692 next = line + strcspn(line, " \t\r\n");
693 eq = strchr(line, '=');
699 remove_option(global, line, len);
700 r = add_option(a, global, line, next - line);
708 process_global_unset(struct archive_read *a,
709 struct mtree_option **global, const char *line)
715 if (strchr(line, '=') != NULL) {
716 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
717 "/unset shall not contain `='");
718 return ARCHIVE_FATAL;
722 next = line + strspn(line, " \t\r\n");
726 len = strcspn(line, " \t\r\n");
728 if (len == 3 && strncmp(line, "all", 3) == 0) {
729 free_options(*global);
732 remove_option(global, line, len);
740 process_add_entry(struct archive_read *a, struct mtree *mtree,
741 struct mtree_option **global, const char *line,
742 struct mtree_entry **last_entry)
744 struct mtree_entry *entry;
745 struct mtree_option *iter;
746 const char *next, *eq;
750 if ((entry = malloc(sizeof(*entry))) == NULL) {
751 archive_set_error(&a->archive, errno, "Can't allocate memory");
752 return (ARCHIVE_FATAL);
755 entry->options = NULL;
760 /* Add this entry to list. */
761 if (*last_entry == NULL)
762 mtree->entries = entry;
764 (*last_entry)->next = entry;
767 len = strcspn(line, " \t\r\n");
768 if ((entry->name = malloc(len + 1)) == NULL) {
769 archive_set_error(&a->archive, errno, "Can't allocate memory");
770 return (ARCHIVE_FATAL);
773 memcpy(entry->name, line, len);
774 entry->name[len] = '\0';
775 parse_escapes(entry->name, entry);
778 for (iter = *global; iter != NULL; iter = iter->next) {
779 r = add_option(a, &entry->options, iter->value,
780 strlen(iter->value));
786 next = line + strspn(line, " \t\r\n");
790 next = line + strcspn(line, " \t\r\n");
791 eq = strchr(line, '=');
792 if (eq == NULL || eq > next)
797 remove_option(&entry->options, line, len);
798 r = add_option(a, &entry->options, line, next - line);
806 read_mtree(struct archive_read *a, struct mtree *mtree)
811 struct mtree_option *global;
812 struct mtree_entry *last_entry;
815 mtree->archive_format = ARCHIVE_FORMAT_MTREE;
816 mtree->archive_format_name = "mtree";
821 for (counter = 1; ; ++counter) {
822 len = readline(a, mtree, &p, 65536);
824 mtree->this_entry = mtree->entries;
825 free_options(global);
829 free_options(global);
832 /* Leading whitespace is never significant, ignore it. */
833 while (*p == ' ' || *p == '\t') {
837 /* Skip content lines and blank lines. */
840 if (*p == '\r' || *p == '\n' || *p == '\0')
843 r = process_add_entry(a, mtree, &global, p,
845 } else if (strncmp(p, "/set", 4) == 0) {
846 if (p[4] != ' ' && p[4] != '\t')
848 r = process_global_set(a, &global, p);
849 } else if (strncmp(p, "/unset", 6) == 0) {
850 if (p[6] != ' ' && p[6] != '\t')
852 r = process_global_unset(a, &global, p);
856 if (r != ARCHIVE_OK) {
857 free_options(global);
862 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
863 "Can't parse line %ju", counter);
864 free_options(global);
865 return (ARCHIVE_FATAL);
869 * Read in the entire mtree file into memory on the first request.
870 * Then use the next unused file to satisfy each header request.
873 read_header(struct archive_read *a, struct archive_entry *entry)
879 mtree = (struct mtree *)(a->format->data);
881 if (mtree->fd >= 0) {
886 if (mtree->entries == NULL) {
887 mtree->resolver = archive_entry_linkresolver_new();
888 if (mtree->resolver == NULL)
889 return ARCHIVE_FATAL;
890 archive_entry_linkresolver_set_strategy(mtree->resolver,
891 ARCHIVE_FORMAT_MTREE);
892 r = read_mtree(a, mtree);
897 a->archive.archive_format = mtree->archive_format;
898 a->archive.archive_format_name = mtree->archive_format_name;
901 if (mtree->this_entry == NULL)
902 return (ARCHIVE_EOF);
903 if (strcmp(mtree->this_entry->name, "..") == 0) {
904 mtree->this_entry->used = 1;
905 if (archive_strlen(&mtree->current_dir) > 0) {
906 /* Roll back current path. */
907 p = mtree->current_dir.s
908 + mtree->current_dir.length - 1;
909 while (p >= mtree->current_dir.s && *p != '/')
911 if (p >= mtree->current_dir.s)
913 mtree->current_dir.length
914 = p - mtree->current_dir.s + 1;
917 if (!mtree->this_entry->used) {
919 r = parse_file(a, entry, mtree, mtree->this_entry, &use_next);
923 mtree->this_entry = mtree->this_entry->next;
928 * A single file can have multiple lines contribute specifications.
929 * Parse as many lines as necessary, then pull additional information
930 * from a backing file on disk as necessary.
933 parse_file(struct archive_read *a, struct archive_entry *entry,
934 struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
937 struct stat st_storage, *st;
938 struct mtree_entry *mp;
939 struct archive_entry *sparse_entry;
940 int r = ARCHIVE_OK, r1, parsed_kws;
944 /* Initialize reasonable defaults. */
945 archive_entry_set_filetype(entry, AE_IFREG);
946 archive_entry_set_size(entry, 0);
947 archive_string_empty(&mtree->contents_name);
949 /* Parse options from this line. */
951 r = parse_line(a, entry, mtree, mentry, &parsed_kws);
954 archive_entry_copy_pathname(entry, mentry->name);
956 * "Full" entries are allowed to have multiple lines
957 * and those lines aren't required to be adjacent. We
958 * don't support multiple lines for "relative" entries
959 * nor do we make any attempt to merge data from
960 * separate "relative" and "full" entries. (Merging
961 * "relative" and "full" entries would require dealing
962 * with pathname canonicalization, which is a very
965 for (mp = mentry->next; mp != NULL; mp = mp->next) {
966 if (mp->full && !mp->used
967 && strcmp(mentry->name, mp->name) == 0) {
968 /* Later lines override earlier ones. */
970 r1 = parse_line(a, entry, mtree, mp,
978 * Relative entries require us to construct
979 * the full path and possibly update the
982 size_t n = archive_strlen(&mtree->current_dir);
984 archive_strcat(&mtree->current_dir, "/");
985 archive_strcat(&mtree->current_dir, mentry->name);
986 archive_entry_copy_pathname(entry, mtree->current_dir.s);
987 if (archive_entry_filetype(entry) != AE_IFDIR)
988 mtree->current_dir.length = n;
992 * Try to open and stat the file to get the real size
993 * and other file info. It would be nice to avoid
994 * this here so that getting a listing of an mtree
995 * wouldn't require opening every referenced contents
996 * file. But then we wouldn't know the actual
997 * contents size, so I don't see a really viable way
998 * around this. (Also, we may want to someday pull
999 * other unspecified info from the contents file on
1003 if (archive_strlen(&mtree->contents_name) > 0)
1004 path = mtree->contents_name.s;
1006 path = archive_entry_pathname(entry);
1008 if (archive_entry_filetype(entry) == AE_IFREG ||
1009 archive_entry_filetype(entry) == AE_IFDIR) {
1010 mtree->fd = open(path, O_RDONLY | O_BINARY);
1011 if (mtree->fd == -1 &&
1013 archive_strlen(&mtree->contents_name) > 0)) {
1014 archive_set_error(&a->archive, errno,
1015 "Can't open %s", path);
1021 if (mtree->fd >= 0) {
1022 if (fstat(mtree->fd, st) == -1) {
1023 archive_set_error(&a->archive, errno,
1024 "Could not fstat %s", path);
1026 /* If we can't stat it, don't keep it open. */
1031 } else if (lstat(path, st) == -1) {
1036 * Check for a mismatch between the type in the specification and
1037 * the type of the contents object on disk.
1041 ((st->st_mode & S_IFMT) == S_IFREG &&
1042 archive_entry_filetype(entry) == AE_IFREG)
1044 || ((st->st_mode & S_IFMT) == S_IFLNK &&
1045 archive_entry_filetype(entry) == AE_IFLNK)
1048 || ((st->st_mode & S_IFSOCK) == S_IFSOCK &&
1049 archive_entry_filetype(entry) == AE_IFSOCK)
1052 || ((st->st_mode & S_IFMT) == S_IFCHR &&
1053 archive_entry_filetype(entry) == AE_IFCHR)
1056 || ((st->st_mode & S_IFMT) == S_IFBLK &&
1057 archive_entry_filetype(entry) == AE_IFBLK)
1059 || ((st->st_mode & S_IFMT) == S_IFDIR &&
1060 archive_entry_filetype(entry) == AE_IFDIR)
1062 || ((st->st_mode & S_IFMT) == S_IFIFO &&
1063 archive_entry_filetype(entry) == AE_IFIFO)
1068 /* Types don't match; bail out gracefully. */
1072 if (parsed_kws & MTREE_HAS_OPTIONAL) {
1073 /* It's not an error for an optional entry
1074 to not match disk. */
1076 } else if (r == ARCHIVE_OK) {
1077 archive_set_error(&a->archive,
1079 "mtree specification has different type for %s",
1080 archive_entry_pathname(entry));
1088 * If there is a contents file on disk, pick some of the metadata
1089 * from that file. For most of these, we only set it from the contents
1090 * if it wasn't already parsed from the specification.
1093 if ((parsed_kws & MTREE_HAS_DEVICE) == 0 &&
1094 (archive_entry_filetype(entry) == AE_IFCHR ||
1095 archive_entry_filetype(entry) == AE_IFBLK))
1096 archive_entry_set_rdev(entry, st->st_rdev);
1097 if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME)) == 0)
1098 archive_entry_set_gid(entry, st->st_gid);
1099 if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME)) == 0)
1100 archive_entry_set_uid(entry, st->st_uid);
1101 if ((parsed_kws & MTREE_HAS_MTIME) == 0) {
1102 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1103 archive_entry_set_mtime(entry, st->st_mtime,
1104 st->st_mtimespec.tv_nsec);
1105 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1106 archive_entry_set_mtime(entry, st->st_mtime,
1107 st->st_mtim.tv_nsec);
1108 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1109 archive_entry_set_mtime(entry, st->st_mtime,
1111 #elif HAVE_STRUCT_STAT_ST_UMTIME
1112 archive_entry_set_mtime(entry, st->st_mtime,
1113 st->st_umtime*1000);
1114 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
1115 archive_entry_set_mtime(entry, st->st_mtime,
1116 st->st_mtime_usec*1000);
1118 archive_entry_set_mtime(entry, st->st_mtime, 0);
1121 if ((parsed_kws & MTREE_HAS_NLINK) == 0)
1122 archive_entry_set_nlink(entry, st->st_nlink);
1123 if ((parsed_kws & MTREE_HAS_PERM) == 0)
1124 archive_entry_set_perm(entry, st->st_mode);
1125 if ((parsed_kws & MTREE_HAS_SIZE) == 0)
1126 archive_entry_set_size(entry, st->st_size);
1127 archive_entry_set_ino(entry, st->st_ino);
1128 archive_entry_set_dev(entry, st->st_dev);
1130 archive_entry_linkify(mtree->resolver, &entry, &sparse_entry);
1131 } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
1133 * Couldn't open the entry, stat it or the on-disk type
1134 * didn't match. If this entry is optional, just ignore it
1135 * and read the next header entry.
1141 mtree->cur_size = archive_entry_size(entry);
1148 * Each line contains a sequence of keywords.
1151 parse_line(struct archive_read *a, struct archive_entry *entry,
1152 struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
1154 struct mtree_option *iter;
1155 int r = ARCHIVE_OK, r1;
1157 for (iter = mp->options; iter != NULL; iter = iter->next) {
1158 r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
1162 if (r == ARCHIVE_OK && (*parsed_kws & MTREE_HAS_TYPE) == 0) {
1163 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1164 "Missing type keyword in mtree specification");
1165 return (ARCHIVE_WARN);
1171 * Device entries have one of the following forms:
1173 * format,major,minor[,subdevice]
1175 * Just use major and minor, no translation etc is done
1179 parse_device(struct archive *a, struct archive_entry *entry, char *val)
1181 char *comma1, *comma2;
1183 comma1 = strchr(val, ',');
1184 if (comma1 == NULL) {
1185 archive_entry_set_dev(entry, mtree_atol10(&val));
1186 return (ARCHIVE_OK);
1189 comma2 = strchr(comma1, ',');
1190 if (comma2 == NULL) {
1191 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1192 "Malformed device attribute");
1193 return (ARCHIVE_WARN);
1196 archive_entry_set_rdevmajor(entry, mtree_atol(&comma1));
1197 archive_entry_set_rdevminor(entry, mtree_atol(&comma2));
1198 return (ARCHIVE_OK);
1202 * Parse a single keyword and its value.
1205 parse_keyword(struct archive_read *a, struct mtree *mtree,
1206 struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws)
1213 return (ARCHIVE_OK);
1215 if (strcmp(key, "optional") == 0) {
1216 *parsed_kws |= MTREE_HAS_OPTIONAL;
1217 return (ARCHIVE_OK);
1219 if (strcmp(key, "ignore") == 0) {
1221 * The mtree processing is not recursive, so
1222 * recursion will only happen for explicitly listed
1225 return (ARCHIVE_OK);
1228 val = strchr(key, '=');
1230 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1231 "Malformed attribute \"%s\" (%d)", key, key[0]);
1232 return (ARCHIVE_WARN);
1240 if (strcmp(key, "content") == 0
1241 || strcmp(key, "contents") == 0) {
1242 parse_escapes(val, NULL);
1243 archive_strcpy(&mtree->contents_name, val);
1246 if (strcmp(key, "cksum") == 0)
1249 if (strcmp(key, "device") == 0) {
1250 *parsed_kws |= MTREE_HAS_DEVICE;
1251 return parse_device(&a->archive, entry, val);
1254 if (strcmp(key, "flags") == 0) {
1255 *parsed_kws |= MTREE_HAS_FFLAGS;
1256 archive_entry_copy_fflags_text(entry, val);
1260 if (strcmp(key, "gid") == 0) {
1261 *parsed_kws |= MTREE_HAS_GID;
1262 archive_entry_set_gid(entry, mtree_atol10(&val));
1265 if (strcmp(key, "gname") == 0) {
1266 *parsed_kws |= MTREE_HAS_GNAME;
1267 archive_entry_copy_gname(entry, val);
1271 if (strcmp(key, "link") == 0) {
1272 archive_entry_copy_symlink(entry, val);
1276 if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
1278 if (strcmp(key, "mode") == 0) {
1279 if (val[0] >= '0' && val[0] <= '9') {
1280 *parsed_kws |= MTREE_HAS_PERM;
1281 archive_entry_set_perm(entry,
1284 archive_set_error(&a->archive,
1285 ARCHIVE_ERRNO_FILE_FORMAT,
1286 "Symbolic mode \"%s\" unsupported", val);
1287 return ARCHIVE_WARN;
1292 if (strcmp(key, "nlink") == 0) {
1293 *parsed_kws |= MTREE_HAS_NLINK;
1294 archive_entry_set_nlink(entry, mtree_atol10(&val));
1298 if (strcmp(key, "rmd160") == 0 ||
1299 strcmp(key, "rmd160digest") == 0)
1302 if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
1304 if (strcmp(key, "sha256") == 0 ||
1305 strcmp(key, "sha256digest") == 0)
1307 if (strcmp(key, "sha384") == 0 ||
1308 strcmp(key, "sha384digest") == 0)
1310 if (strcmp(key, "sha512") == 0 ||
1311 strcmp(key, "sha512digest") == 0)
1313 if (strcmp(key, "size") == 0) {
1314 archive_entry_set_size(entry, mtree_atol10(&val));
1318 if (strcmp(key, "tags") == 0) {
1320 * Comma delimited list of tags.
1321 * Ignore the tags for now, but the interface
1322 * should be extended to allow inclusion/exclusion.
1326 if (strcmp(key, "time") == 0) {
1328 int64_t my_time_t_max = get_time_t_max();
1329 int64_t my_time_t_min = get_time_t_min();
1332 *parsed_kws |= MTREE_HAS_MTIME;
1333 m = mtree_atol10(&val);
1334 /* Replicate an old mtree bug:
1335 * 123456789.1 represents 123456789
1336 * seconds and 1 nanosecond. */
1339 ns = (long)mtree_atol10(&val);
1342 if (m > my_time_t_max)
1344 else if (m < my_time_t_min)
1346 archive_entry_set_mtime(entry, (time_t)m, ns);
1349 if (strcmp(key, "type") == 0) {
1352 if (strcmp(val, "block") == 0) {
1353 archive_entry_set_filetype(entry, AE_IFBLK);
1357 if (strcmp(val, "char") == 0) {
1358 archive_entry_set_filetype(entry, AE_IFCHR);
1362 if (strcmp(val, "dir") == 0) {
1363 archive_entry_set_filetype(entry, AE_IFDIR);
1367 if (strcmp(val, "fifo") == 0) {
1368 archive_entry_set_filetype(entry, AE_IFIFO);
1371 if (strcmp(val, "file") == 0) {
1372 archive_entry_set_filetype(entry, AE_IFREG);
1376 if (strcmp(val, "link") == 0) {
1377 archive_entry_set_filetype(entry, AE_IFLNK);
1381 archive_set_error(&a->archive,
1382 ARCHIVE_ERRNO_FILE_FORMAT,
1383 "Unrecognized file type \"%s\"; assuming \"file\"", val);
1384 archive_entry_set_filetype(entry, AE_IFREG);
1385 return (ARCHIVE_WARN);
1387 *parsed_kws |= MTREE_HAS_TYPE;
1391 if (strcmp(key, "uid") == 0) {
1392 *parsed_kws |= MTREE_HAS_UID;
1393 archive_entry_set_uid(entry, mtree_atol10(&val));
1396 if (strcmp(key, "uname") == 0) {
1397 *parsed_kws |= MTREE_HAS_UNAME;
1398 archive_entry_copy_uname(entry, val);
1402 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1403 "Unrecognized key %s=%s", key, val);
1404 return (ARCHIVE_WARN);
1406 return (ARCHIVE_OK);
1410 read_data(struct archive_read *a, const void **buff, size_t *size, int64_t *offset)
1412 size_t bytes_to_read;
1414 struct mtree *mtree;
1416 mtree = (struct mtree *)(a->format->data);
1417 if (mtree->fd < 0) {
1421 return (ARCHIVE_EOF);
1423 if (mtree->buff == NULL) {
1424 mtree->buffsize = 64 * 1024;
1425 mtree->buff = malloc(mtree->buffsize);
1426 if (mtree->buff == NULL) {
1427 archive_set_error(&a->archive, ENOMEM,
1428 "Can't allocate memory");
1429 return (ARCHIVE_FATAL);
1433 *buff = mtree->buff;
1434 *offset = mtree->offset;
1435 if ((int64_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1436 bytes_to_read = mtree->cur_size - mtree->offset;
1438 bytes_to_read = mtree->buffsize;
1439 bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1440 if (bytes_read < 0) {
1441 archive_set_error(&a->archive, errno, "Can't read");
1442 return (ARCHIVE_WARN);
1444 if (bytes_read == 0) {
1446 return (ARCHIVE_EOF);
1448 mtree->offset += bytes_read;
1450 return (ARCHIVE_OK);
1453 /* Skip does nothing except possibly close the contents file. */
1455 skip(struct archive_read *a)
1457 struct mtree *mtree;
1459 mtree = (struct mtree *)(a->format->data);
1460 if (mtree->fd >= 0) {
1464 return (ARCHIVE_OK);
1468 * Since parsing backslash sequences always makes strings shorter,
1469 * we can always do this conversion in-place.
1472 parse_escapes(char *src, struct mtree_entry *mentry)
1477 if (mentry != NULL && strcmp(src, ".") == 0)
1480 while (*src != '\0') {
1482 if (c == '/' && mentry != NULL)
1487 if (src[1] < '0' || src[1] > '7') {
1496 if (src[1] >= '0' && src[1] <= '7' &&
1497 src[2] >= '0' && src[2] <= '7') {
1498 c = (src[0] - '0') << 6;
1499 c |= (src[1] - '0') << 3;
1500 c |= (src[2] - '0');
1544 * Note that this implementation does not (and should not!) obey
1545 * locale settings; you cannot simply substitute strtol here, since
1546 * it does obey locale.
1549 mtree_atol8(char **p)
1551 int64_t l, limit, last_digit_limit;
1555 limit = INT64_MAX / base;
1556 last_digit_limit = INT64_MAX % base;
1560 while (digit >= 0 && digit < base) {
1561 if (l>limit || (l == limit && digit > last_digit_limit)) {
1562 l = INT64_MAX; /* Truncate on overflow. */
1565 l = (l * base) + digit;
1566 digit = *++(*p) - '0';
1572 * Note that this implementation does not (and should not!) obey
1573 * locale settings; you cannot simply substitute strtol here, since
1574 * it does obey locale.
1577 mtree_atol10(char **p)
1579 int64_t l, limit, last_digit_limit;
1580 int base, digit, sign;
1586 limit = ((uint64_t)(INT64_MAX) + 1) / base;
1587 last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
1591 limit = INT64_MAX / base;
1592 last_digit_limit = INT64_MAX % base;
1597 while (digit >= 0 && digit < base) {
1598 if (l > limit || (l == limit && digit > last_digit_limit))
1599 return (sign < 0) ? INT64_MIN : INT64_MAX;
1600 l = (l * base) + digit;
1601 digit = *++(*p) - '0';
1603 return (sign < 0) ? -l : l;
1606 /* Parse a hex digit. */
1610 if (c >= '0' && c <= '9')
1612 else if (c >= 'a' && c <= 'f')
1614 else if (c >= 'A' && c <= 'F')
1621 * Note that this implementation does not (and should not!) obey
1622 * locale settings; you cannot simply substitute strtol here, since
1623 * it does obey locale.
1626 mtree_atol16(char **p)
1628 int64_t l, limit, last_digit_limit;
1629 int base, digit, sign;
1635 limit = ((uint64_t)(INT64_MAX) + 1) / base;
1636 last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
1640 limit = INT64_MAX / base;
1641 last_digit_limit = INT64_MAX % base;
1645 digit = parsehex(**p);
1646 while (digit >= 0 && digit < base) {
1647 if (l > limit || (l == limit && digit > last_digit_limit))
1648 return (sign < 0) ? INT64_MIN : INT64_MAX;
1649 l = (l * base) + digit;
1650 digit = parsehex(*++(*p));
1652 return (sign < 0) ? -l : l;
1656 mtree_atol(char **p)
1659 return mtree_atol10(p);
1660 if ((*p)[1] == 'x' || (*p)[1] == 'X') {
1662 return mtree_atol16(p);
1664 return mtree_atol8(p);
1668 * Returns length of line (including trailing newline)
1669 * or negative on error. 'start' argument is updated to
1670 * point to first character of line.
1673 readline(struct archive_read *a, struct mtree *mtree, char **start, ssize_t limit)
1676 ssize_t total_size = 0;
1677 ssize_t find_off = 0;
1683 /* Accumulate line in a line buffer. */
1685 /* Read some more. */
1686 t = __archive_read_ahead(a, 1, &bytes_read);
1690 return (ARCHIVE_FATAL);
1691 s = t; /* Start of line? */
1692 p = memchr(t, '\n', bytes_read);
1693 /* If we found '\n', trim the read. */
1695 bytes_read = 1 + ((const char *)p) - s;
1697 if (total_size + bytes_read + 1 > limit) {
1698 archive_set_error(&a->archive,
1699 ARCHIVE_ERRNO_FILE_FORMAT,
1701 return (ARCHIVE_FATAL);
1703 if (archive_string_ensure(&mtree->line,
1704 total_size + bytes_read + 1) == NULL) {
1705 archive_set_error(&a->archive, ENOMEM,
1706 "Can't allocate working buffer");
1707 return (ARCHIVE_FATAL);
1709 memcpy(mtree->line.s + total_size, t, bytes_read);
1710 __archive_read_consume(a, bytes_read);
1711 total_size += bytes_read;
1712 /* Null terminate. */
1713 mtree->line.s[total_size] = '\0';
1714 /* If we found an unescaped '\n', clean up and return. */
1715 for (u = mtree->line.s + find_off; *u; ++u) {
1717 *start = mtree->line.s;
1723 *start = mtree->line.s;
1734 total_size - (u - mtree->line.s) + 1);
1742 find_off = u - mtree->line.s;