xref: /freebsd/contrib/libarchive/tar/test/test_option_newer_than.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
DEFINE_TEST(test_option_newer_than)9 DEFINE_TEST(test_option_newer_than)
10 {
11   struct stat st;
12 
13   /*
14    * Basic test of --newer-than.
15    * First, create three files with different mtimes.
16    * Create test1.tar with --newer-than, test2.tar without.
17    */
18   assertMakeDir("test1in", 0755);
19   assertChdir("test1in");
20   assertMakeDir("a", 0755);
21   assertMakeDir("a/b", 0755);
22   assertMakeFile("old.txt", 0644, "old.txt");
23   assertEqualInt(0, stat("old.txt", &st));
24   sleepUntilAfter(st.st_mtime);
25   assertMakeFile("middle.txt", 0644, "middle.txt");
26   assertEqualInt(0, stat("middle.txt", &st));
27   sleepUntilAfter(st.st_mtime);
28   assertMakeFile("new.txt", 0644, "new");
29   assertMakeFile("a/b/new.txt", 0644, "new file in old directory");
30 
31   /* Test --newer-than on create */
32   assertEqualInt(0,
33 	systemf("%s --format pax -cf ../test1.tar "
34 		"--newer-than middle.txt *.txt a", testprog));
35   assertEqualInt(0,
36 	systemf("%s --format pax -cf ../test2.tar *.txt a", testprog));
37   assertChdir("..");
38 
39   /* Extract test1.tar to a clean dir and verify what got archived. */
40   assertMakeDir("test1out", 0755);
41   assertChdir("test1out");
42   assertEqualInt(0, systemf("%s xf ../test1.tar", testprog));
43   assertFileExists("new.txt");
44   assertFileExists("a/b/new.txt");
45   assertFileNotExists("middle.txt");
46   assertFileNotExists("old.txt");
47   assertChdir("..");
48 
49   /* Extract test2.tar to a clean dir with --newer-than and verify. */
50   assertMakeDir("test2out", 0755);
51   assertChdir("test2out");
52   assertEqualInt(0, systemf("%s xf ../test2.tar --newer-than ../test1in/middle.txt", testprog));
53   assertFileExists("new.txt");
54   assertFileExists("a/b/new.txt");
55   assertFileNotExists("middle.txt");
56   assertFileNotExists("old.txt");
57   assertChdir("..");
58 
59 }
60