123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308 |
- /*-
- * Copyright (c) 2003-2007 Tim Kientzle
- * Copyright (c) 2008 Joerg Sonnenberger
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "archive_platform.h"
- __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_mtree.c,v 1.11 2008/12/06 06:45:15 kientzle Exp $");
- #ifdef HAVE_SYS_STAT_H
- #include <sys/stat.h>
- #endif
- #ifdef HAVE_ERRNO_H
- #include <errno.h>
- #endif
- #ifdef HAVE_FCNTL_H
- #include <fcntl.h>
- #endif
- #include <stddef.h>
- /* #include <stdint.h> */ /* See archive_platform.h */
- #ifdef HAVE_STDLIB_H
- #include <stdlib.h>
- #endif
- #ifdef HAVE_STRING_H
- #include <string.h>
- #endif
- #include "archive.h"
- #include "archive_entry.h"
- #include "archive_private.h"
- #include "archive_read_private.h"
- #include "archive_string.h"
- #ifndef O_BINARY
- #define O_BINARY 0
- #endif
- #define MTREE_HAS_DEVICE 0x0001
- #define MTREE_HAS_FFLAGS 0x0002
- #define MTREE_HAS_GID 0x0004
- #define MTREE_HAS_GNAME 0x0008
- #define MTREE_HAS_MTIME 0x0010
- #define MTREE_HAS_NLINK 0x0020
- #define MTREE_HAS_PERM 0x0040
- #define MTREE_HAS_SIZE 0x0080
- #define MTREE_HAS_TYPE 0x0100
- #define MTREE_HAS_UID 0x0200
- #define MTREE_HAS_UNAME 0x0400
- #define MTREE_HAS_OPTIONAL 0x0800
- struct mtree_option {
- struct mtree_option *next;
- char *value;
- };
- struct mtree_entry {
- struct mtree_entry *next;
- struct mtree_option *options;
- char *name;
- char full;
- char used;
- };
- struct mtree {
- struct archive_string line;
- size_t buffsize;
- char *buff;
- off_t offset;
- int fd;
- int filetype;
- int archive_format;
- const char *archive_format_name;
- struct mtree_entry *entries;
- struct mtree_entry *this_entry;
- struct archive_string current_dir;
- struct archive_string contents_name;
- struct archive_entry_linkresolver *resolver;
- off_t cur_size;
- };
- static int cleanup(struct archive_read *);
- static int mtree_bid(struct archive_read *);
- static int parse_file(struct archive_read *, struct archive_entry *,
- struct mtree *, struct mtree_entry *, int *);
- static void parse_escapes(char *, struct mtree_entry *);
- static int parse_line(struct archive_read *, struct archive_entry *,
- struct mtree *, struct mtree_entry *, int *);
- static int parse_keyword(struct archive_read *, struct mtree *,
- struct archive_entry *, struct mtree_option *, int *);
- static int read_data(struct archive_read *a,
- const void **buff, size_t *size, off_t *offset);
- static ssize_t readline(struct archive_read *, struct mtree *, char **, ssize_t);
- static int skip(struct archive_read *a);
- static int read_header(struct archive_read *,
- struct archive_entry *);
- static int64_t mtree_atol10(char **);
- static int64_t mtree_atol8(char **);
- static int64_t mtree_atol(char **);
- static void
- free_options(struct mtree_option *head)
- {
- struct mtree_option *next;
- for (; head != NULL; head = next) {
- next = head->next;
- free(head->value);
- free(head);
- }
- }
- int
- archive_read_support_format_mtree(struct archive *_a)
- {
- struct archive_read *a = (struct archive_read *)_a;
- struct mtree *mtree;
- int r;
- mtree = (struct mtree *)malloc(sizeof(*mtree));
- if (mtree == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate mtree data");
- return (ARCHIVE_FATAL);
- }
- memset(mtree, 0, sizeof(*mtree));
- mtree->fd = -1;
- r = __archive_read_register_format(a, mtree, "mtree",
- mtree_bid, NULL, read_header, read_data, NULL, NULL, skip, cleanup);
- if (r != ARCHIVE_OK)
- free(mtree);
- return (ARCHIVE_OK);
- }
- static int
- cleanup(struct archive_read *a)
- {
- struct mtree *mtree;
- struct mtree_entry *p, *q;
- mtree = (struct mtree *)(a->format->data);
- p = mtree->entries;
- while (p != NULL) {
- q = p->next;
- free(p->name);
- free_options(p->options);
- free(p);
- p = q;
- }
- archive_string_free(&mtree->line);
- archive_string_free(&mtree->current_dir);
- archive_string_free(&mtree->contents_name);
- archive_entry_linkresolver_free(mtree->resolver);
- free(mtree->buff);
- free(mtree);
- (a->format->data) = NULL;
- return (ARCHIVE_OK);
- }
- static int
- mtree_bid(struct archive_read *a)
- {
- const char *signature = "#mtree";
- const char *p;
- /* Now let's look at the actual header and see if it matches. */
- p = __archive_read_ahead(a, strlen(signature), NULL);
- if (p == NULL)
- return (-1);
- if (strncmp(p, signature, strlen(signature)) == 0)
- return (8 * strlen(signature));
- return (0);
- }
- /*
- * The extended mtree format permits multiple lines specifying
- * attributes for each file. For those entries, only the last line
- * is actually used. Practically speaking, that means we have
- * to read the entire mtree file into memory up front.
- *
- * The parsing is done in two steps. First, it is decided if a line
- * changes the global defaults and if it is, processed accordingly.
- * Otherwise, the options of the line are merged with the current
- * global options.
- */
- static int
- add_option(struct archive_read *a, struct mtree_option **global,
- const char *value, size_t len)
- {
- struct mtree_option *option;
- if ((option = malloc(sizeof(*option))) == NULL) {
- archive_set_error(&a->archive, errno, "Can't allocate memory");
- return (ARCHIVE_FATAL);
- }
- if ((option->value = malloc(len + 1)) == NULL) {
- free(option);
- archive_set_error(&a->archive, errno, "Can't allocate memory");
- return (ARCHIVE_FATAL);
- }
- memcpy(option->value, value, len);
- option->value[len] = '\0';
- option->next = *global;
- *global = option;
- return (ARCHIVE_OK);
- }
- static void
- remove_option(struct mtree_option **global, const char *value, size_t len)
- {
- struct mtree_option *iter, *last;
- last = NULL;
- for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
- if (strncmp(iter->value, value, len) == 0 &&
- (iter->value[len] == '\0' ||
- iter->value[len] == '='))
- break;
- }
- if (iter == NULL)
- return;
- if (last == NULL)
- *global = iter->next;
- else
- last->next = iter->next;
- free(iter->value);
- free(iter);
- }
- static int
- process_global_set(struct archive_read *a,
- struct mtree_option **global, const char *line)
- {
- const char *next, *eq;
- size_t len;
- int r;
- line += 4;
- for (;;) {
- next = line + strspn(line, " \t\r\n");
- if (*next == '\0')
- return (ARCHIVE_OK);
- line = next;
- next = line + strcspn(line, " \t\r\n");
- eq = strchr(line, '=');
- if (eq > next)
- len = next - line;
- else
- len = eq - line;
- remove_option(global, line, len);
- r = add_option(a, global, line, next - line);
- if (r != ARCHIVE_OK)
- return (r);
- line = next;
- }
- }
- static int
- process_global_unset(struct archive_read *a,
- struct mtree_option **global, const char *line)
- {
- const char *next;
- size_t len;
- line += 6;
- if (strchr(line, '=') != NULL) {
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "/unset shall not contain `='");
- return ARCHIVE_FATAL;
- }
- for (;;) {
- next = line + strspn(line, " \t\r\n");
- if (*next == '\0')
- return (ARCHIVE_OK);
- line = next;
- len = strcspn(line, " \t\r\n");
- if (len == 3 && strncmp(line, "all", 3) == 0) {
- free_options(*global);
- *global = NULL;
- } else {
- remove_option(global, line, len);
- }
- line += len;
- }
- }
- static int
- process_add_entry(struct archive_read *a, struct mtree *mtree,
- struct mtree_option **global, const char *line,
- struct mtree_entry **last_entry)
- {
- struct mtree_entry *entry;
- struct mtree_option *iter;
- const char *next, *eq;
- size_t len;
- int r;
- if ((entry = malloc(sizeof(*entry))) == NULL) {
- archive_set_error(&a->archive, errno, "Can't allocate memory");
- return (ARCHIVE_FATAL);
- }
- entry->next = NULL;
- entry->options = NULL;
- entry->name = NULL;
- entry->used = 0;
- entry->full = 0;
- /* Add this entry to list. */
- if (*last_entry == NULL)
- mtree->entries = entry;
- else
- (*last_entry)->next = entry;
- *last_entry = entry;
- len = strcspn(line, " \t\r\n");
- if ((entry->name = malloc(len + 1)) == NULL) {
- archive_set_error(&a->archive, errno, "Can't allocate memory");
- return (ARCHIVE_FATAL);
- }
- memcpy(entry->name, line, len);
- entry->name[len] = '\0';
- parse_escapes(entry->name, entry);
- line += len;
- for (iter = *global; iter != NULL; iter = iter->next) {
- r = add_option(a, &entry->options, iter->value,
- strlen(iter->value));
- if (r != ARCHIVE_OK)
- return (r);
- }
- for (;;) {
- next = line + strspn(line, " \t\r\n");
- if (*next == '\0')
- return (ARCHIVE_OK);
- line = next;
- next = line + strcspn(line, " \t\r\n");
- eq = strchr(line, '=');
- if (eq > next)
- len = next - line;
- else
- len = eq - line;
- remove_option(&entry->options, line, len);
- r = add_option(a, &entry->options, line, next - line);
- if (r != ARCHIVE_OK)
- return (r);
- line = next;
- }
- }
- static int
- read_mtree(struct archive_read *a, struct mtree *mtree)
- {
- ssize_t len;
- uintmax_t counter;
- char *p;
- struct mtree_option *global;
- struct mtree_entry *last_entry;
- int r;
- mtree->archive_format = ARCHIVE_FORMAT_MTREE;
- mtree->archive_format_name = "mtree";
- global = NULL;
- last_entry = NULL;
- for (counter = 1; ; ++counter) {
- len = readline(a, mtree, &p, 256);
- if (len == 0) {
- mtree->this_entry = mtree->entries;
- free_options(global);
- return (ARCHIVE_OK);
- }
- if (len < 0) {
- free_options(global);
- return (len);
- }
- /* Leading whitespace is never significant, ignore it. */
- while (*p == ' ' || *p == '\t') {
- ++p;
- --len;
- }
- /* Skip content lines and blank lines. */
- if (*p == '#')
- continue;
- if (*p == '\r' || *p == '\n' || *p == '\0')
- continue;
- if (*p != '/') {
- r = process_add_entry(a, mtree, &global, p,
- &last_entry);
- } else if (strncmp(p, "/set", 4) == 0) {
- if (p[4] != ' ' && p[4] != '\t')
- break;
- r = process_global_set(a, &global, p);
- } else if (strncmp(p, "/unset", 6) == 0) {
- if (p[6] != ' ' && p[6] != '\t')
- break;
- r = process_global_unset(a, &global, p);
- } else
- break;
- if (r != ARCHIVE_OK) {
- free_options(global);
- return r;
- }
- }
- archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
- "Can't parse line %ju", counter);
- free_options(global);
- return (ARCHIVE_FATAL);
- }
- /*
- * Read in the entire mtree file into memory on the first request.
- * Then use the next unused file to satisfy each header request.
- */
- static int
- read_header(struct archive_read *a, struct archive_entry *entry)
- {
- struct mtree *mtree;
- char *p;
- int r, use_next;
- mtree = (struct mtree *)(a->format->data);
- if (mtree->fd >= 0) {
- close(mtree->fd);
- mtree->fd = -1;
- }
- if (mtree->entries == NULL) {
- mtree->resolver = archive_entry_linkresolver_new();
- if (mtree->resolver == NULL)
- return ARCHIVE_FATAL;
- archive_entry_linkresolver_set_strategy(mtree->resolver,
- ARCHIVE_FORMAT_MTREE);
- r = read_mtree(a, mtree);
- if (r != ARCHIVE_OK)
- return (r);
- }
- a->archive.archive_format = mtree->archive_format;
- a->archive.archive_format_name = mtree->archive_format_name;
- for (;;) {
- if (mtree->this_entry == NULL)
- return (ARCHIVE_EOF);
- if (strcmp(mtree->this_entry->name, "..") == 0) {
- mtree->this_entry->used = 1;
- if (archive_strlen(&mtree->current_dir) > 0) {
- /* Roll back current path. */
- p = mtree->current_dir.s
- + mtree->current_dir.length - 1;
- while (p >= mtree->current_dir.s && *p != '/')
- --p;
- if (p >= mtree->current_dir.s)
- --p;
- mtree->current_dir.length
- = p - mtree->current_dir.s + 1;
- }
- }
- if (!mtree->this_entry->used) {
- use_next = 0;
- r = parse_file(a, entry, mtree, mtree->this_entry, &use_next);
- if (use_next == 0)
- return (r);
- }
- mtree->this_entry = mtree->this_entry->next;
- }
- }
- /*
- * A single file can have multiple lines contribute specifications.
- * Parse as many lines as necessary, then pull additional information
- * from a backing file on disk as necessary.
- */
- static int
- parse_file(struct archive_read *a, struct archive_entry *entry,
- struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
- {
- const char *path;
- struct stat st_storage, *st;
- struct mtree_entry *mp;
- struct archive_entry *sparse_entry;
- int r = ARCHIVE_OK, r1, parsed_kws, mismatched_type;
- mentry->used = 1;
- /* Initialize reasonable defaults. */
- mtree->filetype = AE_IFREG;
- archive_entry_set_size(entry, 0);
- /* Parse options from this line. */
- parsed_kws = 0;
- r = parse_line(a, entry, mtree, mentry, &parsed_kws);
- if (mentry->full) {
- archive_entry_copy_pathname(entry, mentry->name);
- /*
- * "Full" entries are allowed to have multiple lines
- * and those lines aren't required to be adjacent. We
- * don't support multiple lines for "relative" entries
- * nor do we make any attempt to merge data from
- * separate "relative" and "full" entries. (Merging
- * "relative" and "full" entries would require dealing
- * with pathname canonicalization, which is a very
- * tricky subject.)
- */
- for (mp = mentry->next; mp != NULL; mp = mp->next) {
- if (mp->full && !mp->used
- && strcmp(mentry->name, mp->name) == 0) {
- /* Later lines override earlier ones. */
- mp->used = 1;
- r1 = parse_line(a, entry, mtree, mp,
- &parsed_kws);
- if (r1 < r)
- r = r1;
- }
- }
- } else {
- /*
- * Relative entries require us to construct
- * the full path and possibly update the
- * current directory.
- */
- size_t n = archive_strlen(&mtree->current_dir);
- if (n > 0)
- archive_strcat(&mtree->current_dir, "/");
- archive_strcat(&mtree->current_dir, mentry->name);
- archive_entry_copy_pathname(entry, mtree->current_dir.s);
- if (archive_entry_filetype(entry) != AE_IFDIR)
- mtree->current_dir.length = n;
- }
- /*
- * Try to open and stat the file to get the real size
- * and other file info. It would be nice to avoid
- * this here so that getting a listing of an mtree
- * wouldn't require opening every referenced contents
- * file. But then we wouldn't know the actual
- * contents size, so I don't see a really viable way
- * around this. (Also, we may want to someday pull
- * other unspecified info from the contents file on
- * disk.)
- */
- mtree->fd = -1;
- if (archive_strlen(&mtree->contents_name) > 0)
- path = mtree->contents_name.s;
- else
- path = archive_entry_pathname(entry);
- if (archive_entry_filetype(entry) == AE_IFREG ||
- archive_entry_filetype(entry) == AE_IFDIR) {
- mtree->fd = open(path,
- O_RDONLY | O_BINARY);
- if (mtree->fd == -1 &&
- (errno != ENOENT ||
- archive_strlen(&mtree->contents_name) > 0)) {
- archive_set_error(&a->archive, errno,
- "Can't open %s", path);
- r = ARCHIVE_WARN;
- }
- }
- st = &st_storage;
- if (mtree->fd >= 0) {
- if (fstat(mtree->fd, st) == -1) {
- archive_set_error(&a->archive, errno,
- "Could not fstat %s", path);
- r = ARCHIVE_WARN;
- /* If we can't stat it, don't keep it open. */
- close(mtree->fd);
- mtree->fd = -1;
- st = NULL;
- }
- } else if (lstat(path, st) == -1) {
- st = NULL;
- }
- /*
- * If there is a contents file on disk, use that size;
- * otherwise leave it as-is (it might have been set from
- * the mtree size= keyword).
- */
- if (st != NULL) {
- mismatched_type = 0;
- if ((st->st_mode & S_IFMT) == S_IFREG &&
- archive_entry_filetype(entry) != AE_IFREG)
- mismatched_type = 1;
- if ((st->st_mode & S_IFMT) == S_IFLNK &&
- archive_entry_filetype(entry) != AE_IFLNK)
- mismatched_type = 1;
- if ((st->st_mode & S_IFSOCK) == S_IFSOCK &&
- archive_entry_filetype(entry) != AE_IFSOCK)
- mismatched_type = 1;
- if ((st->st_mode & S_IFMT) == S_IFCHR &&
- archive_entry_filetype(entry) != AE_IFCHR)
- mismatched_type = 1;
- if ((st->st_mode & S_IFMT) == S_IFBLK &&
- archive_entry_filetype(entry) != AE_IFBLK)
- mismatched_type = 1;
- if ((st->st_mode & S_IFMT) == S_IFDIR &&
- archive_entry_filetype(entry) != AE_IFDIR)
- mismatched_type = 1;
- if ((st->st_mode & S_IFMT) == S_IFIFO &&
- archive_entry_filetype(entry) != AE_IFIFO)
- mismatched_type = 1;
- if (mismatched_type) {
- if ((parsed_kws & MTREE_HAS_OPTIONAL) == 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "mtree specification has different type for %s",
- archive_entry_pathname(entry));
- r = ARCHIVE_WARN;
- } else {
- *use_next = 1;
- }
- /* Don't hold a non-regular file open. */
- if (mtree->fd >= 0)
- close(mtree->fd);
- mtree->fd = -1;
- st = NULL;
- return r;
- }
- }
- if (st != NULL) {
- if ((parsed_kws & MTREE_HAS_DEVICE) == 0 &&
- (archive_entry_filetype(entry) == AE_IFCHR ||
- archive_entry_filetype(entry) == AE_IFBLK))
- archive_entry_set_rdev(entry, st->st_rdev);
- if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME)) == 0)
- archive_entry_set_gid(entry, st->st_gid);
- if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME)) == 0)
- archive_entry_set_uid(entry, st->st_uid);
- if ((parsed_kws & MTREE_HAS_MTIME) == 0) {
- #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
- archive_entry_set_mtime(entry, st->st_mtime,
- st->st_mtimespec.tv_nsec);
- #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
- archive_entry_set_mtime(entry, st->st_mtime,
- st->st_mtim.tv_nsec);
- #elif HAVE_STRUCT_STAT_ST_MTIME_N
- archive_entry_set_mtime(entry, st->st_mtime,
- st->st_mtime_n);
- #elif HAVE_STRUCT_STAT_ST_UMTIME
- archive_entry_set_mtime(entry, st->st_mtime,
- st->st_umtime*1000);
- #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
- archive_entry_set_mtime(entry, st->st_mtime,
- st->st_mtime_usec*1000);
- #else
- archive_entry_set_mtime(entry, st->st_mtime, 0);
- #endif
- }
- if ((parsed_kws & MTREE_HAS_NLINK) == 0)
- archive_entry_set_nlink(entry, st->st_nlink);
- if ((parsed_kws & MTREE_HAS_PERM) == 0)
- archive_entry_set_perm(entry, st->st_mode);
- if ((parsed_kws & MTREE_HAS_SIZE) == 0)
- archive_entry_set_size(entry, st->st_size);
- archive_entry_set_ino(entry, st->st_ino);
- archive_entry_set_dev(entry, st->st_dev);
- archive_entry_linkify(mtree->resolver, &entry, &sparse_entry);
- } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
- /*
- * Couldn't open the entry, stat it or the on-disk type
- * didn't match. If this entry is optional, just ignore it
- * and read the next header entry.
- */
- *use_next = 1;
- return ARCHIVE_OK;
- }
- mtree->cur_size = archive_entry_size(entry);
- mtree->offset = 0;
- return r;
- }
- /*
- * Each line contains a sequence of keywords.
- */
- static int
- parse_line(struct archive_read *a, struct archive_entry *entry,
- struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
- {
- struct mtree_option *iter;
- int r = ARCHIVE_OK, r1;
- for (iter = mp->options; iter != NULL; iter = iter->next) {
- r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
- if (r1 < r)
- r = r1;
- }
- if ((*parsed_kws & MTREE_HAS_TYPE) == 0) {
- archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
- "Missing type keyword in mtree specification");
- return (ARCHIVE_WARN);
- }
- return (r);
- }
- /*
- * Device entries have one of the following forms:
- * raw dev_t
- * format,major,minor[,subdevice]
- *
- * Just use major and minor, no translation etc is done
- * between formats.
- */
- static int
- parse_device(struct archive *a, struct archive_entry *entry, char *val)
- {
- char *comma1, *comma2;
- comma1 = strchr(val, ',');
- if (comma1 == NULL) {
- archive_entry_set_dev(entry, mtree_atol10(&val));
- return (ARCHIVE_OK);
- }
- ++comma1;
- comma2 = strchr(comma1, ',');
- if (comma2 == NULL) {
- archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
- "Malformed device attribute");
- return (ARCHIVE_WARN);
- }
- ++comma2;
- archive_entry_set_rdevmajor(entry, mtree_atol(&comma1));
- archive_entry_set_rdevminor(entry, mtree_atol(&comma2));
- return (ARCHIVE_OK);
- }
- /*
- * Parse a single keyword and its value.
- */
- static int
- parse_keyword(struct archive_read *a, struct mtree *mtree,
- struct archive_entry *entry, struct mtree_option *option, int *parsed_kws)
- {
- char *val, *key;
- key = option->value;
- if (*key == '\0')
- return (ARCHIVE_OK);
- if (strcmp(key, "optional") == 0) {
- *parsed_kws |= MTREE_HAS_OPTIONAL;
- return (ARCHIVE_OK);
- }
- if (strcmp(key, "ignore") == 0) {
- /*
- * The mtree processing is not recursive, so
- * recursion will only happen for explicitly listed
- * entries.
- */
- return (ARCHIVE_OK);
- }
- val = strchr(key, '=');
- if (val == NULL) {
- archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
- "Malformed attribute \"%s\" (%d)", key, key[0]);
- return (ARCHIVE_WARN);
- }
- *val = '\0';
- ++val;
- switch (key[0]) {
- case 'c':
- if (strcmp(key, "content") == 0
- || strcmp(key, "contents") == 0) {
- parse_escapes(val, NULL);
- archive_strcpy(&mtree->contents_name, val);
- break;
- }
- if (strcmp(key, "cksum") == 0)
- break;
- case 'd':
- if (strcmp(key, "device") == 0) {
- *parsed_kws |= MTREE_HAS_DEVICE;
- return parse_device(&a->archive, entry, val);
- }
- case 'f':
- if (strcmp(key, "flags") == 0) {
- *parsed_kws |= MTREE_HAS_FFLAGS;
- archive_entry_copy_fflags_text(entry, val);
- break;
- }
- case 'g':
- if (strcmp(key, "gid") == 0) {
- *parsed_kws |= MTREE_HAS_GID;
- archive_entry_set_gid(entry, mtree_atol10(&val));
- break;
- }
- if (strcmp(key, "gname") == 0) {
- *parsed_kws |= MTREE_HAS_GNAME;
- archive_entry_copy_gname(entry, val);
- break;
- }
- case 'l':
- if (strcmp(key, "link") == 0) {
- archive_entry_copy_symlink(entry, val);
- break;
- }
- case 'm':
- if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
- break;
- if (strcmp(key, "mode") == 0) {
- if (val[0] >= '0' && val[0] <= '9') {
- *parsed_kws |= MTREE_HAS_PERM;
- archive_entry_set_perm(entry,
- mtree_atol8(&val));
- } else {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_FILE_FORMAT,
- "Symbolic mode \"%s\" unsupported", val);
- return ARCHIVE_WARN;
- }
- break;
- }
- case 'n':
- if (strcmp(key, "nlink") == 0) {
- *parsed_kws |= MTREE_HAS_NLINK;
- archive_entry_set_nlink(entry, mtree_atol10(&val));
- break;
- }
- case 'r':
- if (strcmp(key, "rmd160") == 0 ||
- strcmp(key, "rmd160digest") == 0)
- break;
- case 's':
- if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
- break;
- if (strcmp(key, "sha256") == 0 ||
- strcmp(key, "sha256digest") == 0)
- break;
- if (strcmp(key, "sha384") == 0 ||
- strcmp(key, "sha384digest") == 0)
- break;
- if (strcmp(key, "sha512") == 0 ||
- strcmp(key, "sha512digest") == 0)
- break;
- if (strcmp(key, "size") == 0) {
- archive_entry_set_size(entry, mtree_atol10(&val));
- break;
- }
- case 't':
- if (strcmp(key, "tags") == 0) {
- /*
- * Comma delimited list of tags.
- * Ignore the tags for now, but the interface
- * should be extended to allow inclusion/exclusion.
- */
- break;
- }
- if (strcmp(key, "time") == 0) {
- time_t m;
- long ns;
- *parsed_kws |= MTREE_HAS_MTIME;
- m = (time_t)mtree_atol10(&val);
- if (*val == '.') {
- ++val;
- ns = (long)mtree_atol10(&val);
- } else
- ns = 0;
- archive_entry_set_mtime(entry, m, ns);
- break;
- }
- if (strcmp(key, "type") == 0) {
- *parsed_kws |= MTREE_HAS_TYPE;
- switch (val[0]) {
- case 'b':
- if (strcmp(val, "block") == 0) {
- mtree->filetype = AE_IFBLK;
- break;
- }
- case 'c':
- if (strcmp(val, "char") == 0) {
- mtree->filetype = AE_IFCHR;
- break;
- }
- case 'd':
- if (strcmp(val, "dir") == 0) {
- mtree->filetype = AE_IFDIR;
- break;
- }
- case 'f':
- if (strcmp(val, "fifo") == 0) {
- mtree->filetype = AE_IFIFO;
- break;
- }
- if (strcmp(val, "file") == 0) {
- mtree->filetype = AE_IFREG;
- break;
- }
- case 'l':
- if (strcmp(val, "link") == 0) {
- mtree->filetype = AE_IFLNK;
- break;
- }
- default:
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_FILE_FORMAT,
- "Unrecognized file type \"%s\"", val);
- return (ARCHIVE_WARN);
- }
- archive_entry_set_filetype(entry, mtree->filetype);
- break;
- }
- case 'u':
- if (strcmp(key, "uid") == 0) {
- *parsed_kws |= MTREE_HAS_UID;
- archive_entry_set_uid(entry, mtree_atol10(&val));
- break;
- }
- if (strcmp(key, "uname") == 0) {
- *parsed_kws |= MTREE_HAS_UNAME;
- archive_entry_copy_uname(entry, val);
- break;
- }
- default:
- archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
- "Unrecognized key %s=%s", key, val);
- return (ARCHIVE_WARN);
- }
- return (ARCHIVE_OK);
- }
- static int
- read_data(struct archive_read *a, const void **buff, size_t *size, off_t *offset)
- {
- size_t bytes_to_read;
- ssize_t bytes_read;
- struct mtree *mtree;
- mtree = (struct mtree *)(a->format->data);
- if (mtree->fd < 0) {
- *buff = NULL;
- *offset = 0;
- *size = 0;
- return (ARCHIVE_EOF);
- }
- if (mtree->buff == NULL) {
- mtree->buffsize = 64 * 1024;
- mtree->buff = malloc(mtree->buffsize);
- if (mtree->buff == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate memory");
- }
- return (ARCHIVE_FATAL);
- }
- *buff = mtree->buff;
- *offset = mtree->offset;
- if ((off_t)mtree->buffsize > mtree->cur_size - mtree->offset)
- bytes_to_read = mtree->cur_size - mtree->offset;
- else
- bytes_to_read = mtree->buffsize;
- bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
- if (bytes_read < 0) {
- archive_set_error(&a->archive, errno, "Can't read");
- return (ARCHIVE_WARN);
- }
- if (bytes_read == 0) {
- *size = 0;
- return (ARCHIVE_EOF);
- }
- mtree->offset += bytes_read;
- *size = bytes_read;
- return (ARCHIVE_OK);
- }
- /* Skip does nothing except possibly close the contents file. */
- static int
- skip(struct archive_read *a)
- {
- struct mtree *mtree;
- mtree = (struct mtree *)(a->format->data);
- if (mtree->fd >= 0) {
- close(mtree->fd);
- mtree->fd = -1;
- }
- return (ARCHIVE_OK);
- }
- /*
- * Since parsing backslash sequences always makes strings shorter,
- * we can always do this conversion in-place.
- */
- static void
- parse_escapes(char *src, struct mtree_entry *mentry)
- {
- char *dest = src;
- char c;
- if (mentry != NULL && strcmp(src, ".") == 0)
- mentry->full = 1;
- while (*src != '\0') {
- c = *src++;
- if (c == '/' && mentry != NULL)
- mentry->full = 1;
- if (c == '\\') {
- switch (src[0]) {
- case '0':
- if (src[1] < '0' || src[1] > '7') {
- c = 0;
- ++src;
- break;
- }
- /* FALLTHROUGH */
- case '1':
- case '2':
- case '3':
- if (src[1] >= '0' && src[1] <= '7' &&
- src[2] >= '0' && src[2] <= '7') {
- c = (src[0] - '0') << 6;
- c |= (src[1] - '0') << 3;
- c |= (src[2] - '0');
- src += 3;
- }
- break;
- case 'a':
- c = '\a';
- ++src;
- break;
- case 'b':
- c = '\b';
- ++src;
- break;
- case 'f':
- c = '\f';
- ++src;
- break;
- case 'n':
- c = '\n';
- ++src;
- break;
- case 'r':
- c = '\r';
- ++src;
- break;
- case 's':
- c = ' ';
- ++src;
- break;
- case 't':
- c = '\t';
- ++src;
- break;
- case 'v':
- c = '\v';
- ++src;
- break;
- }
- }
- *dest++ = c;
- }
- *dest = '\0';
- }
- /*
- * Note that this implementation does not (and should not!) obey
- * locale settings; you cannot simply substitute strtol here, since
- * it does obey locale.
- */
- static int64_t
- mtree_atol8(char **p)
- {
- int64_t l, limit, last_digit_limit;
- int digit, base;
- base = 8;
- limit = INT64_MAX / base;
- last_digit_limit = INT64_MAX % base;
- l = 0;
- digit = **p - '0';
- while (digit >= 0 && digit < base) {
- if (l>limit || (l == limit && digit > last_digit_limit)) {
- l = INT64_MAX; /* Truncate on overflow. */
- break;
- }
- l = (l * base) + digit;
- digit = *++(*p) - '0';
- }
- return (l);
- }
- /*
- * Note that this implementation does not (and should not!) obey
- * locale settings; you cannot simply substitute strtol here, since
- * it does obey locale.
- */
- static int64_t
- mtree_atol10(char **p)
- {
- int64_t l, limit, last_digit_limit;
- int base, digit, sign;
- base = 10;
- if (**p == '-') {
- sign = -1;
- limit = ((uint64_t)(INT64_MAX) + 1) / base;
- last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
- ++(*p);
- } else {
- sign = 1;
- limit = INT64_MAX / base;
- last_digit_limit = INT64_MAX % base;
- }
- l = 0;
- digit = **p - '0';
- while (digit >= 0 && digit < base) {
- if (l > limit || (l == limit && digit > last_digit_limit))
- return (sign < 0) ? INT64_MIN : INT64_MAX;
- l = (l * base) + digit;
- digit = *++(*p) - '0';
- }
- return (sign < 0) ? -l : l;
- }
- /* Parse a hex digit. */
- static int
- parsehex(char c)
- {
- if (c >= '0' && c <= '9')
- return c - '0';
- else if (c >= 'a' && c <= 'f')
- return c - 'a';
- else if (c >= 'A' && c <= 'F')
- return c - 'A';
- else
- return -1;
- }
- /*
- * Note that this implementation does not (and should not!) obey
- * locale settings; you cannot simply substitute strtol here, since
- * it does obey locale.
- */
- static int64_t
- mtree_atol16(char **p)
- {
- int64_t l, limit, last_digit_limit;
- int base, digit, sign;
- base = 16;
- if (**p == '-') {
- sign = -1;
- limit = ((uint64_t)(INT64_MAX) + 1) / base;
- last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
- ++(*p);
- } else {
- sign = 1;
- limit = INT64_MAX / base;
- last_digit_limit = INT64_MAX % base;
- }
- l = 0;
- digit = parsehex(**p);
- while (digit >= 0 && digit < base) {
- if (l > limit || (l == limit && digit > last_digit_limit))
- return (sign < 0) ? INT64_MIN : INT64_MAX;
- l = (l * base) + digit;
- digit = parsehex(*++(*p));
- }
- return (sign < 0) ? -l : l;
- }
- static int64_t
- mtree_atol(char **p)
- {
- if (**p != '0')
- return mtree_atol10(p);
- if ((*p)[1] == 'x' || (*p)[1] == 'X') {
- *p += 2;
- return mtree_atol16(p);
- }
- return mtree_atol8(p);
- }
- /*
- * Returns length of line (including trailing newline)
- * or negative on error. 'start' argument is updated to
- * point to first character of line.
- */
- static ssize_t
- readline(struct archive_read *a, struct mtree *mtree, char **start, ssize_t limit)
- {
- ssize_t bytes_read;
- ssize_t total_size = 0;
- ssize_t find_off = 0;
- const void *t;
- const char *s;
- void *p;
- char *u;
- /* Accumulate line in a line buffer. */
- for (;;) {
- /* Read some more. */
- t = __archive_read_ahead(a, 1, &bytes_read);
- if (t == NULL)
- return (0);
- if (bytes_read < 0)
- return (ARCHIVE_FATAL);
- s = t; /* Start of line? */
- p = memchr(t, '\n', bytes_read);
- /* If we found '\n', trim the read. */
- if (p != NULL) {
- bytes_read = 1 + ((const char *)p) - s;
- }
- if (total_size + bytes_read + 1 > limit) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_FILE_FORMAT,
- "Line too long");
- return (ARCHIVE_FATAL);
- }
- if (archive_string_ensure(&mtree->line,
- total_size + bytes_read + 1) == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate working buffer");
- return (ARCHIVE_FATAL);
- }
- memcpy(mtree->line.s + total_size, t, bytes_read);
- __archive_read_consume(a, bytes_read);
- total_size += bytes_read;
- /* Null terminate. */
- mtree->line.s[total_size] = '\0';
- /* If we found an unescaped '\n', clean up and return. */
- for (u = mtree->line.s + find_off; *u; ++u) {
- if (u[0] == '\n') {
- *start = mtree->line.s;
- return total_size;
- }
- if (u[0] == '#') {
- if (p == NULL)
- break;
- *start = mtree->line.s;
- return total_size;
- }
- if (u[0] != '\\')
- continue;
- if (u[1] == '\\') {
- ++u;
- continue;
- }
- if (u[1] == '\n') {
- memmove(u, u + 1,
- total_size - (u - mtree->line.s) + 1);
- --total_size;
- ++u;
- break;
- }
- if (u[1] == '\0')
- break;
- }
- find_off = u - mtree->line.s;
- }
- }
|