164287048SMartin Matuska /*-
2*bd66c1b4SMartin Matuska * SPDX-License-Identifier: BSD-2-Clause
3*bd66c1b4SMartin Matuska *
464287048SMartin Matuska * Copyright (c) 2017 Martin Matuska
564287048SMartin Matuska * All rights reserved.
664287048SMartin Matuska */
764287048SMartin Matuska #include "test.h"
864287048SMartin Matuska
9f1951fd7SMartin Matuska #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__BORLANDC__)
10f1951fd7SMartin Matuska #define chmod _chmod
11f1951fd7SMartin Matuska #endif
12f1951fd7SMartin Matuska
1364287048SMartin Matuska static void
clear_fflags(const char * pathname)1464287048SMartin Matuska clear_fflags(const char *pathname)
1564287048SMartin Matuska {
1664287048SMartin Matuska #if defined(HAVE_STRUCT_STAT_ST_FLAGS)
1764287048SMartin Matuska chflags(pathname, 0);
1864287048SMartin Matuska #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
1964287048SMartin Matuska (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
2064287048SMartin Matuska int fd;
2164287048SMartin Matuska
2264287048SMartin Matuska fd = open(pathname, O_RDONLY | O_NONBLOCK);
2364287048SMartin Matuska if (fd < 0)
2464287048SMartin Matuska return;
2564287048SMartin Matuska ioctl(fd,
2664287048SMartin Matuska #ifdef FS_IOC_GETFLAGS
2764287048SMartin Matuska FS_IOC_GETFLAGS,
2864287048SMartin Matuska #else
2964287048SMartin Matuska EXT2_IOC_GETFLAGS,
3064287048SMartin Matuska #endif
3164287048SMartin Matuska 0);
3264287048SMartin Matuska #else
3364287048SMartin Matuska (void)pathname; /* UNUSED */
3464287048SMartin Matuska #endif
3564287048SMartin Matuska return;
3664287048SMartin Matuska }
3764287048SMartin Matuska
DEFINE_TEST(test_option_fflags)3864287048SMartin Matuska DEFINE_TEST(test_option_fflags)
3964287048SMartin Matuska {
4064287048SMartin Matuska int r;
4164287048SMartin Matuska
4264287048SMartin Matuska if (!canNodump()) {
4364287048SMartin Matuska skipping("Can't test nodump flag on this filesystem");
4464287048SMartin Matuska return;
4564287048SMartin Matuska }
4664287048SMartin Matuska
4764287048SMartin Matuska /* Create a file. */
4864287048SMartin Matuska assertMakeFile("f", 0644, "a");
4964287048SMartin Matuska
5064287048SMartin Matuska /* Set nodump flag on the file */
5164287048SMartin Matuska assertSetNodump("f");
5264287048SMartin Matuska
5364287048SMartin Matuska /* FreeBSD ZFS workaround: ZFS sets uarch on all touched files and dirs */
5464287048SMartin Matuska chmod("f", 0644);
5564287048SMartin Matuska
5664287048SMartin Matuska /* Archive it with fflags */
5764287048SMartin Matuska r = systemf("%s -c --fflags -f fflags.tar f >fflags.out 2>fflags.err", testprog);
5864287048SMartin Matuska assertEqualInt(r, 0);
5964287048SMartin Matuska
6064287048SMartin Matuska /* Archive it without fflags */
6164287048SMartin Matuska r = systemf("%s -c --no-fflags -f nofflags.tar f >nofflags.out 2>nofflags.err", testprog);
6264287048SMartin Matuska assertEqualInt(r, 0);
6364287048SMartin Matuska
6464287048SMartin Matuska /* Extract fflags with fflags */
6564287048SMartin Matuska assertMakeDir("fflags_fflags", 0755);
6664287048SMartin Matuska clear_fflags("fflags_fflags");
6764287048SMartin Matuska r = systemf("%s -x -C fflags_fflags --no-same-permissions --fflags -f fflags.tar >fflags_fflags.out 2>fflags_fflags.err", testprog);
6864287048SMartin Matuska assertEqualInt(r, 0);
6964287048SMartin Matuska assertEqualFflags("f", "fflags_fflags/f");
7064287048SMartin Matuska
7164287048SMartin Matuska /* Extract fflags without fflags */
7264287048SMartin Matuska assertMakeDir("fflags_nofflags", 0755);
7364287048SMartin Matuska clear_fflags("fflags_nofflags");
7464287048SMartin Matuska r = systemf("%s -x -C fflags_nofflags -p --no-fflags -f fflags.tar >fflags_nofflags.out 2>fflags_nofflags.err", testprog);
7564287048SMartin Matuska assertEqualInt(r, 0);
7664287048SMartin Matuska assertUnequalFflags("f", "fflags_nofflags/f");
7764287048SMartin Matuska
7864287048SMartin Matuska /* Extract nofflags with fflags */
7964287048SMartin Matuska assertMakeDir("nofflags_fflags", 0755);
8064287048SMartin Matuska clear_fflags("nofflags_fflags");
8164287048SMartin Matuska r = systemf("%s -x -C nofflags_fflags --no-same-permissions --fflags -f nofflags.tar >nofflags_fflags.out 2>nofflags_fflags.err", testprog);
8264287048SMartin Matuska assertEqualInt(r, 0);
8364287048SMartin Matuska assertUnequalFflags("f", "nofflags_fflags/f");
8464287048SMartin Matuska
8564287048SMartin Matuska /* Extract nofflags with nofflags */
8664287048SMartin Matuska assertMakeDir("nofflags_nofflags", 0755);
8764287048SMartin Matuska clear_fflags("nofflags_nofflags");
8864287048SMartin Matuska r = systemf("%s -x -C nofflags_nofflags -p --no-fflags -f nofflags.tar >nofflags_nofflags.out 2>nofflags_nofflags.err", testprog);
8964287048SMartin Matuska assertEqualInt(r, 0);
9064287048SMartin Matuska assertUnequalFflags("f", "nofflags_nofflags/f");
9164287048SMartin Matuska }
92