test_sparse_basic.c revision 238856
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 {
121231200Smm			DWORD w, wr, size;
122231200Smm
123231200Smm			size = s->size;
124231200Smm			while (size) {
125231200Smm				if (size > sizeof(buff))
126231200Smm					w = sizeof(buff);
127231200Smm				else
128231200Smm					w = size;
129231200Smm				assert(WriteFile(handle, buff, w, &wr, NULL) != 0);
130231200Smm				size -= wr;
131231200Smm			}
132231200Smm		}
133231200Smm		s++;
134231200Smm	}
135231200Smm	assertEqualInt(CloseHandle(handle), 1);
136231200Smm}
137231200Smm
138231200Smm#else
139231200Smm
140231200Smm#if defined(_PC_MIN_HOLE_SIZE)
141231200Smm
142231200Smm/*
143231200Smm * FreeBSD and Solaris can detect 'hole' of a sparse file
144231200Smm * through lseek(HOLE) on ZFS. (UFS does not support yet)
145231200Smm */
146231200Smm
147231200Smmstatic int
148231200Smmis_sparse_supported(const char *path)
149231200Smm{
150231200Smm	return (pathconf(path, _PC_MIN_HOLE_SIZE) > 0);
151231200Smm}
152231200Smm
153231200Smm#elif defined(__linux__)&& defined(HAVE_LINUX_FIEMAP_H)
154231200Smm
155231200Smm/*
156231200Smm * FIEMAP, which can detect 'hole' of a sparse file, has
157231200Smm * been supported from 2.6.28
158231200Smm */
159231200Smm
160231200Smmstatic int
161231200Smmis_sparse_supported(const char *path)
162231200Smm{
163231200Smm	const struct sparse sparse_file[] = {
164231200Smm 		/* This hole size is too small to create a sparse
165231200Smm		 * files for almost filesystem. */
166231200Smm		{ HOLE,	 1024 }, { DATA, 10240 },
167231200Smm		{ END,	0 }
168231200Smm	};
169231200Smm	int fd, r;
170231200Smm	struct fiemap *fm;
171231200Smm	char buff[1024];
172231200Smm	const char *testfile = "can_sparse";
173231200Smm
174232153Smm	(void)path; /* UNUSED */
175231200Smm	create_sparse_file(testfile, sparse_file);
176231200Smm	fd = open(testfile,  O_RDWR);
177231200Smm	if (fd < 0)
178231200Smm		return (0);
179231200Smm	fm = (struct fiemap *)buff;
180231200Smm	fm->fm_start = 0;
181231200Smm	fm->fm_length = ~0ULL;;
182231200Smm	fm->fm_flags = FIEMAP_FLAG_SYNC;
183231200Smm	fm->fm_extent_count = (sizeof(buff) - sizeof(*fm))/
184231200Smm		sizeof(struct fiemap_extent);
185231200Smm	r = ioctl(fd, FS_IOC_FIEMAP, fm);
186231200Smm	close(fd);
187231200Smm	unlink(testfile);
188238856Smm	return (r >= 0);
189231200Smm}
190231200Smm
191231200Smm#else
192231200Smm
193231200Smm/*
194231200Smm * Other system may do not have the API such as lseek(HOLE),
195231200Smm * which detect 'hole' of a sparse file.
196231200Smm */
197231200Smm
198231200Smmstatic int
199231200Smmis_sparse_supported(const char *path)
200231200Smm{
201232153Smm	(void)path; /* UNUSED */
202231200Smm	return (0);
203231200Smm}
204231200Smm
205231200Smm#endif
206231200Smm
207231200Smm/*
208231200Smm * Create a sparse file on POSIX like system.
209231200Smm */
210231200Smm
211231200Smmstatic void
212231200Smmcreate_sparse_file(const char *path, const struct sparse *s)
213231200Smm{
214231200Smm	char buff[1024];
215231200Smm	int fd;
216231200Smm
217231200Smm	memset(buff, ' ', sizeof(buff));
218231200Smm	assert((fd = open(path, O_CREAT | O_WRONLY, 0600)) != -1);
219231200Smm	while (s->type != END) {
220231200Smm		if (s->type == HOLE) {
221231200Smm			assert(lseek(fd, s->size, SEEK_CUR) != (off_t)-1);
222231200Smm		} else {
223231200Smm			size_t w, size;
224231200Smm
225231200Smm			size = s->size;
226231200Smm			while (size) {
227231200Smm				if (size > sizeof(buff))
228231200Smm					w = sizeof(buff);
229231200Smm				else
230231200Smm					w = size;
231231200Smm				assert(write(fd, buff, w) != (ssize_t)-1);
232231200Smm				size -= w;
233231200Smm			}
234231200Smm		}
235231200Smm		s++;
236231200Smm	}
237231200Smm	close(fd);
238231200Smm}
239231200Smm
240231200Smm#endif
241231200Smm
242231200Smm/*
243231200Smm * Sparse test with directory traversals.
244231200Smm */
245231200Smmstatic void
246231200Smmverify_sparse_file(struct archive *a, const char *path,
247231200Smm    const struct sparse *sparse, int blocks)
248231200Smm{
249231200Smm	struct archive_entry *ae;
250231200Smm	const void *buff;
251231200Smm	size_t bytes_read;
252231200Smm	int64_t offset;
253231200Smm	int64_t total;
254231200Smm	int data_blocks, hole;
255231200Smm
256231200Smm	create_sparse_file(path, sparse);
257231200Smm	assert((ae = archive_entry_new()) != NULL);
258231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_open(a, path));
259231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header2(a, ae));
260231200Smm	/* Verify the number of holes only, not its offset nor its
261231200Smm	 * length because those alignments are deeply dependence on
262231200Smm	 * its filesystem. */
263231200Smm	assertEqualInt(blocks, archive_entry_sparse_count(ae));
264231200Smm	total = 0;
265231200Smm	data_blocks = 0;
266231200Smm	hole = 0;
267231200Smm	while (ARCHIVE_OK == archive_read_data_block(a, &buff, &bytes_read,
268231200Smm	    &offset)) {
269231200Smm		if (offset > total || offset == 0) {
270231200Smm			if (offset > total)
271231200Smm				hole = 1;
272231200Smm			data_blocks++;
273231200Smm		}
274231200Smm		total = offset + bytes_read;
275231200Smm	}
276231200Smm	if (!hole && data_blocks == 1)
277231200Smm		data_blocks = 0;/* There are no holes */
278231200Smm	assertEqualInt(blocks, data_blocks);
279231200Smm
280231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
281231200Smm	archive_entry_free(ae);
282231200Smm}
283231200Smm
284231200Smm#if defined(_WIN32) && !defined(__CYGWIN__)
285231200Smm#define	close		_close
286231200Smm#define	open		_open
287231200Smm#endif
288231200Smm
289231200Smm/*
290231200Smm * Sparse test without directory traversals.
291231200Smm */
292231200Smmstatic void
293231200Smmverify_sparse_file2(struct archive *a, const char *path,
294231200Smm    const struct sparse *sparse, int blocks, int preopen)
295231200Smm{
296231200Smm	struct archive_entry *ae;
297231200Smm	int fd;
298231200Smm
299231200Smm	(void)sparse; /* UNUSED */
300231200Smm	assert((ae = archive_entry_new()) != NULL);
301231200Smm	archive_entry_set_pathname(ae, path);
302231200Smm	if (preopen)
303231200Smm		fd = open(path, O_RDONLY | O_BINARY);
304231200Smm	else
305231200Smm		fd = -1;
306231200Smm	assertEqualIntA(a, ARCHIVE_OK,
307231200Smm	    archive_read_disk_entry_from_file(a, ae, fd, NULL));
308231200Smm	if (fd >= 0)
309231200Smm		close(fd);
310231200Smm	/* Verify the number of holes only, not its offset nor its
311231200Smm	 * length because those alignments are deeply dependence on
312231200Smm	 * its filesystem. */
313231200Smm	assertEqualInt(blocks, archive_entry_sparse_count(ae));
314231200Smm	archive_entry_free(ae);
315231200Smm}
316231200Smm
317231200Smmstatic void
318231200Smmtest_sparse_whole_file_data()
319231200Smm{
320231200Smm	struct archive_entry *ae;
321231200Smm	int64_t offset;
322231200Smm	int i;
323231200Smm
324231200Smm	assert((ae = archive_entry_new()) != NULL);
325231200Smm	archive_entry_set_size(ae, 1024*10);
326231200Smm
327231200Smm	/*
328231200Smm	 * Add sparse block data up to the file size.
329231200Smm	 */
330231200Smm	offset = 0;
331231200Smm	for (i = 0; i < 10; i++) {
332231200Smm		archive_entry_sparse_add_entry(ae, offset, 1024);
333231200Smm		offset += 1024;
334231200Smm	}
335231200Smm
336231200Smm	failure("There should be no sparse");
337231200Smm	assertEqualInt(0, archive_entry_sparse_count(ae));
338231200Smm	archive_entry_free(ae);
339231200Smm}
340231200Smm
341231200SmmDEFINE_TEST(test_sparse_basic)
342231200Smm{
343231200Smm	char *cwd;
344231200Smm	struct archive *a;
345231200Smm	/*
346231200Smm	 * The alignment of the hole of sparse files deeply depends
347231200Smm	 * on filesystem. In my experience, sparse_file2 test with
348231200Smm	 * 204800 bytes hole size did not pass on ZFS and the result
349231200Smm	 * of that test seemed the size was too small, thus you should
350231200Smm	 * keep a hole size more than 409600 bytes to pass this test
351231200Smm	 * on all platform.
352231200Smm	 */
353231200Smm	const struct sparse sparse_file0[] = {
354231200Smm		{ DATA,	 1024 }, { HOLE,   2048000 },
355231200Smm		{ DATA,	 2048 }, { HOLE,   2048000 },
356231200Smm		{ DATA,	 4096 }, { HOLE,  20480000 },
357231200Smm		{ DATA,	 8192 }, { HOLE, 204800000 },
358231200Smm		{ DATA,     1 }, { END,	0 }
359231200Smm	};
360231200Smm	const struct sparse sparse_file1[] = {
361231200Smm		{ HOLE,	409600 }, { DATA, 1 },
362231200Smm		{ HOLE,	409600 }, { DATA, 1 },
363231200Smm		{ HOLE,	409600 }, { END,  0 }
364231200Smm	};
365231200Smm	const struct sparse sparse_file2[] = {
366231200Smm		{ HOLE,	409600 * 1 }, { DATA, 1024 },
367231200Smm		{ HOLE,	409600 * 2 }, { DATA, 1024 },
368231200Smm		{ HOLE,	409600 * 3 }, { DATA, 1024 },
369231200Smm		{ HOLE,	409600 * 4 }, { DATA, 1024 },
370231200Smm		{ HOLE,	409600 * 5 }, { DATA, 1024 },
371231200Smm		{ HOLE,	409600 * 6 }, { DATA, 1024 },
372231200Smm		{ HOLE,	409600 * 7 }, { DATA, 1024 },
373231200Smm		{ HOLE,	409600 * 8 }, { DATA, 1024 },
374231200Smm		{ HOLE,	409600 * 9 }, { DATA, 1024 },
375231200Smm		{ HOLE,	409600 * 10}, { DATA, 1024 },/* 10 */
376231200Smm		{ HOLE,	409600 * 1 }, { DATA, 1024 * 1 },
377231200Smm		{ HOLE,	409600 * 2 }, { DATA, 1024 * 2 },
378231200Smm		{ HOLE,	409600 * 3 }, { DATA, 1024 * 3 },
379231200Smm		{ HOLE,	409600 * 4 }, { DATA, 1024 * 4 },
380231200Smm		{ HOLE,	409600 * 5 }, { DATA, 1024 * 5 },
381231200Smm		{ HOLE,	409600 * 6 }, { DATA, 1024 * 6 },
382231200Smm		{ HOLE,	409600 * 7 }, { DATA, 1024 * 7 },
383231200Smm		{ HOLE,	409600 * 8 }, { DATA, 1024 * 8 },
384231200Smm		{ HOLE,	409600 * 9 }, { DATA, 1024 * 9 },
385231200Smm		{ HOLE,	409600 * 10}, { DATA, 1024 * 10},/* 20 */
386231200Smm		{ END,	0 }
387231200Smm	};
388231200Smm	const struct sparse sparse_file3[] = {
389231200Smm 		/* This hole size is too small to create a sparse
390231200Smm		 * files for almost filesystem. */
391231200Smm		{ HOLE,	 1024 }, { DATA, 10240 },
392231200Smm		{ END,	0 }
393231200Smm	};
394231200Smm
395231200Smm	/*
396231200Smm	 * Test for the case that sparse data indicates just the whole file
397231200Smm	 * data.
398231200Smm	 */
399231200Smm	test_sparse_whole_file_data();
400231200Smm
401231200Smm	/* Check if the filesystem where CWD on can
402231200Smm	 * report the number of the holes of a sparse file. */
403231200Smm#ifdef PATH_MAX
404231200Smm	cwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
405231200Smm#else
406231200Smm	cwd = getcwd(NULL, 0);
407231200Smm#endif
408231200Smm	if (!assert(cwd != NULL))
409231200Smm		return;
410231200Smm	if (!is_sparse_supported(cwd)) {
411231200Smm		free(cwd);
412231200Smm		skipping("This filesystem or platform do not support "
413231200Smm		    "the reporting of the holes of a sparse file through "
414231200Smm		    "API such as lseek(HOLE)");
415231200Smm		return;
416231200Smm	}
417231200Smm
418231200Smm	/*
419231200Smm	 * Get sparse data through directory traversals.
420231200Smm	 */
421231200Smm	assert((a = archive_read_disk_new()) != NULL);
422231200Smm
423231200Smm	verify_sparse_file(a, "file0", sparse_file0, 5);
424231200Smm	verify_sparse_file(a, "file1", sparse_file1, 2);
425231200Smm	verify_sparse_file(a, "file2", sparse_file2, 20);
426231200Smm	verify_sparse_file(a, "file3", sparse_file3, 0);
427231200Smm
428231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
429231200Smm
430231200Smm	/*
431231200Smm	 * Get sparse data through archive_read_disk_entry_from_file().
432231200Smm	 */
433231200Smm	assert((a = archive_read_disk_new()) != NULL);
434231200Smm
435231200Smm	verify_sparse_file2(a, "file0", sparse_file0, 5, 0);
436231200Smm	verify_sparse_file2(a, "file0", sparse_file0, 5, 1);
437231200Smm
438231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
439231200Smm	free(cwd);
440231200Smm}
441