1231200Smm/*-
2231200Smm * Copyright (c) 2011 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 *    in this position and unchanged.
11231200Smm * 2. Redistributions in binary form must reproduce the above copyright
12231200Smm *    notice, this list of conditions and the following disclaimer in the
13231200Smm *    documentation and/or other materials provided with the distribution.
14231200Smm *
15231200Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16231200Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17231200Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18231200Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19231200Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20231200Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21231200Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22231200Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23231200Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24231200Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25231200Smm */
26231200Smm
27231200Smm#include "test.h"
28231200Smm__FBSDID("$FreeBSD$");
29231200Smm#ifdef HAVE_SYS_STAT_H
30231200Smm#include <sys/stat.h>
31231200Smm#endif
32231200Smm
33231200Smm/*
34231200Smm * Test UFS file flags with/without use-set option.
35231200Smm */
36231200Smm#if defined(UF_IMMUTABLE) && defined(UF_NODUMP)
37231200Smm
38231200Smmstatic char buff[4096];
39231200Smmstatic struct {
40231200Smm	const char	*path;
41231200Smm	unsigned long	 fflags;
42231200Smm} entries[] = {
43231200Smm	{ "./f1", 	UF_IMMUTABLE | UF_NODUMP },
44248616Smm	{ "./f11", 	UF_IMMUTABLE | UF_NODUMP },
45231200Smm	{ "./f2", 	0 },
46231200Smm	{ "./f3", 	UF_NODUMP },
47231200Smm	{ NULL, 0 }
48231200Smm};
49231200Smm
50231200Smmstatic void
51231200Smmtest_write_format_mtree_sub(int use_set)
52231200Smm{
53231200Smm	struct archive_entry *ae;
54231200Smm	struct archive* a;
55231200Smm	size_t used;
56231200Smm	int i;
57231200Smm
58231200Smm	/* Create a mtree format archive. */
59231200Smm	assert((a = archive_write_new()) != NULL);
60231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree(a));
61231200Smm	if (use_set)
62231200Smm		assertEqualIntA(a, ARCHIVE_OK,
63231200Smm		    archive_write_set_options(a, "use-set,!all,flags,type"));
64231200Smm	else
65231200Smm		assertEqualIntA(a, ARCHIVE_OK,
66231200Smm		    archive_write_set_options(a, "!all,flags,type"));
67231200Smm	assertEqualIntA(a, ARCHIVE_OK,
68231200Smm	    archive_write_open_memory(a, buff, sizeof(buff)-1, &used));
69231200Smm
70231200Smm	/* Write entries */
71231200Smm	for (i = 0; entries[i].path != NULL; i++) {
72231200Smm		assert((ae = archive_entry_new()) != NULL);
73231200Smm		archive_entry_set_fflags(ae, entries[i].fflags, 0);
74231200Smm		archive_entry_copy_pathname(ae, entries[i].path);
75231200Smm		archive_entry_set_size(ae, 0);
76231200Smm		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
77231200Smm		archive_entry_free(ae);
78231200Smm	}
79231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
80231200Smm        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
81231200Smm
82231200Smm	if (use_set) {
83231200Smm		const char *p;
84231200Smm
85231200Smm		buff[used] = '\0';
86231200Smm		assert(NULL != (p = strstr(buff, "\n/set ")));
87231200Smm		if (p != NULL) {
88231200Smm			char *r;
89231200Smm			const char *o;
90231200Smm			p++;
91231200Smm			r = strchr(p, '\n');
92231200Smm			if (r != NULL)
93231200Smm				*r = '\0';
94231200Smm			o = "/set type=file flags=uchg,nodump";
95231200Smm			assertEqualString(o, p);
96231200Smm			if (r != NULL)
97231200Smm				*r = '\n';
98231200Smm		}
99231200Smm	}
100231200Smm
101231200Smm	/*
102231200Smm	 * Read the data and check it.
103231200Smm	 */
104231200Smm	assert((a = archive_read_new()) != NULL);
105231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
106231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
107231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
108231200Smm
109231200Smm	/* Read entries */
110231200Smm	for (i = 0; entries[i].path != NULL; i++) {
111231200Smm		unsigned long fset, fclr;
112231200Smm
113231200Smm		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
114231200Smm		archive_entry_fflags(ae, &fset, &fclr);
115231200Smm		assertEqualInt((int)entries[i].fflags, (int)fset);
116231200Smm		assertEqualInt(0, (int)fclr);
117231200Smm		assertEqualString(entries[i].path, archive_entry_pathname(ae));
118231200Smm	}
119231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
120231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
121231200Smm}
122231200Smm
123231200Smm#endif
124231200Smm
125231200SmmDEFINE_TEST(test_write_format_mtree_fflags)
126231200Smm{
127231200Smm#if defined(UF_IMMUTABLE) && defined(UF_NODUMP)
128231200Smm	/* Default setting */
129231200Smm	test_write_format_mtree_sub(0);
130231200Smm	/* Use /set keyword */
131231200Smm	test_write_format_mtree_sub(1);
132231200Smm#else
133231200Smm	skipping("This platform does not support UFS file flags");
134231200Smm#endif
135231200Smm}
136