2 * Copyright (c) 2003-2004 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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
30 #ifdef HAVE_SYS_STAT_H
51 struct read_file_data {
55 mode_t st_mode; /* Mode bits for opened file. */
56 char filename[1]; /* Must be last! */
59 static int file_close(struct archive *, void *);
60 static int file_open(struct archive *, void *);
61 static ssize_t file_read(struct archive *, void *, const void **buff);
62 static ssize_t file_skip(struct archive *, void *, size_t request);
65 archive_read_open_file(struct archive *a, const char *filename,
68 struct read_file_data *mine;
70 if (filename == NULL || filename[0] == '\0') {
71 mine = (struct read_file_data *)malloc(sizeof(*mine));
73 archive_set_error(a, ENOMEM, "No memory");
74 return (ARCHIVE_FATAL);
76 mine->filename[0] = '\0';
78 mine = (struct read_file_data *)malloc(sizeof(*mine) + strlen(filename));
80 archive_set_error(a, ENOMEM, "No memory");
81 return (ARCHIVE_FATAL);
83 strcpy(mine->filename, filename);
85 mine->block_size = block_size;
88 return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
92 file_open(struct archive *a, void *client_data)
94 struct read_file_data *mine = (struct read_file_data *)client_data;
97 mine->buffer = malloc(mine->block_size);
98 if (mine->buffer == NULL) {
99 archive_set_error(a, ENOMEM, "No memory");
100 return (ARCHIVE_FATAL);
102 if (mine->filename[0] != '\0')
103 mine->fd = open(mine->filename, O_RDONLY);
105 mine->fd = 0; /* Fake "open" for stdin. */
107 archive_set_error(a, errno, "Failed to open '%s'",
109 return (ARCHIVE_FATAL);
111 if (fstat(mine->fd, &st) == 0) {
112 /* If we're reading a file from disk, ensure that we don't
113 overwrite it with an extracted file. */
114 if (S_ISREG(st.st_mode))
115 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
116 /* Remember mode so close can decide whether to flush. */
117 mine->st_mode = st.st_mode;
119 if (mine->filename[0] == '\0')
120 archive_set_error(a, errno, "Can't stat stdin");
122 archive_set_error(a, errno, "Can't stat '%s'",
124 return (ARCHIVE_FATAL);
130 file_read(struct archive *a, void *client_data, const void **buff)
132 struct read_file_data *mine = (struct read_file_data *)client_data;
135 *buff = mine->buffer;
136 bytes_read = read(mine->fd, mine->buffer, mine->block_size);
137 if (bytes_read < 0) {
138 if (mine->filename[0] == '\0')
139 archive_set_error(a, errno, "Error reading stdin");
141 archive_set_error(a, errno, "Error reading '%s'",
148 file_skip(struct archive *a, void *client_data, size_t request)
150 struct read_file_data *mine = (struct read_file_data *)client_data;
151 off_t old_offset, new_offset;
153 /* Reduce request to the next smallest multiple of block_size */
154 request = (request / mine->block_size) * mine->block_size;
156 * Hurray for lazy evaluation: if the first lseek fails, the second
157 * one will not be executed.
159 if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
160 ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
165 * Failure to lseek() can be caused by the file
166 * descriptor pointing to a pipe, socket or FIFO.
167 * Return 0 here, so the compression layer will use
168 * read()s instead to advance the file descriptor.
169 * It's slower of course, but works as well.
174 * There's been an error other than ESPIPE. This is most
175 * likely caused by a programmer error (too large request)
176 * or a corrupted archive file.
178 if (mine->filename[0] == '\0')
180 * Should never get here, since lseek() on stdin ought
181 * to return an ESPIPE error.
183 archive_set_error(a, errno, "Error seeking in stdin");
185 archive_set_error(a, errno, "Error seeking in '%s'",
189 return (new_offset - old_offset);
193 file_close(struct archive *a, void *client_data)
195 struct read_file_data *mine = (struct read_file_data *)client_data;
197 (void)a; /* UNUSED */
200 * Sometimes, we should flush the input before closing.
201 * Regular files: faster to just close without flush.
202 * Devices: must not flush (user might need to
203 * read the "next" item on a non-rewind device).
204 * Pipes and sockets: must flush (otherwise, the
205 * program feeding the pipe or socket may complain).
206 * Here, I flush everything except for regular files and
209 if (!S_ISREG(mine->st_mode)
210 && !S_ISCHR(mine->st_mode)
211 && !S_ISBLK(mine->st_mode)) {
214 bytesRead = read(mine->fd, mine->buffer,
216 } while (bytesRead > 0);
218 /* If a named file was opened, then it needs to be closed. */
219 if (mine->filename[0] != '\0')
221 if (mine->buffer != NULL)