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