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