Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-2.0 / libarchive / archive_read_open_filename.c
blob47bc4efaa53c785d8c8c7323429e9f68ad613687
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.18 2007/01/09 08:05:55 kientzle Exp $");
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35 #ifdef HAVE_FCNTL_H
36 #include <fcntl.h>
37 #endif
38 #ifdef HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #endif
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
48 #include "archive.h"
50 struct read_file_data {
51 int fd;
52 size_t block_size;
53 void *buffer;
54 mode_t st_mode; /* Mode bits for opened file. */
55 char filename[1]; /* Must be last! */
58 static int file_close(struct archive *, void *);
59 static int file_open(struct archive *, void *);
60 static ssize_t file_read(struct archive *, void *, const void **buff);
61 #if ARCHIVE_API_VERSION < 2
62 static ssize_t file_skip(struct archive *, void *, size_t request);
63 #else
64 static off_t file_skip(struct archive *, void *, off_t request);
65 #endif
67 int
68 archive_read_open_file(struct archive *a, const char *filename,
69 size_t block_size)
71 return (archive_read_open_filename(a, filename, block_size));
74 int
75 archive_read_open_filename(struct archive *a, const char *filename,
76 size_t block_size)
78 struct read_file_data *mine;
80 if (filename == NULL || filename[0] == '\0') {
81 mine = (struct read_file_data *)malloc(sizeof(*mine));
82 if (mine == NULL) {
83 archive_set_error(a, ENOMEM, "No memory");
84 return (ARCHIVE_FATAL);
86 mine->filename[0] = '\0';
87 } else {
88 mine = (struct read_file_data *)malloc(sizeof(*mine) + strlen(filename));
89 if (mine == NULL) {
90 archive_set_error(a, ENOMEM, "No memory");
91 return (ARCHIVE_FATAL);
93 strcpy(mine->filename, filename);
95 mine->block_size = block_size;
96 mine->buffer = NULL;
97 mine->fd = -1;
98 return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
101 static int
102 file_open(struct archive *a, void *client_data)
104 struct read_file_data *mine = (struct read_file_data *)client_data;
105 struct stat st;
107 mine->buffer = malloc(mine->block_size);
108 if (mine->buffer == NULL) {
109 archive_set_error(a, ENOMEM, "No memory");
110 return (ARCHIVE_FATAL);
112 if (mine->filename[0] != '\0')
113 mine->fd = open(mine->filename, O_RDONLY);
114 else
115 mine->fd = 0; /* Fake "open" for stdin. */
116 if (mine->fd < 0) {
117 archive_set_error(a, errno, "Failed to open '%s'",
118 mine->filename);
119 return (ARCHIVE_FATAL);
121 if (fstat(mine->fd, &st) == 0) {
122 /* If we're reading a file from disk, ensure that we don't
123 overwrite it with an extracted file. */
124 if (S_ISREG(st.st_mode))
125 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
126 /* Remember mode so close can decide whether to flush. */
127 mine->st_mode = st.st_mode;
128 } else {
129 if (mine->filename[0] == '\0')
130 archive_set_error(a, errno, "Can't stat stdin");
131 else
132 archive_set_error(a, errno, "Can't stat '%s'",
133 mine->filename);
134 return (ARCHIVE_FATAL);
136 return (0);
139 static ssize_t
140 file_read(struct archive *a, void *client_data, const void **buff)
142 struct read_file_data *mine = (struct read_file_data *)client_data;
143 ssize_t bytes_read;
145 *buff = mine->buffer;
146 bytes_read = read(mine->fd, mine->buffer, mine->block_size);
147 if (bytes_read < 0) {
148 if (mine->filename[0] == '\0')
149 archive_set_error(a, errno, "Error reading stdin");
150 else
151 archive_set_error(a, errno, "Error reading '%s'",
152 mine->filename);
154 return (bytes_read);
157 #if ARCHIVE_API_VERSION < 2
158 static ssize_t
159 file_skip(struct archive *a, void *client_data, size_t request)
160 #else
161 static off_t
162 file_skip(struct archive *a, void *client_data, off_t request)
163 #endif
165 struct read_file_data *mine = (struct read_file_data *)client_data;
166 off_t old_offset, new_offset;
168 /* Reduce request to the next smallest multiple of block_size */
169 request = (request / mine->block_size) * mine->block_size;
171 * Hurray for lazy evaluation: if the first lseek fails, the second
172 * one will not be executed.
174 if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
175 ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
177 if (errno == ESPIPE)
180 * Failure to lseek() can be caused by the file
181 * descriptor pointing to a pipe, socket or FIFO.
182 * Return 0 here, so the compression layer will use
183 * read()s instead to advance the file descriptor.
184 * It's slower of course, but works as well.
186 return (0);
189 * There's been an error other than ESPIPE. This is most
190 * likely caused by a programmer error (too large request)
191 * or a corrupted archive file.
193 if (mine->filename[0] == '\0')
195 * Should never get here, since lseek() on stdin ought
196 * to return an ESPIPE error.
198 archive_set_error(a, errno, "Error seeking in stdin");
199 else
200 archive_set_error(a, errno, "Error seeking in '%s'",
201 mine->filename);
202 return (-1);
204 return (new_offset - old_offset);
207 static int
208 file_close(struct archive *a, void *client_data)
210 struct read_file_data *mine = (struct read_file_data *)client_data;
212 (void)a; /* UNUSED */
215 * Sometimes, we should flush the input before closing.
216 * Regular files: faster to just close without flush.
217 * Devices: must not flush (user might need to
218 * read the "next" item on a non-rewind device).
219 * Pipes and sockets: must flush (otherwise, the
220 * program feeding the pipe or socket may complain).
221 * Here, I flush everything except for regular files and
222 * device nodes.
224 if (!S_ISREG(mine->st_mode)
225 && !S_ISCHR(mine->st_mode)
226 && !S_ISBLK(mine->st_mode)) {
227 ssize_t bytesRead;
228 do {
229 bytesRead = read(mine->fd, mine->buffer,
230 mine->block_size);
231 } while (bytesRead > 0);
233 /* If a named file was opened, then it needs to be closed. */
234 if (mine->filename[0] != '\0')
235 close(mine->fd);
236 if (mine->buffer != NULL)
237 free(mine->buffer);
238 free(mine);
239 return (ARCHIVE_OK);