Fix 'bsdtar -t' on tape drives. Libarchive uses the

skip() callback to skip over data when reading uncompressed
archives.  This gets invoked, for example, during tar -t
or tar -x with a filename argument.  The revised code
only calls [lf]seek() on regular files, instead of depending
on the kernel to return an error.

Thanks to: bde for explaining the implementation of lseek()
Thanks to: Daniel O'Connor for testing
Approved by: re (Ken Smith)
MFC after: 5 days
This commit is contained in:
kientzle 2007-06-26 03:06:48 +00:00
parent a404044c90
commit 0d5ae86293
3 changed files with 45 additions and 12 deletions

View File

@ -78,7 +78,8 @@ archive_read_open_fd(struct archive *a, int fd, size_t block_size)
return (ARCHIVE_FATAL);
}
mine->fd = fd;
mine->can_skip = 1;
/* lseek() hardly ever works, so disable it by default. See below. */
mine->can_skip = 0;
return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
}
@ -93,8 +94,18 @@ file_open(struct archive *a, void *client_data)
return (ARCHIVE_FATAL);
}
if (S_ISREG(st.st_mode))
if (S_ISREG(st.st_mode)) {
archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
/*
* Enabling skip here is a performance optimization for
* anything that supports lseek(). On FreeBSD, only
* regular files and raw disk devices support lseek() and
* there's no portable way to determine if a device is
* a raw disk device, so we only enable this optimization
* for regular files.
*/
mine->can_skip = 1;
}
return (ARCHIVE_OK);
}

View File

@ -51,6 +51,7 @@ struct read_FILE_data {
FILE *f;
size_t block_size;
void *buffer;
char can_skip;
};
static int file_close(struct archive *, void *);
@ -80,6 +81,8 @@ archive_read_open_FILE(struct archive *a, FILE *f)
return (ARCHIVE_FATAL);
}
mine->f = f;
/* Suppress skip by default. See below. */
mine->can_skip = 0;
return (archive_read_open2(a, mine, file_open, file_read,
file_skip, file_close));
}
@ -95,8 +98,11 @@ file_open(struct archive *a, void *client_data)
* it's not a file. (FILE * objects can wrap many kinds
* of I/O streams.)
*/
if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode))
if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) {
archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
/* Enable the seek optimization for regular files. */
mine->can_skip = 1;
}
return (ARCHIVE_OK);
}
@ -125,21 +131,25 @@ file_skip(struct archive *a, void *client_data, off_t request)
{
struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
(void)a; /* UNUSED */
/*
* Note: the 'fd' and 'filename' versions round the request
* down to a multiple of the block size to ensure proper
* operation on block-oriented media such as tapes. But stdio
* doesn't work with such media (it doesn't ensure blocking),
* so we don't need to bother.
* If we can't skip, return 0 as the amount we did step and
* the caller will work around by reading and discarding.
*/
if (!mine->can_skip)
return (0);
if (request == 0)
return (0);
#if HAVE_FSEEKO
if (fseeko(mine->f, request, SEEK_CUR) != 0)
#else
if (fseek(mine->f, request, SEEK_CUR) != 0)
#endif
{
archive_set_error(a, errno, "Error skipping forward");
return (ARCHIVE_FATAL);
mine->can_skip = 0;
return (0);
}
return (request);
}

View File

@ -96,7 +96,8 @@ archive_read_open_filename(struct archive *a, const char *filename,
mine->block_size = block_size;
mine->buffer = NULL;
mine->fd = -1;
mine->can_skip = 1;
/* lseek() almost never works; disable it by default. See below. */
mine->can_skip = 0;
return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
}
@ -123,8 +124,19 @@ file_open(struct archive *a, void *client_data)
if (fstat(mine->fd, &st) == 0) {
/* If we're reading a file from disk, ensure that we don't
overwrite it with an extracted file. */
if (S_ISREG(st.st_mode))
if (S_ISREG(st.st_mode)) {
archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
/*
* Enabling skip here is a performance
* optimization for anything that supports
* lseek(). On FreeBSD, only regular files
* and raw disk devices support lseek() and
* there's no portable way to determine if a
* device is a raw disk device, so we only
* enable this optimization for regular files.
*/
mine->can_skip = 1;
}
/* Remember mode so close can decide whether to flush. */
mine->st_mode = st.st_mode;
} else {