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: src/lib/libarchive/archive_read_open_filename.c,v 1.21 2008/02/19 06:10:48 kientzle Exp $");
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 int file_open(struct archive *, void *);
65 static ssize_t file_read(struct archive *, void *, const void **buff);
66 #if ARCHIVE_API_VERSION < 2
67 static ssize_t file_skip(struct archive *, void *, size_t request);
69 static off_t file_skip(struct archive *, void *, off_t request);
73 archive_read_open_file(struct archive *a, const char *filename,
76 return (archive_read_open_filename(a, filename, block_size));
80 archive_read_open_filename(struct archive *a, const char *filename,
83 struct read_file_data *mine;
85 if (filename == NULL || filename[0] == '\0') {
86 mine = (struct read_file_data *)malloc(sizeof(*mine));
88 archive_set_error(a, ENOMEM, "No memory");
89 return (ARCHIVE_FATAL);
91 mine->filename[0] = '\0';
93 mine = (struct read_file_data *)malloc(sizeof(*mine) + strlen(filename));
95 archive_set_error(a, ENOMEM, "No memory");
96 return (ARCHIVE_FATAL);
98 strcpy(mine->filename, filename);
100 mine->block_size = block_size;
103 /* lseek() almost never works; disable it by default. See below. */
105 return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
109 file_open(struct archive *a, void *client_data)
111 struct read_file_data *mine = (struct read_file_data *)client_data;
114 mine->buffer = malloc(mine->block_size);
115 if (mine->buffer == NULL) {
116 archive_set_error(a, ENOMEM, "No memory");
117 return (ARCHIVE_FATAL);
119 if (mine->filename[0] != '\0')
120 mine->fd = open(mine->filename, O_RDONLY | O_BINARY);
122 mine->fd = 0; /* Fake "open" for stdin. */
124 archive_set_error(a, errno, "Failed to open '%s'",
126 return (ARCHIVE_FATAL);
128 if (fstat(mine->fd, &st) == 0) {
129 /* If we're reading a file from disk, ensure that we don't
130 overwrite it with an extracted file. */
131 if (S_ISREG(st.st_mode)) {
132 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
134 * Enabling skip here is a performance
135 * optimization for anything that supports
136 * lseek(). On FreeBSD, only regular files
137 * and raw disk devices support lseek() and
138 * there's no portable way to determine if a
139 * device is a raw disk device, so we only
140 * enable this optimization for regular files.
144 /* Remember mode so close can decide whether to flush. */
145 mine->st_mode = st.st_mode;
147 if (mine->filename[0] == '\0')
148 archive_set_error(a, errno, "Can't stat stdin");
150 archive_set_error(a, errno, "Can't stat '%s'",
152 return (ARCHIVE_FATAL);
158 file_read(struct archive *a, void *client_data, const void **buff)
160 struct read_file_data *mine = (struct read_file_data *)client_data;
163 *buff = mine->buffer;
164 bytes_read = read(mine->fd, mine->buffer, mine->block_size);
165 if (bytes_read < 0) {
166 if (mine->filename[0] == '\0')
167 archive_set_error(a, errno, "Error reading stdin");
169 archive_set_error(a, errno, "Error reading '%s'",
175 #if ARCHIVE_API_VERSION < 2
177 file_skip(struct archive *a, void *client_data, size_t request)
180 file_skip(struct archive *a, void *client_data, off_t request)
183 struct read_file_data *mine = (struct read_file_data *)client_data;
184 off_t old_offset, new_offset;
186 if (!mine->can_skip) /* We can't skip, so ... */
187 return (0); /* ... skip zero bytes. */
189 /* Reduce request to the next smallest multiple of block_size */
190 request = (request / mine->block_size) * mine->block_size;
195 * Hurray for lazy evaluation: if the first lseek fails, the second
196 * one will not be executed.
198 if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
199 ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
201 /* If skip failed once, it will probably fail again. */
207 * Failure to lseek() can be caused by the file
208 * descriptor pointing to a pipe, socket or FIFO.
209 * Return 0 here, so the compression layer will use
210 * read()s instead to advance the file descriptor.
211 * It's slower of course, but works as well.
216 * There's been an error other than ESPIPE. This is most
217 * likely caused by a programmer error (too large request)
218 * or a corrupted archive file.
220 if (mine->filename[0] == '\0')
222 * Should never get here, since lseek() on stdin ought
223 * to return an ESPIPE error.
225 archive_set_error(a, errno, "Error seeking in stdin");
227 archive_set_error(a, errno, "Error seeking in '%s'",
231 return (new_offset - old_offset);
235 file_close(struct archive *a, void *client_data)
237 struct read_file_data *mine = (struct read_file_data *)client_data;
239 (void)a; /* UNUSED */
242 * Sometimes, we should flush the input before closing.
243 * Regular files: faster to just close without flush.
244 * Devices: must not flush (user might need to
245 * read the "next" item on a non-rewind device).
246 * Pipes and sockets: must flush (otherwise, the
247 * program feeding the pipe or socket may complain).
248 * Here, I flush everything except for regular files and
251 if (!S_ISREG(mine->st_mode)
252 && !S_ISCHR(mine->st_mode)
253 && !S_ISBLK(mine->st_mode)) {
256 bytesRead = read(mine->fd, mine->buffer,
258 } while (bytesRead > 0);
260 /* If a named file was opened, then it needs to be closed. */
261 if (mine->filename[0] != '\0')
263 if (mine->buffer != NULL)