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$");
39 struct read_file_data {
43 mode_t st_mode; /* Mode bits for opened file. */
44 char filename[1]; /* Must be last! */
47 static int file_close(struct archive *, void *);
48 static int file_open(struct archive *, void *);
49 static ssize_t file_read(struct archive *, void *, const void **buff);
50 static ssize_t file_skip(struct archive *, void *, size_t request);
53 archive_read_open_file(struct archive *a, const char *filename,
56 return (archive_read_open_filename(a, filename, block_size));
60 archive_read_open_filename(struct archive *a, const char *filename,
63 struct read_file_data *mine;
65 if (filename == NULL || filename[0] == '\0') {
66 mine = malloc(sizeof(*mine));
68 archive_set_error(a, ENOMEM, "No memory");
69 return (ARCHIVE_FATAL);
71 mine->filename[0] = '\0';
73 mine = malloc(sizeof(*mine) + strlen(filename));
75 archive_set_error(a, ENOMEM, "No memory");
76 return (ARCHIVE_FATAL);
78 strcpy(mine->filename, filename);
80 mine->block_size = block_size;
83 return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
87 file_open(struct archive *a, void *client_data)
89 struct read_file_data *mine = client_data;
92 mine->buffer = malloc(mine->block_size);
93 if (mine->buffer == NULL) {
94 archive_set_error(a, ENOMEM, "No memory");
95 return (ARCHIVE_FATAL);
97 if (mine->filename[0] != '\0')
98 mine->fd = open(mine->filename, O_RDONLY);
100 mine->fd = 0; /* Fake "open" for stdin. */
102 archive_set_error(a, errno, "Failed to open '%s'",
104 return (ARCHIVE_FATAL);
106 if (fstat(mine->fd, &st) == 0) {
107 if (S_ISREG(st.st_mode)) {
108 /* Set dev/ino of archive file so extract
110 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
112 /* Remember mode so close can decide whether to flush. */
113 mine->st_mode = st.st_mode;
115 if (mine->filename[0] == '\0')
116 archive_set_error(a, errno, "Can't stat stdin");
118 archive_set_error(a, errno, "Can't stat '%s'",
120 return (ARCHIVE_FATAL);
126 file_read(struct archive *a, void *client_data, const void **buff)
128 struct read_file_data *mine = client_data;
131 *buff = mine->buffer;
132 bytes_read = read(mine->fd, mine->buffer, mine->block_size);
133 if (bytes_read < 0) {
134 if (mine->filename[0] == '\0')
135 archive_set_error(a, errno, "Error reading stdin");
137 archive_set_error(a, errno, "Error reading '%s'",
144 file_skip(struct archive *a, void *client_data, size_t request)
146 struct read_file_data *mine = client_data;
147 off_t old_offset, new_offset;
149 /* Reduce request to the next smallest multiple of block_size */
150 request = (request / mine->block_size) * mine->block_size;
152 * Hurray for lazy evaluation: if the first lseek fails, the second
153 * one will not be executed.
155 if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
156 ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
161 * Failure to lseek() can be caused by the file
162 * descriptor pointing to a pipe, socket or FIFO.
163 * Return 0 here, so the compression layer will use
164 * read()s instead to advance the file descriptor.
165 * It's slower of course, but works as well.
170 * There's been an error other than ESPIPE. This is most
171 * likely caused by a programmer error (too large request)
172 * or a corrupted archive file.
174 if (mine->filename[0] == '\0')
176 * Should never get here, since lseek() on stdin ought
177 * to return an ESPIPE error.
179 archive_set_error(a, errno, "Error seeking in stdin");
181 archive_set_error(a, errno, "Error seeking in '%s'",
185 return (new_offset - old_offset);
189 file_close(struct archive *a, void *client_data)
191 struct read_file_data *mine = client_data;
193 (void)a; /* UNUSED */
196 * Sometimes, we should flush the input before closing.
197 * Regular files: faster to just close without flush.
198 * Devices: must not flush (user might need to
199 * read the "next" item on a non-rewind device).
200 * Pipes and sockets: must flush (otherwise, the
201 * program feeding the pipe or socket may complain).
202 * Here, I flush everything except for regular files and
205 if (!S_ISREG(mine->st_mode)
206 && !S_ISCHR(mine->st_mode)
207 && !S_ISBLK(mine->st_mode)) {
210 bytesRead = read(mine->fd, mine->buffer,
212 } while (bytesRead > 0);
214 /* If a named file was opened, then it needs to be closed. */
215 if (mine->filename[0] != '\0')
217 if (mine->buffer != NULL)