Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-1.3.1 / libarchive / archive_read_open_filename.c
blob20509ae5f71df253fdac485ec80f7963f9b4f040
1 /*-
2 * Copyright (c) 2003-2004 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 * 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 #include <sys/stat.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
37 #include "archive.h"
39 struct read_file_data {
40 int fd;
41 size_t block_size;
42 void *buffer;
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);
52 int
53 archive_read_open_file(struct archive *a, const char *filename,
54 size_t block_size)
56 return (archive_read_open_filename(a, filename, block_size));
59 int
60 archive_read_open_filename(struct archive *a, const char *filename,
61 size_t block_size)
63 struct read_file_data *mine;
65 if (filename == NULL || filename[0] == '\0') {
66 mine = malloc(sizeof(*mine));
67 if (mine == NULL) {
68 archive_set_error(a, ENOMEM, "No memory");
69 return (ARCHIVE_FATAL);
71 mine->filename[0] = '\0';
72 } else {
73 mine = malloc(sizeof(*mine) + strlen(filename));
74 if (mine == NULL) {
75 archive_set_error(a, ENOMEM, "No memory");
76 return (ARCHIVE_FATAL);
78 strcpy(mine->filename, filename);
80 mine->block_size = block_size;
81 mine->buffer = NULL;
82 mine->fd = -1;
83 return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
86 static int
87 file_open(struct archive *a, void *client_data)
89 struct read_file_data *mine = client_data;
90 struct stat st;
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);
99 else
100 mine->fd = 0; /* Fake "open" for stdin. */
101 if (mine->fd < 0) {
102 archive_set_error(a, errno, "Failed to open '%s'",
103 mine->filename);
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
109 won't overwrite. */
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;
114 } else {
115 if (mine->filename[0] == '\0')
116 archive_set_error(a, errno, "Can't stat stdin");
117 else
118 archive_set_error(a, errno, "Can't stat '%s'",
119 mine->filename);
120 return (ARCHIVE_FATAL);
122 return (0);
125 static ssize_t
126 file_read(struct archive *a, void *client_data, const void **buff)
128 struct read_file_data *mine = client_data;
129 ssize_t bytes_read;
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");
136 else
137 archive_set_error(a, errno, "Error reading '%s'",
138 mine->filename);
140 return (bytes_read);
143 static ssize_t
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))
158 if (errno == ESPIPE)
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.
167 return (0);
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");
180 else
181 archive_set_error(a, errno, "Error seeking in '%s'",
182 mine->filename);
183 return (-1);
185 return (new_offset - old_offset);
188 static int
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
203 * device nodes.
205 if (!S_ISREG(mine->st_mode)
206 && !S_ISCHR(mine->st_mode)
207 && !S_ISBLK(mine->st_mode)) {
208 ssize_t bytesRead;
209 do {
210 bytesRead = read(mine->fd, mine->buffer,
211 mine->block_size);
212 } while (bytesRead > 0);
214 /* If a named file was opened, then it needs to be closed. */
215 if (mine->filename[0] != '\0')
216 close(mine->fd);
217 if (mine->buffer != NULL)
218 free(mine->buffer);
219 free(mine);
220 return (ARCHIVE_OK);