2 * Copyright (c) 2010 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>
34 #ifdef HAVE_SYS_UTSNAME_H
35 #include <sys/utsname.h>
46 #ifdef HAVE_LINUX_FIEMAP_H
47 #include <linux/fiemap.h>
49 #ifdef HAVE_LINUX_FS_H
54 * NOTE: On FreeBSD and Solaris, this test needs ZFS.
55 * You may should perfom this test as
56 * 'TMPDIR=<a directory on the ZFS> libarchive_test'.
60 enum { DATA, HOLE, END } type;
64 static void create_sparse_file(const char *, const struct sparse *);
66 #if defined(_WIN32) && !defined(__CYGWIN__)
69 * Create a sparse file on Windows.
72 #if !defined(PATH_MAX)
73 #define PATH_MAX MAX_PATH
75 #if !defined(__BORLANDC__)
76 #define getcwd _getcwd
80 is_sparse_supported(const char *path)
82 char root[MAX_PATH+1];
88 strncpy(root, path, sizeof(root)-1);
89 if (((root[0] >= 'c' && root[0] <= 'z') ||
90 (root[0] >= 'C' && root[0] <= 'Z')) &&
92 (root[2] == '\\' || root[2] == '/'))
96 assertEqualInt((r = GetVolumeInformation(root, vol,
97 sizeof(vol), NULL, NULL, &flags, sys, sizeof(sys))), 1);
98 return (r != 0 && (flags & FILE_SUPPORTS_SPARSE_FILES) != 0);
102 create_sparse_file(const char *path, const struct sparse *s)
108 memset(buff, ' ', sizeof(buff));
110 handle = CreateFileA(path, GENERIC_WRITE, 0,
111 NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
113 assert(handle != INVALID_HANDLE_VALUE);
114 assert(DeviceIoControl(handle, FSCTL_SET_SPARSE, NULL, 0,
115 NULL, 0, &dmy, NULL) != 0);
116 while (s->type != END) {
117 if (s->type == HOLE) {
118 LARGE_INTEGER distance;
120 distance.QuadPart = s->size;
121 assert(SetFilePointerEx(handle, distance,
122 NULL, FILE_CURRENT) != 0);
128 if (size > sizeof(buff))
132 assert(WriteFile(handle, buff, w, &wr, NULL) != 0);
138 assertEqualInt(CloseHandle(handle), 1);
143 #if defined(_PC_MIN_HOLE_SIZE)
146 * FreeBSD and Solaris can detect 'hole' of a sparse file
147 * through lseek(HOLE) on ZFS. (UFS does not support yet)
151 is_sparse_supported(const char *path)
153 return (pathconf(path, _PC_MIN_HOLE_SIZE) > 0);
156 #elif defined(__linux__)&& defined(HAVE_LINUX_FIEMAP_H)
159 * FIEMAP, which can detect 'hole' of a sparse file, has
160 * been supported from 2.6.28
164 is_sparse_supported(const char *path)
166 const struct sparse sparse_file[] = {
167 /* This hole size is too small to create a sparse
168 * files for almost filesystem. */
169 { HOLE, 1024 }, { DATA, 10240 },
178 const char *testfile = "can_sparse";
180 (void)path; /* UNUSED */
181 memset(&ut, 0, sizeof(ut));
182 assertEqualInt(uname(&ut), 0);
184 d = strtol(p, &e, 10);
185 if (d < 2 || *e != '.')
189 d = strtol(p, &e, 10);
190 if (d < 6 || *e != '.')
193 d = strtol(p, NULL, 10);
197 create_sparse_file(testfile, sparse_file);
198 fd = open(testfile, O_RDWR);
201 fm = (struct fiemap *)buff;
203 fm->fm_length = ~0ULL;;
204 fm->fm_flags = FIEMAP_FLAG_SYNC;
205 fm->fm_extent_count = (sizeof(buff) - sizeof(*fm))/
206 sizeof(struct fiemap_extent);
207 r = ioctl(fd, FS_IOC_FIEMAP, fm);
208 if (r < 0 && (errno == ENOTTY || errno == EOPNOTSUPP))
209 return (0);/* Not supported. */
218 * Other system may do not have the API such as lseek(HOLE),
219 * which detect 'hole' of a sparse file.
223 is_sparse_supported(const char *path)
225 (void)path; /* UNUSED */
232 * Create a sparse file on POSIX like system.
236 create_sparse_file(const char *path, const struct sparse *s)
241 memset(buff, ' ', sizeof(buff));
242 assert((fd = open(path, O_CREAT | O_WRONLY, 0600)) != -1);
243 while (s->type != END) {
244 if (s->type == HOLE) {
245 assert(lseek(fd, s->size, SEEK_CUR) != (off_t)-1);
251 if (size > sizeof(buff))
255 assert(write(fd, buff, w) != (ssize_t)-1);
267 * Sparse test with directory traversals.
270 verify_sparse_file(struct archive *a, const char *path,
271 const struct sparse *sparse, int blocks)
273 struct archive_entry *ae;
278 int data_blocks, hole;
280 create_sparse_file(path, sparse);
281 assert((ae = archive_entry_new()) != NULL);
282 assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_open(a, path));
283 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header2(a, ae));
284 /* Verify the number of holes only, not its offset nor its
285 * length because those alignments are deeply dependence on
287 assertEqualInt(blocks, archive_entry_sparse_count(ae));
291 while (ARCHIVE_OK == archive_read_data_block(a, &buff, &bytes_read,
293 if (offset > total || offset == 0) {
298 total = offset + bytes_read;
300 if (!hole && data_blocks == 1)
301 data_blocks = 0;/* There are no holes */
302 assertEqualInt(blocks, data_blocks);
304 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
305 archive_entry_free(ae);
308 #if defined(_WIN32) && !defined(__CYGWIN__)
314 * Sparse test without directory traversals.
317 verify_sparse_file2(struct archive *a, const char *path,
318 const struct sparse *sparse, int blocks, int preopen)
320 struct archive_entry *ae;
323 (void)sparse; /* UNUSED */
324 assert((ae = archive_entry_new()) != NULL);
325 archive_entry_set_pathname(ae, path);
327 fd = open(path, O_RDONLY | O_BINARY);
330 assertEqualIntA(a, ARCHIVE_OK,
331 archive_read_disk_entry_from_file(a, ae, fd, NULL));
334 /* Verify the number of holes only, not its offset nor its
335 * length because those alignments are deeply dependence on
337 assertEqualInt(blocks, archive_entry_sparse_count(ae));
338 archive_entry_free(ae);
342 test_sparse_whole_file_data()
344 struct archive_entry *ae;
348 assert((ae = archive_entry_new()) != NULL);
349 archive_entry_set_size(ae, 1024*10);
352 * Add sparse block data up to the file size.
355 for (i = 0; i < 10; i++) {
356 archive_entry_sparse_add_entry(ae, offset, 1024);
360 failure("There should be no sparse");
361 assertEqualInt(0, archive_entry_sparse_count(ae));
362 archive_entry_free(ae);
365 DEFINE_TEST(test_sparse_basic)
370 * The alignment of the hole of sparse files deeply depends
371 * on filesystem. In my experience, sparse_file2 test with
372 * 204800 bytes hole size did not pass on ZFS and the result
373 * of that test seemed the size was too small, thus you should
374 * keep a hole size more than 409600 bytes to pass this test
377 const struct sparse sparse_file0[] = {
378 { DATA, 1024 }, { HOLE, 2048000 },
379 { DATA, 2048 }, { HOLE, 2048000 },
380 { DATA, 4096 }, { HOLE, 20480000 },
381 { DATA, 8192 }, { HOLE, 204800000 },
382 { DATA, 1 }, { END, 0 }
384 const struct sparse sparse_file1[] = {
385 { HOLE, 409600 }, { DATA, 1 },
386 { HOLE, 409600 }, { DATA, 1 },
387 { HOLE, 409600 }, { END, 0 }
389 const struct sparse sparse_file2[] = {
390 { HOLE, 409600 * 1 }, { DATA, 1024 },
391 { HOLE, 409600 * 2 }, { DATA, 1024 },
392 { HOLE, 409600 * 3 }, { DATA, 1024 },
393 { HOLE, 409600 * 4 }, { DATA, 1024 },
394 { HOLE, 409600 * 5 }, { DATA, 1024 },
395 { HOLE, 409600 * 6 }, { DATA, 1024 },
396 { HOLE, 409600 * 7 }, { DATA, 1024 },
397 { HOLE, 409600 * 8 }, { DATA, 1024 },
398 { HOLE, 409600 * 9 }, { DATA, 1024 },
399 { HOLE, 409600 * 10}, { DATA, 1024 },/* 10 */
400 { HOLE, 409600 * 1 }, { DATA, 1024 * 1 },
401 { HOLE, 409600 * 2 }, { DATA, 1024 * 2 },
402 { HOLE, 409600 * 3 }, { DATA, 1024 * 3 },
403 { HOLE, 409600 * 4 }, { DATA, 1024 * 4 },
404 { HOLE, 409600 * 5 }, { DATA, 1024 * 5 },
405 { HOLE, 409600 * 6 }, { DATA, 1024 * 6 },
406 { HOLE, 409600 * 7 }, { DATA, 1024 * 7 },
407 { HOLE, 409600 * 8 }, { DATA, 1024 * 8 },
408 { HOLE, 409600 * 9 }, { DATA, 1024 * 9 },
409 { HOLE, 409600 * 10}, { DATA, 1024 * 10},/* 20 */
412 const struct sparse sparse_file3[] = {
413 /* This hole size is too small to create a sparse
414 * files for almost filesystem. */
415 { HOLE, 1024 }, { DATA, 10240 },
420 * Test for the case that sparse data indicates just the whole file
423 test_sparse_whole_file_data();
425 /* Check if the filesystem where CWD on can
426 * report the number of the holes of a sparse file. */
428 cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
430 cwd = getcwd(NULL, 0);
432 if (!assert(cwd != NULL))
434 if (!is_sparse_supported(cwd)) {
436 skipping("This filesystem or platform do not support "
437 "the reporting of the holes of a sparse file through "
438 "API such as lseek(HOLE)");
443 * Get sparse data through directory traversals.
445 assert((a = archive_read_disk_new()) != NULL);
447 verify_sparse_file(a, "file0", sparse_file0, 5);
448 verify_sparse_file(a, "file1", sparse_file1, 2);
449 verify_sparse_file(a, "file2", sparse_file2, 20);
450 verify_sparse_file(a, "file3", sparse_file3, 0);
452 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
455 * Get sparse data through archive_read_disk_entry_from_file().
457 assert((a = archive_read_disk_new()) != NULL);
459 verify_sparse_file2(a, "file0", sparse_file0, 5, 0);
460 verify_sparse_file2(a, "file0", sparse_file0, 5, 1);
462 assertEqualInt(ARCHIVE_OK, archive_read_free(a));