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_fd.c 201103 2009-12-28 03:13:49Z kientzle $");
29 #ifdef HAVE_SYS_STAT_H
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_seek(struct archive *, void *, int64_t request, int);
63 static int64_t file_skip(struct archive *, void *, int64_t request);
66 archive_read_open_fd(struct archive *a, int fd, size_t block_size)
69 struct read_fd_data *mine;
72 archive_clear_error(a);
73 if (fstat(fd, &st) != 0) {
74 archive_set_error(a, errno, "Can't stat fd %d", fd);
75 return (ARCHIVE_FATAL);
78 mine = (struct read_fd_data *)calloc(1, sizeof(*mine));
79 b = malloc(block_size);
80 if (mine == NULL || b == NULL) {
81 archive_set_error(a, ENOMEM, "No memory");
84 return (ARCHIVE_FATAL);
86 mine->block_size = block_size;
90 * Skip support is a performance optimization for anything
91 * that supports lseek(). On FreeBSD, only regular files and
92 * raw disk devices support lseek() and there's no portable
93 * way to determine if a device is a raw disk device, so we
94 * only enable this optimization for regular files.
96 if (S_ISREG(st.st_mode)) {
97 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
100 #if defined(__CYGWIN__) || defined(_WIN32)
101 setmode(mine->fd, O_BINARY);
104 archive_read_set_read_callback(a, file_read);
105 archive_read_set_skip_callback(a, file_skip);
106 archive_read_set_seek_callback(a, file_seek);
107 archive_read_set_close_callback(a, file_close);
108 archive_read_set_callback_data(a, mine);
109 return (archive_read_open1(a));
113 file_read(struct archive *a, void *client_data, const void **buff)
115 struct read_fd_data *mine = (struct read_fd_data *)client_data;
118 *buff = mine->buffer;
120 bytes_read = read(mine->fd, mine->buffer, mine->block_size);
121 if (bytes_read < 0) {
124 archive_set_error(a, errno, "Error reading fd %d",
132 file_skip(struct archive *a, void *client_data, int64_t request)
134 struct read_fd_data *mine = (struct read_fd_data *)client_data;
135 int64_t skip = request;
136 int64_t old_offset, new_offset;
137 int skip_bits = sizeof(skip) * 8 - 1; /* off_t is a signed type. */
139 if (!mine->use_lseek)
142 /* Reduce a request that would overflow the 'skip' variable. */
143 if (sizeof(request) > sizeof(skip)) {
145 (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
146 if (request > max_skip)
150 /* Reduce request to the next smallest multiple of block_size */
151 request = (request / mine->block_size) * mine->block_size;
155 if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0) &&
156 ((new_offset = lseek(mine->fd, skip, SEEK_CUR)) >= 0))
157 return (new_offset - old_offset);
159 /* If seek failed once, it will probably fail again. */
162 /* Let libarchive recover with read+discard. */
167 * There's been an error other than ESPIPE. This is most
168 * likely caused by a programmer error (too large request)
169 * or a corrupted archive file.
171 archive_set_error(a, errno, "Error seeking");
176 * TODO: Store the offset and use it in the read callback.
179 file_seek(struct archive *a, void *client_data, int64_t request, int whence)
181 struct read_fd_data *mine = (struct read_fd_data *)client_data;
184 /* We use off_t here because lseek() is declared that way. */
185 /* See above for notes about when off_t is less than 64 bits. */
186 r = lseek(mine->fd, request, whence);
190 if (errno == ESPIPE) {
191 archive_set_error(a, errno,
192 "A file descriptor(%d) is not seekable(PIPE)", mine->fd);
193 return (ARCHIVE_FAILED);
195 /* If the input is corrupted or truncated, fail. */
196 archive_set_error(a, errno,
197 "Error seeking in a file descriptor(%d)", mine->fd);
198 return (ARCHIVE_FATAL);
203 file_close(struct archive *a, void *client_data)
205 struct read_fd_data *mine = (struct read_fd_data *)client_data;
207 (void)a; /* UNUSED */