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" 27229592Smm__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 */ 48228753Smmint 49228753Smmarchive_read_data_into_fd(struct archive *a, int fd) 50228753Smm{ 51228753Smm int r; 52228753Smm const void *buff; 53228753Smm size_t size, bytes_to_write; 54228753Smm ssize_t bytes_written, total_written; 55228753Smm off_t offset; 56228753Smm off_t output_offset; 57228753Smm 58228753Smm __archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, "archive_read_data_into_fd"); 59228753Smm 60228753Smm total_written = 0; 61228753Smm output_offset = 0; 62228753Smm 63228753Smm while ((r = archive_read_data_block(a, &buff, &size, &offset)) == 64228753Smm ARCHIVE_OK) { 65228753Smm const char *p = buff; 66228753Smm if (offset > output_offset) { 67228753Smm output_offset = lseek(fd, 68228753Smm offset - output_offset, SEEK_CUR); 69228753Smm if (output_offset != offset) { 70228753Smm archive_set_error(a, errno, "Seek error"); 71228753Smm return (ARCHIVE_FATAL); 72228753Smm } 73228753Smm } 74228753Smm while (size > 0) { 75228753Smm bytes_to_write = size; 76228753Smm if (bytes_to_write > MAX_WRITE) 77228753Smm bytes_to_write = MAX_WRITE; 78228753Smm bytes_written = write(fd, p, bytes_to_write); 79228753Smm if (bytes_written < 0) { 80228753Smm archive_set_error(a, errno, "Write error"); 81228753Smm return (ARCHIVE_FATAL); 82228753Smm } 83228753Smm output_offset += bytes_written; 84228753Smm total_written += bytes_written; 85228753Smm p += bytes_written; 86228753Smm size -= bytes_written; 87228753Smm } 88228753Smm } 89228753Smm 90228753Smm if (r != ARCHIVE_EOF) 91228753Smm return (r); 92228753Smm return (ARCHIVE_OK); 93228753Smm} 94