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 struct read_file_data {
58 mode_t st_mode; /* Mode bits for opened file. */
59 char can_skip; /* This file supports skipping. */
60 char filename[1]; /* Must be last! */
63 static int file_close(struct archive *, void *);
64 static ssize_t file_read(struct archive *, void *, const void **buff);
65 #if ARCHIVE_API_VERSION < 2
66 static ssize_t file_skip(struct archive *, void *, size_t request);
68 static off_t file_skip(struct archive *, void *, off_t request);
72 archive_read_open_file(struct archive *a, const char *filename,
75 return (archive_read_open_filename(a, filename, block_size));
79 archive_read_open_filename(struct archive *a, const char *filename,
83 struct read_file_data *mine;
87 archive_clear_error(a);
88 if (filename == NULL || filename[0] == '\0') {
89 /* We used to invoke archive_read_open_fd(a,0,block_size)
90 * here, but that doesn't (and shouldn't) handle the
91 * end-of-file flush when reading stdout from a pipe.
92 * Basically, read_open_fd() is intended for folks who
93 * are willing to handle such details themselves. This
94 * API is intended to be a little smarter for folks who
95 * want easy handling of the common case.
97 filename = ""; /* Normalize NULL to "" */
100 fd = open(filename, O_RDONLY | O_BINARY);
102 archive_set_error(a, errno,
103 "Failed to open '%s'", filename);
104 return (ARCHIVE_FATAL);
107 if (fstat(fd, &st) != 0) {
108 archive_set_error(a, errno, "Can't stat '%s'", filename);
109 return (ARCHIVE_FATAL);
112 mine = (struct read_file_data *)calloc(1,
113 sizeof(*mine) + strlen(filename));
114 b = malloc(block_size);
115 if (mine == NULL || b == NULL) {
116 archive_set_error(a, ENOMEM, "No memory");
119 return (ARCHIVE_FATAL);
121 strcpy(mine->filename, filename);
122 mine->block_size = block_size;
125 /* Remember mode so close can decide whether to flush. */
126 mine->st_mode = st.st_mode;
127 /* If we're reading a file from disk, ensure that we don't
128 overwrite it with an extracted file. */
129 if (S_ISREG(st.st_mode)) {
130 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
132 * Enabling skip here is a performance optimization
133 * for anything that supports lseek(). On FreeBSD
134 * (and probably many other systems), only regular
135 * files and raw disk devices support lseek() (on
136 * other input types, lseek() returns success but
137 * doesn't actually change the file pointer, which
138 * just completely screws up the position-tracking
139 * logic). In addition, I've yet to find a portable
140 * way to determine if a device is a raw disk device.
141 * So I don't see a way to do much better than to only
142 * enable this optimization for regular files.
146 return (archive_read_open2(a, mine,
147 NULL, file_read, file_skip, file_close));
151 file_read(struct archive *a, void *client_data, const void **buff)
153 struct read_file_data *mine = (struct read_file_data *)client_data;
156 *buff = mine->buffer;
157 bytes_read = read(mine->fd, mine->buffer, mine->block_size);
158 if (bytes_read < 0) {
159 if (mine->filename[0] == '\0')
160 archive_set_error(a, errno, "Error reading stdin");
162 archive_set_error(a, errno, "Error reading '%s'",
168 #if ARCHIVE_API_VERSION < 2
170 file_skip(struct archive *a, void *client_data, size_t request)
173 file_skip(struct archive *a, void *client_data, off_t request)
176 struct read_file_data *mine = (struct read_file_data *)client_data;
177 off_t old_offset, new_offset;
179 if (!mine->can_skip) /* We can't skip, so ... */
180 return (0); /* ... skip zero bytes. */
182 /* Reduce request to the next smallest multiple of block_size */
183 request = (request / mine->block_size) * mine->block_size;
188 * Hurray for lazy evaluation: if the first lseek fails, the second
189 * one will not be executed.
191 if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
192 ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
194 /* If skip failed once, it will probably fail again. */
200 * Failure to lseek() can be caused by the file
201 * descriptor pointing to a pipe, socket or FIFO.
202 * Return 0 here, so the compression layer will use
203 * read()s instead to advance the file descriptor.
204 * It's slower of course, but works as well.
209 * There's been an error other than ESPIPE. This is most
210 * likely caused by a programmer error (too large request)
211 * or a corrupted archive file.
213 if (mine->filename[0] == '\0')
215 * Should never get here, since lseek() on stdin ought
216 * to return an ESPIPE error.
218 archive_set_error(a, errno, "Error seeking in stdin");
220 archive_set_error(a, errno, "Error seeking in '%s'",
224 return (new_offset - old_offset);
228 file_close(struct archive *a, void *client_data)
230 struct read_file_data *mine = (struct read_file_data *)client_data;
232 (void)a; /* UNUSED */
234 /* Only flush and close if open succeeded. */
237 * Sometimes, we should flush the input before closing.
238 * Regular files: faster to just close without flush.
239 * Devices: must not flush (user might need to
240 * read the "next" item on a non-rewind device).
241 * Pipes and sockets: must flush (otherwise, the
242 * program feeding the pipe or socket may complain).
243 * Here, I flush everything except for regular files and
246 if (!S_ISREG(mine->st_mode)
247 && !S_ISCHR(mine->st_mode)
248 && !S_ISBLK(mine->st_mode)) {
251 bytesRead = read(mine->fd, mine->buffer,
253 } while (bytesRead > 0);
255 /* If a named file was opened, then it needs to be closed. */
256 if (mine->filename[0] != '\0')