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$");
29 #ifdef HAVE_SYS_STAT_H
54 static int file_close(struct archive *, void *);
55 static ssize_t file_read(struct archive *, void *, const void **buff);
56 #if ARCHIVE_API_VERSION < 2
57 static ssize_t file_skip(struct archive *, void *, size_t request);
59 static off_t file_skip(struct archive *, void *, off_t request);
63 archive_read_open_fd(struct archive *a, int fd, size_t block_size)
66 struct read_fd_data *mine;
69 archive_clear_error(a);
70 if (fstat(fd, &st) != 0) {
71 archive_set_error(a, errno, "Can't stat fd %d", fd);
72 return (ARCHIVE_FATAL);
75 mine = (struct read_fd_data *)malloc(sizeof(*mine));
76 b = malloc(block_size);
77 if (mine == NULL || b == NULL) {
78 archive_set_error(a, ENOMEM, "No memory");
81 return (ARCHIVE_FATAL);
83 mine->block_size = block_size;
87 * Skip support is a performance optimization for anything
88 * that supports lseek(). On FreeBSD, only regular files and
89 * raw disk devices support lseek() and there's no portable
90 * way to determine if a device is a raw disk device, so we
91 * only enable this optimization for regular files.
93 if (S_ISREG(st.st_mode)) {
94 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
99 return (archive_read_open2(a, mine,
100 NULL, file_read, file_skip, file_close));
104 file_read(struct archive *a, void *client_data, const void **buff)
106 struct read_fd_data *mine = (struct read_fd_data *)client_data;
109 *buff = mine->buffer;
110 bytes_read = read(mine->fd, mine->buffer, mine->block_size);
111 if (bytes_read < 0) {
112 archive_set_error(a, errno, "Error reading fd %d", mine->fd);
117 #if ARCHIVE_API_VERSION < 2
119 file_skip(struct archive *a, void *client_data, size_t request)
122 file_skip(struct archive *a, void *client_data, off_t request)
125 struct read_fd_data *mine = (struct read_fd_data *)client_data;
126 off_t old_offset, new_offset;
131 /* Reduce request to the next smallest multiple of block_size */
132 request = (request / mine->block_size) * mine->block_size;
137 * Hurray for lazy evaluation: if the first lseek fails, the second
138 * one will not be executed.
140 if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
141 ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
143 /* If seek failed once, it will probably fail again. */
149 * Failure to lseek() can be caused by the file
150 * descriptor pointing to a pipe, socket or FIFO.
151 * Return 0 here, so the compression layer will use
152 * read()s instead to advance the file descriptor.
153 * It's slower of course, but works as well.
158 * There's been an error other than ESPIPE. This is most
159 * likely caused by a programmer error (too large request)
160 * or a corrupted archive file.
162 archive_set_error(a, errno, "Error seeking");
165 return (new_offset - old_offset);
169 file_close(struct archive *a, void *client_data)
171 struct read_fd_data *mine = (struct read_fd_data *)client_data;
173 (void)a; /* UNUSED */