2007-12-30 04:58:22 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003-2007 Tim Kientzle
|
2008-05-26 17:00:24 +00:00
|
|
|
* Copyright (c) 2008 Joerg Sonnenberger
|
2007-12-30 04:58:22 +00:00
|
|
|
* 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$");
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2008-02-19 06:10:48 +00:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
2007-12-30 04:58:22 +00:00
|
|
|
struct mtree_entry {
|
|
|
|
struct mtree_entry *next;
|
2008-05-26 17:00:24 +00:00
|
|
|
struct mtree_option *options;
|
2007-12-30 04:58:22 +00:00
|
|
|
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;
|
2008-03-15 07:10:24 +00:00
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
struct archive_entry_linkresolver *resolver;
|
|
|
|
|
2008-03-15 07:10:24 +00:00
|
|
|
off_t cur_size, cur_offset;
|
2007-12-30 04:58:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int cleanup(struct archive_read *);
|
|
|
|
static int mtree_bid(struct archive_read *);
|
2008-03-15 07:10:24 +00:00
|
|
|
static int parse_file(struct archive_read *, struct archive_entry *,
|
2008-05-26 17:00:24 +00:00
|
|
|
struct mtree *, struct mtree_entry *, int *);
|
2007-12-30 04:58:22 +00:00
|
|
|
static void parse_escapes(char *, struct mtree_entry *);
|
2008-03-15 07:10:24 +00:00
|
|
|
static int parse_line(struct archive_read *, struct archive_entry *,
|
2008-05-26 17:00:24 +00:00
|
|
|
struct mtree *, struct mtree_entry *, int *);
|
2008-03-15 07:10:24 +00:00
|
|
|
static int parse_keyword(struct archive_read *, struct mtree *,
|
2008-05-26 17:00:24 +00:00
|
|
|
struct archive_entry *, struct mtree_option *, int *);
|
2007-12-30 04:58:22 +00:00
|
|
|
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 **);
|
2008-05-26 17:00:24 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 04:58:22 +00:00
|
|
|
|
|
|
|
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_bid, read_header, read_data, 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);
|
2008-05-26 17:00:24 +00:00
|
|
|
|
2007-12-30 04:58:22 +00:00
|
|
|
p = mtree->entries;
|
|
|
|
while (p != NULL) {
|
|
|
|
q = p->next;
|
|
|
|
free(p->name);
|
2008-05-26 17:00:24 +00:00
|
|
|
free_options(p->options);
|
2007-12-30 04:58:22 +00:00
|
|
|
free(p);
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
archive_string_free(&mtree->line);
|
|
|
|
archive_string_free(&mtree->current_dir);
|
|
|
|
archive_string_free(&mtree->contents_name);
|
2008-05-26 17:00:24 +00:00
|
|
|
archive_entry_linkresolver_free(mtree->resolver);
|
|
|
|
|
2007-12-30 04:58:22 +00:00
|
|
|
free(mtree->buff);
|
|
|
|
free(mtree);
|
|
|
|
(a->format->data) = NULL;
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mtree_bid(struct archive_read *a)
|
|
|
|
{
|
|
|
|
struct mtree *mtree;
|
|
|
|
ssize_t bytes_read;
|
|
|
|
const void *h;
|
|
|
|
const char *signature = "#mtree";
|
|
|
|
const char *p;
|
|
|
|
int bid;
|
|
|
|
|
|
|
|
mtree = (struct mtree *)(a->format->data);
|
|
|
|
|
|
|
|
/* Now let's look at the actual header and see if it matches. */
|
|
|
|
bytes_read = (a->decompressor->read_ahead)(a, &h, strlen(signature));
|
|
|
|
|
|
|
|
if (bytes_read <= 0)
|
|
|
|
return (bytes_read);
|
|
|
|
|
|
|
|
p = h;
|
|
|
|
bid = 0;
|
|
|
|
while (bytes_read > 0 && *signature != '\0') {
|
|
|
|
if (*p != *signature)
|
|
|
|
return (bid = 0);
|
|
|
|
bid += 8;
|
|
|
|
p++;
|
|
|
|
signature++;
|
|
|
|
bytes_read--;
|
|
|
|
}
|
|
|
|
return (bid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The extended mtree format permits multiple lines specifying
|
2008-05-26 17:00:24 +00:00
|
|
|
* attributes for each file. For those entries, only the last line
|
|
|
|
* is actually used. Practically speaking, that means we have
|
2007-12-30 04:58:22 +00:00
|
|
|
* to read the entire mtree file into memory up front.
|
2008-05-26 17:00:24 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2007-12-30 04:58:22 +00:00
|
|
|
*/
|
2008-05-26 17:00:24 +00:00
|
|
|
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;
|
2008-06-09 14:44:56 +00:00
|
|
|
if (strchr(line, '=') != NULL) {
|
2008-05-26 17:00:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-30 04:58:22 +00:00
|
|
|
static int
|
|
|
|
read_mtree(struct archive_read *a, struct mtree *mtree)
|
|
|
|
{
|
|
|
|
ssize_t len;
|
2008-05-26 17:00:24 +00:00
|
|
|
uintmax_t counter;
|
2007-12-30 04:58:22 +00:00
|
|
|
char *p;
|
2008-05-26 17:00:24 +00:00
|
|
|
struct mtree_option *global;
|
|
|
|
struct mtree_entry *last_entry;
|
|
|
|
int r;
|
2007-12-30 04:58:22 +00:00
|
|
|
|
|
|
|
mtree->archive_format = ARCHIVE_FORMAT_MTREE_V1;
|
|
|
|
mtree->archive_format_name = "mtree";
|
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
global = NULL;
|
|
|
|
last_entry = NULL;
|
|
|
|
r = ARCHIVE_OK;
|
|
|
|
|
|
|
|
for (counter = 1; ; ++counter) {
|
2007-12-30 04:58:22 +00:00
|
|
|
len = readline(a, mtree, &p, 256);
|
|
|
|
if (len == 0) {
|
|
|
|
mtree->this_entry = mtree->entries;
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
if (len < 0)
|
|
|
|
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;
|
2008-05-26 17:00:24 +00:00
|
|
|
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;
|
2007-12-30 04:58:22 +00:00
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
return r;
|
2007-12-30 04:58:22 +00:00
|
|
|
}
|
2008-05-26 17:00:24 +00:00
|
|
|
|
|
|
|
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
|
|
|
|
"Can't parse line %ju", counter);
|
|
|
|
return ARCHIVE_FATAL;
|
2007-12-30 04:58:22 +00:00
|
|
|
}
|
|
|
|
|
2008-03-15 07:10:24 +00:00
|
|
|
/*
|
|
|
|
* Read in the entire mtree file into memory on the first request.
|
|
|
|
* Then use the next unused file to satisfy each header request.
|
|
|
|
*/
|
2007-12-30 04:58:22 +00:00
|
|
|
static int
|
|
|
|
read_header(struct archive_read *a, struct archive_entry *entry)
|
|
|
|
{
|
|
|
|
struct mtree *mtree;
|
2008-03-15 07:10:24 +00:00
|
|
|
char *p;
|
2008-05-26 17:00:24 +00:00
|
|
|
int r, use_next;
|
2007-12-30 04:58:22 +00:00
|
|
|
|
|
|
|
mtree = (struct mtree *)(a->format->data);
|
|
|
|
|
|
|
|
if (mtree->fd >= 0) {
|
|
|
|
close(mtree->fd);
|
|
|
|
mtree->fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mtree->entries == NULL) {
|
2008-05-26 17:00:24 +00:00
|
|
|
mtree->resolver = archive_entry_linkresolver_new();
|
|
|
|
if (mtree->resolver == NULL)
|
|
|
|
return ARCHIVE_FATAL;
|
|
|
|
archive_entry_linkresolver_set_strategy(mtree->resolver,
|
|
|
|
ARCHIVE_FORMAT_MTREE);
|
2007-12-30 04:58:22 +00:00
|
|
|
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 (;;) {
|
2008-03-15 07:10:24 +00:00
|
|
|
if (mtree->this_entry == NULL)
|
2007-12-30 04:58:22 +00:00
|
|
|
return (ARCHIVE_EOF);
|
2008-03-15 07:10:24 +00:00
|
|
|
if (strcmp(mtree->this_entry->name, "..") == 0) {
|
|
|
|
mtree->this_entry->used = 1;
|
2007-12-30 04:58:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2008-03-15 07:10:24 +00:00
|
|
|
if (!mtree->this_entry->used) {
|
2008-05-26 17:00:24 +00:00
|
|
|
use_next = 0;
|
|
|
|
r = parse_file(a, entry, mtree, mtree->this_entry, &use_next);
|
|
|
|
if (use_next == 0)
|
|
|
|
return (r);
|
2008-03-15 07:10:24 +00:00
|
|
|
}
|
|
|
|
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,
|
2008-05-26 17:00:24 +00:00
|
|
|
struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
|
2008-03-15 07:10:24 +00:00
|
|
|
{
|
2008-05-26 17:00:24 +00:00
|
|
|
const char *path;
|
|
|
|
struct stat st_storage, *st;
|
2008-03-15 07:10:24 +00:00
|
|
|
struct mtree_entry *mp;
|
2008-05-26 17:00:24 +00:00
|
|
|
struct archive_entry *sparse_entry;
|
|
|
|
int r = ARCHIVE_OK, r1, parsed_kws, mismatched_type;
|
2007-12-30 04:58:22 +00:00
|
|
|
|
2008-03-15 07:10:24 +00:00
|
|
|
mentry->used = 1;
|
2007-12-30 04:58:22 +00:00
|
|
|
|
2008-03-15 07:10:24 +00:00
|
|
|
/* Initialize reasonable defaults. */
|
|
|
|
mtree->filetype = AE_IFREG;
|
|
|
|
archive_entry_set_size(entry, 0);
|
2007-12-30 04:58:22 +00:00
|
|
|
|
2008-03-15 07:10:24 +00:00
|
|
|
/* Parse options from this line. */
|
2008-05-26 17:00:24 +00:00
|
|
|
parsed_kws = 0;
|
|
|
|
r = parse_line(a, entry, mtree, mentry, &parsed_kws);
|
2007-12-30 04:58:22 +00:00
|
|
|
|
2008-03-15 07:10:24 +00:00
|
|
|
if (mentry->full) {
|
|
|
|
archive_entry_copy_pathname(entry, mentry->name);
|
2007-12-30 04:58:22 +00:00
|
|
|
/*
|
2008-03-15 07:10:24 +00:00
|
|
|
* "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.)
|
2007-12-30 04:58:22 +00:00
|
|
|
*/
|
2008-03-15 07:10:24 +00:00
|
|
|
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;
|
2008-05-26 17:00:24 +00:00
|
|
|
r1 = parse_line(a, entry, mtree, mp,
|
|
|
|
&parsed_kws);
|
2008-03-15 07:10:24 +00:00
|
|
|
if (r1 < r)
|
|
|
|
r = r1;
|
2007-12-30 04:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
2008-03-15 07:10:24 +00:00
|
|
|
} else {
|
2007-12-30 04:58:22 +00:00
|
|
|
/*
|
2008-03-15 07:10:24 +00:00
|
|
|
* Relative entries require us to construct
|
|
|
|
* the full path and possibly update the
|
|
|
|
* current directory.
|
2007-12-30 04:58:22 +00:00
|
|
|
*/
|
2008-03-15 07:10:24 +00:00
|
|
|
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;
|
2008-05-26 17:00:24 +00:00
|
|
|
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,
|
2008-03-15 07:10:24 +00:00
|
|
|
O_RDONLY | O_BINARY);
|
2008-05-26 17:00:24 +00:00
|
|
|
if (mtree->fd == -1 &&
|
|
|
|
(errno != ENOENT ||
|
|
|
|
archive_strlen(&mtree->contents_name) > 0)) {
|
2008-03-15 07:10:24 +00:00
|
|
|
archive_set_error(&a->archive, errno,
|
2008-05-26 17:00:24 +00:00
|
|
|
"Can't open %s", path);
|
2008-03-15 07:10:24 +00:00
|
|
|
r = ARCHIVE_WARN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
st = &st_storage;
|
2008-03-15 07:10:24 +00:00
|
|
|
if (mtree->fd >= 0) {
|
2008-05-26 17:00:24 +00:00
|
|
|
if (fstat(mtree->fd, st) == -1) {
|
2008-03-15 07:10:24 +00:00
|
|
|
archive_set_error(&a->archive, errno,
|
2008-05-26 17:00:24 +00:00
|
|
|
"Could not fstat %s", path);
|
2008-03-15 07:10:24 +00:00
|
|
|
r = ARCHIVE_WARN;
|
|
|
|
/* If we can't stat it, don't keep it open. */
|
|
|
|
close(mtree->fd);
|
|
|
|
mtree->fd = -1;
|
2008-05-26 17:00:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2008-03-15 07:10:24 +00:00
|
|
|
/* Don't hold a non-regular file open. */
|
2008-06-15 10:43:59 +00:00
|
|
|
if (mtree->fd >= 0)
|
|
|
|
close(mtree->fd);
|
2008-03-15 07:10:24 +00:00
|
|
|
mtree->fd = -1;
|
2008-05-26 17:00:24 +00:00
|
|
|
st = NULL;
|
|
|
|
return r;
|
2007-12-30 04:58:22 +00:00
|
|
|
}
|
2008-03-15 07:10:24 +00:00
|
|
|
}
|
2008-05-26 17:00:24 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2008-03-15 07:10:24 +00:00
|
|
|
mtree->cur_size = archive_entry_size(entry);
|
|
|
|
mtree->offset = 0;
|
2007-12-30 04:58:22 +00:00
|
|
|
|
2008-03-15 07:10:24 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each line contains a sequence of keywords.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_line(struct archive_read *a, struct archive_entry *entry,
|
2008-05-26 17:00:24 +00:00
|
|
|
struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
|
2008-03-15 07:10:24 +00:00
|
|
|
{
|
2008-05-26 17:00:24 +00:00
|
|
|
struct mtree_option *iter;
|
2008-03-15 07:10:24 +00:00
|
|
|
int r = ARCHIVE_OK, r1;
|
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
for (iter = mp->options; iter != NULL; iter = iter->next) {
|
|
|
|
r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
|
2008-03-15 07:10:24 +00:00
|
|
|
if (r1 < r)
|
|
|
|
r = r1;
|
2008-05-26 17:00:24 +00:00
|
|
|
}
|
|
|
|
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);
|
2007-12-30 04:58:22 +00:00
|
|
|
}
|
2008-03-15 07:10:24 +00:00
|
|
|
return (r);
|
2007-12-30 04:58:22 +00:00
|
|
|
}
|
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
/*
|
|
|
|
* 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, ',');
|
2008-06-09 14:44:56 +00:00
|
|
|
if (comma2 == NULL) {
|
2008-05-26 17:00:24 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-03-15 07:10:24 +00:00
|
|
|
/*
|
|
|
|
* Parse a single keyword and its value.
|
|
|
|
*/
|
2007-12-30 04:58:22 +00:00
|
|
|
static int
|
2008-03-15 07:10:24 +00:00
|
|
|
parse_keyword(struct archive_read *a, struct mtree *mtree,
|
2008-05-26 17:00:24 +00:00
|
|
|
struct archive_entry *entry, struct mtree_option *option, int *parsed_kws)
|
2007-12-30 04:58:22 +00:00
|
|
|
{
|
2008-05-26 17:00:24 +00:00
|
|
|
char *val, *key;
|
|
|
|
|
|
|
|
key = option->value;
|
2007-12-30 04:58:22 +00:00
|
|
|
|
|
|
|
if (*key == '\0')
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-12-30 04:58:22 +00:00
|
|
|
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':
|
2008-03-15 07:10:24 +00:00
|
|
|
if (strcmp(key, "content") == 0
|
|
|
|
|| strcmp(key, "contents") == 0) {
|
2007-12-30 04:58:22 +00:00
|
|
|
parse_escapes(val, NULL);
|
|
|
|
archive_strcpy(&mtree->contents_name, val);
|
|
|
|
break;
|
|
|
|
}
|
2008-05-26 17:00:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-12-30 04:58:22 +00:00
|
|
|
case 'g':
|
|
|
|
if (strcmp(key, "gid") == 0) {
|
2008-05-26 17:00:24 +00:00
|
|
|
*parsed_kws |= MTREE_HAS_GID;
|
2007-12-30 04:58:22 +00:00
|
|
|
archive_entry_set_gid(entry, mtree_atol10(&val));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strcmp(key, "gname") == 0) {
|
2008-05-26 17:00:24 +00:00
|
|
|
*parsed_kws |= MTREE_HAS_GNAME;
|
2007-12-30 04:58:22 +00:00
|
|
|
archive_entry_copy_gname(entry, val);
|
|
|
|
break;
|
|
|
|
}
|
2008-03-15 07:10:24 +00:00
|
|
|
case 'l':
|
|
|
|
if (strcmp(key, "link") == 0) {
|
2008-05-26 17:00:24 +00:00
|
|
|
archive_entry_copy_symlink(entry, val);
|
2008-03-15 07:10:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-12-30 04:58:22 +00:00
|
|
|
case 'm':
|
2008-05-26 17:00:24 +00:00
|
|
|
if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
|
|
|
|
break;
|
2007-12-30 04:58:22 +00:00
|
|
|
if (strcmp(key, "mode") == 0) {
|
2008-05-26 17:00:24 +00:00
|
|
|
if (val[0] >= '0' && val[0] <= '9') {
|
|
|
|
*parsed_kws |= MTREE_HAS_PERM;
|
2007-12-30 04:58:22 +00:00
|
|
|
archive_entry_set_perm(entry,
|
|
|
|
mtree_atol8(&val));
|
2008-05-26 17:00:24 +00:00
|
|
|
} else {
|
2007-12-30 04:58:22 +00:00
|
|
|
archive_set_error(&a->archive,
|
|
|
|
ARCHIVE_ERRNO_FILE_FORMAT,
|
|
|
|
"Symbolic mode \"%s\" unsupported", val);
|
2008-05-26 17:00:24 +00:00
|
|
|
return ARCHIVE_WARN;
|
|
|
|
}
|
2007-12-30 04:58:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-05-26 17:00:24 +00:00
|
|
|
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;
|
2008-03-15 07:10:24 +00:00
|
|
|
case 's':
|
2008-05-26 17:00:24 +00:00
|
|
|
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;
|
2008-03-15 07:10:24 +00:00
|
|
|
if (strcmp(key, "size") == 0) {
|
|
|
|
archive_entry_set_size(entry, mtree_atol10(&val));
|
|
|
|
break;
|
|
|
|
}
|
2007-12-30 04:58:22 +00:00
|
|
|
case 't':
|
2008-05-26 17:00:24 +00:00
|
|
|
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) {
|
|
|
|
*parsed_kws |= MTREE_HAS_MTIME;
|
|
|
|
archive_entry_set_mtime(entry, mtree_atol10(&val), 0);
|
|
|
|
break;
|
|
|
|
}
|
2007-12-30 04:58:22 +00:00
|
|
|
if (strcmp(key, "type") == 0) {
|
2008-05-26 17:00:24 +00:00
|
|
|
*parsed_kws |= MTREE_HAS_TYPE;
|
2007-12-30 04:58:22 +00:00
|
|
|
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) {
|
2008-05-26 17:00:24 +00:00
|
|
|
*parsed_kws |= MTREE_HAS_UID;
|
2007-12-30 04:58:22 +00:00
|
|
|
archive_entry_set_uid(entry, mtree_atol10(&val));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strcmp(key, "uname") == 0) {
|
2008-05-26 17:00:24 +00:00
|
|
|
*parsed_kws |= MTREE_HAS_UNAME;
|
2007-12-30 04:58:22 +00:00
|
|
|
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)
|
|
|
|
{
|
2008-03-15 07:10:24 +00:00
|
|
|
size_t bytes_to_read;
|
2007-12-30 04:58:22 +00:00
|
|
|
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");
|
|
|
|
}
|
2008-05-19 18:06:48 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
2007-12-30 04:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*buff = mtree->buff;
|
|
|
|
*offset = mtree->offset;
|
2008-03-15 11:02:47 +00:00
|
|
|
if ((off_t)mtree->buffsize > mtree->cur_size - mtree->offset)
|
2008-03-15 07:10:24 +00:00
|
|
|
bytes_to_read = mtree->cur_size - mtree->offset;
|
|
|
|
else
|
|
|
|
bytes_to_read = mtree->buffsize;
|
|
|
|
bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
|
2007-12-30 04:58:22 +00:00
|
|
|
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;
|
2008-03-15 11:02:47 +00:00
|
|
|
*size = bytes_read;
|
2007-12-30 04:58:22 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-06-21 19:06:37 +00:00
|
|
|
* Since parsing backslash sequences always makes strings shorter,
|
2007-12-30 04:58:22 +00:00
|
|
|
* we can always do this conversion in-place.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
parse_escapes(char *src, struct mtree_entry *mentry)
|
|
|
|
{
|
|
|
|
char *dest = src;
|
|
|
|
char c;
|
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
/*
|
|
|
|
* The current directory is somewhat special, it should be archived
|
|
|
|
* only once as it will confuse extraction otherwise.
|
|
|
|
*/
|
|
|
|
if (strcmp(src, ".") == 0)
|
|
|
|
mentry->full = 1;
|
|
|
|
|
2007-12-30 04:58:22 +00:00
|
|
|
while (*src != '\0') {
|
|
|
|
c = *src++;
|
|
|
|
if (c == '/' && mentry != NULL)
|
|
|
|
mentry->full = 1;
|
|
|
|
if (c == '\\') {
|
2008-06-21 19:06:37 +00:00
|
|
|
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;
|
2007-12-30 04:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*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;
|
|
|
|
limit = INT64_MAX / base;
|
|
|
|
last_digit_limit = INT64_MAX % base;
|
|
|
|
|
|
|
|
if (**p == '-') {
|
|
|
|
sign = -1;
|
|
|
|
++(*p);
|
|
|
|
} else
|
|
|
|
sign = 1;
|
|
|
|
|
|
|
|
l = 0;
|
|
|
|
digit = **p - '0';
|
|
|
|
while (digit >= 0 && digit < base) {
|
|
|
|
if (l > limit || (l == limit && digit > last_digit_limit)) {
|
|
|
|
l = UINT64_MAX; /* Truncate on overflow. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
l = (l * base) + digit;
|
|
|
|
digit = *++(*p) - '0';
|
|
|
|
}
|
|
|
|
return (sign < 0) ? -l : l;
|
|
|
|
}
|
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
limit = INT64_MAX / base;
|
|
|
|
last_digit_limit = INT64_MAX % base;
|
|
|
|
|
|
|
|
if (**p == '-') {
|
|
|
|
sign = -1;
|
|
|
|
++(*p);
|
|
|
|
} else
|
|
|
|
sign = 1;
|
|
|
|
|
|
|
|
l = 0;
|
|
|
|
if (**p >= '0' && **p <= '9')
|
|
|
|
digit = **p - '0';
|
|
|
|
else if (**p >= 'a' && **p <= 'f')
|
|
|
|
digit = **p - 'a' + 10;
|
|
|
|
else if (**p >= 'A' && **p <= 'F')
|
|
|
|
digit = **p - 'A' + 10;
|
|
|
|
else
|
|
|
|
digit = -1;
|
|
|
|
while (digit >= 0 && digit < base) {
|
|
|
|
if (l > limit || (l == limit && digit > last_digit_limit)) {
|
|
|
|
l = UINT64_MAX; /* Truncate on overflow. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
l = (l * base) + digit;
|
|
|
|
if (**p >= '0' && **p <= '9')
|
|
|
|
digit = **p - '0';
|
|
|
|
else if (**p >= 'a' && **p <= 'f')
|
|
|
|
digit = **p - 'a' + 10;
|
|
|
|
else if (**p >= 'A' && **p <= 'F')
|
|
|
|
digit = **p - 'A' + 10;
|
|
|
|
else
|
|
|
|
digit = -1;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-12-30 04:58:22 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
const void *t;
|
|
|
|
const char *s;
|
|
|
|
void *p;
|
2008-06-21 19:06:37 +00:00
|
|
|
char *u;
|
2007-12-30 04:58:22 +00:00
|
|
|
|
|
|
|
/* Accumulate line in a line buffer. */
|
|
|
|
for (;;) {
|
|
|
|
/* Read some more. */
|
|
|
|
bytes_read = (a->decompressor->read_ahead)(a, &t, 1);
|
|
|
|
if (bytes_read == 0)
|
|
|
|
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);
|
|
|
|
(a->decompressor->consume)(a, bytes_read);
|
|
|
|
total_size += bytes_read;
|
|
|
|
/* Null terminate. */
|
|
|
|
mtree->line.s[total_size] = '\0';
|
2008-06-21 19:06:37 +00:00
|
|
|
/* If we found an unescaped '\n', clean up and return. */
|
|
|
|
if (p == NULL)
|
|
|
|
continue;
|
|
|
|
for (u = mtree->line.s; *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;
|
|
|
|
continue;
|
|
|
|
}
|
2007-12-30 04:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|