2004-02-09 23:22:54 +00:00
|
|
|
/*-
|
2007-01-09 08:05:56 +00:00
|
|
|
* Copyright (c) 2003-2007 Tim Kientzle
|
2004-02-09 23:22:54 +00:00
|
|
|
* 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
|
2007-01-09 08:05:56 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-02-09 23:22:54 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
#include "archive_platform.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
2004-06-27 23:36:39 +00:00
|
|
|
#include <sys/stat.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <errno.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <fcntl.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <stdlib.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <string.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <unistd.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
#include "archive.h"
|
|
|
|
|
2006-11-24 02:00:48 +00:00
|
|
|
struct read_FILE_data {
|
|
|
|
FILE *f;
|
2004-02-09 23:22:54 +00:00
|
|
|
size_t block_size;
|
|
|
|
void *buffer;
|
2007-06-26 03:06:48 +00:00
|
|
|
char can_skip;
|
2004-02-09 23:22:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int file_close(struct archive *, void *);
|
|
|
|
static int file_open(struct archive *, void *);
|
|
|
|
static ssize_t file_read(struct archive *, void *, const void **buff);
|
2007-01-05 10:48:18 +00:00
|
|
|
#if ARCHIVE_API_VERSION < 2
|
2006-07-30 00:29:01 +00:00
|
|
|
static ssize_t file_skip(struct archive *, void *, size_t request);
|
2007-01-05 10:48:18 +00:00
|
|
|
#else
|
|
|
|
static off_t file_skip(struct archive *, void *, off_t request);
|
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
int
|
2006-11-24 02:00:48 +00:00
|
|
|
archive_read_open_FILE(struct archive *a, FILE *f)
|
2006-11-15 05:33:38 +00:00
|
|
|
{
|
2006-11-24 02:00:48 +00:00
|
|
|
struct read_FILE_data *mine;
|
2006-11-15 05:33:38 +00:00
|
|
|
|
2006-11-24 02:00:48 +00:00
|
|
|
mine = (struct read_FILE_data *)malloc(sizeof(*mine));
|
|
|
|
if (mine == NULL) {
|
|
|
|
archive_set_error(a, ENOMEM, "No memory");
|
|
|
|
return (ARCHIVE_FATAL);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2006-11-24 02:00:48 +00:00
|
|
|
mine->block_size = 128 * 1024;
|
|
|
|
mine->buffer = malloc(mine->block_size);
|
|
|
|
if (mine->buffer == NULL) {
|
|
|
|
archive_set_error(a, ENOMEM, "No memory");
|
|
|
|
free(mine);
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
mine->f = f;
|
2007-06-26 03:06:48 +00:00
|
|
|
/* Suppress skip by default. See below. */
|
|
|
|
mine->can_skip = 0;
|
2006-11-24 02:00:48 +00:00
|
|
|
return (archive_read_open2(a, mine, file_open, file_read,
|
|
|
|
file_skip, file_close));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
file_open(struct archive *a, void *client_data)
|
|
|
|
{
|
2006-11-24 02:00:48 +00:00
|
|
|
struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
|
2004-06-27 23:36:39 +00:00
|
|
|
struct stat st;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-11-24 02:00:48 +00:00
|
|
|
/*
|
|
|
|
* If we can't fstat() the file, it may just be that
|
|
|
|
* it's not a file. (FILE * objects can wrap many kinds
|
|
|
|
* of I/O streams.)
|
|
|
|
*/
|
2007-06-26 03:06:48 +00:00
|
|
|
if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) {
|
2006-11-24 02:00:48 +00:00
|
|
|
archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
|
2007-06-26 03:06:48 +00:00
|
|
|
/* Enable the seek optimization for regular files. */
|
|
|
|
mine->can_skip = 1;
|
|
|
|
}
|
2006-11-24 02:00:48 +00:00
|
|
|
|
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
file_read(struct archive *a, void *client_data, const void **buff)
|
|
|
|
{
|
2006-11-24 02:00:48 +00:00
|
|
|
struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
|
2005-03-13 01:51:16 +00:00
|
|
|
ssize_t bytes_read;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
*buff = mine->buffer;
|
2006-11-24 02:00:48 +00:00
|
|
|
bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f);
|
2005-03-13 01:51:16 +00:00
|
|
|
if (bytes_read < 0) {
|
2006-11-24 02:00:48 +00:00
|
|
|
archive_set_error(a, errno, "Error reading file");
|
2005-03-13 01:51:16 +00:00
|
|
|
}
|
|
|
|
return (bytes_read);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2007-01-05 10:48:18 +00:00
|
|
|
#if ARCHIVE_API_VERSION < 2
|
2006-07-30 00:29:01 +00:00
|
|
|
static ssize_t
|
|
|
|
file_skip(struct archive *a, void *client_data, size_t request)
|
2007-01-05 10:48:18 +00:00
|
|
|
#else
|
|
|
|
static off_t
|
|
|
|
file_skip(struct archive *a, void *client_data, off_t request)
|
|
|
|
#endif
|
2006-07-30 00:29:01 +00:00
|
|
|
{
|
2006-11-24 02:00:48 +00:00
|
|
|
struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
|
2006-09-05 05:59:46 +00:00
|
|
|
|
2007-06-26 03:06:48 +00:00
|
|
|
(void)a; /* UNUSED */
|
|
|
|
|
2006-07-30 00:29:01 +00:00
|
|
|
/*
|
2007-06-26 03:06:48 +00:00
|
|
|
* If we can't skip, return 0 as the amount we did step and
|
|
|
|
* the caller will work around by reading and discarding.
|
2006-07-30 00:29:01 +00:00
|
|
|
*/
|
2007-06-26 03:06:48 +00:00
|
|
|
if (!mine->can_skip)
|
|
|
|
return (0);
|
|
|
|
if (request == 0)
|
|
|
|
return (0);
|
|
|
|
|
2006-11-24 02:00:48 +00:00
|
|
|
#if HAVE_FSEEKO
|
|
|
|
if (fseeko(mine->f, request, SEEK_CUR) != 0)
|
|
|
|
#else
|
|
|
|
if (fseek(mine->f, request, SEEK_CUR) != 0)
|
|
|
|
#endif
|
2006-07-30 00:29:01 +00:00
|
|
|
{
|
2007-06-26 03:06:48 +00:00
|
|
|
mine->can_skip = 0;
|
|
|
|
return (0);
|
2006-07-30 00:29:01 +00:00
|
|
|
}
|
2006-11-24 02:00:48 +00:00
|
|
|
return (request);
|
2006-07-30 00:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
static int
|
|
|
|
file_close(struct archive *a, void *client_data)
|
|
|
|
{
|
2006-11-24 02:00:48 +00:00
|
|
|
struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
(void)a; /* UNUSED */
|
2004-04-13 23:45:37 +00:00
|
|
|
if (mine->buffer != NULL)
|
|
|
|
free(mine->buffer);
|
2004-02-09 23:22:54 +00:00
|
|
|
free(mine);
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|