diff --git a/configure.ac b/configure.ac index 3580ae80dd..20cfeb1c01 100644 --- a/configure.ac +++ b/configure.ac @@ -712,6 +712,7 @@ tests/src/vfs/extfs/helpers-list/data/config.sh tests/src/vfs/extfs/helpers-list/misc/Makefile tests/src/vfs/ftpfs/Makefile tests/src/vfs/local/Makefile +tests/src/vfs/tar/Makefile ]) AC_OUTPUT diff --git a/lib/vfs/direntry.c b/lib/vfs/direntry.c index d61cd6f91d..e3b169be25 100644 --- a/lib/vfs/direntry.c +++ b/lib/vfs/direntry.c @@ -117,6 +117,7 @@ vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int f *sep = '\0'; res = vfs_s_generate_entry (me, path, dir, (flags & FL_MKDIR) != 0 ? (0777 | S_IFDIR) : 0777); + vfs_copy_stat_times (&dir->super->root->st, &res->ino->st); vfs_s_insert_entry (me, dir, res); if (sep != NULL) diff --git a/src/vfs/tar/tar-internal.c b/src/vfs/tar/tar-internal.c index c8673e6c74..782581fb48 100644 --- a/src/vfs/tar/tar-internal.c +++ b/src/vfs/tar/tar-internal.c @@ -158,8 +158,9 @@ tar_seek_archive (tar_super_t *archive, off_t size) if (size <= skipped) return 0; - // Compute number of records to skip - nrec = (size - skipped) / record_size; + // Compute number of records to skip, but read the last one: a seek doesn't stop at the end of + // a cut-off archive + nrec = (size - skipped - 1) / record_size; if (nrec == 0) return 0; @@ -510,7 +511,8 @@ off_from_header (const char *p, size_t s) union block * tar_find_next_block (tar_super_t *archive) { - if (current_block == record_end) + // after the end of the archive, tar_set_next_block_after() can go past the empty record + if (current_block >= record_end) { if (hit_eof) return NULL; diff --git a/src/vfs/tar/tar-internal.h b/src/vfs/tar/tar-internal.h index 65c067ba13..cd93bcbab8 100644 --- a/src/vfs/tar/tar-internal.h +++ b/src/vfs/tar/tar-internal.h @@ -312,7 +312,8 @@ gboolean tar_skip_file (tar_super_t *archive, off_t size); /* tar-sparse.c */ gboolean tar_sparse_member_p (tar_super_t *archive, struct tar_stat_info *st); gboolean tar_sparse_fixup_header (tar_super_t *archive, struct tar_stat_info *st); -enum dump_status tar_sparse_skip_file (tar_super_t *archive, struct tar_stat_info *st); +enum dump_status tar_sparse_skip_file (tar_super_t *archive, struct tar_stat_info *st, + off_t *data_offset); /* tar-xheader.c */ gboolean tar_xheader_decode (struct tar_stat_info *st); diff --git a/src/vfs/tar/tar-sparse.c b/src/vfs/tar/tar-sparse.c index 2989fc1407..45d1c21efc 100644 --- a/src/vfs/tar/tar-sparse.c +++ b/src/vfs/tar/tar-sparse.c @@ -522,8 +522,8 @@ star_get_sparse_info (tar_super_t *archive, struct tar_sparse_file *file) static gboolean pax_sparse_member_p (struct tar_sparse_file *file) { - return file->stat_info->sparse_map != NULL && file->stat_info->sparse_map->len > 0 - && file->stat_info->sparse_major > 0; + return (file->stat_info->sparse_map != NULL && file->stat_info->sparse_map->len > 0) + || file->stat_info->sparse_major > 0; } /* --------------------------------------------------------------------------------------------- */ @@ -556,11 +556,8 @@ pax_decode_header (tar_super_t *archive, struct tar_sparse_file *file) return FALSE; } - if (file->stat_info->sparse_map == NULL) - file->stat_info->sparse_map = - g_array_sized_new (FALSE, FALSE, sizeof (struct sp_array), u); - else - g_array_set_size (file->stat_info->sparse_map, u); + if (file->stat_info->sparse_map != NULL) + g_array_set_size (file->stat_info->sparse_map, 0); sparse_map_len = u; @@ -628,7 +625,7 @@ tar_sparse_fixup_header (tar_super_t *archive, struct tar_stat_info *st) /* --------------------------------------------------------------------------------------------- */ enum dump_status -tar_sparse_skip_file (tar_super_t *archive, struct tar_stat_info *st) +tar_sparse_skip_file (tar_super_t *archive, struct tar_stat_info *st, off_t *data_offset) { gboolean rc = TRUE; struct tar_sparse_file file; @@ -640,6 +637,8 @@ tar_sparse_skip_file (tar_super_t *archive, struct tar_stat_info *st) file.fd = -1; rc = sparse_decode_header (archive, &file); + // the data follows the sparse map or the extension headers, if any + *data_offset = BLOCKSIZE * tar_current_block_ordinal (archive); (void) tar_skip_file (archive, file.stat_info->archive_file_size - file.dumped_size); return (sparse_done (&file) && rc) ? dump_status_ok : dump_status_short; } diff --git a/src/vfs/tar/tar-xheader.c b/src/vfs/tar/tar-xheader.c index d087f422d0..9b848d3134 100644 --- a/src/vfs/tar/tar-xheader.c +++ b/src/vfs/tar/tar-xheader.c @@ -737,9 +737,9 @@ sparse_numblocks_decoder (struct tar_stat_info *st, const char *keyword, const c return FALSE; if (st->sparse_map == NULL) - st->sparse_map = g_array_sized_new (FALSE, FALSE, sizeof (struct sp_array), u); + st->sparse_map = g_array_new (FALSE, FALSE, sizeof (struct sp_array)); else - g_array_set_size (st->sparse_map, u); + g_array_set_size (st->sparse_map, 0); return TRUE; } @@ -750,15 +750,20 @@ static gboolean sparse_offset_decoder (struct tar_stat_info *st, const char *keyword, const char *arg, size_t size) { uintmax_t u; - struct sp_array *s; + struct sp_array s; (void) size; + // the offset starts a chunk, which needs GNU.sparse.numblocks before it + if (st->sparse_map == NULL) + return FALSE; + if (!decode_num (&u, arg, TYPE_MAXIMUM (off_t), keyword)) return FALSE; - s = &g_array_index (st->sparse_map, struct sp_array, st->sparse_map->len - 1); - s->offset = u; + s.offset = u; + s.numbytes = 0; + g_array_append_val (st->sparse_map, s); return TRUE; } @@ -769,16 +774,17 @@ sparse_numbytes_decoder (struct tar_stat_info *st, const char *keyword, const ch size_t size) { uintmax_t u; - struct sp_array s; (void) size; + // the size ends the chunk started by GNU.sparse.offset + if (st->sparse_map == NULL || st->sparse_map->len == 0) + return FALSE; + if (!decode_num (&u, arg, SIZE_MAX, keyword)) return FALSE; - s.offset = 0; - s.numbytes = u; - g_array_append_val (st->sparse_map, s); + g_array_index (st->sparse_map, struct sp_array, st->sparse_map->len - 1).numbytes = u; return TRUE; } @@ -906,7 +912,9 @@ tar_xheader_read (tar_super_t *archive, struct xheader *xhdr, union block *p, of return FALSE; size = size_plus_1 - 1; xhdr->size = size; - xhdr->buffer = g_malloc (size_plus_1); + xhdr->buffer = g_try_malloc (size_plus_1); + if (xhdr->buffer == NULL) + return FALSE; xhdr->buffer[size] = '\0'; do diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index 4d9c2ff99f..36e08fe3ee 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -251,7 +251,7 @@ tar_calc_sparse_offsets (struct vfs_s_inode *inode) sp = &g_array_index (sm, struct sp_array, i); sp->arch_offset = begin; - begin += BLOCKSIZE * (sp->numbytes / BLOCKSIZE + sp->numbytes % BLOCKSIZE); + begin += BLOCKSIZE * (sp->numbytes / BLOCKSIZE + (sp->numbytes % BLOCKSIZE != 0)); } } @@ -271,19 +271,27 @@ tar_skip_member (tar_super_t *archive, struct vfs_s_inode *inode) if (current_stat_info.is_sparse) { - if (inode != NULL) - inode->data_offset = BLOCKSIZE * tar_current_block_ordinal (archive); + off_t data_offset = BLOCKSIZE * tar_current_block_ordinal (archive); - (void) tar_sparse_skip_file (archive, ¤t_stat_info); + (void) tar_sparse_skip_file (archive, ¤t_stat_info, &data_offset); if (inode != NULL) { + inode->data_offset = data_offset; + // use vfs_s_inode::user_data to keep the sparse map inode->user_data = current_stat_info.sparse_map; current_stat_info.sparse_map = NULL; + // without chunks, e.g. if the archive ends in the map, the whole member is a hole + if (inode->user_data == NULL) + inode->user_data = g_array_new (FALSE, FALSE, sizeof (struct sp_array)); + tar_calc_sparse_offsets (inode); } + + // FALSE if the map or the data is cut off + return !hit_eof; } else if (save_typeflag != DIRTYPE) { @@ -564,7 +572,19 @@ tar_insert_entry (struct vfs_class *me, struct vfs_s_super *archive, union block { entry = VFS_SUBCLASS (me)->find_entry (me, parent, p, LINK_NO_FOLLOW, FL_NONE); if (entry != NULL) + { + if (S_ISDIR (entry->ino->st.st_mode)) + { + entry->ino->st.st_mode = current_stat_info.stat.st_mode; + entry->ino->st.st_uid = current_stat_info.stat.st_uid; + entry->ino->st.st_gid = current_stat_info.stat.st_gid; + vfs_zero_stat_times (&entry->ino->st); + entry->ino->st.st_mtime = current_stat_info.mtime.tv_sec; + entry->ino->st.st_atime = current_stat_info.atime.tv_sec; + entry->ino->st.st_ctime = current_stat_info.ctime.tv_sec; + } return HEADER_SUCCESS; + } } *inode = vfs_s_new_inode (me, archive, ¤t_stat_info.stat); @@ -599,6 +619,12 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) current_header = header; if (header == NULL) { + // the archive is cut off after an extended header or a long name of a member + if (hit_eof + && (next_long_name != NULL || next_long_link != NULL + || current_stat_info.xhdr.buffer != NULL)) + message (D_ERROR, MSG_ERROR, _ ("Unexpected EOF on archive file")); + status = HEADER_END_OF_FILE; goto ret; } @@ -635,14 +661,18 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) if (ckd_add (&size, current_stat_info.stat.st_size, 2 * BLOCKSIZE - 1)) { - message (D_ERROR, MSG_ERROR, _ ("Inconsistent tar archive")); status = HEADER_FAILURE; goto ret; } size -= size % BLOCKSIZE; - header_copy = g_malloc (size + 1); + header_copy = g_try_malloc (size + 1); + if (header_copy == NULL) + { + status = HEADER_FAILURE; + goto ret; + } if (header->header.typeflag == GNUTYPE_LONGNAME) { @@ -666,7 +696,6 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) data_block = tar_find_next_block (arch); if (data_block == NULL) { - message (D_ERROR, MSG_ERROR, _ ("Unexpected EOF on archive file")); status = HEADER_FAILURE; goto ret; } @@ -684,12 +713,13 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) } else if (header->header.typeflag == XHDTYPE || header->header.typeflag == SOLARIS_XHDTYPE) { - if (arch->type == TAR_UNKNOWN) + // the first header of a pax archive is this extended header, which has the ustar + // magic: like GNU tar, treat an archive with an extended header as pax, not ustar + if (arch->type == TAR_UNKNOWN || arch->type == TAR_USTAR) arch->type = TAR_POSIX; if (!tar_xheader_read (arch, ¤t_stat_info.xhdr, header, OFF_FROM_HEADER (header->header.size))) { - message (D_ERROR, MSG_ERROR, _ ("Unexpected EOF on archive file")); status = HEADER_FAILURE; goto ret; } @@ -703,13 +733,12 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) arch->type = TAR_POSIX; memset (&xhdr, 0, sizeof (xhdr)); - tar_xheader_read (arch, &xhdr, header, OFF_FROM_HEADER (header->header.size)); - ok = tar_xheader_decode_global (&xhdr); + ok = tar_xheader_read (arch, &xhdr, header, OFF_FROM_HEADER (header->header.size)) + && tar_xheader_decode_global (&xhdr); tar_xheader_destroy (&xhdr); if (!ok) { - message (D_ERROR, MSG_ERROR, _ ("Inconsistent tar archive")); status = HEADER_FAILURE; goto ret; } @@ -805,10 +834,7 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) status = tar_insert_entry (me, archive, header, &inode); if (status != HEADER_SUCCESS) - { - message (D_ERROR, MSG_ERROR, _ ("Inconsistent tar archive")); goto ret; - } if (recent_long_name == next_long_name) recent_long_name = NULL; @@ -819,7 +845,11 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) if (tar_skip_member (arch, inode)) status = HEADER_SUCCESS; else if (hit_eof) + { + // the header is good, but the data is cut off: keep the member, and end the archive + message (D_ERROR, MSG_ERROR, _ ("Unexpected EOF on archive file")); status = HEADER_END_OF_FILE; + } else status = HEADER_FAILURE; } @@ -1006,13 +1036,15 @@ tar_open_archive (struct vfs_s_super *archive, const vfs_path_t *vpath, case HEADER_ZERO_BLOCK: case HEADER_SUCCESS: - // Skipping to next header. - break; // AB: FIXME + // Skipping to next header: report it once for the whole cascade. + message (D_ERROR, MSG_ERROR, + hit_eof ? _ ("Unexpected EOF on archive file") + : _ ("Inconsistent tar archive")); + break; case HEADER_END_OF_FILE: case HEADER_FAILURE: - // We are in the middle of a cascade of errors. - // AB: FIXME: TODO: show an error message here + // We are in the middle of a cascade of errors, which is already reported. return -1; default: @@ -1187,6 +1219,13 @@ tar_lseek_sparse (vfs_file_handler_t *fh, off_t offset) break; case 0: + if (sm->len == 0) + { + // without chunks, the whole member is a hole + offset = fh->ino->data_offset; + break; + } + chunk = &g_array_index (sm, struct sp_array, sm->len - 1); // FIXME: can we seek beyond tar archive EOF here? offset = chunk->arch_offset + chunk->numbytes; diff --git a/tests/src/vfs/Makefile.am b/tests/src/vfs/Makefile.am index 9b6789c681..b36731d1f7 100644 --- a/tests/src/vfs/Makefile.am +++ b/tests/src/vfs/Makefile.am @@ -12,3 +12,7 @@ endif if ENABLE_VFS_FTP SUBDIRS += ftpfs endif + +if ENABLE_VFS_TAR +SUBDIRS += tar +endif diff --git a/tests/src/vfs/tar/Makefile.am b/tests/src/vfs/tar/Makefile.am new file mode 100644 index 0000000000..8b2e39881d --- /dev/null +++ b/tests/src/vfs/tar/Makefile.am @@ -0,0 +1,19 @@ +PACKAGE_STRING = "/src/vfs/tar" + +AM_CPPFLAGS = \ + $(GLIB_CFLAGS) \ + -I$(top_srcdir) \ + @CHECK_CFLAGS@ + +LDADD = \ + $(top_builddir)/src/libinternal.la \ + $(top_builddir)/lib/libmc.la \ + @CHECK_LIBS@ + +TESTS = \ + tar_open_archive + +check_PROGRAMS = $(TESTS) + +tar_open_archive_SOURCES = \ + tar_open_archive.c diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c new file mode 100644 index 0000000000..54ee729ccf --- /dev/null +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -0,0 +1,837 @@ +/* + src/vfs/tar - tests for opening of tar archives + + Copyright (C) 2026 + Free Software Foundation, Inc. + + Written by: + Yury V. Zaytsev , 2026 + + This file is part of the Midnight Commander. + + The Midnight Commander is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + The Midnight Commander is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#define TEST_SUITE_NAME "/src/vfs/tar" + +#include "tests/mctest.h" + +#include // snprintf() +#include // unlink(), rmdir() +#include // utime() + +#include "lib/strutil.h" +#include "src/vfs/local/local.h" + +#include "src/vfs/tar/tar.c" + +/* --------------------------------------------------------------------------------------------- */ + +// real size of the sparse file, much larger than the whole archive +#define SPARSE_SIZE (1024 * 1024) +// the last data chunk of the sparse file +#define CHUNK_OFFSET 2048 +#define CHUNK_DATA "hello\n" + +/* --------------------------------------------------------------------------------------------- */ +/* mocked functions */ +/* --------------------------------------------------------------------------------------------- */ + +// text of the last error message, if any +static char *message__text = NULL; +// number of error messages +static int message__count = 0; + +/* @Mock */ +void +message (int flags, const char *title, const char *text, ...) +{ + va_list ap; + + (void) flags; + (void) title; + + message__count++; + g_free (message__text); + va_start (ap, text); + message__text = g_strdup_vprintf (text, ap); + va_end (ap); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @Before */ +static void +setup (void) +{ + str_init_strings (NULL); + + vfs_init (); + vfs_init_localfs (); + vfs_init_tarfs (); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @After */ +static void +teardown (void) +{ + vfs_shut (); + str_uninit_strings (); + + MC_PTR_FREE (message__text); + message__count = 0; +} + +/* --------------------------------------------------------------------------------------------- */ + +/* Append data padded with zeros to the block boundary. */ +static void +append_data (GByteArray *archive, const char *data, size_t len) +{ + g_byte_array_append (archive, (const guint8 *) data, len); + while (archive->len % BLOCKSIZE != 0) + g_byte_array_append (archive, (const guint8 *) "", 1); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* Fill in the checksum of a header. */ +static void +set_checksum (union block *b) +{ + unsigned int sum = 0; + size_t i; + + memset (b->header.chksum, ' ', sizeof (b->header.chksum)); + for (i = 0; i < sizeof (*b); i++) + sum += (unsigned char) b->buffer[i]; + snprintf (b->header.chksum, sizeof (b->header.chksum), "%06o", sum); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* Append an ustar header followed by its data. */ +static void +append_member (GByteArray *archive, const char *name, char typeflag, const char *data, size_t len) +{ + union block b; + + memset (&b, 0, sizeof (b)); + g_strlcpy (b.header.name, name, sizeof (b.header.name)); + strcpy (b.header.mode, "0000644"); + strcpy (b.header.uid, "0000000"); + strcpy (b.header.gid, "0000000"); + snprintf (b.header.size, sizeof (b.header.size), "%011o", (unsigned int) len); + strcpy (b.header.mtime, "00000000000"); + b.header.typeflag = typeflag; + memcpy (b.header.magic, TMAGIC, sizeof (b.header.magic)); + memcpy (b.header.version, "00", sizeof (b.header.version)); + set_checksum (&b); + + g_byte_array_append (archive, (const guint8 *) b.buffer, sizeof (b)); + append_data (archive, data, len); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @DataSource("test_tar_pax_sparse_bad_keywords_ds") */ +static const struct test_tar_pax_sparse_bad_keywords_ds +{ + const char *xheader; +} test_tar_pax_sparse_bad_keywords_ds[] = { + // 0.0 with an offset, but without GNU.sparse.numblocks before it + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.offset=2048\n" + "25 GNU.sparse.numbytes=6\n" + "15 path=sparse\n", + }, + // 0.0 with a size before its offset + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.numblocks=1\n" + "25 GNU.sparse.numbytes=6\n" + "26 GNU.sparse.offset=2048\n" + "15 path=sparse\n", + }, + // 0.0 with a malformed offset + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.numblocks=1\n" + "23 GNU.sparse.offset=x\n" + "25 GNU.sparse.numbytes=6\n" + "15 path=sparse\n", + }, + // 0.0 with a malformed size + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.numblocks=1\n" + "26 GNU.sparse.offset=2048\n" + "26 GNU.sparse.numbytes=-1\n" + "15 path=sparse\n", + }, +}; + +/* @Test(dataSource = "test_tar_pax_sparse_bad_keywords_ds") */ +START_PARAMETRIZED_TEST (test_tar_pax_sparse_bad_keywords, test_tar_pax_sparse_bad_keywords_ds) +{ + static const guint8 zero_blocks[2 * BLOCKSIZE] = { 0 }; + GByteArray *archive; + char *dir, *path, *archive_path; + vfs_path_t *vpath; + DIR *d; + + // given: a pax sparse file with a malformed sparse map between two plain ustar files + archive = g_byte_array_new (); + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "PaxHeader/sparse", XHDTYPE, data->xheader, strlen (data->xheader)); + append_member (archive, "GNUSparseFile.0/sparse", REGTYPE, CHUNK_DATA, sizeof (CHUNK_DATA) - 1); + append_member (archive, "z", REGTYPE, "world\n", 6); + g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + d = mc_opendir (vpath); + + // then: the archive is rejected with one error message instead of a crash + mctest_assert_null (d); + ck_assert_int_eq (message__count, 1); + + // cleanup + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_PARAMETRIZED_TEST + +/* --------------------------------------------------------------------------------------------- */ + +/* @DataSource("test_tar_pax_sparse_ds") */ +static const struct test_tar_pax_sparse_ds +{ + const char *xheader; + const char *map; // sparse map stored in the data, if any + size_t head_len; // size of a chunk at offset 0 before the last chunk, if any + gboolean bad_map; // the map is malformed: check the listing only +} test_tar_pax_sparse_ds[] = { + // GNU tar --sparse-version=0.0: each chunk is a pair of keywords in the extended header + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.numblocks=1\n" + "26 GNU.sparse.offset=2048\n" + "25 GNU.sparse.numbytes=6\n" + "15 path=sparse\n", + NULL, + 0, + FALSE, + }, + // GNU tar --sparse-version=0.1: the map is in the extended header + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.numblocks=1\n" + "25 GNU.sparse.map=2048,6\n" + "15 path=sparse\n", + NULL, + 0, + FALSE, + }, + // GNU tar and bsdtar default (1.0): the map precedes the chunk in the data + { + "22 GNU.sparse.major=1\n" + "22 GNU.sparse.minor=0\n" + "26 GNU.sparse.name=sparse\n" + "31 GNU.sparse.realsize=1048576\n", + "1\n2048\n6\n", + 0, + FALSE, + }, + // 1.0 with a chunk that doesn't fill its last block, which is padded in the archive + { + "22 GNU.sparse.major=1\n" + "22 GNU.sparse.minor=0\n" + "26 GNU.sparse.name=sparse\n" + "31 GNU.sparse.realsize=1048576\n", + "2\n0\n1000\n2048\n6\n", + 1000, + FALSE, + }, + // 0.0 with a chunk count that doesn't fit into memory + { + "27 GNU.sparse.size=1048576\n" + "35 GNU.sparse.numblocks=4294967295\n" + "26 GNU.sparse.offset=2048\n" + "25 GNU.sparse.numbytes=6\n" + "15 path=sparse\n", + NULL, + 0, + FALSE, + }, + // 1.0 with a chunk count that doesn't fit into memory + { + "22 GNU.sparse.major=1\n" + "22 GNU.sparse.minor=0\n" + "26 GNU.sparse.name=sparse\n" + "31 GNU.sparse.realsize=1048576\n", + "4294967295\n2048\n6\n", + 0, + TRUE, + }, + // 1.0 without chunks: the whole file is a hole, and the data in the archive is not used + { + "22 GNU.sparse.major=1\n" + "22 GNU.sparse.minor=0\n" + "26 GNU.sparse.name=sparse\n" + "31 GNU.sparse.realsize=1048576\n", + "0\n", + 0, + TRUE, + }, +}; + +/* @Test(dataSource = "test_tar_pax_sparse_ds") */ +START_PARAMETRIZED_TEST (test_tar_pax_sparse, test_tar_pax_sparse_ds) +{ + static const guint8 zero_blocks[2 * BLOCKSIZE] = { 0 }; + GByteArray *archive, *content; + char *dir, *path, *archive_path, *joined; + vfs_path_t *vpath, *sparse_vpath; + DIR *d; + struct vfs_dirent *de; + GString *listing; + char *expected, *actual; + ssize_t n, total; + int fd; + + // given: a pax sparse file between two plain ustar files, as written by bsdtar + content = g_byte_array_new (); + if (data->map != NULL) + append_data (content, data->map, strlen (data->map)); + if (data->head_len != 0) + { + char *head = g_strnfill (data->head_len, 'x'); + + append_data (content, head, data->head_len); + g_free (head); + } + g_byte_array_append (content, (const guint8 *) CHUNK_DATA, sizeof (CHUNK_DATA) - 1); + + archive = g_byte_array_new (); + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "PaxHeader/sparse", XHDTYPE, data->xheader, strlen (data->xheader)); + append_member (archive, "GNUSparseFile.0/sparse", REGTYPE, (const char *) content->data, + content->len); + append_member (archive, "z", REGTYPE, "world\n", 6); + g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + listing = g_string_new (NULL); + d = mc_opendir (vpath); + mctest_assert_not_null (d); + while ((de = mc_readdir (d)) != NULL) + g_string_append_printf (listing, "%s ", de->d_name); + mc_closedir (d); + + expected = g_malloc0 (SPARSE_SIZE); + memset (expected, 'x', data->head_len); + memcpy (expected + CHUNK_OFFSET, CHUNK_DATA, sizeof (CHUNK_DATA) - 1); + actual = g_malloc0 (SPARSE_SIZE + 1); + sparse_vpath = vfs_path_append_new (vpath, "sparse", (char *) NULL); + fd = mc_open (sparse_vpath, O_RDONLY); + mctest_assert_true (fd != -1); + for (total = 0; (n = mc_read (fd, actual + total, SPARSE_SIZE + 1 - total)) > 0; total += n) + ; + mc_close (fd); + + // then: all members are listed and the sparse file is expanded correctly + joined = g_string_free (listing, FALSE); + mctest_assert_null (message__text); + mctest_assert_str_eq (joined, "a sparse z "); + ck_assert_int_eq (total, SPARSE_SIZE); + if (!data->bad_map) + mctest_assert_true (memcmp (actual, expected, SPARSE_SIZE) == 0); + + // cleanup + g_free (joined); + g_free (actual); + g_free (expected); + vfs_path_free (sparse_vpath, TRUE); + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); + g_byte_array_free (content, TRUE); +} +END_PARAMETRIZED_TEST + +/* --------------------------------------------------------------------------------------------- */ + +/* @DataSource("test_tar_huge_header_size_ds") */ +static const struct test_tar_huge_header_size_ds +{ + char typeflag; +} test_tar_huge_header_size_ds[] = { + { XHDTYPE }, { SOLARIS_XHDTYPE }, { XGLTYPE }, { GNUTYPE_LONGNAME }, { GNUTYPE_LONGLINK }, +}; + +/* @Test(dataSource = "test_tar_huge_header_size_ds") */ +START_PARAMETRIZED_TEST (test_tar_huge_header_size, test_tar_huge_header_size_ds) +{ + static const guint8 zero_blocks[2 * BLOCKSIZE] = { 0 }; + GByteArray *archive; + union block *b; + char *dir, *path, *archive_path; + vfs_path_t *vpath; + DIR *d; + + // given: a header the data of which doesn't fit into memory + archive = g_byte_array_new (); + append_member (archive, "huge", data->typeflag, "", 0); + b = (union block *) archive->data; + // 2^60 bytes in base-256 + memset (b->header.size, 0, sizeof (b->header.size)); + b->header.size[0] = (char) 0x80; + b->header.size[4] = 0x10; + set_checksum (b); + append_member (archive, "a", REGTYPE, "hello\n", 6); + g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + d = mc_opendir (vpath); + + // then: one error message instead of an abort + mctest_assert_null (d); + ck_assert_int_eq (message__count, 1); + + // cleanup + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_PARAMETRIZED_TEST + +/* --------------------------------------------------------------------------------------------- */ + +/* @DataSource("test_tar_bad_header_ds") */ +static const struct test_tar_bad_header_ds +{ + char typeflag; // type of the members between "a" and "z" + int count; // number of these members + gboolean bad_checksum; // these members have a bad checksum + const char *listing; // NULL if the archive is rejected +} test_tar_bad_header_ds[] = { + // tarfs skips a bad header and reads the members after it + { REGTYPE, 1, TRUE, "a z " }, + // tarfs stops at two bad headers in sequence + { REGTYPE, 2, TRUE, NULL }, + // a hard link to a member that isn't in the archive + { LNKTYPE, 1, FALSE, "a z " }, +}; + +/* @Test(dataSource = "test_tar_bad_header_ds") */ +START_PARAMETRIZED_TEST (test_tar_bad_header, test_tar_bad_header_ds) +{ + static const guint8 zero_blocks[2 * BLOCKSIZE] = { 0 }; + GByteArray *archive; + char *dir, *path, *archive_path; + vfs_path_t *vpath; + DIR *d; + GString *listing; + int i; + + // given: bad members between two good ones + archive = g_byte_array_new (); + append_member (archive, "a", REGTYPE, "hello\n", 6); + for (i = 0; i < data->count; i++) + { + append_member (archive, "bad", data->typeflag, "", 0); + if (data->bad_checksum) + archive->data[archive->len - BLOCKSIZE + offsetof (struct posix_header, chksum)] ^= 1; + } + append_member (archive, "z", REGTYPE, "world\n", 6); + g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + listing = g_string_new (NULL); + d = mc_opendir (vpath); + if (d != NULL) + { + struct vfs_dirent *de; + + while ((de = mc_readdir (d)) != NULL) + g_string_append_printf (listing, "%s ", de->d_name); + mc_closedir (d); + } + + // then: one error message, and the good members are listed, if the archive is opened + ck_assert_int_eq (message__count, 1); + if (data->listing == NULL) + { + mctest_assert_null (d); + } + else + { + mctest_assert_str_eq (listing->str, data->listing); + } + + // cleanup + g_string_free (listing, TRUE); + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_PARAMETRIZED_TEST + +/* --------------------------------------------------------------------------------------------- */ + +/* @Test */ +START_TEST (test_tar_eof_after_first_record) +{ + GByteArray *archive; + char *dir, *path, *archive_path; + vfs_path_t *vpath; + DIR *d; + struct vfs_dirent *de; + GString *listing; + int i; + + // given: members that fill the first record of 20 blocks, a member, and an extended header the + // data of which is cut off by the end of the archive + archive = g_byte_array_new (); + for (i = 0; i < 10; i++) + { + char name[] = "a0"; + + name[1] = (char) ('0' + i); + append_member (archive, name, REGTYPE, "hello\n", 6); + } + append_member (archive, "b", REGTYPE, "", 0); + append_member (archive, "PaxHeader/c", XHDTYPE, "13 path=cccc\n", 13); + g_byte_array_set_size (archive, archive->len - BLOCKSIZE); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + listing = g_string_new (NULL); + d = mc_opendir (vpath); + mctest_assert_not_null (d); + while ((de = mc_readdir (d)) != NULL) + g_string_append_printf (listing, "%s ", de->d_name); + mc_closedir (d); + + // then: one error message, and the members before the end are listed once + ck_assert_int_eq (message__count, 1); + mctest_assert_str_eq (listing->str, "a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 b "); + + // cleanup + g_string_free (listing, TRUE); + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_TEST + +/* --------------------------------------------------------------------------------------------- */ + +/* Append a pax 1.0 sparse member with a chunk of CHUNK_DATA at CHUNK_OFFSET. */ +static void +append_pax_sparse_member (GByteArray *archive) +{ + static const char xheader[] = "22 GNU.sparse.major=1\n" + "22 GNU.sparse.minor=0\n" + "26 GNU.sparse.name=sparse\n" + "31 GNU.sparse.realsize=1048576\n"; + static const char map[] = "1\n2048\n6\n"; + GByteArray *content; + + content = g_byte_array_new (); + append_data (content, map, sizeof (map) - 1); + g_byte_array_append (content, (const guint8 *) CHUNK_DATA, sizeof (CHUNK_DATA) - 1); + + append_member (archive, "PaxHeader/sparse", XHDTYPE, xheader, sizeof (xheader) - 1); + append_member (archive, "GNUSparseFile.0/sparse", REGTYPE, (const char *) content->data, + content->len); + g_byte_array_free (content, TRUE); +} + +/* --------------------------------------------------------------------------------------------- */ + +static void +cut_in_data (GByteArray *archive) +{ + char data[2000]; + + memset (data, 'x', sizeof (data)); + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "m", REGTYPE, data, sizeof (data)); + g_byte_array_set_size (archive, archive->len - 2 * BLOCKSIZE); +} + +static void +cut_in_data_of_first (GByteArray *archive) +{ + char data[2000]; + + memset (data, 'x', sizeof (data)); + append_member (archive, "m", REGTYPE, data, sizeof (data)); + g_byte_array_set_size (archive, archive->len - 2 * BLOCKSIZE); +} + +static void +cut_in_seeked_data (GByteArray *archive) +{ + // the data starts at block 3: tarfs reads 17 blocks of it in the first record of 20 blocks, + // and seeks over the rest, which is one whole record + const size_t len = 17 * BLOCKSIZE + 20 * BLOCKSIZE; + char *data; + + data = g_malloc0 (len); + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "m", REGTYPE, data, len); + g_byte_array_set_size (archive, 20 * BLOCKSIZE); + g_free (data); +} + +static void +cut_in_sparse_data (GByteArray *archive) +{ + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_pax_sparse_member (archive); + g_byte_array_set_size (archive, archive->len - BLOCKSIZE); +} + +static void +cut_in_sparse_map (GByteArray *archive) +{ + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_pax_sparse_member (archive); + g_byte_array_set_size (archive, archive->len - 2 * BLOCKSIZE); +} + +static void +cut_after_xheader (GByteArray *archive) +{ + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "PaxHeader/m", XHDTYPE, "10 path=m\n", 10); +} + +static void +cut_after_long_name (GByteArray *archive) +{ + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "././@LongLink", GNUTYPE_LONGNAME, "m", 2); +} + +/* @DataSource("test_tar_cut_off_ds") */ +static const struct test_tar_cut_off_ds +{ + void (*build) (GByteArray *archive); + const char *listing; +} test_tar_cut_off_ds[] = { + { cut_in_data, "a m " }, + { cut_in_data_of_first, "m " }, + { cut_in_seeked_data, "a m " }, + { cut_in_sparse_data, "a sparse " }, + { cut_in_sparse_map, "a sparse " }, + { cut_after_xheader, "a " }, + { cut_after_long_name, "a " }, +}; + +/* @Test(dataSource = "test_tar_cut_off_ds") */ +START_PARAMETRIZED_TEST (test_tar_cut_off, test_tar_cut_off_ds) +{ + GByteArray *archive; + char *dir, *path, *archive_path; + vfs_path_t *vpath; + DIR *d; + struct vfs_dirent *de; + GString *listing; + + // given: an archive that ends before the end of its last member + archive = g_byte_array_new (); + data->build (archive); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + listing = g_string_new (NULL); + d = mc_opendir (vpath); + mctest_assert_not_null (d); + while ((de = mc_readdir (d)) != NULL) + g_string_append_printf (listing, "%s ", de->d_name); + mc_closedir (d); + + // then: one error message, and the members are listed up to the cut-off one + ck_assert_int_eq (message__count, 1); + mctest_assert_str_eq (message__text, "Unexpected EOF on archive file"); + mctest_assert_str_eq (listing->str, data->listing); + + // cleanup + g_string_free (listing, TRUE); + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_PARAMETRIZED_TEST + +/* --------------------------------------------------------------------------------------------- */ + +/* @Test */ +START_TEST (test_tar_parent_dirs) +{ + static const guint8 zero_blocks[2 * BLOCKSIZE] = { 0 }; + const struct utimbuf archive_time = { 1234567890, 1234567890 }; + GByteArray *archive; + char *dir, *path, *archive_path; + vfs_path_t *vpath, *dir_vpath, *late_vpath; + struct stat st, late_st; + + // given: a member in a directory that has no entry of its own, + // and a member in a directory the entry of which comes after it + archive = g_byte_array_new (); + append_member (archive, "dir/a", REGTYPE, "hello\n", 6); + append_member (archive, "late/a", REGTYPE, "hello\n", 6); + append_member (archive, "late/", DIRTYPE, "", 0); + g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + ck_assert_int_eq (utime (path, &archive_time), 0); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + dir_vpath = vfs_path_append_new (vpath, "dir", (char *) NULL); + late_vpath = vfs_path_append_new (vpath, "late", (char *) NULL); + + // when + ck_assert_int_eq (mc_stat (dir_vpath, &st), 0); + ck_assert_int_eq (mc_stat (late_vpath, &late_st), 0); + + // then: the generated directory has the time of the archive, + // and the other one has the mode and the time of its own entry + mctest_assert_true (S_ISDIR (st.st_mode)); + ck_assert_int_eq (st.st_mtime, archive_time.modtime); + mctest_assert_true (S_ISDIR (late_st.st_mode)); + ck_assert_int_eq (late_st.st_mode & 07777, 0644); + ck_assert_int_eq (late_st.st_mtime, 0); + + // cleanup + vfs_path_free (late_vpath, TRUE); + vfs_path_free (dir_vpath, TRUE); + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_TEST + +/* --------------------------------------------------------------------------------------------- */ + +int +main (void) +{ + TCase *tc_core; + + tc_core = tcase_create ("Core"); + + tcase_add_checked_fixture (tc_core, setup, teardown); + + // Add new tests here: *************** + mctest_add_parameterized_test (tc_core, test_tar_pax_sparse_bad_keywords, + test_tar_pax_sparse_bad_keywords_ds); + mctest_add_parameterized_test (tc_core, test_tar_pax_sparse, test_tar_pax_sparse_ds); + mctest_add_parameterized_test (tc_core, test_tar_huge_header_size, + test_tar_huge_header_size_ds); + mctest_add_parameterized_test (tc_core, test_tar_bad_header, test_tar_bad_header_ds); + tcase_add_test (tc_core, test_tar_eof_after_first_record); + mctest_add_parameterized_test (tc_core, test_tar_cut_off, test_tar_cut_off_ds); + tcase_add_test (tc_core, test_tar_parent_dirs); + // *********************************** + + return mctest_run_all (tc_core); +} + +/* --------------------------------------------------------------------------------------------- */