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