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 __FBSDID("$FreeBSD$");
33 * This is a somewhat tricky test that verifies the ability to
34 * write and read very large entries to tar archives. It
35 * writes entries from 2GB up to 1TB to an archive in memory.
36 * The memory storage here carefully avoids actually storing
37 * any part of the file bodies, so it runs very quickly and requires
38 * very little memory. If you're willing to wait a few minutes,
39 * you should be able to exercise petabyte entries with this code.
43 * Each file is built up by duplicating the following block.
45 static size_t filedatasize;
46 static void *filedata;
49 * We store the archive as blocks of data generated by libarchive,
50 * each possibly followed by bytes of file data.
53 struct memblock *next;
60 * The total memory store is just a list of memblocks plus
61 * some accounting overhead.
66 struct memblock *first;
67 struct memblock *last;
70 /* The following size definitions simplify things below. */
71 #define KB ((int64_t)1024)
72 #define MB ((int64_t)1024 * KB)
73 #define GB ((int64_t)1024 * MB)
74 #define TB ((int64_t)1024 * GB)
76 static int64_t memory_read_skip(struct archive *, void *, int64_t request);
77 static ssize_t memory_read(struct archive *, void *, const void **buff);
78 static ssize_t memory_write(struct archive *, void *, const void *, size_t);
82 memory_write(struct archive *a, void *_private, const void *buff, size_t size)
84 struct memdata *private = _private;
85 struct memblock *block;
90 * Since libarchive tries to behave in a zero-copy manner, if
91 * you give a pointer to filedata to the library, a pointer
92 * into that data will (usually) pop out here. This way, we
93 * can tell the difference between filedata and library header
96 if ((const char *)filedata <= (const char *)buff
97 && (const char *)buff < (const char *)filedata + filedatasize) {
98 /* We don't need to store a block of file data. */
99 private->last->filebytes += (int64_t)size;
101 /* Yes, we're assuming the very first write is metadata. */
102 /* It's header or metadata, copy and save it. */
103 block = (struct memblock *)malloc(sizeof(*block));
104 memset(block, 0, sizeof(*block));
106 block->buff = malloc(size);
107 memcpy(block->buff, buff, size);
108 if (private->last == NULL) {
109 private->first = private->last = block;
111 private->last->next = block;
112 private->last = block;
120 memory_read(struct archive *a, void *_private, const void **buff)
122 struct memdata *private = _private;
123 struct memblock *block;
129 private->buff = NULL;
130 if (private->first == NULL) {
131 private->last = NULL;
132 return (ARCHIVE_EOF);
134 if (private->filebytes > 0) {
136 * We're returning file bytes, simulate it by
137 * passing blocks from the template data.
139 if (private->filebytes > (int64_t)filedatasize)
140 size = (ssize_t)filedatasize;
142 size = (ssize_t)private->filebytes;
143 private->filebytes -= size;
147 * We need to get some real data to return.
149 block = private->first;
150 private->first = block->next;
151 size = (ssize_t)block->size;
152 if (block->buff != NULL) {
153 private->buff = block->buff;
156 private->buff = NULL;
159 private->filebytes = block->filebytes;
167 memory_read_skip(struct archive *a, void *_private, int64_t skip)
169 struct memdata *private = _private;
173 if (private->first == NULL) {
174 private->last = NULL;
177 if (private->filebytes > 0) {
178 if (private->filebytes < skip)
179 skip = (off_t)private->filebytes;
180 private->filebytes -= skip;
187 DEFINE_TEST(test_tar_large)
189 /* The sizes of the entries we're going to generate. */
190 static int64_t tests[] = {
191 /* Test for 32-bit signed overflow. */
192 2 * GB - 1, 2 * GB, 2 * GB + 1,
193 /* Test for 32-bit unsigned overflow. */
194 4 * GB - 1, 4 * GB, 4 * GB + 1,
195 /* 8GB is the "official" max for ustar. */
196 8 * GB - 1, 8 * GB, 8 * GB + 1,
197 /* Bend ustar a tad and you can get 64GB (12 octal digits). */
198 64 * GB - 1, 64 * GB,
199 /* And larger entries that require non-ustar extensions. */
200 256 * GB, 1 * TB, 0 };
203 struct memdata memdata;
204 struct archive_entry *ae;
209 filedatasize = (size_t)(1 * MB);
210 filedata = malloc(filedatasize);
211 memset(filedata, 0xAA, filedatasize);
212 memset(&memdata, 0, sizeof(memdata));
215 * Open an archive for writing.
217 a = archive_write_new();
218 archive_write_set_format_pax_restricted(a);
219 archive_write_set_bytes_per_block(a, 0); /* No buffering. */
220 archive_write_open(a, &memdata, NULL, memory_write, NULL);
223 * Write a series of large files to it.
225 for (i = 0; tests[i] != 0; i++) {
226 assert((ae = archive_entry_new()) != NULL);
227 sprintf(namebuff, "file_%d", i);
228 archive_entry_copy_pathname(ae, namebuff);
229 archive_entry_set_mode(ae, S_IFREG | 0755);
232 archive_entry_set_size(ae, filesize);
234 assertA(0 == archive_write_header(a, ae));
235 archive_entry_free(ae);
238 * Write the actual data to the archive.
240 while (filesize > 0) {
241 writesize = filedatasize;
242 if ((int64_t)writesize > filesize)
243 writesize = (size_t)filesize;
244 assertA((int)writesize
245 == archive_write_data(a, filedata, writesize));
246 filesize -= writesize;
250 assert((ae = archive_entry_new()) != NULL);
251 archive_entry_copy_pathname(ae, "lastfile");
252 archive_entry_set_mode(ae, S_IFREG | 0755);
253 assertA(0 == archive_write_header(a, ae));
254 archive_entry_free(ae);
257 /* Close out the archive. */
258 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
259 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
262 * Open the same archive for reading.
264 a = archive_read_new();
265 archive_read_support_format_tar(a);
266 archive_read_open2(a, &memdata, NULL,
267 memory_read, memory_read_skip, NULL);
272 for (i = 0; tests[i] > 0; i++) {
273 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
274 sprintf(namebuff, "file_%d", i);
275 assertEqualString(namebuff, archive_entry_pathname(ae));
276 assert(tests[i] == archive_entry_size(ae));
278 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
279 assertEqualString("lastfile", archive_entry_pathname(ae));
281 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
283 /* Close out the archive. */
284 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
285 assertEqualInt(ARCHIVE_OK, archive_read_free(a));