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_fd.c 201103 2009-12-28 03:13:49Z 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 struct read_fd_data {
54 	int	 fd;
55 	size_t	 block_size;
56 	char	 can_skip;
57 	void	*buffer;
58 };
59 
60 static int	file_close(struct archive *, void *);
61 static ssize_t	file_read(struct archive *, void *, const void **buff);
62 #if ARCHIVE_API_VERSION < 2
63 static ssize_t	file_skip(struct archive *, void *, size_t request);
64 #else
65 static off_t	file_skip(struct archive *, void *, off_t request);
66 #endif
67 
68 int
69 archive_read_open_fd(struct archive *a, int fd, size_t block_size)
70 {
71 	struct stat st;
72 	struct read_fd_data *mine;
73 	void *b;
74 
75 	archive_clear_error(a);
76 	if (fstat(fd, &st) != 0) {
77 		archive_set_error(a, errno, "Can't stat fd %d", fd);
78 		return (ARCHIVE_FATAL);
79 	}
80 
81 	mine = (struct read_fd_data *)malloc(sizeof(*mine));
82 	b = malloc(block_size);
83 	if (mine == NULL || b == NULL) {
84 		archive_set_error(a, ENOMEM, "No memory");
85 		free(mine);
86 		free(b);
87 		return (ARCHIVE_FATAL);
88 	}
89 	mine->block_size = block_size;
90 	mine->buffer = b;
91 	mine->fd = fd;
92 	/*
93 	 * Skip support is a performance optimization for anything
94 	 * that supports lseek().  On FreeBSD, only regular files and
95 	 * raw disk devices support lseek() and there's no portable
96 	 * way to determine if a device is a raw disk device, so we
97 	 * only enable this optimization for regular files.
98 	 */
99 	if (S_ISREG(st.st_mode)) {
100 		archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
101 		mine->can_skip = 1;
102 	} else
103 		mine->can_skip = 0;
104 #if defined(__CYGWIN__) || defined(_WIN32)
105 	setmode(mine->fd, O_BINARY);
106 #endif
107 
108 	return (archive_read_open2(a, mine,
109 		NULL, file_read, file_skip, file_close));
110 }
111 
112 static ssize_t
113 file_read(struct archive *a, void *client_data, const void **buff)
114 {
115 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
116 	ssize_t bytes_read;
117 
118 	*buff = mine->buffer;
119 	for (;;) {
120 		bytes_read = read(mine->fd, mine->buffer, mine->block_size);
121 		if (bytes_read < 0) {
122 			if (errno == EINTR)
123 				continue;
124 			archive_set_error(a, errno, "Error reading fd %d", mine->fd);
125 		}
126 		return (bytes_read);
127 	}
128 }
129 
130 #if ARCHIVE_API_VERSION < 2
131 static ssize_t
132 file_skip(struct archive *a, void *client_data, size_t request)
133 #else
134 static off_t
135 file_skip(struct archive *a, void *client_data, off_t request)
136 #endif
137 {
138 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
139 	off_t old_offset, new_offset;
140 
141 	if (!mine->can_skip)
142 		return (0);
143 
144 	/* Reduce request to the next smallest multiple of block_size */
145 	request = (request / mine->block_size) * mine->block_size;
146 	if (request == 0)
147 		return (0);
148 
149 	/*
150 	 * Hurray for lazy evaluation: if the first lseek fails, the second
151 	 * one will not be executed.
152 	 */
153 	if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
154 	    ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
155 	{
156 		/* If seek failed once, it will probably fail again. */
157 		mine->can_skip = 0;
158 
159 		if (errno == ESPIPE)
160 		{
161 			/*
162 			 * Failure to lseek() can be caused by the file
163 			 * descriptor pointing to a pipe, socket or FIFO.
164 			 * Return 0 here, so the compression layer will use
165 			 * read()s instead to advance the file descriptor.
166 			 * It's slower of course, but works as well.
167 			 */
168 			return (0);
169 		}
170 		/*
171 		 * There's been an error other than ESPIPE. This is most
172 		 * likely caused by a programmer error (too large request)
173 		 * or a corrupted archive file.
174 		 */
175 		archive_set_error(a, errno, "Error seeking");
176 		return (-1);
177 	}
178 	return (new_offset - old_offset);
179 }
180 
181 static int
182 file_close(struct archive *a, void *client_data)
183 {
184 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
185 
186 	(void)a; /* UNUSED */
187 	free(mine->buffer);
188 	free(mine);
189 	return (ARCHIVE_OK);
190 }
191