1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25 
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 $");
28 
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
47 
48 #include "archive.h"
49 
50 #ifndef O_BINARY
51 #define O_BINARY 0
52 #endif
53 
54 struct read_file_data {
55 	int	 fd;
56 	size_t	 block_size;
57 	void	*buffer;
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! */
61 };
62 
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);
68 #else
69 static off_t	file_skip(struct archive *, void *, off_t request);
70 #endif
71 
72 int
73 archive_read_open_file(struct archive *a, const char *filename,
74     size_t block_size)
75 {
76 	return (archive_read_open_filename(a, filename, block_size));
77 }
78 
79 int
80 archive_read_open_filename(struct archive *a, const char *filename,
81     size_t block_size)
82 {
83 	struct read_file_data *mine;
84 
85 	if (filename == NULL || filename[0] == '\0') {
86 		mine = (struct read_file_data *)malloc(sizeof(*mine));
87 		if (mine == NULL) {
88 			archive_set_error(a, ENOMEM, "No memory");
89 			return (ARCHIVE_FATAL);
90 		}
91 		mine->filename[0] = '\0';
92 	} else {
93 		mine = (struct read_file_data *)malloc(sizeof(*mine) + strlen(filename));
94 		if (mine == NULL) {
95 			archive_set_error(a, ENOMEM, "No memory");
96 			return (ARCHIVE_FATAL);
97 		}
98 		strcpy(mine->filename, filename);
99 	}
100 	mine->block_size = block_size;
101 	mine->buffer = NULL;
102 	mine->fd = -1;
103 	/* lseek() almost never works; disable it by default.  See below. */
104 	mine->can_skip = 0;
105 	return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
106 }
107 
108 static int
109 file_open(struct archive *a, void *client_data)
110 {
111 	struct read_file_data *mine = (struct read_file_data *)client_data;
112 	struct stat st;
113 
114 	mine->buffer = malloc(mine->block_size);
115 	if (mine->buffer == NULL) {
116 		archive_set_error(a, ENOMEM, "No memory");
117 		return (ARCHIVE_FATAL);
118 	}
119 	if (mine->filename[0] != '\0')
120 		mine->fd = open(mine->filename, O_RDONLY | O_BINARY);
121 	else
122 		mine->fd = 0; /* Fake "open" for stdin. */
123 	if (mine->fd < 0) {
124 		archive_set_error(a, errno, "Failed to open '%s'",
125 		    mine->filename);
126 		return (ARCHIVE_FATAL);
127 	}
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);
133 			/*
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.
141 			 */
142 			mine->can_skip = 1;
143 		}
144 		/* Remember mode so close can decide whether to flush. */
145 		mine->st_mode = st.st_mode;
146 	} else {
147 		if (mine->filename[0] == '\0')
148 			archive_set_error(a, errno, "Can't stat stdin");
149 		else
150 			archive_set_error(a, errno, "Can't stat '%s'",
151 			    mine->filename);
152 		return (ARCHIVE_FATAL);
153 	}
154 	return (0);
155 }
156 
157 static ssize_t
158 file_read(struct archive *a, void *client_data, const void **buff)
159 {
160 	struct read_file_data *mine = (struct read_file_data *)client_data;
161 	ssize_t bytes_read;
162 
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");
168 		else
169 			archive_set_error(a, errno, "Error reading '%s'",
170 			    mine->filename);
171 	}
172 	return (bytes_read);
173 }
174 
175 #if ARCHIVE_API_VERSION < 2
176 static ssize_t
177 file_skip(struct archive *a, void *client_data, size_t request)
178 #else
179 static off_t
180 file_skip(struct archive *a, void *client_data, off_t request)
181 #endif
182 {
183 	struct read_file_data *mine = (struct read_file_data *)client_data;
184 	off_t old_offset, new_offset;
185 
186 	if (!mine->can_skip) /* We can't skip, so ... */
187 		return (0); /* ... skip zero bytes. */
188 
189 	/* Reduce request to the next smallest multiple of block_size */
190 	request = (request / mine->block_size) * mine->block_size;
191 	if (request == 0)
192 		return (0);
193 
194 	/*
195 	 * Hurray for lazy evaluation: if the first lseek fails, the second
196 	 * one will not be executed.
197 	 */
198 	if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
199 	    ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
200 	{
201 		/* If skip failed once, it will probably fail again. */
202 		mine->can_skip = 0;
203 
204 		if (errno == ESPIPE)
205 		{
206 			/*
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.
212 			 */
213 			return (0);
214 		}
215 		/*
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.
219 		 */
220 		if (mine->filename[0] == '\0')
221 			/*
222 			 * Should never get here, since lseek() on stdin ought
223 			 * to return an ESPIPE error.
224 			 */
225 			archive_set_error(a, errno, "Error seeking in stdin");
226 		else
227 			archive_set_error(a, errno, "Error seeking in '%s'",
228 			    mine->filename);
229 		return (-1);
230 	}
231 	return (new_offset - old_offset);
232 }
233 
234 static int
235 file_close(struct archive *a, void *client_data)
236 {
237 	struct read_file_data *mine = (struct read_file_data *)client_data;
238 
239 	(void)a; /* UNUSED */
240 
241 	/*
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
249 	 * device nodes.
250 	 */
251 	if (!S_ISREG(mine->st_mode)
252 	    && !S_ISCHR(mine->st_mode)
253 	    && !S_ISBLK(mine->st_mode)) {
254 		ssize_t bytesRead;
255 		do {
256 			bytesRead = read(mine->fd, mine->buffer,
257 			    mine->block_size);
258 		} while (bytesRead > 0);
259 	}
260 	/* If a named file was opened, then it needs to be closed. */
261 	if (mine->filename[0] != '\0')
262 		close(mine->fd);
263 	if (mine->buffer != NULL)
264 		free(mine->buffer);
265 	free(mine);
266 	return (ARCHIVE_OK);
267 }
268