51
51
#include "archive.h"
53
53
struct read_fd_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);
60
static int file_close(struct archive *, void *);
61
static ssize_t file_read(struct archive *, void *, const void **buff);
62
static int64_t file_skip(struct archive *, void *, int64_t request);
69
65
archive_read_open_fd(struct archive *a, int fd, size_t block_size)
72
struct read_fd_data *mine;
75
archive_clear_error(a);
76
if (fstat(fd, &st) != 0) {
77
archive_set_error(a, errno, "Can't stat fd %d", fd);
78
return (ARCHIVE_FATAL);
81
mine = (struct read_fd_data *)malloc(sizeof(*mine));
82
b = malloc(block_size);
83
if (mine == NULL || b == NULL) {
84
archive_set_error(a, ENOMEM, "No memory");
87
return (ARCHIVE_FATAL);
89
mine->block_size = block_size;
93
* Skip support is a performance optimization for anything
94
* that supports lseek(). On FreeBSD, only regular files and
95
* raw disk devices support lseek() and there's no portable
96
* way to determine if a device is a raw disk device, so we
97
* only enable this optimization for regular files.
99
if (S_ISREG(st.st_mode)) {
100
archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
104
#if defined(__CYGWIN__) || defined(__BORLANDC__)
105
setmode(mine->fd, O_BINARY);
106
#elif defined(_WIN32)
107
_setmode(mine->fd, _O_BINARY);
68
struct read_fd_data *mine;
71
archive_clear_error(a);
72
if (fstat(fd, &st) != 0) {
73
archive_set_error(a, errno, "Can't stat fd %d", fd);
74
return (ARCHIVE_FATAL);
77
mine = (struct read_fd_data *)calloc(1, 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
* Skip support is a performance optimization for anything
90
* that supports lseek(). On FreeBSD, only regular files and
91
* raw disk devices support lseek() and there's no portable
92
* way to determine if a device is a raw disk device, so we
93
* only enable this optimization for regular files.
95
if (S_ISREG(st.st_mode)) {
96
archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
99
#if defined(__CYGWIN__) || defined(_WIN32)
100
setmode(mine->fd, O_BINARY);
110
return (archive_read_open2(a, mine,
111
NULL, file_read, file_skip, file_close));
103
archive_read_set_read_callback(a, file_read);
104
archive_read_set_skip_callback(a, file_skip);
105
archive_read_set_close_callback(a, file_close);
106
archive_read_set_callback_data(a, mine);
107
return (archive_read_open1(a));
115
111
file_read(struct archive *a, void *client_data, const void **buff)
117
struct read_fd_data *mine = (struct read_fd_data *)client_data;
113
struct read_fd_data *mine = (struct read_fd_data *)client_data;
120
*buff = mine->buffer;
121
bytes_read = read(mine->fd, mine->buffer, mine->block_size);
122
if (bytes_read < 0) {
123
archive_set_error(a, errno, "Error reading fd %d", mine->fd);
116
*buff = mine->buffer;
118
bytes_read = read(mine->fd, mine->buffer, mine->block_size);
119
if (bytes_read < 0) {
122
archive_set_error(a, errno, "Error reading fd %d", mine->fd);
128
#if ARCHIVE_API_VERSION < 2
130
file_skip(struct archive *a, void *client_data, size_t request)
133
file_skip(struct archive *a, void *client_data, off_t request)
129
file_skip(struct archive *a, void *client_data, int64_t request)
136
struct read_fd_data *mine = (struct read_fd_data *)client_data;
137
off_t old_offset, new_offset;
142
/* Reduce request to the next smallest multiple of block_size */
143
request = (request / mine->block_size) * mine->block_size;
148
* Hurray for lazy evaluation: if the first lseek fails, the second
149
* one will not be executed.
151
if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
152
((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
154
/* If seek failed once, it will probably fail again. */
160
* Failure to lseek() can be caused by the file
161
* descriptor pointing to a pipe, socket or FIFO.
162
* Return 0 here, so the compression layer will use
163
* read()s instead to advance the file descriptor.
164
* It's slower of course, but works as well.
169
* There's been an error other than ESPIPE. This is most
170
* likely caused by a programmer error (too large request)
171
* or a corrupted archive file.
173
archive_set_error(a, errno, "Error seeking");
176
return (new_offset - old_offset);
131
struct read_fd_data *mine = (struct read_fd_data *)client_data;
132
off_t skip = (off_t)request;
133
off_t old_offset, new_offset;
134
int skip_bits = sizeof(skip) * 8 - 1; /* off_t is a signed type. */
136
if (!mine->use_lseek)
139
/* Reduce a request that would overflow the 'skip' variable. */
140
if (sizeof(request) > sizeof(skip)) {
142
(((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
143
if (request > max_skip)
147
/* Reduce request to the next smallest multiple of block_size */
148
request = (request / mine->block_size) * mine->block_size;
152
if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0) &&
153
((new_offset = lseek(mine->fd, skip, SEEK_CUR)) >= 0))
154
return (new_offset - old_offset);
156
/* If seek failed once, it will probably fail again. */
159
/* Let libarchive recover with read+discard. */
164
* There's been an error other than ESPIPE. This is most
165
* likely caused by a programmer error (too large request)
166
* or a corrupted archive file.
168
archive_set_error(a, errno, "Error seeking");
180
173
file_close(struct archive *a, void *client_data)
182
struct read_fd_data *mine = (struct read_fd_data *)client_data;
175
struct read_fd_data *mine = (struct read_fd_data *)client_data;
184
(void)a; /* UNUSED */
177
(void)a; /* UNUSED */