xref: /freebsd/contrib/libarchive/libarchive/archive_read_open_fd.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
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 
28 #ifdef HAVE_SYS_STAT_H
29 #include <sys/stat.h>
30 #endif
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37 #ifdef HAVE_IO_H
38 #include <io.h>
39 #endif
40 #ifdef HAVE_STDLIB_H
41 #include <stdlib.h>
42 #endif
43 #ifdef HAVE_STRING_H
44 #include <string.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 
50 #include "archive.h"
51 
52 struct read_fd_data {
53 	int	 fd;
54 	size_t	 block_size;
55 	int64_t	 size;
56 	char	 use_lseek;
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 static int64_t	file_seek(struct archive *, void *, int64_t request, int);
63 static int64_t	file_skip(struct archive *, void *, int64_t request);
64 
65 int
archive_read_open_fd(struct archive * a,int fd,size_t block_size)66 archive_read_open_fd(struct archive *a, int fd, size_t block_size)
67 {
68 	struct stat st;
69 	struct read_fd_data *mine;
70 	void *b;
71 
72 	archive_clear_error(a);
73 	if (fstat(fd, &st) != 0) {
74 		archive_set_error(a, errno, "Can't stat fd %d", fd);
75 		return (ARCHIVE_FATAL);
76 	}
77 
78 	mine = calloc(1, sizeof(*mine));
79 	b = malloc(block_size);
80 	if (mine == NULL || b == NULL) {
81 		archive_set_error(a, ENOMEM, "No memory");
82 		free(mine);
83 		free(b);
84 		return (ARCHIVE_FATAL);
85 	}
86 	mine->block_size = block_size;
87 	mine->buffer = b;
88 	mine->fd = fd;
89 	/*
90 	 * Skip support is a performance optimization for anything
91 	 * that supports lseek().  On FreeBSD, only regular files and
92 	 * raw disk devices support lseek() and there's no portable
93 	 * way to determine if a device is a raw disk device, so we
94 	 * only enable this optimization for regular files.
95 	 */
96 	if (S_ISREG(st.st_mode)) {
97 		archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
98 		mine->use_lseek = 1;
99 		mine->size = st.st_size;
100 	}
101 #if defined(__CYGWIN__) || defined(_WIN32)
102 	setmode(mine->fd, O_BINARY);
103 #endif
104 
105 	archive_read_set_read_callback(a, file_read);
106 	archive_read_set_skip_callback(a, file_skip);
107 	archive_read_set_seek_callback(a, file_seek);
108 	archive_read_set_close_callback(a, file_close);
109 	archive_read_set_callback_data(a, mine);
110 	return (archive_read_open1(a));
111 }
112 
113 static ssize_t
file_read(struct archive * a,void * client_data,const void ** buff)114 file_read(struct archive *a, void *client_data, const void **buff)
115 {
116 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
117 	ssize_t bytes_read;
118 
119 	*buff = mine->buffer;
120 	for (;;) {
121 		bytes_read = read(mine->fd, mine->buffer, mine->block_size);
122 		if (bytes_read < 0) {
123 			if (errno == EINTR)
124 				continue;
125 			archive_set_error(a, errno, "Error reading fd %d",
126 			    mine->fd);
127 		}
128 		return (bytes_read);
129 	}
130 }
131 
132 static int64_t
file_skip(struct archive * a,void * client_data,int64_t request)133 file_skip(struct archive *a, void *client_data, int64_t request)
134 {
135 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
136 	off_t skip = (off_t)request;
137 	int64_t old_offset, new_offset;
138 	int skip_bits = sizeof(skip) * 8 - 1;  /* off_t is a signed type. */
139 
140 	if (!mine->use_lseek)
141 		return (0);
142 
143 	/* Reduce a request that would overflow the 'skip' variable. */
144 	if (sizeof(request) > sizeof(skip)) {
145 		const int64_t max_skip =
146 		    (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
147 		if (request > max_skip)
148 			skip = (off_t)max_skip;
149 	}
150 
151 	/* Reduce 'skip' to the next smallest multiple of block_size */
152 	skip = (off_t)(((int64_t)skip / mine->block_size) * mine->block_size);
153 	if (skip == 0)
154 		return (0);
155 
156 	if ((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0) {
157 		if (old_offset >= mine->size ||
158 		    skip > mine->size - old_offset) {
159 			/* Do not seek past end of file. */
160 			errno = ESPIPE;
161 		} else if ((new_offset = lseek(mine->fd, skip, SEEK_CUR)) >= 0)
162 			return (new_offset - old_offset);
163 	}
164 
165 	/* If seek failed once, it will probably fail again. */
166 	mine->use_lseek = 0;
167 
168 	/* Let libarchive recover with read+discard. */
169 	if (errno == ESPIPE)
170 		return (0);
171 
172 	/*
173 	 * There's been an error other than ESPIPE. This is most
174 	 * likely caused by a programmer error (too large request)
175 	 * or a corrupted archive file.
176 	 */
177 	archive_set_error(a, errno, "Error seeking");
178 	return (-1);
179 }
180 
181 /*
182  * TODO: Store the offset and use it in the read callback.
183  */
184 static int64_t
file_seek(struct archive * a,void * client_data,int64_t request,int whence)185 file_seek(struct archive *a, void *client_data, int64_t request, int whence)
186 {
187 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
188 	off_t seek = (off_t)request;
189 	int64_t r;
190 	int seek_bits = sizeof(seek) * 8 - 1;  /* off_t is a signed type. */
191 
192 	/* We use off_t here because lseek() is declared that way. */
193 
194 	/* Reduce a request that would overflow the 'seek' variable. */
195 	if (sizeof(request) > sizeof(seek)) {
196 		const int64_t max_seek =
197 		    (((int64_t)1 << (seek_bits - 1)) - 1) * 2 + 1;
198 		const int64_t min_seek = ~max_seek;
199 		if (request > max_seek)
200 			seek = (off_t)max_seek;
201 		else if (request < min_seek)
202 			seek = (off_t)min_seek;
203 	}
204 
205 	r = lseek(mine->fd, seek, whence);
206 	if (r >= 0)
207 		return r;
208 
209 	if (errno == ESPIPE) {
210 		archive_set_error(a, errno,
211 		    "A file descriptor(%d) is not seekable(PIPE)", mine->fd);
212 		return (ARCHIVE_FAILED);
213 	} else {
214 		/* If the input is corrupted or truncated, fail. */
215 		archive_set_error(a, errno,
216 		    "Error seeking in a file descriptor(%d)", mine->fd);
217 		return (ARCHIVE_FATAL);
218 	}
219 }
220 
221 static int
file_close(struct archive * a,void * client_data)222 file_close(struct archive *a, void *client_data)
223 {
224 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
225 
226 	(void)a; /* UNUSED */
227 	free(mine->buffer);
228 	free(mine);
229 	return (ARCHIVE_OK);
230 }
231