1 /*-
2 * Copyright (c) 2010-2012 Michihiro NAKAJIMA
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 #include "test.h"
26
27 #ifdef HAVE_SYS_IOCTL_H
28 #include <sys/ioctl.h>
29 #endif
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
32 #endif
33 #ifdef HAVE_FCNTL_H
34 #include <fcntl.h>
35 #endif
36 #ifdef HAVE_LIMITS_H
37 #include <limits.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #ifdef HAVE_LINUX_TYPES_H
43 #include <linux/types.h>
44 #endif
45 #ifdef HAVE_LINUX_FIEMAP_H
46 #include <linux/fiemap.h>
47 #endif
48 #ifdef HAVE_LINUX_FS_H
49 #include <linux/fs.h>
50 #endif
51
52 /* The logic to compare sparse file data read from disk with the
53 * specification is a little involved. Set to 1 to have the progress
54 * dumped. */
55 #define DEBUG 0
56
57 /*
58 * NOTE: On FreeBSD and Solaris, this test needs ZFS.
59 * You may perform this test as
60 * 'TMPDIR=<a directory on the ZFS> libarchive_test'.
61 */
62
63 struct sparse {
64 enum { DATA, HOLE, END } type;
65 size_t size;
66 };
67
68 static void create_sparse_file(const char *, const struct sparse *);
69
70 /* This should be large enough that any OS/filesystem that
71 * does support sparse files is certain to store a gap this big
72 * as a hole. */
73 /* A few data points:
74 * = ZFS on FreeBSD needs this to be at least 200kB
75 * = macOS APFS needs this to be at least 4096x4097 bytes
76 * = Linux tmpfs on 16KB-page architectures (like LoongArch64) uses
77 * 32MiB Transparent Huge Pages (THP). If a hole is exactly the
78 * size of a THP, the data blocks on either side can end up in
79 * adjacent physical folios, causing SEEK_HOLE to report the range
80 * as contiguous data.
81 *
82 * 64MiB here is enough to ensure a hole exists between THP folios on all
83 * common architectures.
84 */
85 #define MIN_HOLE (64 * 1024UL * 1024UL)
86
87 #if defined(_WIN32) && !defined(__CYGWIN__)
88 #include <winioctl.h>
89 /*
90 * Create a sparse file on Windows.
91 */
92
93 #if !defined(PATH_MAX)
94 #define PATH_MAX MAX_PATH
95 #endif
96 #if !defined(__BORLANDC__)
97 #define getcwd _getcwd
98 #endif
99
100 static int
is_sparse_supported(const char * path)101 is_sparse_supported(const char *path)
102 {
103 char root[MAX_PATH+1];
104 char vol[MAX_PATH+1];
105 char sys[MAX_PATH+1];
106 DWORD flags;
107 BOOL r;
108
109 strncpy(root, path, sizeof(root)-1);
110 if (((root[0] >= 'c' && root[0] <= 'z') ||
111 (root[0] >= 'C' && root[0] <= 'Z')) &&
112 root[1] == ':' &&
113 (root[2] == '\\' || root[2] == '/'))
114 root[3] = '\0';
115 else
116 return (0);
117 assertEqualInt((r = GetVolumeInformation(root, vol,
118 sizeof(vol), NULL, NULL, &flags, sys, sizeof(sys))), 1);
119 return (r != 0 && (flags & FILE_SUPPORTS_SPARSE_FILES) != 0);
120 }
121
122 static void
create_sparse_file(const char * path,const struct sparse * s)123 create_sparse_file(const char *path, const struct sparse *s)
124 {
125 char buff[1024];
126 HANDLE handle;
127 DWORD dmy;
128
129 memset(buff, ' ', sizeof(buff));
130
131 handle = CreateFileA(path, GENERIC_WRITE, 0,
132 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
133 NULL);
134 assert(handle != INVALID_HANDLE_VALUE);
135 assert(DeviceIoControl(handle, FSCTL_SET_SPARSE, NULL, 0,
136 NULL, 0, &dmy, NULL) != 0);
137
138 uint64_t offsetSoFar = 0;
139
140 while (s->type != END) {
141 if (s->type == HOLE) {
142 LARGE_INTEGER fileOffset, beyondOffset, distanceToMove;
143 fileOffset.QuadPart = offsetSoFar;
144 beyondOffset.QuadPart = offsetSoFar + s->size;
145 distanceToMove.QuadPart = s->size;
146
147 FILE_ZERO_DATA_INFORMATION zeroInformation;
148 zeroInformation.FileOffset = fileOffset;
149 zeroInformation.BeyondFinalZero = beyondOffset;
150
151 DWORD bytesReturned;
152 assert(SetFilePointerEx(handle, distanceToMove,
153 NULL, FILE_CURRENT) != 0);
154 assert(SetEndOfFile(handle) != 0);
155 assert(DeviceIoControl(handle, FSCTL_SET_ZERO_DATA, &zeroInformation,
156 sizeof(FILE_ZERO_DATA_INFORMATION), NULL, 0, &bytesReturned, NULL) != 0);
157 } else {
158 DWORD w, wr;
159 size_t size;
160
161 size = s->size;
162 while (size) {
163 if (size > sizeof(buff))
164 w = sizeof(buff);
165 else
166 w = (DWORD)size;
167 assert(WriteFile(handle, buff, w, &wr, NULL) != 0);
168 size -= wr;
169 }
170 }
171 offsetSoFar += s->size;
172 s++;
173 }
174 assertEqualInt(CloseHandle(handle), 1);
175 }
176
177 #else
178
179 #if defined(HAVE_LINUX_FIEMAP_H)
180 /*
181 * FIEMAP, which can detect 'hole' of a sparse file, has
182 * been supported from 2.6.28
183 */
184
185 static int
is_sparse_supported_fiemap(const char * path)186 is_sparse_supported_fiemap(const char *path)
187 {
188 const struct sparse sparse_file[] = {
189 /* This hole size is too small to create a sparse
190 * files for almost filesystem. */
191 { HOLE, 1024 }, { DATA, 10240 },
192 { END, 0 }
193 };
194 int fd, r;
195 struct fiemap *fm;
196 char buff[1024];
197 const char *testfile = "can_sparse";
198
199 (void)path; /* UNUSED */
200 memset(buff, 0, sizeof(buff));
201 create_sparse_file(testfile, sparse_file);
202 fd = open(testfile, O_RDWR);
203 if (fd < 0)
204 return (0);
205 fm = (struct fiemap *)buff;
206 fm->fm_start = 0;
207 fm->fm_length = ~0ULL;
208 fm->fm_flags = FIEMAP_FLAG_SYNC;
209 fm->fm_extent_count = (sizeof(buff) - sizeof(*fm))/
210 sizeof(struct fiemap_extent);
211 r = ioctl(fd, FS_IOC_FIEMAP, fm);
212 close(fd);
213 unlink(testfile);
214 return (r >= 0);
215 }
216
217 #if !defined(SEEK_HOLE) || !defined(SEEK_DATA)
218 static int
is_sparse_supported(const char * path)219 is_sparse_supported(const char *path)
220 {
221 return is_sparse_supported_fiemap(path);
222 }
223 #endif
224 #endif
225
226 #if defined(_PC_MIN_HOLE_SIZE)
227
228 /*
229 * FreeBSD and Solaris can detect 'hole' of a sparse file
230 * through lseek(HOLE) on ZFS. (UFS does not support yet)
231 */
232
233 static int
is_sparse_supported(const char * path)234 is_sparse_supported(const char *path)
235 {
236 return (pathconf(path, _PC_MIN_HOLE_SIZE) > 0);
237 }
238
239 #elif defined(SEEK_HOLE) && defined(SEEK_DATA)
240
241 static int
is_sparse_supported(const char * path)242 is_sparse_supported(const char *path)
243 {
244 const struct sparse sparse_file[] = {
245 /* This hole size is too small to create a sparse
246 * files for almost filesystem. */
247 { HOLE, 1024 }, { DATA, 10240 },
248 { END, 0 }
249 };
250 int fd, r;
251 const char *testfile = "can_sparse";
252
253 (void)path; /* UNUSED */
254 create_sparse_file(testfile, sparse_file);
255 fd = open(testfile, O_RDWR);
256 if (fd < 0)
257 return (0);
258 r = lseek(fd, 0, SEEK_HOLE);
259 close(fd);
260 unlink(testfile);
261 #if defined(HAVE_LINUX_FIEMAP_H)
262 if (r < 0)
263 return (is_sparse_supported_fiemap(path));
264 return (1);
265 #else
266 return (r >= 0);
267 #endif
268 }
269
270 #elif !defined(HAVE_LINUX_FIEMAP_H)
271
272 /*
273 * Other system may do not have the API such as lseek(HOLE),
274 * which detect 'hole' of a sparse file.
275 */
276
277 static int
is_sparse_supported(const char * path)278 is_sparse_supported(const char *path)
279 {
280 (void)path; /* UNUSED */
281 return (0);
282 }
283
284 #endif
285
286 /*
287 * Create a sparse file on POSIX like system.
288 */
289
290 static void
create_sparse_file(const char * path,const struct sparse * s)291 create_sparse_file(const char *path, const struct sparse *s)
292 {
293 char buff[1024];
294 int fd;
295 uint64_t total_size = 0;
296 const struct sparse *cur = s;
297
298 memset(buff, ' ', sizeof(buff));
299 assert((fd = open(path, O_CREAT | O_WRONLY, 0600)) != -1);
300
301 /* Handle holes at the end by extending the file */
302 while (cur->type != END) {
303 total_size += cur->size;
304 ++cur;
305 }
306 assert(ftruncate(fd, total_size) != -1);
307
308 while (s->type != END) {
309 if (s->type == HOLE) {
310 assert(lseek(fd, s->size, SEEK_CUR) != (off_t)-1);
311 } else {
312 size_t w, size;
313
314 size = s->size;
315 while (size) {
316 if (size > sizeof(buff))
317 w = sizeof(buff);
318 else
319 w = size;
320 assert(write(fd, buff, w) != (ssize_t)-1);
321 size -= w;
322 }
323 }
324 s++;
325 }
326 close(fd);
327 }
328
329 #endif
330
331 /*
332 * Sparse test with directory traversals.
333 */
334 static void
verify_sparse_file(struct archive * a,const char * path,const struct sparse * sparse,int expected_holes)335 verify_sparse_file(struct archive *a, const char *path,
336 const struct sparse *sparse, int expected_holes)
337 {
338 struct archive_entry *ae;
339 const void *buff;
340 size_t bytes_read;
341 int64_t offset, expected_offset, last_offset;
342 int holes_seen = 0;
343
344 create_sparse_file(path, sparse);
345 assert((ae = archive_entry_new()) != NULL);
346 assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_open(a, path));
347 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header2(a, ae));
348
349 expected_offset = 0;
350 last_offset = 0;
351 while (ARCHIVE_OK == archive_read_data_block(a, &buff, &bytes_read,
352 &offset)) {
353 const char *start = buff;
354 #if DEBUG
355 fprintf(stderr, "%s: bytes_read=%d offset=%d\n", path, (int)bytes_read, (int)offset);
356 #endif
357 if (offset > last_offset) {
358 ++holes_seen;
359 }
360 /* Blocks entirely before the data we just read. */
361 while (expected_offset + (int64_t)sparse->size < offset) {
362 #if DEBUG
363 fprintf(stderr, " skipping expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
364 #endif
365 /* Must be holes. */
366 assert(sparse->type == HOLE);
367 expected_offset += sparse->size;
368 ++sparse;
369 }
370 /* Block that overlaps beginning of data */
371 if (expected_offset < offset
372 && expected_offset + (int64_t)sparse->size <= offset + (int64_t)bytes_read) {
373 /* Avoid forming an intermediate pointer before buff. */
374 const char *end = (const char *)buff
375 + ((expected_offset - offset) + (int64_t)sparse->size);
376 #if DEBUG
377 fprintf(stderr, " overlapping hole expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
378 #endif
379 if (sparse->type == HOLE) {
380 assertMemoryFilledWith(start, end - start, '\0');
381 } else if (assert(sparse->type == DATA)) {
382 assertMemoryFilledWith(start, end - start, ' ');
383 }
384 start = end;
385 expected_offset += sparse->size;
386 ++sparse;
387 }
388 /* Blocks completely contained in data we just read. */
389 while (expected_offset + (int64_t)sparse->size <= offset + (int64_t)bytes_read) {
390 const char *end = (const char *)buff
391 + ((expected_offset - offset) + (int64_t)sparse->size);
392 if (sparse->type == HOLE) {
393 #if DEBUG
394 fprintf(stderr, " contained hole expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
395 #endif
396
397 /* verify data corresponding to hole is '\0' */
398 if (end > (const char *)buff + bytes_read) {
399 end = (const char *)buff + bytes_read;
400 }
401 assertMemoryFilledWith(start, end - start, '\0');
402 start = end;
403 expected_offset += sparse->size;
404 ++sparse;
405 } else if (sparse->type == DATA) {
406 #if DEBUG
407 fprintf(stderr, " contained data expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
408 #endif
409 /* verify data corresponding to hole is ' ' */
410 if (assert(expected_offset + sparse->size <= offset + bytes_read)) {
411 assert(start == (const char *)buff + (size_t)(expected_offset - offset));
412 assertMemoryFilledWith(start, end - start, ' ');
413 }
414 start = end;
415 expected_offset += sparse->size;
416 ++sparse;
417 } else {
418 break;
419 }
420 }
421 /* Block that overlaps end of data */
422 if (expected_offset < offset + (int64_t)bytes_read) {
423 const char *end = (const char *)buff + bytes_read;
424 #if DEBUG
425 fprintf(stderr, " trailing overlap expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
426 #endif
427 if (sparse->type == HOLE) {
428 assertMemoryFilledWith(start, end - start, '\0');
429 } else if (assert(sparse->type == DATA)) {
430 assertMemoryFilledWith(start, end - start, ' ');
431 }
432 }
433 last_offset = offset + bytes_read;
434 }
435 /* Count a hole at EOF? */
436 if (last_offset < archive_entry_size(ae)) {
437 ++holes_seen;
438 }
439
440 /* Verify blocks after last read */
441 while (sparse->type == HOLE) {
442 expected_offset += sparse->size;
443 ++sparse;
444 }
445 assert(sparse->type == END);
446 assertEqualInt(expected_offset, archive_entry_size(ae));
447
448 failure("%s", path);
449 assertEqualInt(holes_seen, expected_holes);
450
451 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
452 archive_entry_free(ae);
453 }
454
455 #if defined(_WIN32) && !defined(__CYGWIN__)
456 #define close _close
457 #define open _open
458 #endif
459
460 /*
461 * Sparse test without directory traversals.
462 */
463 static void
verify_sparse_file2(struct archive * a,const char * path,const struct sparse * sparse,int blocks,int preopen)464 verify_sparse_file2(struct archive *a, const char *path,
465 const struct sparse *sparse, int blocks, int preopen)
466 {
467 struct archive_entry *ae;
468 int fd;
469
470 (void)sparse; /* UNUSED */
471 assert((ae = archive_entry_new()) != NULL);
472 archive_entry_set_pathname(ae, path);
473 if (preopen)
474 fd = open(path, O_RDONLY | O_BINARY);
475 else
476 fd = -1;
477 assertEqualIntA(a, ARCHIVE_OK,
478 archive_read_disk_entry_from_file(a, ae, fd, NULL));
479 if (fd >= 0)
480 close(fd);
481 /* Verify the number of holes only, not its offset nor its
482 * length because those alignments are deeply dependence on
483 * its filesystem. */
484 failure("%s", path);
485 assertEqualInt(blocks, archive_entry_sparse_count(ae));
486 archive_entry_free(ae);
487 }
488
489 static void
test_sparse_whole_file_data(void)490 test_sparse_whole_file_data(void)
491 {
492 struct archive_entry *ae;
493 int64_t offset;
494 int i;
495
496 assert((ae = archive_entry_new()) != NULL);
497 archive_entry_set_size(ae, 1024*10);
498
499 /*
500 * Add sparse block data up to the file size.
501 */
502 offset = 0;
503 for (i = 0; i < 10; i++) {
504 archive_entry_sparse_add_entry(ae, offset, 1024);
505 offset += 1024;
506 }
507
508 failure("There should be no sparse");
509 assertEqualInt(0, archive_entry_sparse_count(ae));
510 archive_entry_free(ae);
511 }
512
DEFINE_TEST(test_sparse_basic)513 DEFINE_TEST(test_sparse_basic)
514 {
515 char *cwd;
516 struct archive *a;
517 const char *skip_sparse_tests;
518 /*
519 * The alignment of the hole of sparse files deeply depends
520 * on filesystem. In my experience, sparse_file2 test with
521 * 204800 bytes hole size did not pass on ZFS and the result
522 * of that test seemed the size was too small, thus you should
523 * keep a hole size more than 409600 bytes to pass this test
524 * on all platform.
525 */
526 const struct sparse sparse_file0[] = {
527 // 0 // 1024
528 { DATA, 1024 }, { HOLE, MIN_HOLE + 1638400 },
529 // 2049024 // 2051072
530 { DATA, 2048 }, { HOLE, MIN_HOLE + 1638400 },
531 // 4099072 // 4103168
532 { DATA, 4096 }, { HOLE, MIN_HOLE + 20070400 },
533 // 24583168 // 24591360
534 { DATA, 8192 }, { HOLE, MIN_HOLE + 204390400 },
535 // 229391360 // 229391361
536 { DATA, 1 }, { END, 0 }
537 };
538 const struct sparse sparse_file1[] = {
539 { HOLE, MIN_HOLE }, { DATA, 1 },
540 { HOLE, MIN_HOLE }, { DATA, 1 },
541 { HOLE, MIN_HOLE }, { END, 0 }
542 };
543 const struct sparse sparse_file2[] = {
544 { HOLE, MIN_HOLE }, { DATA, 1024 },
545 { HOLE, MIN_HOLE + 409600 * 1 }, { DATA, 1024 },
546 { HOLE, MIN_HOLE + 409600 * 2 }, { DATA, 1024 },
547 { HOLE, MIN_HOLE + 409600 * 3 }, { DATA, 1024 },
548 { HOLE, MIN_HOLE + 409600 * 4 }, { DATA, 1024 },
549 { HOLE, MIN_HOLE + 409600 * 5 }, { DATA, 1024 },
550 { HOLE, MIN_HOLE + 409600 * 6 }, { DATA, 1024 },
551 { HOLE, MIN_HOLE + 409600 * 7 }, { DATA, 1024 },
552 { HOLE, MIN_HOLE + 409600 * 8 }, { DATA, 1024 },
553 { HOLE, MIN_HOLE + 409600 * 9}, { DATA, 1024 },/* 10 */
554 { HOLE, MIN_HOLE }, { DATA, 1024 * 1 },
555 { HOLE, MIN_HOLE + 409600 * 1 }, { DATA, 1024 * 2 },
556 { HOLE, MIN_HOLE + 409600 * 2 }, { DATA, 1024 * 3 },
557 { HOLE, MIN_HOLE + 409600 * 3 }, { DATA, 1024 * 4 },
558 { HOLE, MIN_HOLE + 409600 * 4 }, { DATA, 1024 * 5 },
559 { HOLE, MIN_HOLE + 409600 * 5 }, { DATA, 1024 * 6 },
560 { HOLE, MIN_HOLE + 409600 * 6 }, { DATA, 1024 * 7 },
561 { HOLE, MIN_HOLE + 409600 * 7 }, { DATA, 1024 * 8 },
562 { HOLE, MIN_HOLE + 409600 * 8 }, { DATA, 1024 * 9 },
563 { HOLE, MIN_HOLE + 409600 * 9}, { DATA, 1024 * 10},/* 20 */
564 { END, 0 }
565 };
566 const struct sparse sparse_file3[] = {
567 /* This hole size is too small to create a sparse file */
568 { HOLE, 1 }, { DATA, 10240 },
569 { HOLE, 1 }, { DATA, 10240 },
570 { HOLE, 1 }, { DATA, 10240 },
571 { END, 0 }
572 };
573 const struct sparse sparse_file4[] = {
574 { DATA, 4096 }, { HOLE, 0xc0000000 },
575 /* This hole overflows the offset if stored in 32 bits. */
576 { DATA, 4096 }, { HOLE, 0x50000000 },
577 { END, 0 }
578 };
579
580 /*
581 * Test for the case that sparse data indicates just the whole file
582 * data.
583 */
584 test_sparse_whole_file_data();
585
586 skip_sparse_tests = getenv("SKIP_TEST_SPARSE");
587 if (skip_sparse_tests != NULL) {
588 skipping("Skipping sparse tests due to SKIP_TEST_SPARSE "
589 "environment variable");
590 return;
591 }
592
593 /* Check if the filesystem where CWD on can
594 * report the number of the holes of a sparse file. */
595 #if defined(PATH_MAX) && !defined(__GLIBC__)
596 cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
597 #else
598 cwd = getcwd(NULL, 0);
599 #endif
600 if (!assert(cwd != NULL))
601 return;
602 if (!is_sparse_supported(cwd)) {
603 free(cwd);
604 skipping("This filesystem or platform do not support "
605 "the reporting of the holes of a sparse file through "
606 "API such as lseek(HOLE)");
607 return;
608 }
609
610 /*
611 * Get sparse data through directory traversals.
612 */
613 assert((a = archive_read_disk_new()) != NULL);
614
615 verify_sparse_file(a, "file0", sparse_file0, 4);
616 verify_sparse_file(a, "file1", sparse_file1, 3);
617 verify_sparse_file(a, "file2", sparse_file2, 20);
618 /* Encoded non sparse; expect a data block but no sparse entries. */
619 verify_sparse_file(a, "file3", sparse_file3, 0);
620 if (sizeof(off_t) > 4)
621 verify_sparse_file(a, "file4", sparse_file4, 2);
622
623 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
624
625 /*
626 * Get sparse data through archive_read_disk_entry_from_file().
627 */
628 assert((a = archive_read_disk_new()) != NULL);
629
630 verify_sparse_file2(a, "file0", sparse_file0, 5, 0);
631 verify_sparse_file2(a, "file0", sparse_file0, 5, 1);
632
633 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
634
635 /*
636 * Test that setting ARCHIVE_READDISK_NO_SPARSE
637 * creates no sparse entries.
638 */
639 assert((a = archive_read_disk_new()) != NULL);
640
641 assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_set_behavior(a,
642 ARCHIVE_READDISK_NO_SPARSE));
643
644 verify_sparse_file(a, "file0", sparse_file0, 0);
645 verify_sparse_file(a, "file1", sparse_file1, 0);
646 verify_sparse_file(a, "file2", sparse_file2, 0);
647 verify_sparse_file(a, "file3", sparse_file3, 0);
648 if (sizeof(off_t) > 4)
649 verify_sparse_file(a, "file4", sparse_file4, 0);
650
651 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
652
653 assert((a = archive_read_disk_new()) != NULL);
654
655 assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_set_behavior(a,
656 ARCHIVE_READDISK_NO_SPARSE));
657
658 verify_sparse_file2(a, "file0", sparse_file0, 0, 0);
659 verify_sparse_file2(a, "file0", sparse_file0, 0, 1);
660
661 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
662 free(cwd);
663 }
664
DEFINE_TEST(test_fully_sparse_files)665 DEFINE_TEST(test_fully_sparse_files)
666 {
667 char *cwd;
668 struct archive *a;
669 const char *skip_sparse_tests;
670
671 const struct sparse sparse_file[] = {
672 { HOLE, MIN_HOLE }, { END, 0 }
673 };
674
675 skip_sparse_tests = getenv("SKIP_TEST_SPARSE");
676 if (skip_sparse_tests != NULL) {
677 skipping("Skipping sparse tests due to SKIP_TEST_SPARSE "
678 "environment variable");
679 return;
680 }
681
682 /* Check if the filesystem where CWD on can
683 * report the number of the holes of a sparse file. */
684 #if defined(PATH_MAX) && !defined(__GLIBC__)
685 cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
686 #else
687 cwd = getcwd(NULL, 0);
688 #endif
689 if (!assert(cwd != NULL))
690 return;
691 if (!is_sparse_supported(cwd)) {
692 free(cwd);
693 skipping("This filesystem or platform do not support "
694 "the reporting of the holes of a sparse file through "
695 "API such as lseek(HOLE)");
696 return;
697 }
698
699 assert((a = archive_read_disk_new()) != NULL);
700
701 /* Fully sparse files are encoded with a zero-length "data" block. */
702 verify_sparse_file(a, "file0", sparse_file, 1);
703
704 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
705 free(cwd);
706 }
707
DEFINE_TEST(test_sparse_iterator)708 DEFINE_TEST(test_sparse_iterator)
709 {
710 struct archive_entry *entry;
711 int64_t offset, length;
712 int count;
713
714 entry = archive_entry_new();
715 archive_entry_set_pathname(entry, "testfile");
716 archive_entry_set_mode(entry, 0100644);
717 archive_entry_set_size(entry, 1024);
718
719 /* Add one sparse block covering the entire file */
720 archive_entry_sparse_add_entry(entry, 0, 1024);
721
722 /* Should remove the only block covering the entire file */
723 archive_entry_sparse_reset(entry);
724
725 count = 0;
726 while (archive_entry_sparse_next(entry, &offset, &length) == ARCHIVE_OK)
727 count++;
728 assertEqualInt(0, count);
729 assertEqualInt(0, archive_entry_sparse_count(entry));
730
731 archive_entry_free(entry);
732 }
733