2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.16 2008/05/23 05:01:29 cperciva Exp $");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.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.
49 pad_to(struct archive
*a
, int fd
, int can_lseek
,
50 size_t nulls_size
, const char *nulls
,
51 int64_t target_offset
, int64_t actual_offset
)
54 ssize_t bytes_written
;
57 actual_offset
= lseek(fd
,
58 target_offset
- actual_offset
, SEEK_CUR
);
59 if (actual_offset
!= target_offset
) {
60 archive_set_error(a
, errno
, "Seek error");
61 return (ARCHIVE_FATAL
);
65 while (target_offset
> actual_offset
) {
66 to_write
= nulls_size
;
67 if (target_offset
< actual_offset
+ (int64_t)nulls_size
)
68 to_write
= (size_t)(target_offset
- actual_offset
);
69 bytes_written
= write(fd
, nulls
, to_write
);
70 if (bytes_written
< 0) {
71 archive_set_error(a
, errno
, "Write error");
72 return (ARCHIVE_FATAL
);
74 actual_offset
+= bytes_written
;
81 archive_read_data_into_fd(struct archive
*a
, int fd
)
86 size_t size
, bytes_to_write
;
87 ssize_t bytes_written
;
88 int64_t target_offset
;
89 int64_t actual_offset
= 0;
92 size_t nulls_size
= 16384;
94 archive_check_magic(a
, ARCHIVE_READ_MAGIC
, ARCHIVE_STATE_DATA
,
95 "archive_read_data_into_fd");
97 can_lseek
= (fstat(fd
, &st
) == 0) && S_ISREG(st
.st_mode
);
99 nulls
= calloc(1, nulls_size
);
101 while ((r
= archive_read_data_block(a
, &buff
, &size
, &target_offset
)) ==
103 const char *p
= buff
;
104 if (target_offset
> actual_offset
) {
105 r
= pad_to(a
, fd
, can_lseek
, nulls_size
, nulls
,
106 target_offset
, actual_offset
);
109 actual_offset
= target_offset
;
112 bytes_to_write
= size
;
113 if (bytes_to_write
> MAX_WRITE
)
114 bytes_to_write
= MAX_WRITE
;
115 bytes_written
= write(fd
, p
, bytes_to_write
);
116 if (bytes_written
< 0) {
117 archive_set_error(a
, errno
, "Write error");
121 actual_offset
+= bytes_written
;
123 size
-= bytes_written
;
127 if (r
== ARCHIVE_EOF
&& target_offset
> actual_offset
) {
128 r2
= pad_to(a
, fd
, can_lseek
, nulls_size
, nulls
,
129 target_offset
, actual_offset
);
130 if (r2
!= ARCHIVE_OK
)
136 if (r
!= ARCHIVE_EOF
)