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