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 
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
30 #endif
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 
38 #include "archive.h"
39 #include "archive_private.h"
40 
41 /* Maximum amount of data to write at one time. */
42 #define	MAX_WRITE	(1024 * 1024)
43 
44 /*
45  * This implementation minimizes copying of data and is sparse-file aware.
46  */
47 static int
48 pad_to(struct archive *a, int fd, int can_lseek,
49     size_t nulls_size, const char *nulls,
50     int64_t target_offset, int64_t actual_offset)
51 {
52 	size_t to_write;
53 	ssize_t bytes_written;
54 
55 	if (can_lseek) {
56 		actual_offset = lseek(fd,
57 		    target_offset - actual_offset, SEEK_CUR);
58 		if (actual_offset != target_offset) {
59 			archive_set_error(a, errno, "Seek error");
60 			return (ARCHIVE_FATAL);
61 		}
62 		return (ARCHIVE_OK);
63 	}
64 	while (target_offset > actual_offset) {
65 		to_write = nulls_size;
66 		if (target_offset < actual_offset + (int64_t)nulls_size)
67 			to_write = (size_t)(target_offset - actual_offset);
68 		bytes_written = write(fd, nulls, to_write);
69 		if (bytes_written < 0) {
70 			archive_set_error(a, errno, "Write error");
71 			return (ARCHIVE_FATAL);
72 		}
73 		actual_offset += bytes_written;
74 	}
75 	return (ARCHIVE_OK);
76 }
77 
78 
79 int
80 archive_read_data_into_fd(struct archive *a, int fd)
81 {
82 	struct stat st;
83 	int r, r2;
84 	const void *buff;
85 	size_t size, bytes_to_write;
86 	ssize_t bytes_written;
87 	int64_t target_offset;
88 	int64_t actual_offset = 0;
89 	int can_lseek;
90 	char *nulls = NULL;
91 	size_t nulls_size = 16384;
92 
93 	archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
94 	    "archive_read_data_into_fd");
95 
96 	can_lseek = (fstat(fd, &st) == 0) && S_ISREG(st.st_mode);
97 	if (!can_lseek) {
98 		nulls = calloc(1, nulls_size);
99 		if (!nulls) {
100 			r = ARCHIVE_FATAL;
101 			goto cleanup;
102 		}
103 	}
104 
105 	while ((r = archive_read_data_block(a, &buff, &size, &target_offset)) ==
106 	    ARCHIVE_OK) {
107 		const char *p = buff;
108 		if (target_offset > actual_offset) {
109 			r = pad_to(a, fd, can_lseek, nulls_size, nulls,
110 			    target_offset, actual_offset);
111 			if (r != ARCHIVE_OK)
112 				break;
113 			actual_offset = target_offset;
114 		}
115 		while (size > 0) {
116 			bytes_to_write = size;
117 			if (bytes_to_write > MAX_WRITE)
118 				bytes_to_write = MAX_WRITE;
119 			bytes_written = write(fd, p, bytes_to_write);
120 			if (bytes_written < 0) {
121 				archive_set_error(a, errno, "Write error");
122 				r = ARCHIVE_FATAL;
123 				goto cleanup;
124 			}
125 			actual_offset += bytes_written;
126 			p += bytes_written;
127 			size -= bytes_written;
128 		}
129 	}
130 
131 	if (r == ARCHIVE_EOF && target_offset > actual_offset) {
132 		r2 = pad_to(a, fd, can_lseek, nulls_size, nulls,
133 		    target_offset, actual_offset);
134 		if (r2 != ARCHIVE_OK)
135 			r = r2;
136 	}
137 
138 cleanup:
139 	free(nulls);
140 	if (r != ARCHIVE_EOF)
141 		return (r);
142 	return (ARCHIVE_OK);
143 }
144