1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*-
* Copyright (c) 2003-2007 Tim Kientzle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.h"
__FBSDID("$FreeBSD: head/lib/libarchive/test/test_tar_large.c 201247 2009-12-30 05:59:21Z kientzle $");
#include <errno.h>
#include <stdlib.h>
#include <string.h>
/*
* This is a somewhat tricky test that verifies the ability to
* write and read very large entries to tar archives. It
* writes entries from 2GB up to 1TB to an archive in memory.
* The memory storage here carefully avoids actually storing
* any part of the file bodies, so it runs very quickly and requires
* very little memory. If you're willing to wait a few minutes,
* you should be able to exercise petabyte entries with this code.
*/
/*
* Each file is built up by duplicating the following block.
*/
static size_t filedatasize;
static void *filedata;
/*
* We store the archive as blocks of data generated by libarchive,
* each possibly followed by bytes of file data.
*/
struct memblock {
struct memblock *next;
size_t size;
void *buff;
int64_t filebytes;
};
/*
* The total memory store is just a list of memblocks plus
* some accounting overhead.
*/
struct memdata {
int64_t filebytes;
void *buff;
struct memblock *first;
struct memblock *last;
};
/* The following size definitions simplify things below. */
#define KB ((int64_t)1024)
#define MB ((int64_t)1024 * KB)
#define GB ((int64_t)1024 * MB)
#define TB ((int64_t)1024 * GB)
#if ARCHIVE_VERSION_NUMBER < 2000000
static ssize_t memory_read_skip(struct archive *, void *, size_t request);
#else
static off_t memory_read_skip(struct archive *, void *, off_t request);
#endif
static ssize_t memory_read(struct archive *, void *, const void **buff);
static ssize_t memory_write(struct archive *, void *, const void *, size_t);
static ssize_t
memory_write(struct archive *a, void *_private, const void *buff, size_t size)
{
struct memdata *private = _private;
struct memblock *block;
(void)a;
/*
* Since libarchive tries to behave in a zero-copy manner, if
* you give a pointer to filedata to the library, a pointer
* into that data will (usually) pop out here. This way, we
* can tell the difference between filedata and library header
* and metadata.
*/
if ((const char *)filedata <= (const char *)buff
&& (const char *)buff < (const char *)filedata + filedatasize) {
/* We don't need to store a block of file data. */
private->last->filebytes += (int64_t)size;
} else {
/* Yes, we're assuming the very first write is metadata. */
/* It's header or metadata, copy and save it. */
block = (struct memblock *)malloc(sizeof(*block));
memset(block, 0, sizeof(*block));
block->size = size;
block->buff = malloc(size);
memcpy(block->buff, buff, size);
if (private->last == NULL) {
private->first = private->last = block;
} else {
private->last->next = block;
private->last = block;
}
block->next = NULL;
}
return ((long)size);
}
static ssize_t
memory_read(struct archive *a, void *_private, const void **buff)
{
struct memdata *private = _private;
struct memblock *block;
ssize_t size;
(void)a;
free(private->buff);
private->buff = NULL;
if (private->first == NULL) {
private->last = NULL;
return (ARCHIVE_EOF);
}
if (private->filebytes > 0) {
/*
* We're returning file bytes, simulate it by
* passing blocks from the template data.
*/
if (private->filebytes > (int64_t)filedatasize)
size = (ssize_t)filedatasize;
else
size = (ssize_t)private->filebytes;
private->filebytes -= size;
*buff = filedata;
} else {
/*
* We need to get some real data to return.
*/
block = private->first;
private->first = block->next;
size = (ssize_t)block->size;
if (block->buff != NULL) {
private->buff = block->buff;
*buff = block->buff;
} else {
private->buff = NULL;
*buff = filedata;
}
private->filebytes = block->filebytes;
free(block);
}
return (size);
}
#if ARCHIVE_VERSION_NUMBER < 2000000
static ssize_t
memory_read_skip(struct archive *a, void *private, size_t skip)
{
(void)a; /* UNUSED */
(void)private; /* UNUSED */
(void)skip; /* UNUSED */
return (0);
}
#else
static off_t
memory_read_skip(struct archive *a, void *_private, off_t skip)
{
struct memdata *private = _private;
(void)a;
if (private->first == NULL) {
private->last = NULL;
return (0);
}
if (private->filebytes > 0) {
if (private->filebytes < skip)
skip = (off_t)private->filebytes;
private->filebytes -= skip;
} else {
skip = 0;
}
return (skip);
}
#endif
DEFINE_TEST(test_tar_large)
{
/* The sizes of the entries we're going to generate. */
static int64_t tests[] = {
/* Test for 32-bit signed overflow. */
2 * GB - 1, 2 * GB, 2 * GB + 1,
/* Test for 32-bit unsigned overflow. */
4 * GB - 1, 4 * GB, 4 * GB + 1,
/* 8GB is the "official" max for ustar. */
8 * GB - 1, 8 * GB, 8 * GB + 1,
/* Bend ustar a tad and you can get 64GB (12 octal digits). */
64 * GB - 1, 64 * GB,
/* And larger entries that require non-ustar extensions. */
256 * GB, 1 * TB, 0 };
int i;
char namebuff[64];
struct memdata memdata;
struct archive_entry *ae;
struct archive *a;
int64_t filesize;
size_t writesize;
filedatasize = (size_t)(1 * MB);
filedata = malloc(filedatasize);
memset(filedata, 0xAA, filedatasize);
memset(&memdata, 0, sizeof(memdata));
/*
* Open an archive for writing.
*/
a = archive_write_new();
archive_write_set_format_pax_restricted(a);
archive_write_set_bytes_per_block(a, 0); /* No buffering. */
archive_write_open(a, &memdata, NULL, memory_write, NULL);
/*
* Write a series of large files to it.
*/
for (i = 0; tests[i] != 0; i++) {
assert((ae = archive_entry_new()) != NULL);
sprintf(namebuff, "file_%d", i);
archive_entry_copy_pathname(ae, namebuff);
archive_entry_set_mode(ae, S_IFREG | 0755);
filesize = tests[i];
archive_entry_set_size(ae, filesize);
assertA(0 == archive_write_header(a, ae));
archive_entry_free(ae);
/*
* Write the actual data to the archive.
*/
while (filesize > 0) {
writesize = filedatasize;
if ((int64_t)writesize > filesize)
writesize = (size_t)filesize;
assertA((int)writesize
== archive_write_data(a, filedata, writesize));
filesize -= writesize;
}
}
assert((ae = archive_entry_new()) != NULL);
archive_entry_copy_pathname(ae, "lastfile");
archive_entry_set_mode(ae, S_IFREG | 0755);
assertA(0 == archive_write_header(a, ae));
archive_entry_free(ae);
/* Close out the archive. */
assertA(0 == archive_write_close(a));
#if ARCHIVE_VERSION_NUMBER < 2000000
archive_write_finish(a);
#else
assertA(0 == archive_write_finish(a));
#endif
/*
* Open the same archive for reading.
*/
a = archive_read_new();
archive_read_support_format_tar(a);
archive_read_open2(a, &memdata, NULL,
memory_read, memory_read_skip, NULL);
/*
* Read entries back.
*/
for (i = 0; tests[i] > 0; i++) {
assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
sprintf(namebuff, "file_%d", i);
assertEqualString(namebuff, archive_entry_pathname(ae));
assert(tests[i] == archive_entry_size(ae));
}
assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
assertEqualString("lastfile", archive_entry_pathname(ae));
assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
/* Close out the archive. */
assertA(0 == archive_read_close(a));
#if ARCHIVE_VERSION_NUMBER < 2000000
archive_read_finish(a);
#else
assertA(0 == archive_read_finish(a));
#endif
free(memdata.buff);
free(filedata);
}
|