Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/vfs/direntry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions src/vfs/tar/tar-internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/vfs/tar/tar-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 7 additions & 8 deletions src/vfs/tar/tar-sparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* --------------------------------------------------------------------------------------------- */
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
28 changes: 18 additions & 10 deletions src/vfs/tar/tar-xheader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
Comment thread
zyv marked this conversation as resolved.
Resolved
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;
}

Expand All @@ -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)
Comment thread
zyv marked this conversation as resolved.
Resolved
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;
}

Expand Down Expand Up @@ -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
Expand Down
77 changes: 58 additions & 19 deletions src/vfs/tar/tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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, &current_stat_info);
(void) tar_sparse_skip_file (archive, &current_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)
{
Expand Down Expand Up @@ -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, &current_stat_info.stat);
Expand Down Expand Up @@ -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"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be more natural to say "Unexpected end of archive file". or just "Truncated archive".


status = HEADER_END_OF_FILE;
goto ret;
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand All @@ -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, &current_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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions tests/src/vfs/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ endif
if ENABLE_VFS_FTP
SUBDIRS += ftpfs
endif

if ENABLE_VFS_TAR
SUBDIRS += tar
endif
19 changes: 19 additions & 0 deletions tests/src/vfs/tar/Makefile.am
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading