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 ssize_t	file_read(struct archive *, void *, const void **buff);
65 #if ARCHIVE_API_VERSION < 2
66 static ssize_t	file_skip(struct archive *, void *, size_t request);
67 #else
68 static off_t	file_skip(struct archive *, void *, off_t request);
69 #endif
70 
71 int
72 archive_read_open_file(struct archive *a, const char *filename,
73     size_t block_size)
74 {
75 	return (archive_read_open_filename(a, filename, block_size));
76 }
77 
78 int
79 archive_read_open_filename(struct archive *a, const char *filename,
80     size_t block_size)
81 {
82 	struct stat st;
83 	struct read_file_data *mine;
84 	void *b;
85 	int fd;
86 
87 	if (filename == NULL || filename[0] == '\0') {
88 		/* We used to invoke archive_read_open_fd(a,0,block_size)
89 		 * here, but that doesn't (and shouldn't) handle the
90 		 * end-of-file flush when reading stdout from a pipe.
91 		 * Basically, read_open_fd() is intended for folks who
92 		 * are willing to handle such details themselves.  This
93 		 * API is intended to be a little smarter for folks who
94 		 * want easy handling of the common case.
95 		 */
96 		filename = ""; /* Normalize NULL to "" */
97 		fd = 0;
98 	} else {
99 		fd = open(filename, O_RDONLY | O_BINARY);
100 		if (fd < 0) {
101 			archive_set_error(a, errno,
102 			    "Failed to open '%s'", filename);
103 			return (ARCHIVE_FATAL);
104 		}
105 	}
106 	if (fstat(fd, &st) != 0) {
107 		archive_set_error(a, errno, "Can't stat '%s'", filename);
108 		return (ARCHIVE_FATAL);
109 	}
110 
111 	mine = (struct read_file_data *)calloc(1,
112 	    sizeof(*mine) + strlen(filename));
113 	b = malloc(block_size);
114 	if (mine == NULL || b == NULL) {
115 		archive_set_error(a, ENOMEM, "No memory");
116 		free(mine);
117 		free(b);
118 		return (ARCHIVE_FATAL);
119 	}
120 	strcpy(mine->filename, filename);
121 	mine->block_size = block_size;
122 	mine->buffer = b;
123 	mine->fd = fd;
124 	/* Remember mode so close can decide whether to flush. */
125 	mine->st_mode = st.st_mode;
126 	/* If we're reading a file from disk, ensure that we don't
127 	   overwrite it with an extracted file. */
128 	if (S_ISREG(st.st_mode)) {
129 		archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
130 		/*
131 		 * Enabling skip here is a performance optimization
132 		 * for anything that supports lseek().  On FreeBSD
133 		 * (and probably many other systems), only regular
134 		 * files and raw disk devices support lseek() (on
135 		 * other input types, lseek() returns success but
136 		 * doesn't actually change the file pointer, which
137 		 * just completely screws up the position-tracking
138 		 * logic).  In addition, I've yet to find a portable
139 		 * way to determine if a device is a raw disk device.
140 		 * So I don't see a way to do much better than to only
141 		 * enable this optimization for regular files.
142 		 */
143 		mine->can_skip = 1;
144 	}
145 	return (archive_read_open2(a, mine,
146 		NULL, file_read, file_skip, file_close));
147 }
148 
149 static ssize_t
150 file_read(struct archive *a, void *client_data, const void **buff)
151 {
152 	struct read_file_data *mine = (struct read_file_data *)client_data;
153 	ssize_t bytes_read;
154 
155 	*buff = mine->buffer;
156 	bytes_read = read(mine->fd, mine->buffer, mine->block_size);
157 	if (bytes_read < 0) {
158 		if (mine->filename[0] == '\0')
159 			archive_set_error(a, errno, "Error reading stdin");
160 		else
161 			archive_set_error(a, errno, "Error reading '%s'",
162 			    mine->filename);
163 	}
164 	return (bytes_read);
165 }
166 
167 #if ARCHIVE_API_VERSION < 2
168 static ssize_t
169 file_skip(struct archive *a, void *client_data, size_t request)
170 #else
171 static off_t
172 file_skip(struct archive *a, void *client_data, off_t request)
173 #endif
174 {
175 	struct read_file_data *mine = (struct read_file_data *)client_data;
176 	off_t old_offset, new_offset;
177 
178 	if (!mine->can_skip) /* We can't skip, so ... */
179 		return (0); /* ... skip zero bytes. */
180 
181 	/* Reduce request to the next smallest multiple of block_size */
182 	request = (request / mine->block_size) * mine->block_size;
183 	if (request == 0)
184 		return (0);
185 
186 	/*
187 	 * Hurray for lazy evaluation: if the first lseek fails, the second
188 	 * one will not be executed.
189 	 */
190 	if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
191 	    ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
192 	{
193 		/* If skip failed once, it will probably fail again. */
194 		mine->can_skip = 0;
195 
196 		if (errno == ESPIPE)
197 		{
198 			/*
199 			 * Failure to lseek() can be caused by the file
200 			 * descriptor pointing to a pipe, socket or FIFO.
201 			 * Return 0 here, so the compression layer will use
202 			 * read()s instead to advance the file descriptor.
203 			 * It's slower of course, but works as well.
204 			 */
205 			return (0);
206 		}
207 		/*
208 		 * There's been an error other than ESPIPE. This is most
209 		 * likely caused by a programmer error (too large request)
210 		 * or a corrupted archive file.
211 		 */
212 		if (mine->filename[0] == '\0')
213 			/*
214 			 * Should never get here, since lseek() on stdin ought
215 			 * to return an ESPIPE error.
216 			 */
217 			archive_set_error(a, errno, "Error seeking in stdin");
218 		else
219 			archive_set_error(a, errno, "Error seeking in '%s'",
220 			    mine->filename);
221 		return (-1);
222 	}
223 	return (new_offset - old_offset);
224 }
225 
226 static int
227 file_close(struct archive *a, void *client_data)
228 {
229 	struct read_file_data *mine = (struct read_file_data *)client_data;
230 
231 	(void)a; /* UNUSED */
232 
233 	/* Only flush and close if open succeeded. */
234 	if (mine->fd >= 0) {
235 		/*
236 		 * Sometimes, we should flush the input before closing.
237 		 *   Regular files: faster to just close without flush.
238 		 *   Devices: must not flush (user might need to
239 		 *      read the "next" item on a non-rewind device).
240 		 *   Pipes and sockets:  must flush (otherwise, the
241 		 *      program feeding the pipe or socket may complain).
242 		 * Here, I flush everything except for regular files and
243 		 * device nodes.
244 		 */
245 		if (!S_ISREG(mine->st_mode)
246 		    && !S_ISCHR(mine->st_mode)
247 		    && !S_ISBLK(mine->st_mode)) {
248 			ssize_t bytesRead;
249 			do {
250 				bytesRead = read(mine->fd, mine->buffer,
251 				    mine->block_size);
252 			} while (bytesRead > 0);
253 		}
254 		/* If a named file was opened, then it needs to be closed. */
255 		if (mine->filename[0] != '\0')
256 			close(mine->fd);
257 	}
258 	free(mine->buffer);
259 	free(mine);
260 	return (ARCHIVE_OK);
261 }
262