archive_read_open_fd.c revision 248616
143902Sbrian/*- 243902Sbrian * Copyright (c) 2003-2007 Tim Kientzle 343902Sbrian * All rights reserved. 443902Sbrian * 543902Sbrian * Redistribution and use in source and binary forms, with or without 643902Sbrian * modification, are permitted provided that the following conditions 743902Sbrian * are met: 843902Sbrian * 1. Redistributions of source code must retain the above copyright 943902Sbrian * notice, this list of conditions and the following disclaimer. 1043902Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1143902Sbrian * notice, this list of conditions and the following disclaimer in the 1243902Sbrian * documentation and/or other materials provided with the distribution. 1343902Sbrian * 1443902Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 1550476Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1643902Sbrian * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1743902Sbrian * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 1843902Sbrian * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1943902Sbrian * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2043902Sbrian * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2143902Sbrian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2243902Sbrian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2343902Sbrian * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2443902Sbrian */ 2543902Sbrian 2643902Sbrian#include "archive_platform.h" 2743902Sbrian__FBSDID("$FreeBSD: head/contrib/libarchive/libarchive/archive_read_open_fd.c 248616 2013-03-22 13:36:03Z mm $"); 2851049Sbrian 2943902Sbrian#ifdef HAVE_SYS_STAT_H 3043902Sbrian#include <sys/stat.h> 3143902Sbrian#endif 3243902Sbrian#ifdef HAVE_ERRNO_H 3351049Sbrian#include <errno.h> 3443902Sbrian#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 53struct read_fd_data { 54 int fd; 55 size_t block_size; 56 char use_lseek; 57 void *buffer; 58}; 59 60static int file_close(struct archive *, void *); 61static ssize_t file_read(struct archive *, void *, const void **buff); 62static int64_t file_skip(struct archive *, void *, int64_t request); 63 64int 65archive_read_open_fd(struct archive *a, int fd, size_t block_size) 66{ 67 struct stat st; 68 struct read_fd_data *mine; 69 void *b; 70 71 archive_clear_error(a); 72 if (fstat(fd, &st) != 0) { 73 archive_set_error(a, errno, "Can't stat fd %d", fd); 74 return (ARCHIVE_FATAL); 75 } 76 77 mine = (struct read_fd_data *)calloc(1, sizeof(*mine)); 78 b = malloc(block_size); 79 if (mine == NULL || b == NULL) { 80 archive_set_error(a, ENOMEM, "No memory"); 81 free(mine); 82 free(b); 83 return (ARCHIVE_FATAL); 84 } 85 mine->block_size = block_size; 86 mine->buffer = b; 87 mine->fd = fd; 88 /* 89 * Skip support is a performance optimization for anything 90 * that supports lseek(). On FreeBSD, only regular files and 91 * raw disk devices support lseek() and there's no portable 92 * way to determine if a device is a raw disk device, so we 93 * only enable this optimization for regular files. 94 */ 95 if (S_ISREG(st.st_mode)) { 96 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino); 97 mine->use_lseek = 1; 98 } 99#if defined(__CYGWIN__) || defined(_WIN32) 100 setmode(mine->fd, O_BINARY); 101#endif 102 103 archive_read_set_read_callback(a, file_read); 104 archive_read_set_skip_callback(a, file_skip); 105 archive_read_set_close_callback(a, file_close); 106 archive_read_set_callback_data(a, mine); 107 return (archive_read_open1(a)); 108} 109 110static ssize_t 111file_read(struct archive *a, void *client_data, const void **buff) 112{ 113 struct read_fd_data *mine = (struct read_fd_data *)client_data; 114 ssize_t bytes_read; 115 116 *buff = mine->buffer; 117 for (;;) { 118 bytes_read = read(mine->fd, mine->buffer, mine->block_size); 119 if (bytes_read < 0) { 120 if (errno == EINTR) 121 continue; 122 archive_set_error(a, errno, "Error reading fd %d", 123 mine->fd); 124 } 125 return (bytes_read); 126 } 127} 128 129static int64_t 130file_skip(struct archive *a, void *client_data, int64_t request) 131{ 132 struct read_fd_data *mine = (struct read_fd_data *)client_data; 133 int64_t skip = request; 134 int64_t old_offset, new_offset; 135 int skip_bits = sizeof(skip) * 8 - 1; /* off_t is a signed type. */ 136 137 if (!mine->use_lseek) 138 return (0); 139 140 /* Reduce a request that would overflow the 'skip' variable. */ 141 if (sizeof(request) > sizeof(skip)) { 142 int64_t max_skip = 143 (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1; 144 if (request > max_skip) 145 skip = max_skip; 146 } 147 148 /* Reduce request to the next smallest multiple of block_size */ 149 request = (request / mine->block_size) * mine->block_size; 150 if (request == 0) 151 return (0); 152 153 if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0) && 154 ((new_offset = lseek(mine->fd, skip, SEEK_CUR)) >= 0)) 155 return (new_offset - old_offset); 156 157 /* If seek failed once, it will probably fail again. */ 158 mine->use_lseek = 0; 159 160 /* Let libarchive recover with read+discard. */ 161 if (errno == ESPIPE) 162 return (0); 163 164 /* 165 * There's been an error other than ESPIPE. This is most 166 * likely caused by a programmer error (too large request) 167 * or a corrupted archive file. 168 */ 169 archive_set_error(a, errno, "Error seeking"); 170 return (-1); 171} 172 173static int 174file_close(struct archive *a, void *client_data) 175{ 176 struct read_fd_data *mine = (struct read_fd_data *)client_data; 177 178 (void)a; /* UNUSED */ 179 free(mine->buffer); 180 free(mine); 181 return (ARCHIVE_OK); 182} 183