2
* Copyright (c) 2003-2007 Tim Kientzle
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
#include "archive_platform.h"
27
__FBSDID("$FreeBSD: head/lib/libarchive/archive_read_open_file.c 201093 2009-12-28 02:28:44Z kientzle $");
29
#ifdef HAVE_SYS_STAT_H
53
struct read_FILE_data {
60
static int file_close(struct archive *, void *);
61
static ssize_t file_read(struct archive *, void *, const void **buff);
62
#if ARCHIVE_API_VERSION < 2
63
static ssize_t file_skip(struct archive *, void *, size_t request);
65
static off_t file_skip(struct archive *, void *, off_t request);
69
archive_read_open_FILE(struct archive *a, FILE *f)
72
struct read_FILE_data *mine;
73
size_t block_size = 128 * 1024;
76
archive_clear_error(a);
77
mine = (struct read_FILE_data *)malloc(sizeof(*mine));
78
b = malloc(block_size);
79
if (mine == NULL || b == NULL) {
80
archive_set_error(a, ENOMEM, "No memory");
83
return (ARCHIVE_FATAL);
85
mine->block_size = block_size;
89
* If we can't fstat() the file, it may just be that it's not
90
* a file. (FILE * objects can wrap many kinds of I/O
91
* streams, some of which don't support fileno()).)
93
if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) {
94
archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
95
/* Enable the seek optimization only for regular files. */
100
#if defined(__CYGWIN__) || defined(_WIN32)
101
setmode(fileno(mine->f), O_BINARY);
104
return (archive_read_open2(a, mine, NULL, file_read,
105
file_skip, file_close));
109
file_read(struct archive *a, void *client_data, const void **buff)
111
struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
114
*buff = mine->buffer;
115
bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f);
116
if (bytes_read < 0) {
117
archive_set_error(a, errno, "Error reading file");
122
#if ARCHIVE_API_VERSION < 2
124
file_skip(struct archive *a, void *client_data, size_t request)
127
file_skip(struct archive *a, void *client_data, off_t request)
130
struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
132
(void)a; /* UNUSED */
135
* If we can't skip, return 0 as the amount we did step and
136
* the caller will work around by reading and discarding.
144
if (fseeko(mine->f, request, SEEK_CUR) != 0)
146
if (fseek(mine->f, request, SEEK_CUR) != 0)
156
file_close(struct archive *a, void *client_data)
158
struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
160
(void)a; /* UNUSED */
161
if (mine->buffer != NULL)