1314567Smm/*-
2314567Smm * Copyright (c) 2017 Martin Matuska
3314567Smm * All rights reserved.
4314567Smm *
5314567Smm * Redistribution and use in source and binary forms, with or without
6314567Smm * modification, are permitted provided that the following conditions
7314567Smm * are met:
8314567Smm * 1. Redistributions of source code must retain the above copyright
9314567Smm *    notice, this list of conditions and the following disclaimer.
10314567Smm * 2. Redistributions in binary form must reproduce the above copyright
11314567Smm *    notice, this list of conditions and the following disclaimer in the
12314567Smm *    documentation and/or other materials provided with the distribution.
13314567Smm *
14314567Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15314567Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16314567Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17314567Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18314567Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19314567Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20314567Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21314567Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22314567Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23314567Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24314567Smm */
25314567Smm#include "test.h"
26314567Smm__FBSDID("$FreeBSD: stable/10/contrib/libarchive/tar/test/test_option_fflags.c 337352 2018-08-05 14:36:12Z mm $");
27314567Smm
28337352Smm#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__BORLANDC__)
29337352Smm#define chmod _chmod
30337352Smm#endif
31337352Smm
32314571Smmstatic void
33314571Smmclear_fflags(const char *pathname)
34314571Smm{
35314571Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS)
36314571Smm	chflags(pathname, 0);
37314571Smm#elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
38314571Smm      (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
39314571Smm	int fd;
40314571Smm
41314571Smm	fd = open(pathname, O_RDONLY | O_NONBLOCK);
42314571Smm	if (fd < 0)
43314571Smm		return;
44314571Smm	ioctl(fd,
45314571Smm#ifdef FS_IOC_GETFLAGS
46314571Smm	    FS_IOC_GETFLAGS,
47314571Smm#else
48314571Smm	    EXT2_IOC_GETFLAGS,
49314571Smm#endif
50314571Smm	    0);
51314571Smm#else
52314571Smm	(void)pathname; /* UNUSED */
53314571Smm#endif
54314571Smm	return;
55314571Smm}
56314571Smm
57314567SmmDEFINE_TEST(test_option_fflags)
58314567Smm{
59314567Smm	int r;
60314567Smm
61314567Smm	if (!canNodump()) {
62314567Smm		skipping("Can't test nodump flag on this filesystem");
63314567Smm		return;
64314567Smm	}
65314567Smm
66314567Smm	/* Create a file. */
67314567Smm	assertMakeFile("f", 0644, "a");
68314567Smm
69314567Smm	/* Set nodump flag on the file */
70314567Smm	assertSetNodump("f");
71314567Smm
72314571Smm	/* FreeBSD ZFS workaround: ZFS sets uarch on all touched files and dirs */
73314571Smm	chmod("f", 0644);
74314571Smm
75314567Smm	/* Archive it with fflags */
76314567Smm	r = systemf("%s -c --fflags -f fflags.tar f >fflags.out 2>fflags.err", testprog);
77314567Smm	assertEqualInt(r, 0);
78314567Smm
79314567Smm	/* Archive it without fflags */
80314567Smm	r = systemf("%s -c --no-fflags -f nofflags.tar f >nofflags.out 2>nofflags.err", testprog);
81314567Smm	assertEqualInt(r, 0);
82314567Smm
83314567Smm	/* Extract fflags with fflags */
84314567Smm	assertMakeDir("fflags_fflags", 0755);
85314571Smm	clear_fflags("fflags_fflags");
86314567Smm	r = systemf("%s -x -C fflags_fflags --no-same-permissions --fflags -f fflags.tar >fflags_fflags.out 2>fflags_fflags.err", testprog);
87314567Smm	assertEqualInt(r, 0);
88314567Smm	assertEqualFflags("f", "fflags_fflags/f");
89314567Smm
90314567Smm	/* Extract fflags without fflags */
91314567Smm	assertMakeDir("fflags_nofflags", 0755);
92314571Smm	clear_fflags("fflags_nofflags");
93314567Smm	r = systemf("%s -x -C fflags_nofflags -p --no-fflags -f fflags.tar >fflags_nofflags.out 2>fflags_nofflags.err", testprog);
94314567Smm	assertEqualInt(r, 0);
95314567Smm	assertUnequalFflags("f", "fflags_nofflags/f");
96314567Smm
97314567Smm	/* Extract nofflags with fflags */
98314567Smm	assertMakeDir("nofflags_fflags", 0755);
99314571Smm	clear_fflags("nofflags_fflags");
100314567Smm	r = systemf("%s -x -C nofflags_fflags --no-same-permissions --fflags -f nofflags.tar >nofflags_fflags.out 2>nofflags_fflags.err", testprog);
101314567Smm	assertEqualInt(r, 0);
102314567Smm	assertUnequalFflags("f", "nofflags_fflags/f");
103314567Smm
104314567Smm	/* Extract nofflags with nofflags */
105314567Smm	assertMakeDir("nofflags_nofflags", 0755);
106314571Smm	clear_fflags("nofflags_nofflags");
107314567Smm	r = systemf("%s -x -C nofflags_nofflags -p --no-fflags -f nofflags.tar >nofflags_nofflags.out 2>nofflags_nofflags.err", testprog);
108314567Smm	assertEqualInt(r, 0);
109314567Smm	assertUnequalFflags("f", "nofflags_nofflags/f");
110314567Smm}
111