aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/tar/read_header.c62
-rw-r--r--tests/Makemodule.am8
-rw-r--r--tests/tar_sparse_gnu2.c162
3 files changed, 231 insertions, 1 deletions
diff --git a/lib/tar/read_header.c b/lib/tar/read_header.c
index 23c23c4..e1c9eaf 100644
--- a/lib/tar/read_header.c
+++ b/lib/tar/read_header.c
@@ -163,6 +163,55 @@ static int pax_read_decimal(const char *str, uint64_t *out)
return 0;
}
+static sparse_map_t *read_sparse_map(const char *line)
+{
+ sparse_map_t *last = NULL, *list = NULL, *ent = NULL;
+
+ do {
+ ent = calloc(1, sizeof(*ent));
+ if (ent == NULL)
+ goto fail_errno;
+
+ if (pax_read_decimal(line, &ent->offset))
+ goto fail_format;
+
+ while (isdigit(*line))
+ ++line;
+
+ if (*(line++) != ',')
+ goto fail_format;
+
+ if (pax_read_decimal(line, &ent->count))
+ goto fail_format;
+
+ while (isdigit(*line))
+ ++line;
+
+ if (last == NULL) {
+ list = last = ent;
+ } else {
+ last->next = ent;
+ last = ent;
+ }
+ } while (*(line++) == ',');
+
+ return list;
+fail_errno:
+ perror("parsing GNU pax sparse file record");
+ goto fail;
+fail_format:
+ fputs("malformed GNU pax sparse file record\n", stderr);
+ goto fail;
+fail:
+ while (list != NULL) {
+ ent = list;
+ list = list->next;
+ free(ent);
+ }
+ free(ent);
+ return NULL;
+}
+
static int read_pax_header(int fd, uint64_t entsize, unsigned int *set_by_pax,
tar_header_decoded_t *out)
{
@@ -254,6 +303,19 @@ static int read_pax_header(int fd, uint64_t entsize, unsigned int *set_by_pax,
out->sb.st_ctime = field;
}
*set_by_pax |= PAX_CTIME;
+ } else if (!strncmp(line, "GNU.sparse.name=", 16)) {
+ free(out->name);
+ out->name = strdup(line + 5);
+ if (out->name == NULL)
+ goto fail_errno;
+ *set_by_pax |= PAX_NAME;
+ } else if (!strncmp(line, "GNU.sparse.map=", 15)) {
+ free_sparse_list(out->sparse);
+ sparse_last = NULL;
+
+ out->sparse = read_sparse_map(line + 15);
+ if (out->sparse == NULL)
+ goto fail;
} else if (!strncmp(line, "GNU.sparse.size=", 16)) {
pax_read_decimal(line + 16, &out->actual_size);
*set_by_pax |= PAX_SPARSE_SIZE;
diff --git a/tests/Makemodule.am b/tests/Makemodule.am
index f9232dc..2c8344a 100644
--- a/tests/Makemodule.am
+++ b/tests/Makemodule.am
@@ -55,17 +55,23 @@ test_tar_sparse_gnu1_LDADD = libtar.a libutil.a
test_tar_sparse_gnu1_CPPFLAGS = $(AM_CPPFLAGS)
test_tar_sparse_gnu1_CPPFLAGS += -DTESTPATH=$(top_srcdir)/tests/tar
+test_tar_sparse_gnu2_SOURCES = tests/tar_sparse_gnu1.c
+test_tar_sparse_gnu2_LDADD = libtar.a libutil.a
+test_tar_sparse_gnu2_CPPFLAGS = $(AM_CPPFLAGS)
+test_tar_sparse_gnu2_CPPFLAGS += -DTESTPATH=$(top_srcdir)/tests/tar
+
check_PROGRAMS += test_canonicalize_name test_mknode_simple test_mknode_slink
check_PROGRAMS += test_mknode_reg test_mknode_dir test_gen_inode_table
check_PROGRAMS += test_add_by_path test_get_path test_fstree_sort
check_PROGRAMS += test_fstree_from_file test_fstree_init test_fstree_xattr
check_PROGRAMS += test_tar_ustar test_tar_pax test_tar_gnu test_tar_sparse_gnu
-check_PROGRAMS += test_tar_sparse_gnu1
+check_PROGRAMS += test_tar_sparse_gnu1 test_tar_sparse_gnu2
TESTS += test_canonicalize_name test_mknode_simple test_mknode_slink
TESTS += test_mknode_reg test_mknode_dir test_gen_inode_table
TESTS += test_add_by_path test_get_path test_fstree_sort test_fstree_from_file
TESTS += test_fstree_init test_fstree_xattr test_tar_ustar test_tar_pax
TESTS += test_tar_gnu test_tar_sparse_gnu test_tar_sparse_gnu1
+TESTS += test_tar_sparse_gnu2
EXTRA_DIST += $(top_srcdir)/tests/tar
diff --git a/tests/tar_sparse_gnu2.c b/tests/tar_sparse_gnu2.c
new file mode 100644
index 0000000..edf1fb5
--- /dev/null
+++ b/tests/tar_sparse_gnu2.c
@@ -0,0 +1,162 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "util.h"
+#include "tar.h"
+
+#include <unistd.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+#define STR(x) #x
+#define STRVALUE(x) STR(x)
+
+#define TEST_PATH STRVALUE(TESTPATH)
+
+static int open_read(const char *path)
+{
+ int fd = open(path, O_RDONLY);
+
+ if (fd < 0) {
+ perror(path);
+ exit(EXIT_FAILURE);
+ }
+
+ return fd;
+}
+
+int main(void)
+{
+ tar_header_decoded_t hdr;
+ sparse_map_t *sparse;
+ int fd;
+
+ assert(chdir(TEST_PATH) == 0);
+
+ fd = open_read("sparse-files/pax-gnu0-1.tar");
+ assert(read_header(fd, &hdr) == 0);
+ assert(hdr.sb.st_mode == (S_IFREG | 0644));
+ assert(hdr.sb.st_uid == 01750);
+ assert(hdr.sb.st_gid == 01750);
+ assert(hdr.sb.st_size == 2097152);
+ assert(hdr.actual_size == 2097152);
+ assert(hdr.record_size == 32768);
+ assert(strcmp(hdr.name, "input.bin") == 0);
+ assert(!hdr.unknown_record);
+
+ sparse = hdr.sparse;
+ assert(sparse != NULL);
+ assert(sparse->offset == 0);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 262144);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 524288);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 786432);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 1048576);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 1310720);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 1572864);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 1835008);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 2097152);
+ assert(sparse->count == 0);
+
+ sparse = sparse->next;
+ assert(sparse == NULL);
+
+ clear_header(&hdr);
+ close(fd);
+
+ fd = open_read("sparse-files/pax-gnu1-0.tar");
+ assert(read_header(fd, &hdr) == 0);
+ assert(hdr.sb.st_mode == (S_IFREG | 0644));
+ assert(hdr.sb.st_uid == 01750);
+ assert(hdr.sb.st_gid == 01750);
+ assert(hdr.sb.st_size == 2097152);
+ assert(hdr.actual_size == 2097152);
+ assert(hdr.record_size == 32768);
+ assert(strcmp(hdr.name, "input.bin") == 0);
+ assert(!hdr.unknown_record);
+
+ sparse = hdr.sparse;
+ assert(sparse != NULL);
+ assert(sparse->offset == 0);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 262144);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 524288);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 786432);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 1048576);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 1310720);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 1572864);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 1835008);
+ assert(sparse->count == 4096);
+
+ sparse = sparse->next;
+ assert(sparse != NULL);
+ assert(sparse->offset == 2097152);
+ assert(sparse->count == 0);
+
+ sparse = sparse->next;
+ assert(sparse == NULL);
+
+ clear_header(&hdr);
+ close(fd);
+
+ return EXIT_SUCCESS;
+}