xref: /freebsd/contrib/libarchive/tar/test/test_option_older_than.c (revision 9bc300465e48e19d794d88d0c158a2adb92c7197)
1 /*-
2  * Copyright (c) 2010 Tim Kientzle
3  * Copyright (c) 2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include "test.h"
27 
28 DEFINE_TEST(test_option_older_than)
29 {
30 	struct stat st;
31 
32 	/*
33 	 * Basic test of --older-than.
34 	 * First, create three files with different mtimes.
35 	 * Create test1.tar with --older-than, test2.tar without.
36 	 */
37 	assertMakeDir("test1in", 0755);
38 	assertChdir("test1in");
39 	assertMakeDir("a", 0755);
40 	assertMakeDir("a/b", 0755);
41 	assertMakeFile("old.txt", 0644, "old.txt");
42 	assertMakeFile("a/b/old.txt", 0644, "old file in old directory");
43 	assertEqualInt(0, stat("old.txt", &st));
44 	sleepUntilAfter(st.st_mtime);
45 	assertMakeFile("middle.txt", 0644, "middle.txt");
46 	assertEqualInt(0, stat("middle.txt", &st));
47 	sleepUntilAfter(st.st_mtime);
48 	assertMakeFile("new.txt", 0644, "new");
49 	assertMakeFile("a/b/new.txt", 0644, "new file in old directory");
50 
51 	/* Test --older-than on create */
52 	assertEqualInt(0,
53 		systemf("%s --format pax -cf ../test1.tar "
54 			"--older-than middle.txt *.txt a",
55 			testprog));
56 	assertEqualInt(0,
57 		systemf("%s --format pax -cf ../test2.tar *.txt a",
58 			testprog));
59 	assertChdir("..");
60 
61 	/* Extract test1.tar to a clean dir and verify what got archived. */
62 	assertMakeDir("test1out", 0755);
63 	assertChdir("test1out");
64 	assertEqualInt(0, systemf("%s xf ../test1.tar", testprog));
65 	assertFileNotExists("new.txt");
66 	assertFileNotExists("a/b/new.txt");
67 	assertFileNotExists("middle.txt");
68 	assertFileExists("old.txt");
69 	assertFileExists("a/b/old.txt");
70 	assertChdir("..");
71 
72 	/* Extract test2.tar to a clean dir with --older-than and verify. */
73 	assertMakeDir("test2out", 0755);
74 	assertChdir("test2out");
75 	assertEqualInt(0,
76 		systemf("%s xf ../test2.tar --older-than ../test1in/middle.txt",
77 			testprog));
78 	assertFileNotExists("new.txt");
79 	assertFileNotExists("a/b/new.txt");
80 	assertFileNotExists("middle.txt");
81 	assertFileExists("old.txt");
82 	assertFileExists("a/b/old.txt");
83 	assertChdir("..");
84 }
85