summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorkientzle <kientzle@FreeBSD.org>2007-06-13 03:35:37 +0000
committerkientzle <kientzle@FreeBSD.org>2007-06-13 03:35:37 +0000
commite4d94b2a80a77116aad2cffea36fcf5ef9518336 (patch)
tree71e9394ddd2e303d85dc066ae2a3f935b0be6ed5 /lib
parent4abc2f2f474d5ddbdaffb4265fc7b06abba70c46 (diff)
downloadFreeBSD-src-e4d94b2a80a77116aad2cffea36fcf5ef9518336.zip
FreeBSD-src-e4d94b2a80a77116aad2cffea36fcf5ef9518336.tar.gz
Read support for the new GNU tar sparse formats added in gtar 1.15 and
gtar 1.16.
Diffstat (limited to 'lib')
-rw-r--r--lib/libarchive/archive_read_support_format_tar.c497
-rw-r--r--lib/libarchive/test/Makefile3
-rw-r--r--lib/libarchive/test/test_read_format_gtar_sparse.c154
3 files changed, 570 insertions, 84 deletions
diff --git a/lib/libarchive/archive_read_support_format_tar.c b/lib/libarchive/archive_read_support_format_tar.c
index c05a415..1cc1cd6 100644
--- a/lib/libarchive/archive_read_support_format_tar.c
+++ b/lib/libarchive/archive_read_support_format_tar.c
@@ -150,22 +150,34 @@ struct tar {
struct archive_string longname;
struct archive_string pax_header;
struct archive_string pax_global;
+ struct archive_string line;
wchar_t *pax_entry;
size_t pax_entry_length;
int header_recursion_depth;
off_t entry_bytes_remaining;
off_t entry_offset;
off_t entry_padding;
+ off_t realsize;
struct sparse_block *sparse_list;
+ struct sparse_block *sparse_last;
+ int64_t sparse_offset;
+ int64_t sparse_numbytes;
+ int sparse_gnu_major;
+ int sparse_gnu_minor;
+ char sparse_gnu_pending;
};
static size_t UTF8_mbrtowc(wchar_t *pwc, const char *s, size_t n);
static int archive_block_is_null(const unsigned char *p);
static char *base64_decode(const wchar_t *, size_t, size_t *);
-static int gnu_read_sparse_data(struct archive_read *, struct tar *,
+static void gnu_add_sparse_entry(struct tar *,
+ off_t offset, off_t remaining);
+static int gnu_sparse_old_read(struct archive_read *, struct tar *,
const struct archive_entry_header_gnutar *header);
-static void gnu_parse_sparse_data(struct archive_read *, struct tar *,
+static void gnu_sparse_old_parse(struct tar *,
const struct gnu_sparse *sparse, int length);
+static int gnu_sparse_01_parse(struct tar *, const wchar_t *);
+static ssize_t gnu_sparse_10_read(struct archive_read *, struct tar *);
static int header_Solaris_ACL(struct archive_read *, struct tar *,
struct archive_entry *, const void *);
static int header_common(struct archive_read *, struct tar *,
@@ -194,11 +206,12 @@ static int archive_read_format_tar_skip(struct archive_read *a);
static int archive_read_format_tar_read_header(struct archive_read *,
struct archive_entry *);
static int checksum(struct archive_read *, const void *);
-static int pax_attribute(struct archive_entry *,
+static int pax_attribute(struct tar *, struct archive_entry *,
wchar_t *key, wchar_t *value);
static int pax_header(struct archive_read *, struct tar *,
struct archive_entry *, char *attr);
static void pax_time(const wchar_t *, int64_t *sec, long *nanos);
+static ssize_t readline(struct archive_read *, struct tar *, const char **);
static int read_body_to_string(struct archive_read *, struct tar *,
struct archive_string *, const void *h);
static int64_t tar_atol(const char *, unsigned);
@@ -250,17 +263,23 @@ static int
archive_read_format_tar_cleanup(struct archive_read *a)
{
struct tar *tar;
+ struct sparse_block *p;
tar = (struct tar *)(a->format->data);
+ while (tar->sparse_list != NULL) {
+ p = tar->sparse_list;
+ tar->sparse_list = p->next;
+ free(p);
+ }
archive_string_free(&tar->acl_text);
archive_string_free(&tar->entry_name);
archive_string_free(&tar->entry_linkname);
archive_string_free(&tar->entry_uname);
archive_string_free(&tar->entry_gname);
+ archive_string_free(&tar->line);
archive_string_free(&tar->pax_global);
archive_string_free(&tar->pax_header);
- if (tar->pax_entry != NULL)
- free(tar->pax_entry);
+ free(tar->pax_entry);
free(tar);
(a->format->data) = NULL;
return (ARCHIVE_OK);
@@ -400,9 +419,11 @@ archive_read_format_tar_read_header(struct archive_read *a,
static int default_inode;
static int default_dev;
struct tar *tar;
+ struct sparse_block *sp;
const char *p;
int r;
size_t l;
+ ssize_t size;
/* Assign default device/inode values. */
archive_entry_set_dev(entry, 1 + default_dev); /* Don't use zero. */
@@ -415,9 +436,40 @@ archive_read_format_tar_read_header(struct archive_read *a,
tar = (struct tar *)(a->format->data);
tar->entry_offset = 0;
+ while (tar->sparse_list != NULL) {
+ sp = tar->sparse_list;
+ tar->sparse_list = sp->next;
+ free(sp);
+ }
+ tar->sparse_last = NULL;
r = tar_read_header(a, tar, entry);
+ /*
+ * Yuck. See comments for gnu_sparse_10_read for why this
+ * is here and not in _read_data where it "should" go.
+ */
+ if (tar->sparse_gnu_pending
+ && tar->sparse_gnu_major == 1
+ && tar->sparse_gnu_minor == 0) {
+ tar->sparse_gnu_pending = 0;
+ /* Read initial sparse map. */
+ size = gnu_sparse_10_read(a, tar);
+ if (size < 0)
+ return (size);
+ tar->entry_bytes_remaining -= size;
+ tar->entry_padding += size;
+ }
+
+ /*
+ * "non-sparse" files are really just sparse files with
+ * a single block.
+ */
+ if (tar->sparse_list == NULL)
+ gnu_add_sparse_entry(tar, 0, tar->entry_bytes_remaining);
+
+ tar->realsize = archive_entry_size(entry);
+
if (r == ARCHIVE_OK) {
/*
* "Regular" entry with trailing '/' is really
@@ -442,55 +494,64 @@ archive_read_format_tar_read_data(struct archive_read *a,
struct sparse_block *p;
tar = (struct tar *)(a->format->data);
- if (tar->sparse_list != NULL) {
- /* Remove exhausted entries from sparse list. */
- while (tar->sparse_list != NULL &&
- tar->sparse_list->remaining == 0) {
- p = tar->sparse_list;
- tar->sparse_list = p->next;
- free(p);
- }
- if (tar->sparse_list == NULL) {
- /* We exhausted the entire sparse list. */
- tar->entry_bytes_remaining = 0;
- }
- }
- if (tar->entry_bytes_remaining > 0) {
- bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
- if (bytes_read == 0) {
+ if (tar->sparse_gnu_pending) {
+ if (tar->sparse_gnu_major == 1 && tar->sparse_gnu_minor == 0) {
+ /*
+ * <sigh> We should parse the sparse data
+ * here, but have to parse it as part of the
+ * header because of a bug in GNU tar 1.16.1.
+ */
+ } else {
+ *size = 0;
+ *offset = 0;
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "Truncated tar archive");
- return (ARCHIVE_FATAL);
- }
- if (bytes_read < 0)
- return (ARCHIVE_FATAL);
- if (bytes_read > tar->entry_bytes_remaining)
- bytes_read = tar->entry_bytes_remaining;
- if (tar->sparse_list != NULL) {
- /* Don't read more than is available in the
- * current sparse block. */
- if (tar->sparse_list->remaining < bytes_read)
- bytes_read = tar->sparse_list->remaining;
- tar->entry_offset = tar->sparse_list->offset;
- tar->sparse_list->remaining -= bytes_read;
- tar->sparse_list->offset += bytes_read;
+ "Unrecognized GNU sparse file format");
+ return (ARCHIVE_WARN);
}
- *size = bytes_read;
- *offset = tar->entry_offset;
- tar->entry_offset += bytes_read;
- tar->entry_bytes_remaining -= bytes_read;
- (a->decompressor->consume)(a, bytes_read);
- return (ARCHIVE_OK);
- } else {
+ tar->sparse_gnu_pending = 0;
+ }
+
+ /* Remove exhausted entries from sparse list. */
+ while (tar->sparse_list != NULL &&
+ tar->sparse_list->remaining == 0) {
+ p = tar->sparse_list;
+ tar->sparse_list = p->next;
+ free(p);
+ }
+
+ /* If we're at end of file, return EOF. */
+ if (tar->sparse_list == NULL || tar->entry_bytes_remaining == 0) {
if ((a->decompressor->skip)(a, tar->entry_padding) < 0)
return (ARCHIVE_FATAL);
tar->entry_padding = 0;
*buff = NULL;
*size = 0;
- *offset = tar->entry_offset;
+ *offset = tar->realsize;
return (ARCHIVE_EOF);
}
+
+ bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
+ if (bytes_read == 0) {
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
+ "Truncated tar archive");
+ return (ARCHIVE_FATAL);
+ }
+ if (bytes_read < 0)
+ return (ARCHIVE_FATAL);
+ if (bytes_read > tar->entry_bytes_remaining)
+ bytes_read = tar->entry_bytes_remaining;
+ /* Don't read more than is available in the
+ * current sparse block. */
+ if (tar->sparse_list->remaining < bytes_read)
+ bytes_read = tar->sparse_list->remaining;
+ *size = bytes_read;
+ *offset = tar->sparse_list->offset;
+ tar->sparse_list->remaining -= bytes_read;
+ tar->sparse_list->offset += bytes_read;
+ tar->entry_bytes_remaining -= bytes_read;
+ (a->decompressor->consume)(a, bytes_read);
+ return (ARCHIVE_OK);
}
static int
@@ -521,6 +582,7 @@ archive_read_format_tar_skip(struct archive_read *a)
tar->sparse_list = p->next;
free(p);
}
+ tar->sparse_last = NULL;
return (ARCHIVE_OK);
}
@@ -847,7 +909,8 @@ header_common(struct archive_read *a, struct tar *tar,
archive_entry_set_mode(entry, tar_atol(header->mode, sizeof(header->mode)));
archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid)));
archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid)));
- archive_entry_set_size(entry, tar_atol(header->size, sizeof(header->size)));
+ tar->entry_bytes_remaining = tar_atol(header->size, sizeof(header->size));
+ archive_entry_set_size(entry, tar->entry_bytes_remaining);
archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0);
/* Handle the tar type flag appropriately. */
@@ -892,29 +955,35 @@ header_common(struct archive_read *a, struct tar *tar,
*/
if (archive_entry_size(entry) > 0 &&
a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE &&
- archive_read_format_tar_bid(a) > 50)
+ archive_read_format_tar_bid(a) > 50) {
archive_entry_set_size(entry, 0);
- break;
+ tar->entry_bytes_remaining = 0;
+ }
case '2': /* Symlink */
archive_entry_set_filetype(entry, AE_IFLNK);
archive_entry_set_size(entry, 0);
+ tar->entry_bytes_remaining = 0;
archive_entry_set_symlink(entry, tar->entry_linkname.s);
break;
case '3': /* Character device */
archive_entry_set_filetype(entry, AE_IFCHR);
archive_entry_set_size(entry, 0);
+ tar->entry_bytes_remaining = 0;
break;
case '4': /* Block device */
archive_entry_set_filetype(entry, AE_IFBLK);
archive_entry_set_size(entry, 0);
+ tar->entry_bytes_remaining = 0;
break;
case '5': /* Dir */
archive_entry_set_filetype(entry, AE_IFDIR);
archive_entry_set_size(entry, 0);
+ tar->entry_bytes_remaining = 0;
break;
case '6': /* FIFO device */
archive_entry_set_filetype(entry, AE_IFIFO);
archive_entry_set_size(entry, 0);
+ tar->entry_bytes_remaining = 0;
break;
case 'D': /* GNU incremental directory type */
/*
@@ -972,7 +1041,6 @@ header_old_tar(struct archive_read *a, struct tar *tar,
/* Grab rest of common fields */
header_common(a, tar, entry, h);
- tar->entry_bytes_remaining = archive_entry_size(entry);
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
return (0);
}
@@ -1014,7 +1082,6 @@ header_pax_extensions(struct archive_read *a, struct tar *tar,
*/
err2 = pax_header(a, tar, entry, tar->pax_header.s);
err = err_combine(err, err2);
- tar->entry_bytes_remaining = archive_entry_size(entry);
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
return (err);
}
@@ -1065,7 +1132,6 @@ header_ustar(struct archive_read *a, struct tar *tar,
tar_atol(header->rdevminor, sizeof(header->rdevminor)));
}
- tar->entry_bytes_remaining = archive_entry_size(entry);
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
return (0);
@@ -1160,7 +1226,7 @@ pax_header(struct archive_read *a, struct tar *tar,
value = wp + 1;
/* Identify this attribute and set it in the entry. */
- err2 = pax_attribute(entry, key, value);
+ err2 = pax_attribute(tar, entry, key, value);
err = err_combine(err, err2);
/* Skip to next line */
@@ -1215,19 +1281,72 @@ pax_attribute_xattr(struct archive_entry *entry,
* extensions should always have keywords of the form "VENDOR.attribute"
* In particular, it's quite feasible to support many different
* vendor extensions here. I'm using "LIBARCHIVE" for extensions
- * unique to this library (currently, there are none).
+ * unique to this library.
*
- * Investigate other vendor-specific extensions, as well and see if
+ * Investigate other vendor-specific extensions and see if
* any of them look useful.
*/
static int
-pax_attribute(struct archive_entry *entry,
+pax_attribute(struct tar *tar, struct archive_entry *entry,
wchar_t *key, wchar_t *value)
{
int64_t s;
long n;
switch (key[0]) {
+ case 'G':
+ /* GNU "0.0" sparse pax format. */
+ if (wcscmp(key, L"GNU.sparse.numblocks") == 0) {
+ tar->sparse_offset = -1;
+ tar->sparse_numbytes = -1;
+ tar->sparse_gnu_major = 0;
+ tar->sparse_gnu_minor = 0;
+ }
+ if (wcscmp(key, L"GNU.sparse.offset") == 0) {
+ tar->sparse_offset = tar_atol10(value, wcslen(value));
+ if (tar->sparse_numbytes != -1) {
+ gnu_add_sparse_entry(tar,
+ tar->sparse_offset, tar->sparse_numbytes);
+ tar->sparse_offset = -1;
+ tar->sparse_numbytes = -1;
+ }
+ }
+ if (wcscmp(key, L"GNU.sparse.numbytes") == 0) {
+ tar->sparse_numbytes = tar_atol10(value, wcslen(value));
+ if (tar->sparse_numbytes != -1) {
+ gnu_add_sparse_entry(tar,
+ tar->sparse_offset, tar->sparse_numbytes);
+ tar->sparse_offset = -1;
+ tar->sparse_numbytes = -1;
+ }
+ }
+ if (wcscmp(key, L"GNU.sparse.size") == 0)
+ archive_entry_set_size(entry,
+ tar_atol10(value, wcslen(value)));
+
+ /* GNU "0.1" sparse pax format. */
+ if (wcscmp(key, L"GNU.sparse.map") == 0) {
+ tar->sparse_gnu_major = 0;
+ tar->sparse_gnu_minor = 1;
+ if (gnu_sparse_01_parse(tar, value) != ARCHIVE_OK)
+ return (ARCHIVE_WARN);
+ }
+
+ /* GNU "1.0" sparse pax format */
+ if (wcscmp(key, L"GNU.sparse.major") == 0) {
+ tar->sparse_gnu_major = tar_atol10(value, wcslen(value));
+ tar->sparse_gnu_pending = 1;
+ }
+ if (wcscmp(key, L"GNU.sparse.minor") == 0) {
+ tar->sparse_gnu_minor = tar_atol10(value, wcslen(value));
+ tar->sparse_gnu_pending = 1;
+ }
+ if (wcscmp(key, L"GNU.sparse.name") == 0)
+ archive_entry_copy_pathname_w(entry, value);
+ if (wcscmp(key, L"GNU.sparse.realsize") == 0)
+ archive_entry_set_size(entry,
+ tar_atol10(value, wcslen(value)));
+ break;
case 'L':
/* Our extensions */
/* TODO: Handle arbitrary extended attributes... */
@@ -1306,8 +1425,12 @@ pax_attribute(struct archive_entry *entry,
case 's':
/* POSIX has reserved 'security.*' */
/* Someday: if (wcscmp(key, L"security.acl")==0) { ... } */
- if (wcscmp(key, L"size")==0)
- archive_entry_set_size(entry, tar_atol10(value, wcslen(value)));
+ if (wcscmp(key, L"size")==0) {
+ tar->entry_bytes_remaining = tar_atol10(value, wcslen(value));
+ archive_entry_set_size(entry, tar->entry_bytes_remaining);
+ }
+ tar->entry_bytes_remaining = 0;
+
break;
case 'u':
if (wcscmp(key, L"uid")==0)
@@ -1418,7 +1541,6 @@ header_gnutar(struct archive_read *a, struct tar *tar,
} else
archive_entry_set_rdev(entry, 0);
- tar->entry_bytes_remaining = archive_entry_size(entry);
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
/* Grab GNU-specific fields. */
@@ -1432,7 +1554,7 @@ header_gnutar(struct archive_read *a, struct tar *tar,
}
if (header->sparse[0].offset[0] != 0) {
- gnu_read_sparse_data(a, tar, header);
+ gnu_sparse_old_read(a, tar, header);
} else {
if (header->isextended[0] != 0) {
/* XXX WTF? XXX */
@@ -1442,8 +1564,38 @@ header_gnutar(struct archive_read *a, struct tar *tar,
return (0);
}
+static void
+gnu_add_sparse_entry(struct tar *tar, off_t offset, off_t remaining)
+{
+ struct sparse_block *p;
+
+ p = (struct sparse_block *)malloc(sizeof(*p));
+ if (p == NULL)
+ __archive_errx(1, "Out of memory");
+ memset(p, 0, sizeof(*p));
+ if (tar->sparse_last != NULL)
+ tar->sparse_last->next = p;
+ else
+ tar->sparse_list = p;
+ tar->sparse_last = p;
+ p->offset = offset;
+ p->remaining = remaining;
+}
+
+/*
+ * GNU tar old-format sparse data.
+ *
+ * GNU old-format sparse data is stored in a fixed-field
+ * format. Offset/size values are 11-byte octal fields (same
+ * format as 'size' field in ustart header). These are
+ * stored in the header, allocating subsequent header blocks
+ * as needed. Extending the header in this way is a pretty
+ * severe POSIX violation; this design has earned GNU tar a
+ * lot of criticism.
+ */
+
static int
-gnu_read_sparse_data(struct archive_read *a, struct tar *tar,
+gnu_sparse_old_read(struct archive_read *a, struct tar *tar,
const struct archive_entry_header_gnutar *header)
{
ssize_t bytes_read;
@@ -1455,7 +1607,7 @@ gnu_read_sparse_data(struct archive_read *a, struct tar *tar,
};
const struct extended *ext;
- gnu_parse_sparse_data(a, tar, header->sparse, 4);
+ gnu_sparse_old_parse(tar, header->sparse, 4);
if (header->isextended[0] == 0)
return (ARCHIVE_OK);
@@ -1471,7 +1623,7 @@ gnu_read_sparse_data(struct archive_read *a, struct tar *tar,
}
(a->decompressor->consume)(a, 512);
ext = (const struct extended *)data;
- gnu_parse_sparse_data(a, tar, ext->sparse, 21);
+ gnu_sparse_old_parse(tar, ext->sparse, 21);
} while (ext->isextended[0] != 0);
if (tar->sparse_list != NULL)
tar->entry_offset = tar->sparse_list->offset;
@@ -1479,34 +1631,165 @@ gnu_read_sparse_data(struct archive_read *a, struct tar *tar,
}
static void
-gnu_parse_sparse_data(struct archive_read *a, struct tar *tar,
+gnu_sparse_old_parse(struct tar *tar,
const struct gnu_sparse *sparse, int length)
{
- struct sparse_block *last;
- struct sparse_block *p;
+ while (length > 0 && sparse->offset[0] != 0) {
+ gnu_add_sparse_entry(tar,
+ tar_atol(sparse->offset, sizeof(sparse->offset)),
+ tar_atol(sparse->numbytes, sizeof(sparse->numbytes)));
+ sparse++;
+ length--;
+ }
+}
- (void)a; /* UNUSED */
+/*
+ * GNU tar sparse format 0.0
+ *
+ * Beginning with GNU tar 1.15, sparse files are stored using
+ * information in the pax extended header. The GNU tar maintainers
+ * have gone through a number of variations in the process of working
+ * out this scheme; furtunately, they're all numbered.
+ *
+ * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the
+ * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to
+ * store offset/size for each block. The repeated instances of these
+ * latter fields violate the pax specification (which frowns on
+ * duplicate keys), so this format was quickly replaced.
+ */
- last = tar->sparse_list;
- while (last != NULL && last->next != NULL)
- last = last->next;
+/*
+ * GNU tar sparse format 0.1
+ *
+ * This version replaced the offset/numbytes attributes with
+ * a single "map" attribute that stored a list of integers. This
+ * format had two problems: First, the "map" attribute could be very
+ * long, which caused problems for some implementations. More
+ * importantly, the sparse data was lost when extracted by archivers
+ * that didn't recognize this extension.
+ */
- while (length > 0 && sparse->offset[0] != 0) {
- p = (struct sparse_block *)malloc(sizeof(*p));
- if (p == NULL)
- __archive_errx(1, "Out of memory");
- memset(p, 0, sizeof(*p));
- if (last != NULL)
- last->next = p;
+static int
+gnu_sparse_01_parse(struct tar *tar, const wchar_t *p)
+{
+ const wchar_t *e;
+ off_t offset = -1, size = -1;
+
+ for (;;) {
+ e = p;
+ while (*e != '\0' && *e != ',') {
+ if (*e < '0' || *e > '9')
+ return (ARCHIVE_WARN);
+ e++;
+ }
+ if (offset < 0) {
+ offset = tar_atol10(p, e - p);
+ if (offset < 0)
+ return (ARCHIVE_WARN);
+ } else {
+ size = tar_atol10(p, e - p);
+ if (size < 0)
+ return (ARCHIVE_WARN);
+ gnu_add_sparse_entry(tar, offset, size);
+ offset = -1;
+ }
+ if (*e == '\0')
+ return (ARCHIVE_OK);
+ p = e + 1;
+ }
+}
+
+/*
+ * GNU tar sparse format 1.0
+ *
+ * The idea: The offset/size data is stored as a series of base-10
+ * ASCII numbers prepended to the file data, so that dearchivers that
+ * don't support this format will extract the block map along with the
+ * data and a separate post-process can restore the sparseness.
+ *
+ * Unfortunately, GNU tar 1.16 adds bogus padding to the end of the
+ * entry that depends on the size of the map; this means we have to
+ * parse the sparse map when we read the header (otherwise, entry_skip
+ * will fail). This is why sparse_10_read is called from read_header
+ * above, instead of at the beginning of read_data, where it "should"
+ * go.
+ *
+ * This variant also replaced GNU.sparse.size with GNU.sparse.realsize
+ * and introduced the GNU.sparse.major/GNU.sparse.minor attributes.
+ */
+
+/*
+ * Read the next line from the input, and parse it as a decimal
+ * integer followed by '\n'. Returns positive integer value or
+ * negative on error.
+ */
+static int64_t
+gnu_sparse_10_atol(struct archive_read *a, struct tar *tar,
+ ssize_t *total_read)
+{
+ int64_t l, limit, last_digit_limit;
+ const char *p;
+ ssize_t bytes_read;
+ int base, digit;
+
+ base = 10;
+ limit = INT64_MAX / base;
+ last_digit_limit = INT64_MAX % base;
+
+ bytes_read = readline(a, tar, &p);
+ if (bytes_read <= 0)
+ return (ARCHIVE_FATAL);
+ *total_read += bytes_read;
+
+ l = 0;
+ while (bytes_read > 0) {
+ if (*p == '\n')
+ return (l);
+ if (*p < '0' || *p >= '0' + base)
+ return (ARCHIVE_WARN);
+ digit = *p - '0';
+ if (l > limit || (l == limit && digit > last_digit_limit))
+ l = UINT64_MAX; /* Truncate on overflow. */
else
- tar->sparse_list = p;
- last = p;
- p->offset = tar_atol(sparse->offset, sizeof(sparse->offset));
- p->remaining =
- tar_atol(sparse->numbytes, sizeof(sparse->numbytes));
- sparse++;
- length--;
+ l = (l * base) + digit;
+ p++;
+ bytes_read--;
+ }
+ /* TODO: Error message. */
+ return (ARCHIVE_WARN);
+}
+
+/*
+ * Returns number of bytes consumed to read the sparse block data.
+ */
+static ssize_t
+gnu_sparse_10_read(struct archive_read *a, struct tar *tar)
+{
+ ssize_t bytes_read = 0;
+ int entries;
+ off_t offset, size, to_skip;
+
+ /* Parse entries. */
+ entries = gnu_sparse_10_atol(a, tar, &bytes_read);
+ if (entries < 0)
+ return (ARCHIVE_FATAL);
+ /* Parse the individual entries. */
+ while (entries-- > 0) {
+ /* Parse offset/size */
+ offset = gnu_sparse_10_atol(a, tar, &bytes_read);
+ if (offset < 0)
+ return (ARCHIVE_FATAL);
+ size = gnu_sparse_10_atol(a, tar, &bytes_read);
+ if (size < 0)
+ return (ARCHIVE_FATAL);
+ /* Add a new sparse entry. */
+ gnu_add_sparse_entry(tar, offset, size);
}
+ /* Skip rest of block... */
+ to_skip = 0x1ff & -bytes_read;
+ if (to_skip != (a->decompressor->skip)(a, to_skip))
+ return (ARCHIVE_FATAL);
+ return (bytes_read + to_skip);
}
/*-
@@ -1574,7 +1857,6 @@ tar_atol8(const char *p, unsigned char_cnt)
return (sign < 0) ? -l : l;
}
-
/*
* Note that this implementation does not (and should not!) obey
* locale settings; you cannot simply substitute strtol here, since
@@ -1647,6 +1929,55 @@ tar_atol256(const char *_p, unsigned char_cnt)
return (l);
}
+/*
+ * Returns length of line (including trailing newline)
+ * or negative on error. 'start' argument is updated to
+ * point to first character of line. This avoids copying
+ * when possible.
+ */
+static ssize_t
+readline(struct archive_read *a, struct tar *tar, const char **start)
+{
+ ssize_t bytes_read;
+ ssize_t total_size = 0;
+ const void *t;
+ const char *s;
+ void *p;
+
+ bytes_read = (a->decompressor->read_ahead)(a, &t, 1);
+ if (bytes_read <= 0)
+ return (ARCHIVE_FATAL);
+ s = t; /* Start of line? */
+ p = memchr(t, '\n', bytes_read);
+ /* If we found '\n' in the read buffer, return pointer to that. */
+ if (p != NULL) {
+ bytes_read = 1 + ((const char *)p) - s;
+ (a->decompressor->consume)(a, bytes_read);
+ *start = s;
+ return (bytes_read);
+ }
+ /* Otherwise, we need to accumulate in a line buffer. */
+ for (;;) {
+ archive_string_ensure(&tar->line, total_size + bytes_read);
+ memcpy(tar->line.s + total_size, t, bytes_read);
+ (a->decompressor->consume)(a, bytes_read);
+ total_size += bytes_read;
+ /* Read some more. */
+ bytes_read = (a->decompressor->read_ahead)(a, &t, 1);
+ if (bytes_read <= 0)
+ return (ARCHIVE_FATAL);
+ s = t; /* Start of line? */
+ p = memchr(t, '\n', bytes_read);
+ /* If we found '\n', finish the line. */
+ if (p != NULL) {
+ bytes_read = 1 + ((const char *)p) - s;
+ (a->decompressor->consume)(a, bytes_read);
+ *start = tar->line.s;
+ return (total_size + bytes_read);
+ }
+ }
+}
+
static int
utf8_decode(wchar_t *dest, const char *src, size_t length)
{
diff --git a/lib/libarchive/test/Makefile b/lib/libarchive/test/Makefile
index 7f9d115..03f48d6 100644
--- a/lib/libarchive/test/Makefile
+++ b/lib/libarchive/test/Makefile
@@ -19,6 +19,7 @@ TESTS= \
test_read_format_cpio_svr4c_Z.c \
test_read_format_empty.c \
test_read_format_gtar_gz.c \
+ test_read_format_gtar_sparse.c \
test_read_format_iso_gz.c \
test_read_format_isorr_bz2.c \
test_read_format_pax_bz2.c \
@@ -62,7 +63,7 @@ CFLAGS+= -I${.OBJDIR}
#WARNS=6
test: libarchive_test
- ./libarchive_test
+ ./libarchive_test -k
list.h: ${TESTS} Makefile
(cd ${.CURDIR}; cat ${TESTS}) | grep DEFINE_TEST > list.h
diff --git a/lib/libarchive/test/test_read_format_gtar_sparse.c b/lib/libarchive/test/test_read_format_gtar_sparse.c
new file mode 100644
index 0000000..38a2c0b
--- /dev/null
+++ b/lib/libarchive/test/test_read_format_gtar_sparse.c
@@ -0,0 +1,154 @@
+/*-
+ * Copyright (c) 2003-2007 Tim Kientzle
+ * 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 "test.h"
+__FBSDID("$FreeBSD$");
+
+/*
+ * Each of the following is an archive of a single sparse file
+ * named 'sparse' which has:
+ * * a length of 3M exactly
+ * * a single 'a' byte at offset 100000
+ * * a single 'a' byte at offset 200000
+ */
+
+/* Old GNU tar sparse format. */
+static unsigned char archive_old[] = {
+31,139,8,0,150,221,'l','F',0,3,'+','.','H',',','*','N','e',160,')','0',0,
+2,'3',19,19,16,'m','h','n','j',128,'L',131,128,17,16,'3',24,26,152,25,27,
+27,154,154,25,25,26,'2',0,'e',205,141,140,25,20,130,'i',235,',',8,'(','-',
+'.','I',',','R','P','`','(',201,204,197,171,142,144,'<',217,0,20,4,198,'f',
+'&',134,160,'P',128,134,8,156,13,12,'%','#',',',226,134,'&',16,'>','L',28,
+204,198,'g',1,'\\',3,213,'A','"',245,141,28,5,'#',8,140,166,159,'Q','0',10,
+'F',193,'(',24,24,0,0,'}','}',226,185,0,10,0,0};
+
+/* GNU tar "0.0" posix format, as written by GNU tar 1.15.1. */
+static unsigned char archive_0_0[] = {
+31,139,8,0,171,221,'l','F',0,3,237,147,193,'N',195,'0',12,134,'s',206,'S',
+244,9,186,216,142,147,246,144,'3',156,'&','.','<','@',24,153,'4',193,24,'j',
+':','i',227,233,'I',129,'M','E','*','L','h',21,'U',165,'|',23,'K','v','~',
+203,145,253,151,139,';',127,184,13,254,'1','4',177,180,'h',140,'^',196,'W',
+223,196,' ',198,'C','%',140,214,']',4,203,170,31,'?',208,10,5,'(','C',4,'l',
+'4',161,'P','@',218,178,'(',14,'#',206,240,'#',251,216,250,'&',141,'r','m',
+159,175,191,156,227,'L','@','[',220,',',239,203,207,157,151,'q',243,22,28,
+129,'f',139,149,'D',211,'/',189,236,183,15,207,187,213,'S','t','$',177,234,
+'W','v',235,'u',12,173,171,19,'d',228,247,134,157,234,216,134,232,24,'P',
+'b','=',' ',131,'$',171,',',254,'Y','w',158,146,7,'u','J',162,'*','|',187,
+217,6,7,'P',1,'S',205,'5','v',185,'U','?',167,13,203,169,'7','0','-',163,
+155,'}',128,11,254,199,206,'.','\'',255,'#','@',242,'?','X','R',162,248,23,
+19,157,252,159,206,226,215,'w',151,234,'3',245,255,181,248,169,7,200,204,
+154,'|','?',153,'L','&','3',13,239,',',195,'|','B',0,14,0,0};
+
+/* GNU tar "0.1" posix format, as written by GNU tar 1.15.XXX. */
+static unsigned char archive_0_1[] = {
+31,139,8,0,191,221,'l','F',0,3,237,148,193,'N',195,'0',12,134,'s',238,'S',
+244,1,170,'4','v','b',167,';',244,202,'v','B','H',136,7,8,'#',135,138,'u',
+'L',205,'&','M','<','=','Y',183,161,10,9,'v',216,160,'*',202,'w',177,'e',
+'G',137,19,251,143,',',31,220,'~',225,221,139,239,130,180,200,204,'e',216,
+184,'.','x','q',';','T',132,141,'9','X',176,164,134,182,7,9,5,'(',214,26,
+136,13,145,'P',160,'I','Y',145,239,'o','X',195,183,236,194,214,'u',177,148,
+'k',247,'9',221,229,211,'N',4,180,249,252,254,'I',30,'{','.','C',243,238,
+'k',13,134,',','V',25,242,'0',181,222,181,207,171,183,229,'k',168,245,215,
+140,'k','}','}',244,'3',130,'a',166,'u',155,'z',22,209,'\\',16,'`',1,209,
+173,',',246,254,233,144,'B','e',168,'r',183,'m',226,22,0,21,144,158,'1',244,
+177,229,'0','f',152,178,177,'_',234,127,'"',203,216,175,199,190,']','w',205,
+202,255,202,23,'p','A',255,'x',144,203,'Y',255,8,16,245,'O',192,'$',242,'?',
+17,209,'Y',255,'q',220,'~','\\','w',')','?','Q',253,'_',139,27,187,128,196,
+164,'I',243,147,'H','$',18,227,240,1,'Q',127,'c',137,0,14,0,0};
+
+/* GNU tar "1.0" posix format, as written by GNU tar 1.16.1. */
+static unsigned char archive_1_0[] = {
+31,139,8,0,210,221,'l','F',0,3,237,148,207,'N',195,'0',12,198,'s',206,'S',
+244,9,210,216,206,159,238,208,'+',236,132,144,16,15,16,'A',14,'E',219,'@',
+201,'&','M','<','=','i','G','Q',133,'4','v','`','l',170,240,239,'b',235,'s',
+20,'9','q',190,168,250,'>',236,151,'1','<',199,148,149,'G',231,154,':',191,
+133,148,163,'8',31,186,224,140,233,'#','x',171,167,'q',0,193,10,208,142,8,
+172,'3',30,133,6,178,218,139,'j',127,198,30,142,178,203,219,144,'J','+',191,
+221,231,243,',','_','q','&',' ','V',183,'w',143,234,'0','s',181,14,'/',175,
+169,5,249,'M',237,'6','E',213,18,221,'T',221,132,'u','l',15,185,'$',152,'V',
+'R',12,171,220,189,199,150,192,'X',143,141,'D',']',133,'m','W',150,3,'4',
+'`','i',225,200,244,218,211,'T','3',206,202,'k','_',198,'?','D',213,'e','p',
+15,195,220,'n',186,'U',252,147,'/',224,132,255,169,183,203,232,127,4,'(',
+254,183,224,181,168,'.','b',162,209,255,229,'-',254,184,238,'T','}',166,254,
+'\'',185,'(',144,147,22,'P','B','I',27,143,'C','>','z','W',179,'+',153,227,
+132,'k','7',192,204,26,'~','?',12,195,'0',12,'s','y','>',0,244,'|','e',9,
+0,18,0,0};
+
+static void
+test_data(const char *buff, int buff_size,
+ int start_index, int data_index, const char *data)
+{
+ int i;
+
+ assert(buff_size > data_index - start_index);
+ for (i = 0; i < data_index - start_index; i++)
+ assert(buff[i] == 0);
+ assert(0 == memcmp(buff + (data_index - start_index), data, strlen(data)));
+ i += strlen(data);
+ for (; i < buff_size; i++)
+ assert(buff[i] == 0);
+}
+
+static void
+verify_archive(void *b, size_t l)
+{
+ struct archive_entry *ae;
+ struct archive *a;
+ const void *d;
+ size_t s;
+ off_t o;
+
+ assert((a = archive_read_new()) != NULL);
+ assert(0 == archive_read_support_compression_all(a));
+ assert(0 == archive_read_support_format_all(a));
+ assert(0 == archive_read_open_memory(a, b, l));
+ assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
+
+ assertEqualIntA(a, 0, archive_read_data_block(a, &d, &s, &o));
+ test_data(d, s, o, 1000000, "a");
+ assertEqualIntA(a, 0, archive_read_data_block(a, &d, &s, &o));
+ test_data(d, s, o, 2000000, "a");
+ assertEqualIntA(a, ARCHIVE_EOF, archive_read_data_block(a, &d, &s, &o));
+ failure("Size returned at EOF must be zero");
+ assertEqualInt(s, 0);
+ failure("Offset at EOF must be same as file size");
+ assertEqualInt(o, 3145728);
+
+ assert(0 == archive_read_close(a));
+#if ARCHIVE_API_VERSION > 1
+ assert(0 == archive_read_finish(a));
+#else
+ archive_read_finish(a);
+#endif
+}
+
+DEFINE_TEST(test_read_format_gtar_sparse)
+{
+ verify_archive(archive_old, sizeof(archive_old));
+ verify_archive(archive_0_0, sizeof(archive_0_0));
+ verify_archive(archive_0_1, sizeof(archive_0_1));
+ verify_archive(archive_1_0, sizeof(archive_1_0));
+}
+
+
OpenPOWER on IntegriCloud