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 should perfom 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(_PC_MIN_HOLE_SIZE)
152 * FreeBSD and Solaris can detect 'hole' of a sparse file
153 * through lseek(HOLE) on ZFS. (UFS does not support yet)
157 is_sparse_supported(const char *path)
159 return (pathconf(path, _PC_MIN_HOLE_SIZE) > 0);
162 #elif defined(__linux__)&& defined(HAVE_LINUX_FIEMAP_H)
165 * FIEMAP, which can detect 'hole' of a sparse file, has
166 * been supported from 2.6.28
170 is_sparse_supported(const char *path)
172 const struct sparse sparse_file[] = {
173 /* This hole size is too small to create a sparse
174 * files for almost filesystem. */
175 { HOLE, 1024 }, { DATA, 10240 },
181 const char *testfile = "can_sparse";
183 (void)path; /* UNUSED */
184 memset(buff, 0, sizeof(buff));
185 create_sparse_file(testfile, sparse_file);
186 fd = open(testfile, O_RDWR);
189 fm = (struct fiemap *)buff;
191 fm->fm_length = ~0ULL;;
192 fm->fm_flags = FIEMAP_FLAG_SYNC;
193 fm->fm_extent_count = (sizeof(buff) - sizeof(*fm))/
194 sizeof(struct fiemap_extent);
195 r = ioctl(fd, FS_IOC_FIEMAP, fm);
204 * Other system may do not have the API such as lseek(HOLE),
205 * which detect 'hole' of a sparse file.
209 is_sparse_supported(const char *path)
211 (void)path; /* UNUSED */
218 * Create a sparse file on POSIX like system.
222 create_sparse_file(const char *path, const struct sparse *s)
226 size_t total_size = 0;
227 const struct sparse *cur = s;
229 memset(buff, ' ', sizeof(buff));
230 assert((fd = open(path, O_CREAT | O_WRONLY, 0600)) != -1);
232 /* Handle holes at the end by extending the file */
233 while (cur->type != END) {
234 total_size += cur->size;
237 assert(ftruncate(fd, total_size) != -1);
239 while (s->type != END) {
240 if (s->type == HOLE) {
241 assert(lseek(fd, s->size, SEEK_CUR) != (off_t)-1);
247 if (size > sizeof(buff))
251 assert(write(fd, buff, w) != (ssize_t)-1);
263 * Sparse test with directory traversals.
266 verify_sparse_file(struct archive *a, const char *path,
267 const struct sparse *sparse, int expected_holes)
269 struct archive_entry *ae;
272 int64_t offset, expected_offset, last_offset;
275 create_sparse_file(path, sparse);
276 assert((ae = archive_entry_new()) != NULL);
277 assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_open(a, path));
278 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header2(a, ae));
282 while (ARCHIVE_OK == archive_read_data_block(a, &buff, &bytes_read,
284 const char *start = buff;
286 fprintf(stderr, "%s: bytes_read=%d offset=%d\n", path, (int)bytes_read, (int)offset);
288 if (offset > last_offset) {
291 /* Blocks entirely before the data we just read. */
292 while (expected_offset + (int64_t)sparse->size < offset) {
294 fprintf(stderr, " skipping expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
297 assert(sparse->type == HOLE);
298 expected_offset += sparse->size;
301 /* Block that overlaps beginning of data */
302 if (expected_offset < offset
303 && expected_offset + (int64_t)sparse->size <= offset + (int64_t)bytes_read) {
304 const char *end = (const char *)buff + (expected_offset - offset) + (size_t)sparse->size;
306 fprintf(stderr, " overlapping hole expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
308 /* Must be a hole, overlap must be filled with '\0' */
309 if (assert(sparse->type == HOLE)) {
310 assertMemoryFilledWith(start, end - start, '\0');
313 expected_offset += sparse->size;
316 /* Blocks completely contained in data we just read. */
317 while (expected_offset + (int64_t)sparse->size <= offset + (int64_t)bytes_read) {
318 const char *end = (const char *)buff + (expected_offset - offset) + (size_t)sparse->size;
319 if (sparse->type == HOLE) {
321 fprintf(stderr, " contained hole expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
324 /* verify data corresponding to hole is '\0' */
325 if (end > (const char *)buff + bytes_read) {
326 end = (const char *)buff + bytes_read;
328 assertMemoryFilledWith(start, end - start, '\0');
330 expected_offset += sparse->size;
332 } else if (sparse->type == DATA) {
334 fprintf(stderr, " contained data expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
336 /* verify data corresponding to hole is ' ' */
337 if (assert(expected_offset + sparse->size <= offset + bytes_read)) {
338 assert(start == (const char *)buff + (size_t)(expected_offset - offset));
339 assertMemoryFilledWith(start, end - start, ' ');
342 expected_offset += sparse->size;
348 /* Block that overlaps end of data */
349 if (expected_offset < offset + (int64_t)bytes_read) {
350 const char *end = (const char *)buff + bytes_read;
352 fprintf(stderr, " trailing overlap expected_offset=%d, size=%d\n", (int)expected_offset, (int)sparse->size);
354 /* Must be a hole, overlap must be filled with '\0' */
355 if (assert(sparse->type == HOLE)) {
356 assertMemoryFilledWith(start, end - start, '\0');
359 last_offset = offset + bytes_read;
361 /* Count a hole at EOF? */
362 if (last_offset < archive_entry_size(ae)) {
366 /* Verify blocks after last read */
367 while (sparse->type == HOLE) {
368 expected_offset += sparse->size;
371 assert(sparse->type == END);
372 assertEqualInt(expected_offset, archive_entry_size(ae));
374 assertEqualInt(holes_seen, expected_holes);
376 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
377 archive_entry_free(ae);
380 #if defined(_WIN32) && !defined(__CYGWIN__)
386 * Sparse test without directory traversals.
389 verify_sparse_file2(struct archive *a, const char *path,
390 const struct sparse *sparse, int blocks, int preopen)
392 struct archive_entry *ae;
395 (void)sparse; /* UNUSED */
396 assert((ae = archive_entry_new()) != NULL);
397 archive_entry_set_pathname(ae, path);
399 fd = open(path, O_RDONLY | O_BINARY);
402 assertEqualIntA(a, ARCHIVE_OK,
403 archive_read_disk_entry_from_file(a, ae, fd, NULL));
406 /* Verify the number of holes only, not its offset nor its
407 * length because those alignments are deeply dependence on
409 assertEqualInt(blocks, archive_entry_sparse_count(ae));
410 archive_entry_free(ae);
414 test_sparse_whole_file_data()
416 struct archive_entry *ae;
420 assert((ae = archive_entry_new()) != NULL);
421 archive_entry_set_size(ae, 1024*10);
424 * Add sparse block data up to the file size.
427 for (i = 0; i < 10; i++) {
428 archive_entry_sparse_add_entry(ae, offset, 1024);
432 failure("There should be no sparse");
433 assertEqualInt(0, archive_entry_sparse_count(ae));
434 archive_entry_free(ae);
437 DEFINE_TEST(test_sparse_basic)
442 * The alignment of the hole of sparse files deeply depends
443 * on filesystem. In my experience, sparse_file2 test with
444 * 204800 bytes hole size did not pass on ZFS and the result
445 * of that test seemed the size was too small, thus you should
446 * keep a hole size more than 409600 bytes to pass this test
449 const struct sparse sparse_file0[] = {
450 { DATA, 1024 }, { HOLE, 2048000 },
451 { DATA, 2048 }, { HOLE, 2048000 },
452 { DATA, 4096 }, { HOLE, 20480000 },
453 { DATA, 8192 }, { HOLE, 204800000 },
454 { DATA, 1 }, { END, 0 }
456 const struct sparse sparse_file1[] = {
457 { HOLE, 409600 }, { DATA, 1 },
458 { HOLE, 409600 }, { DATA, 1 },
459 { HOLE, 409600 }, { END, 0 }
461 const struct sparse sparse_file2[] = {
462 { HOLE, 409600 * 1 }, { DATA, 1024 },
463 { HOLE, 409600 * 2 }, { DATA, 1024 },
464 { HOLE, 409600 * 3 }, { DATA, 1024 },
465 { HOLE, 409600 * 4 }, { DATA, 1024 },
466 { HOLE, 409600 * 5 }, { DATA, 1024 },
467 { HOLE, 409600 * 6 }, { DATA, 1024 },
468 { HOLE, 409600 * 7 }, { DATA, 1024 },
469 { HOLE, 409600 * 8 }, { DATA, 1024 },
470 { HOLE, 409600 * 9 }, { DATA, 1024 },
471 { HOLE, 409600 * 10}, { DATA, 1024 },/* 10 */
472 { HOLE, 409600 * 1 }, { DATA, 1024 * 1 },
473 { HOLE, 409600 * 2 }, { DATA, 1024 * 2 },
474 { HOLE, 409600 * 3 }, { DATA, 1024 * 3 },
475 { HOLE, 409600 * 4 }, { DATA, 1024 * 4 },
476 { HOLE, 409600 * 5 }, { DATA, 1024 * 5 },
477 { HOLE, 409600 * 6 }, { DATA, 1024 * 6 },
478 { HOLE, 409600 * 7 }, { DATA, 1024 * 7 },
479 { HOLE, 409600 * 8 }, { DATA, 1024 * 8 },
480 { HOLE, 409600 * 9 }, { DATA, 1024 * 9 },
481 { HOLE, 409600 * 10}, { DATA, 1024 * 10},/* 20 */
484 const struct sparse sparse_file3[] = {
485 /* This hole size is too small to create a sparse file */
486 { HOLE, 1 }, { DATA, 10240 },
487 { HOLE, 1 }, { DATA, 10240 },
488 { HOLE, 1 }, { DATA, 10240 },
493 * Test for the case that sparse data indicates just the whole file
496 test_sparse_whole_file_data();
498 /* Check if the filesystem where CWD on can
499 * report the number of the holes of a sparse file. */
501 cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
503 cwd = getcwd(NULL, 0);
505 if (!assert(cwd != NULL))
507 if (!is_sparse_supported(cwd)) {
509 skipping("This filesystem or platform do not support "
510 "the reporting of the holes of a sparse file through "
511 "API such as lseek(HOLE)");
516 * Get sparse data through directory traversals.
518 assert((a = archive_read_disk_new()) != NULL);
520 verify_sparse_file(a, "file0", sparse_file0, 4);
521 verify_sparse_file(a, "file1", sparse_file1, 3);
522 verify_sparse_file(a, "file2", sparse_file2, 20);
523 /* Encoded non sparse; expect a data block but no sparse entries. */
524 verify_sparse_file(a, "file3", sparse_file3, 0);
526 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
529 * Get sparse data through archive_read_disk_entry_from_file().
531 assert((a = archive_read_disk_new()) != NULL);
533 verify_sparse_file2(a, "file0", sparse_file0, 5, 0);
534 verify_sparse_file2(a, "file0", sparse_file0, 5, 1);
536 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
540 DEFINE_TEST(test_fully_sparse_files)
545 const struct sparse sparse_file[] = {
546 { HOLE, 409600 }, { END, 0 }
548 /* Check if the filesystem where CWD on can
549 * report the number of the holes of a sparse file. */
551 cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
553 cwd = getcwd(NULL, 0);
555 if (!assert(cwd != NULL))
557 if (!is_sparse_supported(cwd)) {
559 skipping("This filesystem or platform do not support "
560 "the reporting of the holes of a sparse file through "
561 "API such as lseek(HOLE)");
565 assert((a = archive_read_disk_new()) != NULL);
567 /* Fully sparse files are encoded with a zero-length "data" block. */
568 verify_sparse_file(a, "file0", sparse_file, 1);
570 assertEqualInt(ARCHIVE_OK, archive_read_free(a));