test_sparse_basic.c revision 248616
1231200Smm/*- 2238856Smm * Copyright (c) 2010-2012 Michihiro NAKAJIMA 3231200Smm * All rights reserved. 4231200Smm * 5231200Smm * Redistribution and use in source and binary forms, with or without 6231200Smm * modification, are permitted provided that the following conditions 7231200Smm * are met: 8231200Smm * 1. Redistributions of source code must retain the above copyright 9231200Smm * notice, this list of conditions and the following disclaimer. 10231200Smm * 2. Redistributions in binary form must reproduce the above copyright 11231200Smm * notice, this list of conditions and the following disclaimer in the 12231200Smm * documentation and/or other materials provided with the distribution. 13231200Smm * 14231200Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15231200Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16231200Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17231200Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18231200Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19231200Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20231200Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21231200Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22231200Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23231200Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24231200Smm */ 25231200Smm#include "test.h" 26231200Smm__FBSDID("$FreeBSD$"); 27231200Smm 28231200Smm#ifdef HAVE_SYS_IOCTL_H 29231200Smm#include <sys/ioctl.h> 30231200Smm#endif 31231200Smm#ifdef HAVE_SYS_PARAM_H 32231200Smm#include <sys/param.h> 33231200Smm#endif 34231200Smm#ifdef HAVE_FCNTL_H 35231200Smm#include <fcntl.h> 36231200Smm#endif 37231200Smm#ifdef HAVE_LIMITS_H 38231200Smm#include <limits.h> 39231200Smm#endif 40231200Smm#ifdef HAVE_UNISTD_H 41231200Smm#include <unistd.h> 42231200Smm#endif 43231200Smm#ifdef HAVE_LINUX_FIEMAP_H 44231200Smm#include <linux/fiemap.h> 45231200Smm#endif 46231200Smm#ifdef HAVE_LINUX_FS_H 47231200Smm#include <linux/fs.h> 48231200Smm#endif 49231200Smm 50231200Smm/* 51231200Smm * NOTE: On FreeBSD and Solaris, this test needs ZFS. 52231200Smm * You may should perfom this test as 53231200Smm * 'TMPDIR=<a directory on the ZFS> libarchive_test'. 54231200Smm */ 55231200Smm 56231200Smmstruct sparse { 57231200Smm enum { DATA, HOLE, END } type; 58231200Smm size_t size; 59231200Smm}; 60231200Smm 61231200Smmstatic void create_sparse_file(const char *, const struct sparse *); 62231200Smm 63231200Smm#if defined(_WIN32) && !defined(__CYGWIN__) 64231200Smm#include <winioctl.h> 65231200Smm/* 66231200Smm * Create a sparse file on Windows. 67231200Smm */ 68231200Smm 69231200Smm#if !defined(PATH_MAX) 70231200Smm#define PATH_MAX MAX_PATH 71231200Smm#endif 72231200Smm#if !defined(__BORLANDC__) 73231200Smm#define getcwd _getcwd 74231200Smm#endif 75231200Smm 76231200Smmstatic int 77231200Smmis_sparse_supported(const char *path) 78231200Smm{ 79231200Smm char root[MAX_PATH+1]; 80231200Smm char vol[MAX_PATH+1]; 81231200Smm char sys[MAX_PATH+1]; 82231200Smm DWORD flags; 83231200Smm BOOL r; 84231200Smm 85231200Smm strncpy(root, path, sizeof(root)-1); 86231200Smm if (((root[0] >= 'c' && root[0] <= 'z') || 87231200Smm (root[0] >= 'C' && root[0] <= 'Z')) && 88231200Smm root[1] == ':' && 89231200Smm (root[2] == '\\' || root[2] == '/')) 90231200Smm root[3] = '\0'; 91231200Smm else 92231200Smm return (0); 93231200Smm assertEqualInt((r = GetVolumeInformation(root, vol, 94231200Smm sizeof(vol), NULL, NULL, &flags, sys, sizeof(sys))), 1); 95231200Smm return (r != 0 && (flags & FILE_SUPPORTS_SPARSE_FILES) != 0); 96231200Smm} 97231200Smm 98231200Smmstatic void 99231200Smmcreate_sparse_file(const char *path, const struct sparse *s) 100231200Smm{ 101231200Smm char buff[1024]; 102231200Smm HANDLE handle; 103231200Smm DWORD dmy; 104231200Smm 105231200Smm memset(buff, ' ', sizeof(buff)); 106231200Smm 107231200Smm handle = CreateFileA(path, GENERIC_WRITE, 0, 108231200Smm NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 109231200Smm NULL); 110231200Smm assert(handle != INVALID_HANDLE_VALUE); 111231200Smm assert(DeviceIoControl(handle, FSCTL_SET_SPARSE, NULL, 0, 112231200Smm NULL, 0, &dmy, NULL) != 0); 113231200Smm while (s->type != END) { 114231200Smm if (s->type == HOLE) { 115231200Smm LARGE_INTEGER distance; 116231200Smm 117231200Smm distance.QuadPart = s->size; 118231200Smm assert(SetFilePointerEx(handle, distance, 119231200Smm NULL, FILE_CURRENT) != 0); 120231200Smm } else { 121248616Smm DWORD w, wr; 122248616Smm size_t size; 123231200Smm 124231200Smm size = s->size; 125231200Smm while (size) { 126231200Smm if (size > sizeof(buff)) 127231200Smm w = sizeof(buff); 128231200Smm else 129248616Smm w = (DWORD)size; 130231200Smm assert(WriteFile(handle, buff, w, &wr, NULL) != 0); 131231200Smm size -= wr; 132231200Smm } 133231200Smm } 134231200Smm s++; 135231200Smm } 136231200Smm assertEqualInt(CloseHandle(handle), 1); 137231200Smm} 138231200Smm 139231200Smm#else 140231200Smm 141231200Smm#if defined(_PC_MIN_HOLE_SIZE) 142231200Smm 143231200Smm/* 144231200Smm * FreeBSD and Solaris can detect 'hole' of a sparse file 145231200Smm * through lseek(HOLE) on ZFS. (UFS does not support yet) 146231200Smm */ 147231200Smm 148231200Smmstatic int 149231200Smmis_sparse_supported(const char *path) 150231200Smm{ 151231200Smm return (pathconf(path, _PC_MIN_HOLE_SIZE) > 0); 152231200Smm} 153231200Smm 154231200Smm#elif defined(__linux__)&& defined(HAVE_LINUX_FIEMAP_H) 155231200Smm 156231200Smm/* 157231200Smm * FIEMAP, which can detect 'hole' of a sparse file, has 158231200Smm * been supported from 2.6.28 159231200Smm */ 160231200Smm 161231200Smmstatic int 162231200Smmis_sparse_supported(const char *path) 163231200Smm{ 164231200Smm const struct sparse sparse_file[] = { 165231200Smm /* This hole size is too small to create a sparse 166231200Smm * files for almost filesystem. */ 167231200Smm { HOLE, 1024 }, { DATA, 10240 }, 168231200Smm { END, 0 } 169231200Smm }; 170231200Smm int fd, r; 171231200Smm struct fiemap *fm; 172231200Smm char buff[1024]; 173231200Smm const char *testfile = "can_sparse"; 174231200Smm 175232153Smm (void)path; /* UNUSED */ 176248616Smm memset(buff, 0, sizeof(buff)); 177231200Smm create_sparse_file(testfile, sparse_file); 178231200Smm fd = open(testfile, O_RDWR); 179231200Smm if (fd < 0) 180231200Smm return (0); 181231200Smm fm = (struct fiemap *)buff; 182231200Smm fm->fm_start = 0; 183231200Smm fm->fm_length = ~0ULL;; 184231200Smm fm->fm_flags = FIEMAP_FLAG_SYNC; 185231200Smm fm->fm_extent_count = (sizeof(buff) - sizeof(*fm))/ 186231200Smm sizeof(struct fiemap_extent); 187231200Smm r = ioctl(fd, FS_IOC_FIEMAP, fm); 188231200Smm close(fd); 189231200Smm unlink(testfile); 190238856Smm return (r >= 0); 191231200Smm} 192231200Smm 193231200Smm#else 194231200Smm 195231200Smm/* 196231200Smm * Other system may do not have the API such as lseek(HOLE), 197231200Smm * which detect 'hole' of a sparse file. 198231200Smm */ 199231200Smm 200231200Smmstatic int 201231200Smmis_sparse_supported(const char *path) 202231200Smm{ 203232153Smm (void)path; /* UNUSED */ 204231200Smm return (0); 205231200Smm} 206231200Smm 207231200Smm#endif 208231200Smm 209231200Smm/* 210231200Smm * Create a sparse file on POSIX like system. 211231200Smm */ 212231200Smm 213231200Smmstatic void 214231200Smmcreate_sparse_file(const char *path, const struct sparse *s) 215231200Smm{ 216231200Smm char buff[1024]; 217231200Smm int fd; 218231200Smm 219231200Smm memset(buff, ' ', sizeof(buff)); 220231200Smm assert((fd = open(path, O_CREAT | O_WRONLY, 0600)) != -1); 221231200Smm while (s->type != END) { 222231200Smm if (s->type == HOLE) { 223231200Smm assert(lseek(fd, s->size, SEEK_CUR) != (off_t)-1); 224231200Smm } else { 225231200Smm size_t w, size; 226231200Smm 227231200Smm size = s->size; 228231200Smm while (size) { 229231200Smm if (size > sizeof(buff)) 230231200Smm w = sizeof(buff); 231231200Smm else 232231200Smm w = size; 233231200Smm assert(write(fd, buff, w) != (ssize_t)-1); 234231200Smm size -= w; 235231200Smm } 236231200Smm } 237231200Smm s++; 238231200Smm } 239231200Smm close(fd); 240231200Smm} 241231200Smm 242231200Smm#endif 243231200Smm 244231200Smm/* 245231200Smm * Sparse test with directory traversals. 246231200Smm */ 247231200Smmstatic void 248231200Smmverify_sparse_file(struct archive *a, const char *path, 249231200Smm const struct sparse *sparse, int blocks) 250231200Smm{ 251231200Smm struct archive_entry *ae; 252231200Smm const void *buff; 253231200Smm size_t bytes_read; 254231200Smm int64_t offset; 255231200Smm int64_t total; 256231200Smm int data_blocks, hole; 257231200Smm 258231200Smm create_sparse_file(path, sparse); 259231200Smm assert((ae = archive_entry_new()) != NULL); 260231200Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_open(a, path)); 261231200Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header2(a, ae)); 262231200Smm /* Verify the number of holes only, not its offset nor its 263231200Smm * length because those alignments are deeply dependence on 264231200Smm * its filesystem. */ 265231200Smm assertEqualInt(blocks, archive_entry_sparse_count(ae)); 266231200Smm total = 0; 267231200Smm data_blocks = 0; 268231200Smm hole = 0; 269231200Smm while (ARCHIVE_OK == archive_read_data_block(a, &buff, &bytes_read, 270231200Smm &offset)) { 271231200Smm if (offset > total || offset == 0) { 272231200Smm if (offset > total) 273231200Smm hole = 1; 274231200Smm data_blocks++; 275231200Smm } 276231200Smm total = offset + bytes_read; 277231200Smm } 278231200Smm if (!hole && data_blocks == 1) 279231200Smm data_blocks = 0;/* There are no holes */ 280231200Smm assertEqualInt(blocks, data_blocks); 281231200Smm 282231200Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 283231200Smm archive_entry_free(ae); 284231200Smm} 285231200Smm 286231200Smm#if defined(_WIN32) && !defined(__CYGWIN__) 287231200Smm#define close _close 288231200Smm#define open _open 289231200Smm#endif 290231200Smm 291231200Smm/* 292231200Smm * Sparse test without directory traversals. 293231200Smm */ 294231200Smmstatic void 295231200Smmverify_sparse_file2(struct archive *a, const char *path, 296231200Smm const struct sparse *sparse, int blocks, int preopen) 297231200Smm{ 298231200Smm struct archive_entry *ae; 299231200Smm int fd; 300231200Smm 301231200Smm (void)sparse; /* UNUSED */ 302231200Smm assert((ae = archive_entry_new()) != NULL); 303231200Smm archive_entry_set_pathname(ae, path); 304231200Smm if (preopen) 305231200Smm fd = open(path, O_RDONLY | O_BINARY); 306231200Smm else 307231200Smm fd = -1; 308231200Smm assertEqualIntA(a, ARCHIVE_OK, 309231200Smm archive_read_disk_entry_from_file(a, ae, fd, NULL)); 310231200Smm if (fd >= 0) 311231200Smm close(fd); 312231200Smm /* Verify the number of holes only, not its offset nor its 313231200Smm * length because those alignments are deeply dependence on 314231200Smm * its filesystem. */ 315231200Smm assertEqualInt(blocks, archive_entry_sparse_count(ae)); 316231200Smm archive_entry_free(ae); 317231200Smm} 318231200Smm 319231200Smmstatic void 320231200Smmtest_sparse_whole_file_data() 321231200Smm{ 322231200Smm struct archive_entry *ae; 323231200Smm int64_t offset; 324231200Smm int i; 325231200Smm 326231200Smm assert((ae = archive_entry_new()) != NULL); 327231200Smm archive_entry_set_size(ae, 1024*10); 328231200Smm 329231200Smm /* 330231200Smm * Add sparse block data up to the file size. 331231200Smm */ 332231200Smm offset = 0; 333231200Smm for (i = 0; i < 10; i++) { 334231200Smm archive_entry_sparse_add_entry(ae, offset, 1024); 335231200Smm offset += 1024; 336231200Smm } 337231200Smm 338231200Smm failure("There should be no sparse"); 339231200Smm assertEqualInt(0, archive_entry_sparse_count(ae)); 340231200Smm archive_entry_free(ae); 341231200Smm} 342231200Smm 343231200SmmDEFINE_TEST(test_sparse_basic) 344231200Smm{ 345231200Smm char *cwd; 346231200Smm struct archive *a; 347231200Smm /* 348231200Smm * The alignment of the hole of sparse files deeply depends 349231200Smm * on filesystem. In my experience, sparse_file2 test with 350231200Smm * 204800 bytes hole size did not pass on ZFS and the result 351231200Smm * of that test seemed the size was too small, thus you should 352231200Smm * keep a hole size more than 409600 bytes to pass this test 353231200Smm * on all platform. 354231200Smm */ 355231200Smm const struct sparse sparse_file0[] = { 356231200Smm { DATA, 1024 }, { HOLE, 2048000 }, 357231200Smm { DATA, 2048 }, { HOLE, 2048000 }, 358231200Smm { DATA, 4096 }, { HOLE, 20480000 }, 359231200Smm { DATA, 8192 }, { HOLE, 204800000 }, 360231200Smm { DATA, 1 }, { END, 0 } 361231200Smm }; 362231200Smm const struct sparse sparse_file1[] = { 363231200Smm { HOLE, 409600 }, { DATA, 1 }, 364231200Smm { HOLE, 409600 }, { DATA, 1 }, 365231200Smm { HOLE, 409600 }, { END, 0 } 366231200Smm }; 367231200Smm const struct sparse sparse_file2[] = { 368231200Smm { HOLE, 409600 * 1 }, { DATA, 1024 }, 369231200Smm { HOLE, 409600 * 2 }, { DATA, 1024 }, 370231200Smm { HOLE, 409600 * 3 }, { DATA, 1024 }, 371231200Smm { HOLE, 409600 * 4 }, { DATA, 1024 }, 372231200Smm { HOLE, 409600 * 5 }, { DATA, 1024 }, 373231200Smm { HOLE, 409600 * 6 }, { DATA, 1024 }, 374231200Smm { HOLE, 409600 * 7 }, { DATA, 1024 }, 375231200Smm { HOLE, 409600 * 8 }, { DATA, 1024 }, 376231200Smm { HOLE, 409600 * 9 }, { DATA, 1024 }, 377231200Smm { HOLE, 409600 * 10}, { DATA, 1024 },/* 10 */ 378231200Smm { HOLE, 409600 * 1 }, { DATA, 1024 * 1 }, 379231200Smm { HOLE, 409600 * 2 }, { DATA, 1024 * 2 }, 380231200Smm { HOLE, 409600 * 3 }, { DATA, 1024 * 3 }, 381231200Smm { HOLE, 409600 * 4 }, { DATA, 1024 * 4 }, 382231200Smm { HOLE, 409600 * 5 }, { DATA, 1024 * 5 }, 383231200Smm { HOLE, 409600 * 6 }, { DATA, 1024 * 6 }, 384231200Smm { HOLE, 409600 * 7 }, { DATA, 1024 * 7 }, 385231200Smm { HOLE, 409600 * 8 }, { DATA, 1024 * 8 }, 386231200Smm { HOLE, 409600 * 9 }, { DATA, 1024 * 9 }, 387231200Smm { HOLE, 409600 * 10}, { DATA, 1024 * 10},/* 20 */ 388231200Smm { END, 0 } 389231200Smm }; 390231200Smm const struct sparse sparse_file3[] = { 391231200Smm /* This hole size is too small to create a sparse 392231200Smm * files for almost filesystem. */ 393231200Smm { HOLE, 1024 }, { DATA, 10240 }, 394231200Smm { END, 0 } 395231200Smm }; 396231200Smm 397231200Smm /* 398231200Smm * Test for the case that sparse data indicates just the whole file 399231200Smm * data. 400231200Smm */ 401231200Smm test_sparse_whole_file_data(); 402231200Smm 403231200Smm /* Check if the filesystem where CWD on can 404231200Smm * report the number of the holes of a sparse file. */ 405231200Smm#ifdef PATH_MAX 406231200Smm cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 407231200Smm#else 408231200Smm cwd = getcwd(NULL, 0); 409231200Smm#endif 410231200Smm if (!assert(cwd != NULL)) 411231200Smm return; 412231200Smm if (!is_sparse_supported(cwd)) { 413231200Smm free(cwd); 414231200Smm skipping("This filesystem or platform do not support " 415231200Smm "the reporting of the holes of a sparse file through " 416231200Smm "API such as lseek(HOLE)"); 417231200Smm return; 418231200Smm } 419231200Smm 420231200Smm /* 421231200Smm * Get sparse data through directory traversals. 422231200Smm */ 423231200Smm assert((a = archive_read_disk_new()) != NULL); 424231200Smm 425231200Smm verify_sparse_file(a, "file0", sparse_file0, 5); 426231200Smm verify_sparse_file(a, "file1", sparse_file1, 2); 427231200Smm verify_sparse_file(a, "file2", sparse_file2, 20); 428231200Smm verify_sparse_file(a, "file3", sparse_file3, 0); 429231200Smm 430231200Smm assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 431231200Smm 432231200Smm /* 433231200Smm * Get sparse data through archive_read_disk_entry_from_file(). 434231200Smm */ 435231200Smm assert((a = archive_read_disk_new()) != NULL); 436231200Smm 437231200Smm verify_sparse_file2(a, "file0", sparse_file0, 5, 0); 438231200Smm verify_sparse_file2(a, "file0", sparse_file0, 5, 1); 439231200Smm 440231200Smm assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 441231200Smm free(cwd); 442231200Smm} 443