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$");
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 		if (!nulls) {
101 			r = ARCHIVE_FATAL;
102 			goto cleanup;
103 		}
104 	}
105 
106 	while ((r = archive_read_data_block(a, &buff, &size, &target_offset)) ==
107 	    ARCHIVE_OK) {
108 		const char *p = buff;
109 		if (target_offset > actual_offset) {
110 			r = pad_to(a, fd, can_lseek, nulls_size, nulls,
111 			    target_offset, actual_offset);
112 			if (r != ARCHIVE_OK)
113 				break;
114 			actual_offset = target_offset;
115 		}
116 		while (size > 0) {
117 			bytes_to_write = size;
118 			if (bytes_to_write > MAX_WRITE)
119 				bytes_to_write = MAX_WRITE;
120 			bytes_written = write(fd, p, bytes_to_write);
121 			if (bytes_written < 0) {
122 				archive_set_error(a, errno, "Write error");
123 				r = ARCHIVE_FATAL;
124 				goto cleanup;
125 			}
126 			actual_offset += bytes_written;
127 			p += bytes_written;
128 			size -= bytes_written;
129 		}
130 	}
131 
132 	if (r == ARCHIVE_EOF && target_offset > actual_offset) {
133 		r2 = pad_to(a, fd, can_lseek, nulls_size, nulls,
134 		    target_offset, actual_offset);
135 		if (r2 != ARCHIVE_OK)
136 			r = r2;
137 	}
138 
139 cleanup:
140 	free(nulls);
141 	if (r != ARCHIVE_EOF)
142 		return (r);
143 	return (ARCHIVE_OK);
144 }
145