1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25 
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 $");
28 
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
38 
39 #include "archive.h"
40 #include "archive_private.h"
41 
42 /* Maximum amount of data to write at one time. */
43 #define	MAX_WRITE	(1024 * 1024)
44 
45 /*
46  * This implementation minimizes copying of data and is sparse-file aware.
47  */
48 static int
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)
52 {
53 	size_t to_write;
54 	ssize_t bytes_written;
55 
56 	if (can_lseek) {
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);
62 		}
63 		return (ARCHIVE_OK);
64 	}
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);
73 		}
74 		actual_offset += bytes_written;
75 	}
76 	return (ARCHIVE_OK);
77 }
78 
79 
80 int
81 archive_read_data_into_fd(struct archive *a, int fd)
82 {
83 	struct stat st;
84 	int r, r2;
85 	const void *buff;
86 	size_t size, bytes_to_write;
87 	ssize_t bytes_written;
88 	int64_t target_offset;
89 	int64_t actual_offset = 0;
90 	int can_lseek;
91 	char *nulls = NULL;
92 	size_t nulls_size = 16384;
93 
94 	archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
95 	    "archive_read_data_into_fd");
96 
97 	can_lseek = (fstat(fd, &st) == 0) && S_ISREG(st.st_mode);
98 	if (!can_lseek)
99 		nulls = calloc(1, nulls_size);
100 
101 	while ((r = archive_read_data_block(a, &buff, &size, &target_offset)) ==
102 	    ARCHIVE_OK) {
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);
107 			if (r != ARCHIVE_OK)
108 				break;
109 			actual_offset = target_offset;
110 		}
111 		while (size > 0) {
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");
118 				r = ARCHIVE_FATAL;
119 				goto cleanup;
120 			}
121 			actual_offset += bytes_written;
122 			p += bytes_written;
123 			size -= bytes_written;
124 		}
125 	}
126 
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)
131 			r = r2;
132 	}
133 
134 cleanup:
135 	free(nulls);
136 	if (r != ARCHIVE_EOF)
137 		return (r);
138 	return (ARCHIVE_OK);
139 }
140