Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-2.1 / libarchive / archive_read_data_into_fd.c
blob664cc2c3c245502729f2b6b723b18f8432c2ed59
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_data_into_fd.c,v 1.15 2007/04/02 00:21:46 kientzle Exp $");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
39 #include "archive.h"
40 #include "archive_private.h"
42 /* Maximum amount of data to write at one time. */
43 #define MAX_WRITE (1024 * 1024)
46 * This implementation minimizes copying of data and is sparse-file aware.
48 int
49 archive_read_data_into_fd(struct archive *a, int fd)
51 int r;
52 const void *buff;
53 size_t size, bytes_to_write;
54 ssize_t bytes_written, total_written;
55 off_t offset;
56 off_t output_offset;
58 __archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, "archive_read_data_into_fd");
60 total_written = 0;
61 output_offset = 0;
63 while ((r = archive_read_data_block(a, &buff, &size, &offset)) ==
64 ARCHIVE_OK) {
65 const char *p = buff;
66 if (offset > output_offset) {
67 lseek(fd, offset - output_offset, SEEK_CUR);
68 output_offset = offset;
70 while (size > 0) {
71 bytes_to_write = size;
72 if (bytes_to_write > MAX_WRITE)
73 bytes_to_write = MAX_WRITE;
74 bytes_written = write(fd, p, bytes_to_write);
75 if (bytes_written < 0) {
76 archive_set_error(a, errno, "Write error");
77 return (-1);
79 output_offset += bytes_written;
80 total_written += bytes_written;
81 p += bytes_written;
82 size -= bytes_written;
86 if (r != ARCHIVE_EOF)
87 return (r);
88 return (ARCHIVE_OK);