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
is_sparse_supported(const char * path)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
create_sparse_file(const char * path,const struct sparse * s)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
is_sparse_supported_fiemap(const char * path)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
is_sparse_supported(const char * path)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
is_sparse_supported(const char * path)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
is_sparse_supported(const char * path)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 #endif
257 	return (r >= 0);
258 }
259 
260 #elif !defined(HAVE_LINUX_FIEMAP_H)
261 
262 /*
263  * Other system may do not have the API such as lseek(HOLE),
264  * which detect 'hole' of a sparse file.
265  */
266 
267 static int
is_sparse_supported(const char * path)268 is_sparse_supported(const char *path)
269 {
270 	(void)path; /* UNUSED */
271 	return (0);
272 }
273 
274 #endif
275 
276 /*
277  * Create a sparse file on POSIX like system.
278  */
279 
280 static void
create_sparse_file(const char * path,const struct sparse * s)281 create_sparse_file(const char *path, const struct sparse *s)
282 {
283 	char buff[1024];
284 	int fd;
285 	uint64_t total_size = 0;
286 	const struct sparse *cur = s;
287 
288 	memset(buff, ' ', sizeof(buff));
289 	assert((fd = open(path, O_CREAT | O_WRONLY, 0600)) != -1);
290 
291 	/* Handle holes at the end by extending the file */
292 	while (cur->type != END) {
293 		total_size += cur->size;
294 		++cur;
295 	}
296 	assert(ftruncate(fd, total_size) != -1);
297 
298 	while (s->type != END) {
299 		if (s->type == HOLE) {
300 			assert(lseek(fd, s->size, SEEK_CUR) != (off_t)-1);
301 		} else {
302 			size_t w, size;
303 
304 			size = s->size;
305 			while (size) {
306 				if (size > sizeof(buff))
307 					w = sizeof(buff);
308 				else
309 					w = size;
310 				assert(write(fd, buff, w) != (ssize_t)-1);
311 				size -= w;
312 			}
313 		}
314 		s++;
315 	}
316 	close(fd);
317 }
318 
319 #endif
320 
321 /*
322  * Sparse test with directory traversals.
323  */
324 static void
verify_sparse_file(struct archive * a,const char * path,const struct sparse * sparse,int expected_holes)325 verify_sparse_file(struct archive *a, const char *path,
326     const struct sparse *sparse, int expected_holes)
327 {
328 	struct archive_entry *ae;
329 	const void *buff;
330 	size_t bytes_read;
331 	int64_t offset, expected_offset, last_offset;
332 	int holes_seen = 0;
333 
334 	create_sparse_file(path, sparse);
335 	assert((ae = archive_entry_new()) != NULL);
336 	assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_open(a, path));
337 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header2(a, ae));
338 
339 	expected_offset = 0;
340 	last_offset = 0;
341 	while (ARCHIVE_OK == archive_read_data_block(a, &buff, &bytes_read,
342 	    &offset)) {
343 		const char *start = buff;
344 #if DEBUG
345 		fprintf(stderr, "%s: bytes_read=%d offset=%d\n", path, (int)bytes_read, (int)offset);
346 #endif
347 		if (offset > last_offset) {
348 			++holes_seen;
349 		}
350 		/* Blocks entirely before the data we just read. */
351 		while (expected_offset + (int64_t)sparse->size < offset) {
352 #if DEBUG
353 			fprintf(stderr, "    skipping expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
354 #endif
355 			/* Must be holes. */
356 			assert(sparse->type == HOLE);
357 			expected_offset += sparse->size;
358 			++sparse;
359 		}
360 		/* Block that overlaps beginning of data */
361 		if (expected_offset < offset
362 		    && expected_offset + (int64_t)sparse->size <= offset + (int64_t)bytes_read) {
363 			const char *end = (const char *)buff + (expected_offset - offset) + (size_t)sparse->size;
364 #if DEBUG
365 			fprintf(stderr, "    overlapping hole expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
366 #endif
367 			if (sparse->type == HOLE) {
368 				assertMemoryFilledWith(start, end - start, '\0');
369 			} else if (assert(sparse->type == DATA)) {
370 				assertMemoryFilledWith(start, end - start, ' ');
371 			}
372 			start = end;
373 			expected_offset += sparse->size;
374 			++sparse;
375 		}
376 		/* Blocks completely contained in data we just read. */
377 		while (expected_offset + (int64_t)sparse->size <= offset + (int64_t)bytes_read) {
378 			const char *end = (const char *)buff + (expected_offset - offset) + (size_t)sparse->size;
379 			if (sparse->type == HOLE) {
380 #if DEBUG
381 				fprintf(stderr, "    contained hole expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
382 #endif
383 
384 				/* verify data corresponding to hole is '\0' */
385 				if (end > (const char *)buff + bytes_read) {
386 					end = (const char *)buff + bytes_read;
387 				}
388 				assertMemoryFilledWith(start, end - start, '\0');
389 				start = end;
390 				expected_offset += sparse->size;
391 				++sparse;
392 			} else if (sparse->type == DATA) {
393 #if DEBUG
394 				fprintf(stderr, "    contained data expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
395 #endif
396 				/* verify data corresponding to hole is ' ' */
397 				if (assert(expected_offset + sparse->size <= offset + bytes_read)) {
398 					assert(start == (const char *)buff + (size_t)(expected_offset - offset));
399 					assertMemoryFilledWith(start, end - start, ' ');
400 				}
401 				start = end;
402 				expected_offset += sparse->size;
403 				++sparse;
404 			} else {
405 				break;
406 			}
407 		}
408 		/* Block that overlaps end of data */
409 		if (expected_offset < offset + (int64_t)bytes_read) {
410 			const char *end = (const char *)buff + bytes_read;
411 #if DEBUG
412 			fprintf(stderr, "    trailing overlap expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
413 #endif
414 			if (sparse->type == HOLE) {
415 				assertMemoryFilledWith(start, end - start, '\0');
416 			} else if (assert(sparse->type == DATA)) {
417 				assertMemoryFilledWith(start, end - start, ' ');
418 			}
419 		}
420 		last_offset = offset + bytes_read;
421 	}
422 	/* Count a hole at EOF? */
423 	if (last_offset < archive_entry_size(ae)) {
424 		++holes_seen;
425 	}
426 
427 	/* Verify blocks after last read */
428 	while (sparse->type == HOLE) {
429 		expected_offset += sparse->size;
430 		++sparse;
431 	}
432 	assert(sparse->type == END);
433 	assertEqualInt(expected_offset, archive_entry_size(ae));
434 
435 	failure("%s", path);
436 	assertEqualInt(holes_seen, expected_holes);
437 
438 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
439 	archive_entry_free(ae);
440 }
441 
442 #if defined(_WIN32) && !defined(__CYGWIN__)
443 #define	close		_close
444 #define	open		_open
445 #endif
446 
447 /*
448  * Sparse test without directory traversals.
449  */
450 static void
verify_sparse_file2(struct archive * a,const char * path,const struct sparse * sparse,int blocks,int preopen)451 verify_sparse_file2(struct archive *a, const char *path,
452     const struct sparse *sparse, int blocks, int preopen)
453 {
454 	struct archive_entry *ae;
455 	int fd;
456 
457 	(void)sparse; /* UNUSED */
458 	assert((ae = archive_entry_new()) != NULL);
459 	archive_entry_set_pathname(ae, path);
460 	if (preopen)
461 		fd = open(path, O_RDONLY | O_BINARY);
462 	else
463 		fd = -1;
464 	assertEqualIntA(a, ARCHIVE_OK,
465 	    archive_read_disk_entry_from_file(a, ae, fd, NULL));
466 	if (fd >= 0)
467 		close(fd);
468 	/* Verify the number of holes only, not its offset nor its
469 	 * length because those alignments are deeply dependence on
470 	 * its filesystem. */
471 	failure("%s", path);
472 	assertEqualInt(blocks, archive_entry_sparse_count(ae));
473 	archive_entry_free(ae);
474 }
475 
476 static void
test_sparse_whole_file_data(void)477 test_sparse_whole_file_data(void)
478 {
479 	struct archive_entry *ae;
480 	int64_t offset;
481 	int i;
482 
483 	assert((ae = archive_entry_new()) != NULL);
484 	archive_entry_set_size(ae, 1024*10);
485 
486 	/*
487 	 * Add sparse block data up to the file size.
488 	 */
489 	offset = 0;
490 	for (i = 0; i < 10; i++) {
491 		archive_entry_sparse_add_entry(ae, offset, 1024);
492 		offset += 1024;
493 	}
494 
495 	failure("There should be no sparse");
496 	assertEqualInt(0, archive_entry_sparse_count(ae));
497 	archive_entry_free(ae);
498 }
499 
DEFINE_TEST(test_sparse_basic)500 DEFINE_TEST(test_sparse_basic)
501 {
502 	char *cwd;
503 	struct archive *a;
504 	const char *skip_sparse_tests;
505 	/*
506 	 * The alignment of the hole of sparse files deeply depends
507 	 * on filesystem. In my experience, sparse_file2 test with
508 	 * 204800 bytes hole size did not pass on ZFS and the result
509 	 * of that test seemed the size was too small, thus you should
510 	 * keep a hole size more than 409600 bytes to pass this test
511 	 * on all platform.
512 	 */
513 	const struct sparse sparse_file0[] = {
514 		// 0             // 1024
515 		{ DATA,	 1024 }, { HOLE,   MIN_HOLE + 1638400 },
516 		// 2049024       // 2051072
517 		{ DATA,	 2048 }, { HOLE,   MIN_HOLE + 1638400 },
518 		// 4099072       // 4103168
519 		{ DATA,	 4096 }, { HOLE,  MIN_HOLE + 20070400 },
520 		// 24583168      // 24591360
521 		{ DATA,	 8192 }, { HOLE, MIN_HOLE + 204390400 },
522 		// 229391360     // 229391361
523 		{ DATA,     1 }, { END,	0 }
524 	};
525 	const struct sparse sparse_file1[] = {
526 		{ HOLE,	MIN_HOLE }, { DATA, 1 },
527 		{ HOLE,	MIN_HOLE }, { DATA, 1 },
528 		{ HOLE, MIN_HOLE }, { END,  0 }
529 	};
530 	const struct sparse sparse_file2[] = {
531 		{ HOLE,	MIN_HOLE }, { DATA, 1024 },
532 		{ HOLE,	MIN_HOLE + 409600 * 1 }, { DATA, 1024 },
533 		{ HOLE,	MIN_HOLE + 409600 * 2 }, { DATA, 1024 },
534 		{ HOLE,	MIN_HOLE + 409600 * 3 }, { DATA, 1024 },
535 		{ HOLE,	MIN_HOLE + 409600 * 4 }, { DATA, 1024 },
536 		{ HOLE,	MIN_HOLE + 409600 * 5 }, { DATA, 1024 },
537 		{ HOLE,	MIN_HOLE + 409600 * 6 }, { DATA, 1024 },
538 		{ HOLE,	MIN_HOLE + 409600 * 7 }, { DATA, 1024 },
539 		{ HOLE,	MIN_HOLE + 409600 * 8 }, { DATA, 1024 },
540 		{ HOLE,	MIN_HOLE + 409600 * 9}, { DATA, 1024 },/* 10 */
541 		{ HOLE,	MIN_HOLE }, { DATA, 1024 * 1 },
542 		{ HOLE,	MIN_HOLE + 409600 * 1 }, { DATA, 1024 * 2 },
543 		{ HOLE,	MIN_HOLE + 409600 * 2 }, { DATA, 1024 * 3 },
544 		{ HOLE,	MIN_HOLE + 409600 * 3 }, { DATA, 1024 * 4 },
545 		{ HOLE,	MIN_HOLE + 409600 * 4 }, { DATA, 1024 * 5 },
546 		{ HOLE,	MIN_HOLE + 409600 * 5 }, { DATA, 1024 * 6 },
547 		{ HOLE,	MIN_HOLE + 409600 * 6 }, { DATA, 1024 * 7 },
548 		{ HOLE,	MIN_HOLE + 409600 * 7 }, { DATA, 1024 * 8 },
549 		{ HOLE,	MIN_HOLE + 409600 * 8 }, { DATA, 1024 * 9 },
550 		{ HOLE,	MIN_HOLE + 409600 * 9}, { DATA, 1024 * 10},/* 20 */
551 		{ END,	0 }
552 	};
553 	const struct sparse sparse_file3[] = {
554  		/* This hole size is too small to create a sparse file */
555 		{ HOLE,	 1 }, { DATA, 10240 },
556 		{ HOLE,	 1 }, { DATA, 10240 },
557 		{ HOLE,	 1 }, { DATA, 10240 },
558 		{ END,	0 }
559 	};
560 	const struct sparse sparse_file4[] = {
561 		{ DATA, 4096 }, { HOLE, 0xc0000000 },
562 		/* This hole overflows the offset if stored in 32 bits. */
563 		{ DATA, 4096 }, { HOLE, 0x50000000 },
564 		{ END, 0 }
565 	};
566 
567 	/*
568 	 * Test for the case that sparse data indicates just the whole file
569 	 * data.
570 	 */
571 	test_sparse_whole_file_data();
572 
573 	skip_sparse_tests = getenv("SKIP_TEST_SPARSE");
574 	if (skip_sparse_tests != NULL) {
575 		skipping("Skipping sparse tests due to SKIP_TEST_SPARSE "
576 		    "environment variable");
577 		return;
578 	}
579 
580 	/* Check if the filesystem where CWD on can
581 	 * report the number of the holes of a sparse file. */
582 #if defined(PATH_MAX) && !defined(__GLIBC__)
583 	cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
584 #else
585 	cwd = getcwd(NULL, 0);
586 #endif
587 	if (!assert(cwd != NULL))
588 		return;
589 	if (!is_sparse_supported(cwd)) {
590 		free(cwd);
591 		skipping("This filesystem or platform do not support "
592 		    "the reporting of the holes of a sparse file through "
593 		    "API such as lseek(HOLE)");
594 		return;
595 	}
596 
597 	/*
598 	 * Get sparse data through directory traversals.
599 	 */
600 	assert((a = archive_read_disk_new()) != NULL);
601 
602 	verify_sparse_file(a, "file0", sparse_file0, 4);
603 	verify_sparse_file(a, "file1", sparse_file1, 3);
604 	verify_sparse_file(a, "file2", sparse_file2, 20);
605 	/* Encoded non sparse; expect a data block but no sparse entries. */
606 	verify_sparse_file(a, "file3", sparse_file3, 0);
607 	verify_sparse_file(a, "file4", sparse_file4, 2);
608 
609 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
610 
611 	/*
612 	 * Get sparse data through archive_read_disk_entry_from_file().
613 	 */
614 	assert((a = archive_read_disk_new()) != NULL);
615 
616 	verify_sparse_file2(a, "file0", sparse_file0, 5, 0);
617 	verify_sparse_file2(a, "file0", sparse_file0, 5, 1);
618 
619 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
620 
621 	/*
622 	 * Test that setting ARCHIVE_READDISK_NO_SPARSE
623 	 * creates no sparse entries.
624 	 */
625 	assert((a = archive_read_disk_new()) != NULL);
626 
627 	assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_set_behavior(a,
628 		ARCHIVE_READDISK_NO_SPARSE));
629 
630 	verify_sparse_file(a, "file0", sparse_file0, 0);
631 	verify_sparse_file(a, "file1", sparse_file1, 0);
632 	verify_sparse_file(a, "file2", sparse_file2, 0);
633 	verify_sparse_file(a, "file3", sparse_file3, 0);
634 	verify_sparse_file(a, "file4", sparse_file4, 0);
635 
636 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
637 
638 	assert((a = archive_read_disk_new()) != NULL);
639 
640 	assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_set_behavior(a,
641 		ARCHIVE_READDISK_NO_SPARSE));
642 
643 	verify_sparse_file2(a, "file0", sparse_file0, 0, 0);
644 	verify_sparse_file2(a, "file0", sparse_file0, 0, 1);
645 
646 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
647 	free(cwd);
648 }
649 
DEFINE_TEST(test_fully_sparse_files)650 DEFINE_TEST(test_fully_sparse_files)
651 {
652 	char *cwd;
653 	struct archive *a;
654 	const char *skip_sparse_tests;
655 
656 	const struct sparse sparse_file[] = {
657 		{ HOLE, MIN_HOLE }, { END, 0 }
658 	};
659 
660 	skip_sparse_tests = getenv("SKIP_TEST_SPARSE");
661 	if (skip_sparse_tests != NULL) {
662 		skipping("Skipping sparse tests due to SKIP_TEST_SPARSE "
663 		    "environment variable");
664 		return;
665 	}
666 
667 	/* Check if the filesystem where CWD on can
668 	 * report the number of the holes of a sparse file. */
669 #if defined(PATH_MAX) && !defined(__GLIBC__)
670 	cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
671 #else
672 	cwd = getcwd(NULL, 0);
673 #endif
674 	if (!assert(cwd != NULL))
675 		return;
676 	if (!is_sparse_supported(cwd)) {
677 		free(cwd);
678 		skipping("This filesystem or platform do not support "
679 		    "the reporting of the holes of a sparse file through "
680 		    "API such as lseek(HOLE)");
681 		return;
682 	}
683 
684 	assert((a = archive_read_disk_new()) != NULL);
685 
686 	/* Fully sparse files are encoded with a zero-length "data" block. */
687 	verify_sparse_file(a, "file0", sparse_file, 1);
688 
689 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
690 	free(cwd);
691 }
692