160b4ad09SPeter Avalos /*-
260b4ad09SPeter Avalos * Copyright (c) 2003-2007 Tim Kientzle
360b4ad09SPeter Avalos * All rights reserved.
460b4ad09SPeter Avalos *
560b4ad09SPeter Avalos * Redistribution and use in source and binary forms, with or without
660b4ad09SPeter Avalos * modification, are permitted provided that the following conditions
760b4ad09SPeter Avalos * are met:
860b4ad09SPeter Avalos * 1. Redistributions of source code must retain the above copyright
960b4ad09SPeter Avalos * notice, this list of conditions and the following disclaimer.
1060b4ad09SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
1160b4ad09SPeter Avalos * notice, this list of conditions and the following disclaimer in the
1260b4ad09SPeter Avalos * documentation and/or other materials provided with the distribution.
1360b4ad09SPeter Avalos *
1460b4ad09SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1560b4ad09SPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1660b4ad09SPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1760b4ad09SPeter Avalos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1860b4ad09SPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1960b4ad09SPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2060b4ad09SPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2160b4ad09SPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2260b4ad09SPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2360b4ad09SPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2460b4ad09SPeter Avalos */
2560b4ad09SPeter Avalos
2660b4ad09SPeter Avalos #include "archive_platform.h"
279c82a63eSPeter Avalos __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_open_fd.c 201103 2009-12-28 03:13:49Z kientzle $");
2860b4ad09SPeter Avalos
2960b4ad09SPeter Avalos #ifdef HAVE_SYS_STAT_H
3060b4ad09SPeter Avalos #include <sys/stat.h>
3160b4ad09SPeter Avalos #endif
3260b4ad09SPeter Avalos #ifdef HAVE_ERRNO_H
3360b4ad09SPeter Avalos #include <errno.h>
3460b4ad09SPeter Avalos #endif
359c82a63eSPeter Avalos #ifdef HAVE_FCNTL_H
369c82a63eSPeter Avalos #include <fcntl.h>
379c82a63eSPeter Avalos #endif
389c82a63eSPeter Avalos #ifdef HAVE_IO_H
399c82a63eSPeter Avalos #include <io.h>
409c82a63eSPeter Avalos #endif
4160b4ad09SPeter Avalos #ifdef HAVE_STDLIB_H
4260b4ad09SPeter Avalos #include <stdlib.h>
4360b4ad09SPeter Avalos #endif
4460b4ad09SPeter Avalos #ifdef HAVE_STRING_H
4560b4ad09SPeter Avalos #include <string.h>
4660b4ad09SPeter Avalos #endif
4760b4ad09SPeter Avalos #ifdef HAVE_UNISTD_H
4860b4ad09SPeter Avalos #include <unistd.h>
4960b4ad09SPeter Avalos #endif
5060b4ad09SPeter Avalos
5160b4ad09SPeter Avalos #include "archive.h"
5260b4ad09SPeter Avalos
5360b4ad09SPeter Avalos struct read_fd_data {
5460b4ad09SPeter Avalos int fd;
5560b4ad09SPeter Avalos size_t block_size;
56c09f92d2SPeter Avalos char use_lseek;
5760b4ad09SPeter Avalos void *buffer;
5860b4ad09SPeter Avalos };
5960b4ad09SPeter Avalos
6060b4ad09SPeter Avalos static int file_close(struct archive *, void *);
6160b4ad09SPeter Avalos static ssize_t file_read(struct archive *, void *, const void **buff);
62*6b384f39SPeter Avalos static int64_t file_seek(struct archive *, void *, int64_t request, int);
63c09f92d2SPeter Avalos static int64_t file_skip(struct archive *, void *, int64_t request);
6460b4ad09SPeter Avalos
6560b4ad09SPeter Avalos int
archive_read_open_fd(struct archive * a,int fd,size_t block_size)6660b4ad09SPeter Avalos archive_read_open_fd(struct archive *a, int fd, size_t block_size)
6760b4ad09SPeter Avalos {
688029ab02SPeter Avalos struct stat st;
6960b4ad09SPeter Avalos struct read_fd_data *mine;
708029ab02SPeter Avalos void *b;
718029ab02SPeter Avalos
729c82a63eSPeter Avalos archive_clear_error(a);
738029ab02SPeter Avalos if (fstat(fd, &st) != 0) {
748029ab02SPeter Avalos archive_set_error(a, errno, "Can't stat fd %d", fd);
758029ab02SPeter Avalos return (ARCHIVE_FATAL);
768029ab02SPeter Avalos }
7760b4ad09SPeter Avalos
78c09f92d2SPeter Avalos mine = (struct read_fd_data *)calloc(1, sizeof(*mine));
798029ab02SPeter Avalos b = malloc(block_size);
808029ab02SPeter Avalos if (mine == NULL || b == NULL) {
8160b4ad09SPeter Avalos archive_set_error(a, ENOMEM, "No memory");
828029ab02SPeter Avalos free(mine);
838029ab02SPeter Avalos free(b);
8460b4ad09SPeter Avalos return (ARCHIVE_FATAL);
8560b4ad09SPeter Avalos }
8660b4ad09SPeter Avalos mine->block_size = block_size;
878029ab02SPeter Avalos mine->buffer = b;
8860b4ad09SPeter Avalos mine->fd = fd;
898029ab02SPeter Avalos /*
908029ab02SPeter Avalos * Skip support is a performance optimization for anything
918029ab02SPeter Avalos * that supports lseek(). On FreeBSD, only regular files and
928029ab02SPeter Avalos * raw disk devices support lseek() and there's no portable
938029ab02SPeter Avalos * way to determine if a device is a raw disk device, so we
948029ab02SPeter Avalos * only enable this optimization for regular files.
958029ab02SPeter Avalos */
9660b4ad09SPeter Avalos if (S_ISREG(st.st_mode)) {
9760b4ad09SPeter Avalos archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
98c09f92d2SPeter Avalos mine->use_lseek = 1;
99c09f92d2SPeter Avalos }
1009c82a63eSPeter Avalos #if defined(__CYGWIN__) || defined(_WIN32)
1019c82a63eSPeter Avalos setmode(mine->fd, O_BINARY);
1029c82a63eSPeter Avalos #endif
1038029ab02SPeter Avalos
104c09f92d2SPeter Avalos archive_read_set_read_callback(a, file_read);
105c09f92d2SPeter Avalos archive_read_set_skip_callback(a, file_skip);
106*6b384f39SPeter Avalos archive_read_set_seek_callback(a, file_seek);
107c09f92d2SPeter Avalos archive_read_set_close_callback(a, file_close);
108c09f92d2SPeter Avalos archive_read_set_callback_data(a, mine);
109c09f92d2SPeter Avalos return (archive_read_open1(a));
11060b4ad09SPeter Avalos }
11160b4ad09SPeter Avalos
11260b4ad09SPeter Avalos static ssize_t
file_read(struct archive * a,void * client_data,const void ** buff)11360b4ad09SPeter Avalos file_read(struct archive *a, void *client_data, const void **buff)
11460b4ad09SPeter Avalos {
11560b4ad09SPeter Avalos struct read_fd_data *mine = (struct read_fd_data *)client_data;
11660b4ad09SPeter Avalos ssize_t bytes_read;
11760b4ad09SPeter Avalos
11860b4ad09SPeter Avalos *buff = mine->buffer;
1199c82a63eSPeter Avalos for (;;) {
12060b4ad09SPeter Avalos bytes_read = read(mine->fd, mine->buffer, mine->block_size);
12160b4ad09SPeter Avalos if (bytes_read < 0) {
1229c82a63eSPeter Avalos if (errno == EINTR)
1239c82a63eSPeter Avalos continue;
124d4d8193eSPeter Avalos archive_set_error(a, errno, "Error reading fd %d",
125d4d8193eSPeter Avalos mine->fd);
12660b4ad09SPeter Avalos }
12760b4ad09SPeter Avalos return (bytes_read);
12860b4ad09SPeter Avalos }
1299c82a63eSPeter Avalos }
13060b4ad09SPeter Avalos
131c09f92d2SPeter Avalos static int64_t
file_skip(struct archive * a,void * client_data,int64_t request)132c09f92d2SPeter Avalos file_skip(struct archive *a, void *client_data, int64_t request)
13360b4ad09SPeter Avalos {
13460b4ad09SPeter Avalos struct read_fd_data *mine = (struct read_fd_data *)client_data;
13559bf7050SPeter Avalos int64_t skip = request;
13659bf7050SPeter Avalos int64_t old_offset, new_offset;
137c09f92d2SPeter Avalos int skip_bits = sizeof(skip) * 8 - 1; /* off_t is a signed type. */
13860b4ad09SPeter Avalos
139c09f92d2SPeter Avalos if (!mine->use_lseek)
14060b4ad09SPeter Avalos return (0);
14160b4ad09SPeter Avalos
142c09f92d2SPeter Avalos /* Reduce a request that would overflow the 'skip' variable. */
143c09f92d2SPeter Avalos if (sizeof(request) > sizeof(skip)) {
144c09f92d2SPeter Avalos int64_t max_skip =
145c09f92d2SPeter Avalos (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
146c09f92d2SPeter Avalos if (request > max_skip)
147c09f92d2SPeter Avalos skip = max_skip;
148c09f92d2SPeter Avalos }
149c09f92d2SPeter Avalos
15060b4ad09SPeter Avalos /* Reduce request to the next smallest multiple of block_size */
15160b4ad09SPeter Avalos request = (request / mine->block_size) * mine->block_size;
15260b4ad09SPeter Avalos if (request == 0)
15360b4ad09SPeter Avalos return (0);
15460b4ad09SPeter Avalos
155c09f92d2SPeter Avalos if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0) &&
156c09f92d2SPeter Avalos ((new_offset = lseek(mine->fd, skip, SEEK_CUR)) >= 0))
157c09f92d2SPeter Avalos return (new_offset - old_offset);
15860b4ad09SPeter Avalos
159c09f92d2SPeter Avalos /* If seek failed once, it will probably fail again. */
160c09f92d2SPeter Avalos mine->use_lseek = 0;
161c09f92d2SPeter Avalos
162c09f92d2SPeter Avalos /* Let libarchive recover with read+discard. */
16360b4ad09SPeter Avalos if (errno == ESPIPE)
16460b4ad09SPeter Avalos return (0);
165c09f92d2SPeter Avalos
16660b4ad09SPeter Avalos /*
16760b4ad09SPeter Avalos * There's been an error other than ESPIPE. This is most
16860b4ad09SPeter Avalos * likely caused by a programmer error (too large request)
16960b4ad09SPeter Avalos * or a corrupted archive file.
17060b4ad09SPeter Avalos */
17160b4ad09SPeter Avalos archive_set_error(a, errno, "Error seeking");
17260b4ad09SPeter Avalos return (-1);
17360b4ad09SPeter Avalos }
17460b4ad09SPeter Avalos
175*6b384f39SPeter Avalos /*
176*6b384f39SPeter Avalos * TODO: Store the offset and use it in the read callback.
177*6b384f39SPeter Avalos */
178*6b384f39SPeter Avalos static int64_t
file_seek(struct archive * a,void * client_data,int64_t request,int whence)179*6b384f39SPeter Avalos file_seek(struct archive *a, void *client_data, int64_t request, int whence)
180*6b384f39SPeter Avalos {
181*6b384f39SPeter Avalos struct read_fd_data *mine = (struct read_fd_data *)client_data;
182*6b384f39SPeter Avalos int64_t r;
183*6b384f39SPeter Avalos
184*6b384f39SPeter Avalos /* We use off_t here because lseek() is declared that way. */
185*6b384f39SPeter Avalos /* See above for notes about when off_t is less than 64 bits. */
186*6b384f39SPeter Avalos r = lseek(mine->fd, request, whence);
187*6b384f39SPeter Avalos if (r >= 0)
188*6b384f39SPeter Avalos return r;
189*6b384f39SPeter Avalos
190*6b384f39SPeter Avalos if (errno == ESPIPE) {
191*6b384f39SPeter Avalos archive_set_error(a, errno,
192*6b384f39SPeter Avalos "A file descriptor(%d) is not seekable(PIPE)", mine->fd);
193*6b384f39SPeter Avalos return (ARCHIVE_FAILED);
194*6b384f39SPeter Avalos } else {
195*6b384f39SPeter Avalos /* If the input is corrupted or truncated, fail. */
196*6b384f39SPeter Avalos archive_set_error(a, errno,
197*6b384f39SPeter Avalos "Error seeking in a file descriptor(%d)", mine->fd);
198*6b384f39SPeter Avalos return (ARCHIVE_FATAL);
199*6b384f39SPeter Avalos }
200*6b384f39SPeter Avalos }
201*6b384f39SPeter Avalos
20260b4ad09SPeter Avalos static int
file_close(struct archive * a,void * client_data)20360b4ad09SPeter Avalos file_close(struct archive *a, void *client_data)
20460b4ad09SPeter Avalos {
20560b4ad09SPeter Avalos struct read_fd_data *mine = (struct read_fd_data *)client_data;
20660b4ad09SPeter Avalos
20760b4ad09SPeter Avalos (void)a; /* UNUSED */
20860b4ad09SPeter Avalos free(mine->buffer);
20960b4ad09SPeter Avalos free(mine);
21060b4ad09SPeter Avalos return (ARCHIVE_OK);
21160b4ad09SPeter Avalos }
212