1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm 26228753Smm#include "archive_platform.h" 27228763Smm__FBSDID("$FreeBSD$"); 28228753Smm 29228753Smm#ifdef HAVE_SYS_TYPES_H 30228753Smm#include <sys/types.h> 31228753Smm#endif 32228753Smm#ifdef HAVE_ERRNO_H 33228753Smm#include <errno.h> 34228753Smm#endif 35228753Smm#ifdef HAVE_UNISTD_H 36228753Smm#include <unistd.h> 37228753Smm#endif 38228753Smm 39228753Smm#include "archive.h" 40228753Smm#include "archive_private.h" 41228753Smm 42228753Smm/* Maximum amount of data to write at one time. */ 43228753Smm#define MAX_WRITE (1024 * 1024) 44228753Smm 45228753Smm/* 46228753Smm * This implementation minimizes copying of data and is sparse-file aware. 47228753Smm */ 48232153Smmstatic int 49232153Smmpad_to(struct archive *a, int fd, int can_lseek, 50232153Smm size_t nulls_size, const char *nulls, 51232153Smm int64_t target_offset, int64_t actual_offset) 52232153Smm{ 53232153Smm size_t to_write; 54232153Smm ssize_t bytes_written; 55232153Smm 56232153Smm if (can_lseek) { 57232153Smm actual_offset = lseek(fd, 58232153Smm target_offset - actual_offset, SEEK_CUR); 59232153Smm if (actual_offset != target_offset) { 60232153Smm archive_set_error(a, errno, "Seek error"); 61232153Smm return (ARCHIVE_FATAL); 62232153Smm } 63232153Smm return (ARCHIVE_OK); 64232153Smm } 65232153Smm while (target_offset > actual_offset) { 66232153Smm to_write = nulls_size; 67232153Smm if (target_offset < actual_offset + (int64_t)nulls_size) 68232153Smm to_write = (size_t)(target_offset - actual_offset); 69232153Smm bytes_written = write(fd, nulls, to_write); 70232153Smm if (bytes_written < 0) { 71232153Smm archive_set_error(a, errno, "Write error"); 72232153Smm return (ARCHIVE_FATAL); 73232153Smm } 74232153Smm actual_offset += bytes_written; 75232153Smm } 76232153Smm return (ARCHIVE_OK); 77232153Smm} 78232153Smm 79232153Smm 80228753Smmint 81228753Smmarchive_read_data_into_fd(struct archive *a, int fd) 82228753Smm{ 83232153Smm struct stat st; 84232153Smm int r, r2; 85228753Smm const void *buff; 86228753Smm size_t size, bytes_to_write; 87232153Smm ssize_t bytes_written; 88232153Smm int64_t target_offset; 89232153Smm int64_t actual_offset = 0; 90232153Smm int can_lseek; 91232153Smm char *nulls = NULL; 92232153Smm size_t nulls_size = 16384; 93228753Smm 94232153Smm archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, 95232153Smm "archive_read_data_into_fd"); 96228753Smm 97232153Smm can_lseek = (fstat(fd, &st) == 0) && S_ISREG(st.st_mode); 98232153Smm if (!can_lseek) 99232153Smm nulls = calloc(1, nulls_size); 100228753Smm 101232153Smm while ((r = archive_read_data_block(a, &buff, &size, &target_offset)) == 102228753Smm ARCHIVE_OK) { 103228753Smm const char *p = buff; 104232153Smm if (target_offset > actual_offset) { 105232153Smm r = pad_to(a, fd, can_lseek, nulls_size, nulls, 106232153Smm target_offset, actual_offset); 107232153Smm if (r != ARCHIVE_OK) 108232153Smm break; 109232153Smm actual_offset = target_offset; 110228753Smm } 111228753Smm while (size > 0) { 112228753Smm bytes_to_write = size; 113228753Smm if (bytes_to_write > MAX_WRITE) 114228753Smm bytes_to_write = MAX_WRITE; 115228753Smm bytes_written = write(fd, p, bytes_to_write); 116228753Smm if (bytes_written < 0) { 117228753Smm archive_set_error(a, errno, "Write error"); 118232153Smm r = ARCHIVE_FATAL; 119232153Smm goto cleanup; 120228753Smm } 121232153Smm actual_offset += bytes_written; 122228753Smm p += bytes_written; 123228753Smm size -= bytes_written; 124228753Smm } 125228753Smm } 126228753Smm 127232153Smm if (r == ARCHIVE_EOF && target_offset > actual_offset) { 128232153Smm r2 = pad_to(a, fd, can_lseek, nulls_size, nulls, 129232153Smm target_offset, actual_offset); 130232153Smm if (r2 != ARCHIVE_OK) 131232153Smm r = r2; 132232153Smm } 133232153Smm 134232153Smmcleanup: 135232153Smm free(nulls); 136228753Smm if (r != ARCHIVE_EOF) 137228753Smm return (r); 138228753Smm return (ARCHIVE_OK); 139228753Smm} 140