1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2008 Joerg Sonnenberger
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_mtree.c 201165 2009-12-29 05:52:13Z kientzle $");
29 
30 #ifdef HAVE_SYS_STAT_H
31 #include <sys/stat.h>
32 #endif
33 #ifdef HAVE_ERRNO_H
34 #include <errno.h>
35 #endif
36 #ifdef HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39 #include <stddef.h>
40 /* #include <stdint.h> */ /* See archive_platform.h */
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #endif
47 
48 #include "archive.h"
49 #include "archive_entry.h"
50 #include "archive_private.h"
51 #include "archive_read_private.h"
52 #include "archive_string.h"
53 
54 #ifndef O_BINARY
55 #define	O_BINARY 0
56 #endif
57 
58 #define	MTREE_HAS_DEVICE	0x0001
59 #define	MTREE_HAS_FFLAGS	0x0002
60 #define	MTREE_HAS_GID		0x0004
61 #define	MTREE_HAS_GNAME		0x0008
62 #define	MTREE_HAS_MTIME		0x0010
63 #define	MTREE_HAS_NLINK		0x0020
64 #define	MTREE_HAS_PERM		0x0040
65 #define	MTREE_HAS_SIZE		0x0080
66 #define	MTREE_HAS_TYPE		0x0100
67 #define	MTREE_HAS_UID		0x0200
68 #define	MTREE_HAS_UNAME		0x0400
69 
70 #define	MTREE_HAS_OPTIONAL	0x0800
71 
72 struct mtree_option {
73 	struct mtree_option *next;
74 	char *value;
75 };
76 
77 struct mtree_entry {
78 	struct mtree_entry *next;
79 	struct mtree_option *options;
80 	char *name;
81 	char full;
82 	char used;
83 };
84 
85 struct mtree {
86 	struct archive_string	 line;
87 	size_t			 buffsize;
88 	char			*buff;
89 	off_t			 offset;
90 	int			 fd;
91 	int			 filetype;
92 	int			 archive_format;
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;
98 
99 	struct archive_entry_linkresolver *resolver;
100 
101 	off_t			 cur_size, cur_offset;
102 };
103 
104 static int	cleanup(struct archive_read *);
105 static int	mtree_bid(struct archive_read *);
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, off_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 **);
122 
123 static void
124 free_options(struct mtree_option *head)
125 {
126 	struct mtree_option *next;
127 
128 	for (; head != NULL; head = next) {
129 		next = head->next;
130 		free(head->value);
131 		free(head);
132 	}
133 }
134 
135 int
136 archive_read_support_format_mtree(struct archive *_a)
137 {
138 	struct archive_read *a = (struct archive_read *)_a;
139 	struct mtree *mtree;
140 	int r;
141 
142 	mtree = (struct mtree *)malloc(sizeof(*mtree));
143 	if (mtree == NULL) {
144 		archive_set_error(&a->archive, ENOMEM,
145 		    "Can't allocate mtree data");
146 		return (ARCHIVE_FATAL);
147 	}
148 	memset(mtree, 0, sizeof(*mtree));
149 	mtree->fd = -1;
150 
151 	r = __archive_read_register_format(a, mtree, "mtree",
152 	    mtree_bid, NULL, read_header, read_data, skip, cleanup);
153 
154 	if (r != ARCHIVE_OK)
155 		free(mtree);
156 	return (ARCHIVE_OK);
157 }
158 
159 static int
160 cleanup(struct archive_read *a)
161 {
162 	struct mtree *mtree;
163 	struct mtree_entry *p, *q;
164 
165 	mtree = (struct mtree *)(a->format->data);
166 
167 	p = mtree->entries;
168 	while (p != NULL) {
169 		q = p->next;
170 		free(p->name);
171 		free_options(p->options);
172 		free(p);
173 		p = q;
174 	}
175 	archive_string_free(&mtree->line);
176 	archive_string_free(&mtree->current_dir);
177 	archive_string_free(&mtree->contents_name);
178 	archive_entry_linkresolver_free(mtree->resolver);
179 
180 	free(mtree->buff);
181 	free(mtree);
182 	(a->format->data) = NULL;
183 	return (ARCHIVE_OK);
184 }
185 
186 
187 static int
188 mtree_bid(struct archive_read *a)
189 {
190 	const char *signature = "#mtree";
191 	const char *p;
192 
193 	/* Now let's look at the actual header and see if it matches. */
194 	p = __archive_read_ahead(a, strlen(signature), NULL);
195 	if (p == NULL)
196 		return (-1);
197 
198 	if (strncmp(p, signature, strlen(signature)) == 0)
199 		return (8 * (int)strlen(signature));
200 	return (0);
201 }
202 
203 /*
204  * The extended mtree format permits multiple lines specifying
205  * attributes for each file.  For those entries, only the last line
206  * is actually used.  Practically speaking, that means we have
207  * to read the entire mtree file into memory up front.
208  *
209  * The parsing is done in two steps.  First, it is decided if a line
210  * changes the global defaults and if it is, processed accordingly.
211  * Otherwise, the options of the line are merged with the current
212  * global options.
213  */
214 static int
215 add_option(struct archive_read *a, struct mtree_option **global,
216     const char *value, size_t len)
217 {
218 	struct mtree_option *option;
219 
220 	if ((option = malloc(sizeof(*option))) == NULL) {
221 		archive_set_error(&a->archive, errno, "Can't allocate memory");
222 		return (ARCHIVE_FATAL);
223 	}
224 	if ((option->value = malloc(len + 1)) == NULL) {
225 		free(option);
226 		archive_set_error(&a->archive, errno, "Can't allocate memory");
227 		return (ARCHIVE_FATAL);
228 	}
229 	memcpy(option->value, value, len);
230 	option->value[len] = '\0';
231 	option->next = *global;
232 	*global = option;
233 	return (ARCHIVE_OK);
234 }
235 
236 static void
237 remove_option(struct mtree_option **global, const char *value, size_t len)
238 {
239 	struct mtree_option *iter, *last;
240 
241 	last = NULL;
242 	for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
243 		if (strncmp(iter->value, value, len) == 0 &&
244 		    (iter->value[len] == '\0' ||
245 		     iter->value[len] == '='))
246 			break;
247 	}
248 	if (iter == NULL)
249 		return;
250 	if (last == NULL)
251 		*global = iter->next;
252 	else
253 		last->next = iter->next;
254 
255 	free(iter->value);
256 	free(iter);
257 }
258 
259 static int
260 process_global_set(struct archive_read *a,
261     struct mtree_option **global, const char *line)
262 {
263 	const char *next, *eq;
264 	size_t len;
265 	int r;
266 
267 	line += 4;
268 	for (;;) {
269 		next = line + strspn(line, " \t\r\n");
270 		if (*next == '\0')
271 			return (ARCHIVE_OK);
272 		line = next;
273 		next = line + strcspn(line, " \t\r\n");
274 		eq = strchr(line, '=');
275 		if (eq > next)
276 			len = next - line;
277 		else
278 			len = eq - line;
279 
280 		remove_option(global, line, len);
281 		r = add_option(a, global, line, next - line);
282 		if (r != ARCHIVE_OK)
283 			return (r);
284 		line = next;
285 	}
286 }
287 
288 static int
289 process_global_unset(struct archive_read *a,
290     struct mtree_option **global, const char *line)
291 {
292 	const char *next;
293 	size_t len;
294 
295 	line += 6;
296 	if (strchr(line, '=') != NULL) {
297 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
298 		    "/unset shall not contain `='");
299 		return ARCHIVE_FATAL;
300 	}
301 
302 	for (;;) {
303 		next = line + strspn(line, " \t\r\n");
304 		if (*next == '\0')
305 			return (ARCHIVE_OK);
306 		line = next;
307 		len = strcspn(line, " \t\r\n");
308 
309 		if (len == 3 && strncmp(line, "all", 3) == 0) {
310 			free_options(*global);
311 			*global = NULL;
312 		} else {
313 			remove_option(global, line, len);
314 		}
315 
316 		line += len;
317 	}
318 }
319 
320 static int
321 process_add_entry(struct archive_read *a, struct mtree *mtree,
322     struct mtree_option **global, const char *line,
323     struct mtree_entry **last_entry)
324 {
325 	struct mtree_entry *entry;
326 	struct mtree_option *iter;
327 	const char *next, *eq;
328 	size_t len;
329 	int r;
330 
331 	if ((entry = malloc(sizeof(*entry))) == NULL) {
332 		archive_set_error(&a->archive, errno, "Can't allocate memory");
333 		return (ARCHIVE_FATAL);
334 	}
335 	entry->next = NULL;
336 	entry->options = NULL;
337 	entry->name = NULL;
338 	entry->used = 0;
339 	entry->full = 0;
340 
341 	/* Add this entry to list. */
342 	if (*last_entry == NULL)
343 		mtree->entries = entry;
344 	else
345 		(*last_entry)->next = entry;
346 	*last_entry = entry;
347 
348 	len = strcspn(line, " \t\r\n");
349 	if ((entry->name = malloc(len + 1)) == NULL) {
350 		archive_set_error(&a->archive, errno, "Can't allocate memory");
351 		return (ARCHIVE_FATAL);
352 	}
353 
354 	memcpy(entry->name, line, len);
355 	entry->name[len] = '\0';
356 	parse_escapes(entry->name, entry);
357 
358 	line += len;
359 	for (iter = *global; iter != NULL; iter = iter->next) {
360 		r = add_option(a, &entry->options, iter->value,
361 		    strlen(iter->value));
362 		if (r != ARCHIVE_OK)
363 			return (r);
364 	}
365 
366 	for (;;) {
367 		next = line + strspn(line, " \t\r\n");
368 		if (*next == '\0')
369 			return (ARCHIVE_OK);
370 		line = next;
371 		next = line + strcspn(line, " \t\r\n");
372 		eq = strchr(line, '=');
373 		if (eq == NULL || eq > next)
374 			len = next - line;
375 		else
376 			len = eq - line;
377 
378 		remove_option(&entry->options, line, len);
379 		r = add_option(a, &entry->options, line, next - line);
380 		if (r != ARCHIVE_OK)
381 			return (r);
382 		line = next;
383 	}
384 }
385 
386 static int
387 read_mtree(struct archive_read *a, struct mtree *mtree)
388 {
389 	ssize_t len;
390 	uintmax_t counter;
391 	char *p;
392 	struct mtree_option *global;
393 	struct mtree_entry *last_entry;
394 	int r;
395 
396 	mtree->archive_format = ARCHIVE_FORMAT_MTREE;
397 	mtree->archive_format_name = "mtree";
398 
399 	global = NULL;
400 	last_entry = NULL;
401 
402 	for (counter = 1; ; ++counter) {
403 		len = readline(a, mtree, &p, 256);
404 		if (len == 0) {
405 			mtree->this_entry = mtree->entries;
406 			free_options(global);
407 			return (ARCHIVE_OK);
408 		}
409 		if (len < 0) {
410 			free_options(global);
411 			return (len);
412 		}
413 		/* Leading whitespace is never significant, ignore it. */
414 		while (*p == ' ' || *p == '\t') {
415 			++p;
416 			--len;
417 		}
418 		/* Skip content lines and blank lines. */
419 		if (*p == '#')
420 			continue;
421 		if (*p == '\r' || *p == '\n' || *p == '\0')
422 			continue;
423 		if (*p != '/') {
424 			r = process_add_entry(a, mtree, &global, p,
425 			    &last_entry);
426 		} else if (strncmp(p, "/set", 4) == 0) {
427 			if (p[4] != ' ' && p[4] != '\t')
428 				break;
429 			r = process_global_set(a, &global, p);
430 		} else if (strncmp(p, "/unset", 6) == 0) {
431 			if (p[6] != ' ' && p[6] != '\t')
432 				break;
433 			r = process_global_unset(a, &global, p);
434 		} else
435 			break;
436 
437 		if (r != ARCHIVE_OK) {
438 			free_options(global);
439 			return r;
440 		}
441 	}
442 
443 	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
444 	    "Can't parse line %ju", counter);
445 	free_options(global);
446 	return (ARCHIVE_FATAL);
447 }
448 
449 /*
450  * Read in the entire mtree file into memory on the first request.
451  * Then use the next unused file to satisfy each header request.
452  */
453 static int
454 read_header(struct archive_read *a, struct archive_entry *entry)
455 {
456 	struct mtree *mtree;
457 	char *p;
458 	int r, use_next;
459 
460 	mtree = (struct mtree *)(a->format->data);
461 
462 	if (mtree->fd >= 0) {
463 		close(mtree->fd);
464 		mtree->fd = -1;
465 	}
466 
467 	if (mtree->entries == NULL) {
468 		mtree->resolver = archive_entry_linkresolver_new();
469 		if (mtree->resolver == NULL)
470 			return ARCHIVE_FATAL;
471 		archive_entry_linkresolver_set_strategy(mtree->resolver,
472 		    ARCHIVE_FORMAT_MTREE);
473 		r = read_mtree(a, mtree);
474 		if (r != ARCHIVE_OK)
475 			return (r);
476 	}
477 
478 	a->archive.archive_format = mtree->archive_format;
479 	a->archive.archive_format_name = mtree->archive_format_name;
480 
481 	for (;;) {
482 		if (mtree->this_entry == NULL)
483 			return (ARCHIVE_EOF);
484 		if (strcmp(mtree->this_entry->name, "..") == 0) {
485 			mtree->this_entry->used = 1;
486 			if (archive_strlen(&mtree->current_dir) > 0) {
487 				/* Roll back current path. */
488 				p = mtree->current_dir.s
489 				    + mtree->current_dir.length - 1;
490 				while (p >= mtree->current_dir.s && *p != '/')
491 					--p;
492 				if (p >= mtree->current_dir.s)
493 					--p;
494 				mtree->current_dir.length
495 				    = p - mtree->current_dir.s + 1;
496 			}
497 		}
498 		if (!mtree->this_entry->used) {
499 			use_next = 0;
500 			r = parse_file(a, entry, mtree, mtree->this_entry, &use_next);
501 			if (use_next == 0)
502 				return (r);
503 		}
504 		mtree->this_entry = mtree->this_entry->next;
505 	}
506 }
507 
508 /*
509  * A single file can have multiple lines contribute specifications.
510  * Parse as many lines as necessary, then pull additional information
511  * from a backing file on disk as necessary.
512  */
513 static int
514 parse_file(struct archive_read *a, struct archive_entry *entry,
515     struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
516 {
517 	const char *path;
518 	struct stat st_storage, *st;
519 	struct mtree_entry *mp;
520 	struct archive_entry *sparse_entry;
521 	int r = ARCHIVE_OK, r1, parsed_kws, mismatched_type;
522 
523 	mentry->used = 1;
524 
525 	/* Initialize reasonable defaults. */
526 	mtree->filetype = AE_IFREG;
527 	archive_entry_set_size(entry, 0);
528 	archive_string_empty(&mtree->contents_name);
529 
530 	/* Parse options from this line. */
531 	parsed_kws = 0;
532 	r = parse_line(a, entry, mtree, mentry, &parsed_kws);
533 
534 	if (mentry->full) {
535 		archive_entry_copy_pathname(entry, mentry->name);
536 		/*
537 		 * "Full" entries are allowed to have multiple lines
538 		 * and those lines aren't required to be adjacent.  We
539 		 * don't support multiple lines for "relative" entries
540 		 * nor do we make any attempt to merge data from
541 		 * separate "relative" and "full" entries.  (Merging
542 		 * "relative" and "full" entries would require dealing
543 		 * with pathname canonicalization, which is a very
544 		 * tricky subject.)
545 		 */
546 		for (mp = mentry->next; mp != NULL; mp = mp->next) {
547 			if (mp->full && !mp->used
548 			    && strcmp(mentry->name, mp->name) == 0) {
549 				/* Later lines override earlier ones. */
550 				mp->used = 1;
551 				r1 = parse_line(a, entry, mtree, mp,
552 				    &parsed_kws);
553 				if (r1 < r)
554 					r = r1;
555 			}
556 		}
557 	} else {
558 		/*
559 		 * Relative entries require us to construct
560 		 * the full path and possibly update the
561 		 * current directory.
562 		 */
563 		size_t n = archive_strlen(&mtree->current_dir);
564 		if (n > 0)
565 			archive_strcat(&mtree->current_dir, "/");
566 		archive_strcat(&mtree->current_dir, mentry->name);
567 		archive_entry_copy_pathname(entry, mtree->current_dir.s);
568 		if (archive_entry_filetype(entry) != AE_IFDIR)
569 			mtree->current_dir.length = n;
570 	}
571 
572 	/*
573 	 * Try to open and stat the file to get the real size
574 	 * and other file info.  It would be nice to avoid
575 	 * this here so that getting a listing of an mtree
576 	 * wouldn't require opening every referenced contents
577 	 * file.  But then we wouldn't know the actual
578 	 * contents size, so I don't see a really viable way
579 	 * around this.  (Also, we may want to someday pull
580 	 * other unspecified info from the contents file on
581 	 * disk.)
582 	 */
583 	mtree->fd = -1;
584 	if (archive_strlen(&mtree->contents_name) > 0)
585 		path = mtree->contents_name.s;
586 	else
587 		path = archive_entry_pathname(entry);
588 
589 	if (archive_entry_filetype(entry) == AE_IFREG ||
590 	    archive_entry_filetype(entry) == AE_IFDIR) {
591 		mtree->fd = open(path, O_RDONLY | O_BINARY);
592 		if (mtree->fd == -1 &&
593 		    (errno != ENOENT ||
594 		     archive_strlen(&mtree->contents_name) > 0)) {
595 			archive_set_error(&a->archive, errno,
596 			    "Can't open %s", path);
597 			r = ARCHIVE_WARN;
598 		}
599 	}
600 
601 	st = &st_storage;
602 	if (mtree->fd >= 0) {
603 		if (fstat(mtree->fd, st) == -1) {
604 			archive_set_error(&a->archive, errno,
605 			    "Could not fstat %s", path);
606 			r = ARCHIVE_WARN;
607 			/* If we can't stat it, don't keep it open. */
608 			close(mtree->fd);
609 			mtree->fd = -1;
610 			st = NULL;
611 		}
612 	} else if (lstat(path, st) == -1) {
613 		st = NULL;
614 	}
615 
616 	/*
617 	 * Check for a mismatch between the type in the specification and
618 	 * the type of the contents object on disk.
619 	 */
620 	if (st != NULL) {
621 		mismatched_type = 0;
622 		if ((st->st_mode & S_IFMT) == S_IFREG &&
623 		    archive_entry_filetype(entry) != AE_IFREG)
624 			mismatched_type = 1;
625 		if ((st->st_mode & S_IFMT) == S_IFLNK &&
626 		    archive_entry_filetype(entry) != AE_IFLNK)
627 			mismatched_type = 1;
628 		if ((st->st_mode & S_IFSOCK) == S_IFSOCK &&
629 		    archive_entry_filetype(entry) != AE_IFSOCK)
630 			mismatched_type = 1;
631 		if ((st->st_mode & S_IFMT) == S_IFCHR &&
632 		    archive_entry_filetype(entry) != AE_IFCHR)
633 			mismatched_type = 1;
634 		if ((st->st_mode & S_IFMT) == S_IFBLK &&
635 		    archive_entry_filetype(entry) != AE_IFBLK)
636 			mismatched_type = 1;
637 		if ((st->st_mode & S_IFMT) == S_IFDIR &&
638 		    archive_entry_filetype(entry) != AE_IFDIR)
639 			mismatched_type = 1;
640 		if ((st->st_mode & S_IFMT) == S_IFIFO &&
641 		    archive_entry_filetype(entry) != AE_IFIFO)
642 			mismatched_type = 1;
643 
644 		if (mismatched_type) {
645 			if ((parsed_kws & MTREE_HAS_OPTIONAL) == 0) {
646 				archive_set_error(&a->archive,
647 				    ARCHIVE_ERRNO_MISC,
648 				    "mtree specification has different type for %s",
649 				    archive_entry_pathname(entry));
650 				r = ARCHIVE_WARN;
651 			} else {
652 				*use_next = 1;
653 			}
654 			/* Don't hold a non-regular file open. */
655 			if (mtree->fd >= 0)
656 				close(mtree->fd);
657 			mtree->fd = -1;
658 			st = NULL;
659 			return r;
660 		}
661 	}
662 
663 	/*
664 	 * If there is a contents file on disk, pick some of the metadata
665 	 * from that file.  For most of these, we only set it from the contents
666 	 * if it wasn't already parsed from the specification.
667 	 */
668 	if (st != NULL) {
669 		if ((parsed_kws & MTREE_HAS_DEVICE) == 0 &&
670 		    (archive_entry_filetype(entry) == AE_IFCHR ||
671 		     archive_entry_filetype(entry) == AE_IFBLK))
672 			archive_entry_set_rdev(entry, st->st_rdev);
673 		if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME)) == 0)
674 			archive_entry_set_gid(entry, st->st_gid);
675 		if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME)) == 0)
676 			archive_entry_set_uid(entry, st->st_uid);
677 		if ((parsed_kws & MTREE_HAS_MTIME) == 0) {
678 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
679 			archive_entry_set_mtime(entry, st->st_mtime,
680 			    st->st_mtimespec.tv_nsec);
681 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
682 			archive_entry_set_mtime(entry, st->st_mtime,
683 			    st->st_mtim.tv_nsec);
684 #elif HAVE_STRUCT_STAT_ST_MTIME_N
685 			archive_entry_set_mtime(entry, st->st_mtime,
686 			    st->st_mtime_n);
687 #elif HAVE_STRUCT_STAT_ST_UMTIME
688 			archive_entry_set_mtime(entry, st->st_mtime,
689 			    st->st_umtime*1000);
690 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
691 			archive_entry_set_mtime(entry, st->st_mtime,
692 			    st->st_mtime_usec*1000);
693 #else
694 			archive_entry_set_mtime(entry, st->st_mtime, 0);
695 #endif
696 		}
697 		if ((parsed_kws & MTREE_HAS_NLINK) == 0)
698 			archive_entry_set_nlink(entry, st->st_nlink);
699 		if ((parsed_kws & MTREE_HAS_PERM) == 0)
700 			archive_entry_set_perm(entry, st->st_mode);
701 		if ((parsed_kws & MTREE_HAS_SIZE) == 0)
702 			archive_entry_set_size(entry, st->st_size);
703 		archive_entry_set_ino(entry, st->st_ino);
704 		archive_entry_set_dev(entry, st->st_dev);
705 
706 		archive_entry_linkify(mtree->resolver, &entry, &sparse_entry);
707 	} else if (parsed_kws & MTREE_HAS_OPTIONAL) {
708 		/*
709 		 * Couldn't open the entry, stat it or the on-disk type
710 		 * didn't match.  If this entry is optional, just ignore it
711 		 * and read the next header entry.
712 		 */
713 		*use_next = 1;
714 		return ARCHIVE_OK;
715 	}
716 
717 	mtree->cur_size = archive_entry_size(entry);
718 	mtree->offset = 0;
719 
720 	return r;
721 }
722 
723 /*
724  * Each line contains a sequence of keywords.
725  */
726 static int
727 parse_line(struct archive_read *a, struct archive_entry *entry,
728     struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
729 {
730 	struct mtree_option *iter;
731 	int r = ARCHIVE_OK, r1;
732 
733 	for (iter = mp->options; iter != NULL; iter = iter->next) {
734 		r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
735 		if (r1 < r)
736 			r = r1;
737 	}
738 	if ((*parsed_kws & MTREE_HAS_TYPE) == 0) {
739 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
740 		    "Missing type keyword in mtree specification");
741 		return (ARCHIVE_WARN);
742 	}
743 	return (r);
744 }
745 
746 /*
747  * Device entries have one of the following forms:
748  * raw dev_t
749  * format,major,minor[,subdevice]
750  *
751  * Just use major and minor, no translation etc is done
752  * between formats.
753  */
754 static int
755 parse_device(struct archive *a, struct archive_entry *entry, char *val)
756 {
757 	char *comma1, *comma2;
758 
759 	comma1 = strchr(val, ',');
760 	if (comma1 == NULL) {
761 		archive_entry_set_dev(entry, mtree_atol10(&val));
762 		return (ARCHIVE_OK);
763 	}
764 	++comma1;
765 	comma2 = strchr(comma1, ',');
766 	if (comma2 == NULL) {
767 		archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
768 		    "Malformed device attribute");
769 		return (ARCHIVE_WARN);
770 	}
771 	++comma2;
772 	archive_entry_set_rdevmajor(entry, mtree_atol(&comma1));
773 	archive_entry_set_rdevminor(entry, mtree_atol(&comma2));
774 	return (ARCHIVE_OK);
775 }
776 
777 /*
778  * Parse a single keyword and its value.
779  */
780 static int
781 parse_keyword(struct archive_read *a, struct mtree *mtree,
782     struct archive_entry *entry, struct mtree_option *option, int *parsed_kws)
783 {
784 	char *val, *key;
785 
786 	key = option->value;
787 
788 	if (*key == '\0')
789 		return (ARCHIVE_OK);
790 
791 	if (strcmp(key, "optional") == 0) {
792 		*parsed_kws |= MTREE_HAS_OPTIONAL;
793 		return (ARCHIVE_OK);
794 	}
795 	if (strcmp(key, "ignore") == 0) {
796 		/*
797 		 * The mtree processing is not recursive, so
798 		 * recursion will only happen for explicitly listed
799 		 * entries.
800 		 */
801 		return (ARCHIVE_OK);
802 	}
803 
804 	val = strchr(key, '=');
805 	if (val == NULL) {
806 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
807 		    "Malformed attribute \"%s\" (%d)", key, key[0]);
808 		return (ARCHIVE_WARN);
809 	}
810 
811 	*val = '\0';
812 	++val;
813 
814 	switch (key[0]) {
815 	case 'c':
816 		if (strcmp(key, "content") == 0
817 		    || strcmp(key, "contents") == 0) {
818 			parse_escapes(val, NULL);
819 			archive_strcpy(&mtree->contents_name, val);
820 			break;
821 		}
822 		if (strcmp(key, "cksum") == 0)
823 			break;
824 	case 'd':
825 		if (strcmp(key, "device") == 0) {
826 			*parsed_kws |= MTREE_HAS_DEVICE;
827 			return parse_device(&a->archive, entry, val);
828 		}
829 	case 'f':
830 		if (strcmp(key, "flags") == 0) {
831 			*parsed_kws |= MTREE_HAS_FFLAGS;
832 			archive_entry_copy_fflags_text(entry, val);
833 			break;
834 		}
835 	case 'g':
836 		if (strcmp(key, "gid") == 0) {
837 			*parsed_kws |= MTREE_HAS_GID;
838 			archive_entry_set_gid(entry, mtree_atol10(&val));
839 			break;
840 		}
841 		if (strcmp(key, "gname") == 0) {
842 			*parsed_kws |= MTREE_HAS_GNAME;
843 			archive_entry_copy_gname(entry, val);
844 			break;
845 		}
846 	case 'l':
847 		if (strcmp(key, "link") == 0) {
848 			archive_entry_copy_symlink(entry, val);
849 			break;
850 		}
851 	case 'm':
852 		if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
853 			break;
854 		if (strcmp(key, "mode") == 0) {
855 			if (val[0] >= '0' && val[0] <= '9') {
856 				*parsed_kws |= MTREE_HAS_PERM;
857 				archive_entry_set_perm(entry,
858 				    mtree_atol8(&val));
859 			} else {
860 				archive_set_error(&a->archive,
861 				    ARCHIVE_ERRNO_FILE_FORMAT,
862 				    "Symbolic mode \"%s\" unsupported", val);
863 				return ARCHIVE_WARN;
864 			}
865 			break;
866 		}
867 	case 'n':
868 		if (strcmp(key, "nlink") == 0) {
869 			*parsed_kws |= MTREE_HAS_NLINK;
870 			archive_entry_set_nlink(entry, mtree_atol10(&val));
871 			break;
872 		}
873 	case 'r':
874 		if (strcmp(key, "rmd160") == 0 ||
875 		    strcmp(key, "rmd160digest") == 0)
876 			break;
877 	case 's':
878 		if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
879 			break;
880 		if (strcmp(key, "sha256") == 0 ||
881 		    strcmp(key, "sha256digest") == 0)
882 			break;
883 		if (strcmp(key, "sha384") == 0 ||
884 		    strcmp(key, "sha384digest") == 0)
885 			break;
886 		if (strcmp(key, "sha512") == 0 ||
887 		    strcmp(key, "sha512digest") == 0)
888 			break;
889 		if (strcmp(key, "size") == 0) {
890 			archive_entry_set_size(entry, mtree_atol10(&val));
891 			break;
892 		}
893 	case 't':
894 		if (strcmp(key, "tags") == 0) {
895 			/*
896 			 * Comma delimited list of tags.
897 			 * Ignore the tags for now, but the interface
898 			 * should be extended to allow inclusion/exclusion.
899 			 */
900 			break;
901 		}
902 		if (strcmp(key, "time") == 0) {
903 			time_t m;
904 			long ns;
905 
906 			*parsed_kws |= MTREE_HAS_MTIME;
907 			m = (time_t)mtree_atol10(&val);
908 			if (*val == '.') {
909 				++val;
910 				ns = (long)mtree_atol10(&val);
911 			} else
912 				ns = 0;
913 			archive_entry_set_mtime(entry, m, ns);
914 			break;
915 		}
916 		if (strcmp(key, "type") == 0) {
917 			*parsed_kws |= MTREE_HAS_TYPE;
918 			switch (val[0]) {
919 			case 'b':
920 				if (strcmp(val, "block") == 0) {
921 					mtree->filetype = AE_IFBLK;
922 					break;
923 				}
924 			case 'c':
925 				if (strcmp(val, "char") == 0) {
926 					mtree->filetype = AE_IFCHR;
927 					break;
928 				}
929 			case 'd':
930 				if (strcmp(val, "dir") == 0) {
931 					mtree->filetype = AE_IFDIR;
932 					break;
933 				}
934 			case 'f':
935 				if (strcmp(val, "fifo") == 0) {
936 					mtree->filetype = AE_IFIFO;
937 					break;
938 				}
939 				if (strcmp(val, "file") == 0) {
940 					mtree->filetype = AE_IFREG;
941 					break;
942 				}
943 			case 'l':
944 				if (strcmp(val, "link") == 0) {
945 					mtree->filetype = AE_IFLNK;
946 					break;
947 				}
948 			default:
949 				archive_set_error(&a->archive,
950 				    ARCHIVE_ERRNO_FILE_FORMAT,
951 				    "Unrecognized file type \"%s\"", val);
952 				return (ARCHIVE_WARN);
953 			}
954 			archive_entry_set_filetype(entry, mtree->filetype);
955 			break;
956 		}
957 	case 'u':
958 		if (strcmp(key, "uid") == 0) {
959 			*parsed_kws |= MTREE_HAS_UID;
960 			archive_entry_set_uid(entry, mtree_atol10(&val));
961 			break;
962 		}
963 		if (strcmp(key, "uname") == 0) {
964 			*parsed_kws |= MTREE_HAS_UNAME;
965 			archive_entry_copy_uname(entry, val);
966 			break;
967 		}
968 	default:
969 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
970 		    "Unrecognized key %s=%s", key, val);
971 		return (ARCHIVE_WARN);
972 	}
973 	return (ARCHIVE_OK);
974 }
975 
976 static int
977 read_data(struct archive_read *a, const void **buff, size_t *size, off_t *offset)
978 {
979 	size_t bytes_to_read;
980 	ssize_t bytes_read;
981 	struct mtree *mtree;
982 
983 	mtree = (struct mtree *)(a->format->data);
984 	if (mtree->fd < 0) {
985 		*buff = NULL;
986 		*offset = 0;
987 		*size = 0;
988 		return (ARCHIVE_EOF);
989 	}
990 	if (mtree->buff == NULL) {
991 		mtree->buffsize = 64 * 1024;
992 		mtree->buff = malloc(mtree->buffsize);
993 		if (mtree->buff == NULL) {
994 			archive_set_error(&a->archive, ENOMEM,
995 			    "Can't allocate memory");
996 			return (ARCHIVE_FATAL);
997 		}
998 	}
999 
1000 	*buff = mtree->buff;
1001 	*offset = mtree->offset;
1002 	if ((off_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1003 		bytes_to_read = mtree->cur_size - mtree->offset;
1004 	else
1005 		bytes_to_read = mtree->buffsize;
1006 	bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1007 	if (bytes_read < 0) {
1008 		archive_set_error(&a->archive, errno, "Can't read");
1009 		return (ARCHIVE_WARN);
1010 	}
1011 	if (bytes_read == 0) {
1012 		*size = 0;
1013 		return (ARCHIVE_EOF);
1014 	}
1015 	mtree->offset += bytes_read;
1016 	*size = bytes_read;
1017 	return (ARCHIVE_OK);
1018 }
1019 
1020 /* Skip does nothing except possibly close the contents file. */
1021 static int
1022 skip(struct archive_read *a)
1023 {
1024 	struct mtree *mtree;
1025 
1026 	mtree = (struct mtree *)(a->format->data);
1027 	if (mtree->fd >= 0) {
1028 		close(mtree->fd);
1029 		mtree->fd = -1;
1030 	}
1031 	return (ARCHIVE_OK);
1032 }
1033 
1034 /*
1035  * Since parsing backslash sequences always makes strings shorter,
1036  * we can always do this conversion in-place.
1037  */
1038 static void
1039 parse_escapes(char *src, struct mtree_entry *mentry)
1040 {
1041 	char *dest = src;
1042 	char c;
1043 
1044 	if (mentry != NULL && strcmp(src, ".") == 0)
1045 		mentry->full = 1;
1046 
1047 	while (*src != '\0') {
1048 		c = *src++;
1049 		if (c == '/' && mentry != NULL)
1050 			mentry->full = 1;
1051 		if (c == '\\') {
1052 			switch (src[0]) {
1053 			case '0':
1054 				if (src[1] < '0' || src[1] > '7') {
1055 					c = 0;
1056 					++src;
1057 					break;
1058 				}
1059 				/* FALLTHROUGH */
1060 			case '1':
1061 			case '2':
1062 			case '3':
1063 				if (src[1] >= '0' && src[1] <= '7' &&
1064 				    src[2] >= '0' && src[2] <= '7') {
1065 					c = (src[0] - '0') << 6;
1066 					c |= (src[1] - '0') << 3;
1067 					c |= (src[2] - '0');
1068 					src += 3;
1069 				}
1070 				break;
1071 			case 'a':
1072 				c = '\a';
1073 				++src;
1074 				break;
1075 			case 'b':
1076 				c = '\b';
1077 				++src;
1078 				break;
1079 			case 'f':
1080 				c = '\f';
1081 				++src;
1082 				break;
1083 			case 'n':
1084 				c = '\n';
1085 				++src;
1086 				break;
1087 			case 'r':
1088 				c = '\r';
1089 				++src;
1090 				break;
1091 			case 's':
1092 				c = ' ';
1093 				++src;
1094 				break;
1095 			case 't':
1096 				c = '\t';
1097 				++src;
1098 				break;
1099 			case 'v':
1100 				c = '\v';
1101 				++src;
1102 				break;
1103 			}
1104 		}
1105 		*dest++ = c;
1106 	}
1107 	*dest = '\0';
1108 }
1109 
1110 /*
1111  * Note that this implementation does not (and should not!) obey
1112  * locale settings; you cannot simply substitute strtol here, since
1113  * it does obey locale.
1114  */
1115 static int64_t
1116 mtree_atol8(char **p)
1117 {
1118 	int64_t	l, limit, last_digit_limit;
1119 	int digit, base;
1120 
1121 	base = 8;
1122 	limit = INT64_MAX / base;
1123 	last_digit_limit = INT64_MAX % base;
1124 
1125 	l = 0;
1126 	digit = **p - '0';
1127 	while (digit >= 0 && digit < base) {
1128 		if (l>limit || (l == limit && digit > last_digit_limit)) {
1129 			l = INT64_MAX; /* Truncate on overflow. */
1130 			break;
1131 		}
1132 		l = (l * base) + digit;
1133 		digit = *++(*p) - '0';
1134 	}
1135 	return (l);
1136 }
1137 
1138 /*
1139  * Note that this implementation does not (and should not!) obey
1140  * locale settings; you cannot simply substitute strtol here, since
1141  * it does obey locale.
1142  */
1143 static int64_t
1144 mtree_atol10(char **p)
1145 {
1146 	int64_t l, limit, last_digit_limit;
1147 	int base, digit, sign;
1148 
1149 	base = 10;
1150 	limit = INT64_MAX / base;
1151 	last_digit_limit = INT64_MAX % base;
1152 
1153 	if (**p == '-') {
1154 		sign = -1;
1155 		++(*p);
1156 	} else
1157 		sign = 1;
1158 
1159 	l = 0;
1160 	digit = **p - '0';
1161 	while (digit >= 0 && digit < base) {
1162 		if (l > limit || (l == limit && digit > last_digit_limit)) {
1163 			l = INT64_MAX; /* Truncate on overflow. */
1164 			break;
1165 		}
1166 		l = (l * base) + digit;
1167 		digit = *++(*p) - '0';
1168 	}
1169 	return (sign < 0) ? -l : l;
1170 }
1171 
1172 /*
1173  * Note that this implementation does not (and should not!) obey
1174  * locale settings; you cannot simply substitute strtol here, since
1175  * it does obey locale.
1176  */
1177 static int64_t
1178 mtree_atol16(char **p)
1179 {
1180 	int64_t l, limit, last_digit_limit;
1181 	int base, digit, sign;
1182 
1183 	base = 16;
1184 	limit = INT64_MAX / base;
1185 	last_digit_limit = INT64_MAX % base;
1186 
1187 	if (**p == '-') {
1188 		sign = -1;
1189 		++(*p);
1190 	} else
1191 		sign = 1;
1192 
1193 	l = 0;
1194 	if (**p >= '0' && **p <= '9')
1195 		digit = **p - '0';
1196 	else if (**p >= 'a' && **p <= 'f')
1197 		digit = **p - 'a' + 10;
1198 	else if (**p >= 'A' && **p <= 'F')
1199 		digit = **p - 'A' + 10;
1200 	else
1201 		digit = -1;
1202 	while (digit >= 0 && digit < base) {
1203 		if (l > limit || (l == limit && digit > last_digit_limit)) {
1204 			l = INT64_MAX; /* Truncate on overflow. */
1205 			break;
1206 		}
1207 		l = (l * base) + digit;
1208 		if (**p >= '0' && **p <= '9')
1209 			digit = **p - '0';
1210 		else if (**p >= 'a' && **p <= 'f')
1211 			digit = **p - 'a' + 10;
1212 		else if (**p >= 'A' && **p <= 'F')
1213 			digit = **p - 'A' + 10;
1214 		else
1215 			digit = -1;
1216 	}
1217 	return (sign < 0) ? -l : l;
1218 }
1219 
1220 static int64_t
1221 mtree_atol(char **p)
1222 {
1223 	if (**p != '0')
1224 		return mtree_atol10(p);
1225 	if ((*p)[1] == 'x' || (*p)[1] == 'X') {
1226 		*p += 2;
1227 		return mtree_atol16(p);
1228 	}
1229 	return mtree_atol8(p);
1230 }
1231 
1232 /*
1233  * Returns length of line (including trailing newline)
1234  * or negative on error.  'start' argument is updated to
1235  * point to first character of line.
1236  */
1237 static ssize_t
1238 readline(struct archive_read *a, struct mtree *mtree, char **start, ssize_t limit)
1239 {
1240 	ssize_t bytes_read;
1241 	ssize_t total_size = 0;
1242 	ssize_t find_off = 0;
1243 	const void *t;
1244 	const char *s;
1245 	void *p;
1246 	char *u;
1247 
1248 	/* Accumulate line in a line buffer. */
1249 	for (;;) {
1250 		/* Read some more. */
1251 		t = __archive_read_ahead(a, 1, &bytes_read);
1252 		if (t == NULL)
1253 			return (0);
1254 		if (bytes_read < 0)
1255 			return (ARCHIVE_FATAL);
1256 		s = t;  /* Start of line? */
1257 		p = memchr(t, '\n', bytes_read);
1258 		/* If we found '\n', trim the read. */
1259 		if (p != NULL) {
1260 			bytes_read = 1 + ((const char *)p) - s;
1261 		}
1262 		if (total_size + bytes_read + 1 > limit) {
1263 			archive_set_error(&a->archive,
1264 			    ARCHIVE_ERRNO_FILE_FORMAT,
1265 			    "Line too long");
1266 			return (ARCHIVE_FATAL);
1267 		}
1268 		if (archive_string_ensure(&mtree->line,
1269 			total_size + bytes_read + 1) == NULL) {
1270 			archive_set_error(&a->archive, ENOMEM,
1271 			    "Can't allocate working buffer");
1272 			return (ARCHIVE_FATAL);
1273 		}
1274 		memcpy(mtree->line.s + total_size, t, bytes_read);
1275 		__archive_read_consume(a, bytes_read);
1276 		total_size += bytes_read;
1277 		/* Null terminate. */
1278 		mtree->line.s[total_size] = '\0';
1279 		/* If we found an unescaped '\n', clean up and return. */
1280 		for (u = mtree->line.s + find_off; *u; ++u) {
1281 			if (u[0] == '\n') {
1282 				*start = mtree->line.s;
1283 				return total_size;
1284 			}
1285 			if (u[0] == '#') {
1286 				if (p == NULL)
1287 					break;
1288 				*start = mtree->line.s;
1289 				return total_size;
1290 			}
1291 			if (u[0] != '\\')
1292 				continue;
1293 			if (u[1] == '\\') {
1294 				++u;
1295 				continue;
1296 			}
1297 			if (u[1] == '\n') {
1298 				memmove(u, u + 1,
1299 				    total_size - (u - mtree->line.s) + 1);
1300 				--total_size;
1301 				++u;
1302 				break;
1303 			}
1304 			if (u[1] == '\0')
1305 				break;
1306 		}
1307 		find_off = u - mtree->line.s;
1308 	}
1309 }
1310