2 * Copyright (c) 2010-2012 Michihiro NAKAJIMA
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$");
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
31 #ifdef HAVE_SYS_PARAM_H
32 #include <sys/param.h>
43 #ifdef HAVE_LINUX_TYPES_H
44 #include <linux/types.h>
46 #ifdef HAVE_LINUX_FIEMAP_H
47 #include <linux/fiemap.h>
49 #ifdef HAVE_LINUX_FS_H
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
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'.
65 enum { DATA, HOLE, END } type;
69 static void create_sparse_file(const char *, const struct sparse *);
71 #if defined(_WIN32) && !defined(__CYGWIN__)
74 * Create a sparse file on Windows.
77 #if !defined(PATH_MAX)
78 #define PATH_MAX MAX_PATH
80 #if !defined(__BORLANDC__)
81 #define getcwd _getcwd
85 is_sparse_supported(const char *path)
87 char root[MAX_PATH+1];
93 strncpy(root, path, sizeof(root)-1);
94 if (((root[0] >= 'c' && root[0] <= 'z') ||
95 (root[0] >= 'C' && root[0] <= 'Z')) &&
97 (root[2] == '\\' || root[2] == '/'))
101 assertEqualInt((r = GetVolumeInformation(root, vol,
102 sizeof(vol), NULL, NULL, &flags, sys, sizeof(sys))), 1);
103 return (r != 0 && (flags & FILE_SUPPORTS_SPARSE_FILES) != 0);
107 create_sparse_file(const char *path, const struct sparse *s)
113 memset(buff, ' ', sizeof(buff));
115 handle = CreateFileA(path, GENERIC_WRITE, 0,
116 NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
118 assert(handle != INVALID_HANDLE_VALUE);
119 assert(DeviceIoControl(handle, FSCTL_SET_SPARSE, NULL, 0,
120 NULL, 0, &dmy, NULL) != 0);
121 while (s->type != END) {
122 if (s->type == HOLE) {
123 LARGE_INTEGER distance;
125 distance.QuadPart = s->size;
126 assert(SetFilePointerEx(handle, distance,
127 NULL, FILE_CURRENT) != 0);
134 if (size > sizeof(buff))
138 assert(WriteFile(handle, buff, w, &wr, NULL) != 0);
144 assertEqualInt(CloseHandle(handle), 1);
149 #if defined(HAVE_LINUX_FIEMAP_H)
151 * FIEMAP, which can detect 'hole' of a sparse file, has
152 * been supported from 2.6.28
156 is_sparse_supported_fiemap(const char *path)
158 const struct sparse sparse_file[] = {
159 /* This hole size is too small to create a sparse
160 * files for almost filesystem. */
161 { HOLE, 1024 }, { DATA, 10240 },
167 const char *testfile = "can_sparse";
169 (void)path; /* UNUSED */
170 memset(buff, 0, sizeof(buff));
171 create_sparse_file(testfile, sparse_file);
172 fd = open(testfile, O_RDWR);
175 fm = (struct fiemap *)buff;
177 fm->fm_length = ~0ULL;;
178 fm->fm_flags = FIEMAP_FLAG_SYNC;
179 fm->fm_extent_count = (sizeof(buff) - sizeof(*fm))/
180 sizeof(struct fiemap_extent);
181 r = ioctl(fd, FS_IOC_FIEMAP, fm);
187 #if !defined(SEEK_HOLE) || !defined(SEEK_DATA)
189 is_sparse_supported(const char *path)
191 return is_sparse_supported_fiemap(path);
196 #if defined(_PC_MIN_HOLE_SIZE)
199 * FreeBSD and Solaris can detect 'hole' of a sparse file
200 * through lseek(HOLE) on ZFS. (UFS does not support yet)
204 is_sparse_supported(const char *path)
206 return (pathconf(path, _PC_MIN_HOLE_SIZE) > 0);
209 #elif defined(SEEK_HOLE) && defined(SEEK_DATA)
212 is_sparse_supported(const char *path)
214 const struct sparse sparse_file[] = {
215 /* This hole size is too small to create a sparse
216 * files for almost filesystem. */
217 { HOLE, 1024 }, { DATA, 10240 },
221 const char *testfile = "can_sparse";
223 (void)path; /* UNUSED */
224 create_sparse_file(testfile, sparse_file);
225 fd = open(testfile, O_RDWR);
228 r = lseek(fd, 0, SEEK_HOLE);
231 #if defined(HAVE_LINUX_FIEMAP_H)
233 return (is_sparse_supported_fiemap(path));
238 #elif !defined(HAVE_LINUX_FIEMAP_H)
241 * Other system may do not have the API such as lseek(HOLE),
242 * which detect 'hole' of a sparse file.
246 is_sparse_supported(const char *path)
248 (void)path; /* UNUSED */
255 * Create a sparse file on POSIX like system.
259 create_sparse_file(const char *path, const struct sparse *s)
263 size_t total_size = 0;
264 const struct sparse *cur = s;
266 memset(buff, ' ', sizeof(buff));
267 assert((fd = open(path, O_CREAT | O_WRONLY, 0600)) != -1);
269 /* Handle holes at the end by extending the file */
270 while (cur->type != END) {
271 total_size += cur->size;
274 assert(ftruncate(fd, total_size) != -1);
276 while (s->type != END) {
277 if (s->type == HOLE) {
278 assert(lseek(fd, s->size, SEEK_CUR) != (off_t)-1);
284 if (size > sizeof(buff))
288 assert(write(fd, buff, w) != (ssize_t)-1);
300 * Sparse test with directory traversals.
303 verify_sparse_file(struct archive *a, const char *path,
304 const struct sparse *sparse, int expected_holes)
306 struct archive_entry *ae;
309 int64_t offset, expected_offset, last_offset;
312 create_sparse_file(path, sparse);
313 assert((ae = archive_entry_new()) != NULL);
314 assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_open(a, path));
315 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header2(a, ae));
319 while (ARCHIVE_OK == archive_read_data_block(a, &buff, &bytes_read,
321 const char *start = buff;
323 fprintf(stderr, "%s: bytes_read=%d offset=%d\n", path, (int)bytes_read, (int)offset);
325 if (offset > last_offset) {
328 /* Blocks entirely before the data we just read. */
329 while (expected_offset + (int64_t)sparse->size < offset) {
331 fprintf(stderr, " skipping expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
334 assert(sparse->type == HOLE);
335 expected_offset += sparse->size;
338 /* Block that overlaps beginning of data */
339 if (expected_offset < offset
340 && expected_offset + (int64_t)sparse->size <= offset + (int64_t)bytes_read) {
341 const char *end = (const char *)buff + (expected_offset - offset) + (size_t)sparse->size;
343 fprintf(stderr, " overlapping hole expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
345 /* Must be a hole, overlap must be filled with '\0' */
346 if (assert(sparse->type == HOLE)) {
347 assertMemoryFilledWith(start, end - start, '\0');
350 expected_offset += sparse->size;
353 /* Blocks completely contained in data we just read. */
354 while (expected_offset + (int64_t)sparse->size <= offset + (int64_t)bytes_read) {
355 const char *end = (const char *)buff + (expected_offset - offset) + (size_t)sparse->size;
356 if (sparse->type == HOLE) {
358 fprintf(stderr, " contained hole expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
361 /* verify data corresponding to hole is '\0' */
362 if (end > (const char *)buff + bytes_read) {
363 end = (const char *)buff + bytes_read;
365 assertMemoryFilledWith(start, end - start, '\0');
367 expected_offset += sparse->size;
369 } else if (sparse->type == DATA) {
371 fprintf(stderr, " contained data expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
373 /* verify data corresponding to hole is ' ' */
374 if (assert(expected_offset + sparse->size <= offset + bytes_read)) {
375 assert(start == (const char *)buff + (size_t)(expected_offset - offset));
376 assertMemoryFilledWith(start, end - start, ' ');
379 expected_offset += sparse->size;
385 /* Block that overlaps end of data */
386 if (expected_offset < offset + (int64_t)bytes_read) {
387 const char *end = (const char *)buff + bytes_read;
389 fprintf(stderr, " trailing overlap expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
391 /* Must be a hole, overlap must be filled with '\0' */
392 if (assert(sparse->type == HOLE)) {
393 assertMemoryFilledWith(start, end - start, '\0');
396 last_offset = offset + bytes_read;
398 /* Count a hole at EOF? */
399 if (last_offset < archive_entry_size(ae)) {
403 /* Verify blocks after last read */
404 while (sparse->type == HOLE) {
405 expected_offset += sparse->size;
408 assert(sparse->type == END);
409 assertEqualInt(expected_offset, archive_entry_size(ae));
411 assertEqualInt(holes_seen, expected_holes);
413 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
414 archive_entry_free(ae);
417 #if defined(_WIN32) && !defined(__CYGWIN__)
423 * Sparse test without directory traversals.
426 verify_sparse_file2(struct archive *a, const char *path,
427 const struct sparse *sparse, int blocks, int preopen)
429 struct archive_entry *ae;
432 (void)sparse; /* UNUSED */
433 assert((ae = archive_entry_new()) != NULL);
434 archive_entry_set_pathname(ae, path);
436 fd = open(path, O_RDONLY | O_BINARY);
439 assertEqualIntA(a, ARCHIVE_OK,
440 archive_read_disk_entry_from_file(a, ae, fd, NULL));
443 /* Verify the number of holes only, not its offset nor its
444 * length because those alignments are deeply dependence on
446 assertEqualInt(blocks, archive_entry_sparse_count(ae));
447 archive_entry_free(ae);
451 test_sparse_whole_file_data()
453 struct archive_entry *ae;
457 assert((ae = archive_entry_new()) != NULL);
458 archive_entry_set_size(ae, 1024*10);
461 * Add sparse block data up to the file size.
464 for (i = 0; i < 10; i++) {
465 archive_entry_sparse_add_entry(ae, offset, 1024);
469 failure("There should be no sparse");
470 assertEqualInt(0, archive_entry_sparse_count(ae));
471 archive_entry_free(ae);
474 DEFINE_TEST(test_sparse_basic)
479 * The alignment of the hole of sparse files deeply depends
480 * on filesystem. In my experience, sparse_file2 test with
481 * 204800 bytes hole size did not pass on ZFS and the result
482 * of that test seemed the size was too small, thus you should
483 * keep a hole size more than 409600 bytes to pass this test
486 const struct sparse sparse_file0[] = {
487 { DATA, 1024 }, { HOLE, 2048000 },
488 { DATA, 2048 }, { HOLE, 2048000 },
489 { DATA, 4096 }, { HOLE, 20480000 },
490 { DATA, 8192 }, { HOLE, 204800000 },
491 { DATA, 1 }, { END, 0 }
493 const struct sparse sparse_file1[] = {
494 { HOLE, 409600 }, { DATA, 1 },
495 { HOLE, 409600 }, { DATA, 1 },
496 { HOLE, 409600 }, { END, 0 }
498 const struct sparse sparse_file2[] = {
499 { HOLE, 409600 * 1 }, { DATA, 1024 },
500 { HOLE, 409600 * 2 }, { DATA, 1024 },
501 { HOLE, 409600 * 3 }, { DATA, 1024 },
502 { HOLE, 409600 * 4 }, { DATA, 1024 },
503 { HOLE, 409600 * 5 }, { DATA, 1024 },
504 { HOLE, 409600 * 6 }, { DATA, 1024 },
505 { HOLE, 409600 * 7 }, { DATA, 1024 },
506 { HOLE, 409600 * 8 }, { DATA, 1024 },
507 { HOLE, 409600 * 9 }, { DATA, 1024 },
508 { HOLE, 409600 * 10}, { DATA, 1024 },/* 10 */
509 { HOLE, 409600 * 1 }, { DATA, 1024 * 1 },
510 { HOLE, 409600 * 2 }, { DATA, 1024 * 2 },
511 { HOLE, 409600 * 3 }, { DATA, 1024 * 3 },
512 { HOLE, 409600 * 4 }, { DATA, 1024 * 4 },
513 { HOLE, 409600 * 5 }, { DATA, 1024 * 5 },
514 { HOLE, 409600 * 6 }, { DATA, 1024 * 6 },
515 { HOLE, 409600 * 7 }, { DATA, 1024 * 7 },
516 { HOLE, 409600 * 8 }, { DATA, 1024 * 8 },
517 { HOLE, 409600 * 9 }, { DATA, 1024 * 9 },
518 { HOLE, 409600 * 10}, { DATA, 1024 * 10},/* 20 */
521 const struct sparse sparse_file3[] = {
522 /* This hole size is too small to create a sparse file */
523 { HOLE, 1 }, { DATA, 10240 },
524 { HOLE, 1 }, { DATA, 10240 },
525 { HOLE, 1 }, { DATA, 10240 },
530 * Test for the case that sparse data indicates just the whole file
533 test_sparse_whole_file_data();
535 /* Check if the filesystem where CWD on can
536 * report the number of the holes of a sparse file. */
538 cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
540 cwd = getcwd(NULL, 0);
542 if (!assert(cwd != NULL))
544 if (!is_sparse_supported(cwd)) {
546 skipping("This filesystem or platform do not support "
547 "the reporting of the holes of a sparse file through "
548 "API such as lseek(HOLE)");
553 * Get sparse data through directory traversals.
555 assert((a = archive_read_disk_new()) != NULL);
557 verify_sparse_file(a, "file0", sparse_file0, 4);
558 verify_sparse_file(a, "file1", sparse_file1, 3);
559 verify_sparse_file(a, "file2", sparse_file2, 20);
560 /* Encoded non sparse; expect a data block but no sparse entries. */
561 verify_sparse_file(a, "file3", sparse_file3, 0);
563 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
566 * Get sparse data through archive_read_disk_entry_from_file().
568 assert((a = archive_read_disk_new()) != NULL);
570 verify_sparse_file2(a, "file0", sparse_file0, 5, 0);
571 verify_sparse_file2(a, "file0", sparse_file0, 5, 1);
573 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
577 DEFINE_TEST(test_fully_sparse_files)
582 const struct sparse sparse_file[] = {
583 { HOLE, 409600 }, { END, 0 }
585 /* Check if the filesystem where CWD on can
586 * report the number of the holes of a sparse file. */
588 cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
590 cwd = getcwd(NULL, 0);
592 if (!assert(cwd != NULL))
594 if (!is_sparse_supported(cwd)) {
596 skipping("This filesystem or platform do not support "
597 "the reporting of the holes of a sparse file through "
598 "API such as lseek(HOLE)");
602 assert((a = archive_read_disk_new()) != NULL);
604 /* Fully sparse files are encoded with a zero-length "data" block. */
605 verify_sparse_file(a, "file0", sparse_file, 1);
607 assertEqualInt(ARCHIVE_OK, archive_read_free(a));