xref: /freebsd/contrib/libarchive/cpio/test/test_option_a.c (revision 52c2bb75163559a6e2866ad374a7de67a4ea1273)
1caf54c4fSMartin Matuska /*-
2caf54c4fSMartin Matuska  * Copyright (c) 2003-2008 Tim Kientzle
3caf54c4fSMartin Matuska  * All rights reserved.
4caf54c4fSMartin Matuska  *
5caf54c4fSMartin Matuska  * Redistribution and use in source and binary forms, with or without
6caf54c4fSMartin Matuska  * modification, are permitted provided that the following conditions
7caf54c4fSMartin Matuska  * are met:
8caf54c4fSMartin Matuska  * 1. Redistributions of source code must retain the above copyright
9caf54c4fSMartin Matuska  *    notice, this list of conditions and the following disclaimer.
10caf54c4fSMartin Matuska  * 2. Redistributions in binary form must reproduce the above copyright
11caf54c4fSMartin Matuska  *    notice, this list of conditions and the following disclaimer in the
12caf54c4fSMartin Matuska  *    documentation and/or other materials provided with the distribution.
13caf54c4fSMartin Matuska  *
14caf54c4fSMartin Matuska  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15caf54c4fSMartin Matuska  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16caf54c4fSMartin Matuska  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17caf54c4fSMartin Matuska  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18caf54c4fSMartin Matuska  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19caf54c4fSMartin Matuska  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20caf54c4fSMartin Matuska  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21caf54c4fSMartin Matuska  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22caf54c4fSMartin Matuska  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23caf54c4fSMartin Matuska  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24caf54c4fSMartin Matuska  */
25caf54c4fSMartin Matuska #include "test.h"
26caf54c4fSMartin Matuska #if defined(HAVE_UTIME_H)
27caf54c4fSMartin Matuska #include <utime.h>
28caf54c4fSMartin Matuska #elif defined(HAVE_SYS_UTIME_H)
29caf54c4fSMartin Matuska #include <sys/utime.h>
30caf54c4fSMartin Matuska #endif
316c22d9efSMartin Matuska __FBSDID("$FreeBSD$");
32caf54c4fSMartin Matuska 
33caf54c4fSMartin Matuska static struct {
34caf54c4fSMartin Matuska 	const char *name;
35caf54c4fSMartin Matuska 	time_t atime_sec;
36caf54c4fSMartin Matuska } files[] = {
37caf54c4fSMartin Matuska 	{ "f0", 0 },
38caf54c4fSMartin Matuska 	{ "f1", 0 },
39caf54c4fSMartin Matuska 	{ "f2", 0 },
40caf54c4fSMartin Matuska 	{ "f3", 0 },
41caf54c4fSMartin Matuska 	{ "f4", 0 },
42caf54c4fSMartin Matuska 	{ "f5", 0 }
43caf54c4fSMartin Matuska };
44caf54c4fSMartin Matuska 
45caf54c4fSMartin Matuska /*
46caf54c4fSMartin Matuska  * Create a bunch of test files and record their atimes.
47caf54c4fSMartin Matuska  * For the atime preserve/change tests, the files must have
48caf54c4fSMartin Matuska  * atimes in the past.  We can accomplish this by explicitly invoking
49caf54c4fSMartin Matuska  * utime() on platforms that support it or by simply sleeping
50caf54c4fSMartin Matuska  * for a second after creating the files.  (Creating all of the files
51caf54c4fSMartin Matuska  * at once means we only need to sleep once.)
52caf54c4fSMartin Matuska  */
53caf54c4fSMartin Matuska static void
54caf54c4fSMartin Matuska test_create(void)
55caf54c4fSMartin Matuska {
56caf54c4fSMartin Matuska 	struct stat st;
57caf54c4fSMartin Matuska 	struct utimbuf times;
58caf54c4fSMartin Matuska 	static const int numfiles = sizeof(files) / sizeof(files[0]);
59caf54c4fSMartin Matuska 	int i;
60caf54c4fSMartin Matuska 
61caf54c4fSMartin Matuska 	for (i = 0; i < numfiles; ++i) {
62caf54c4fSMartin Matuska 		/*
63caf54c4fSMartin Matuska 		 * Note: Have to write at least one byte to the file.
64caf54c4fSMartin Matuska 		 * cpio doesn't bother reading the file if it's zero length,
65caf54c4fSMartin Matuska 		 * so the atime never gets changed in that case, which
66caf54c4fSMartin Matuska 		 * makes the tests below rather pointless.
67caf54c4fSMartin Matuska 		 */
68caf54c4fSMartin Matuska 		assertMakeFile(files[i].name, 0644, "a");
69caf54c4fSMartin Matuska 
70caf54c4fSMartin Matuska 		/* If utime() isn't supported on your platform, just
71caf54c4fSMartin Matuska 		 * #ifdef this section out.  Most of the test below is
72caf54c4fSMartin Matuska 		 * still valid. */
73caf54c4fSMartin Matuska 		memset(&times, 0, sizeof(times));
74*52c2bb75SMartin Matuska #if defined(_WIN32) && !defined(CYGWIN)
75*52c2bb75SMartin Matuska 		times.actime = 86400;
76*52c2bb75SMartin Matuska 		times.modtime = 86400;
77*52c2bb75SMartin Matuska #else
78caf54c4fSMartin Matuska 		times.actime = 1;
79caf54c4fSMartin Matuska 		times.modtime = 3;
80*52c2bb75SMartin Matuska #endif
81caf54c4fSMartin Matuska 		assertEqualInt(0, utime(files[i].name, &times));
82caf54c4fSMartin Matuska 
83caf54c4fSMartin Matuska 		/* Record whatever atime the file ended up with. */
84caf54c4fSMartin Matuska 		/* If utime() is available, this should be 1, but there's
85caf54c4fSMartin Matuska 		 * no harm in being careful. */
86caf54c4fSMartin Matuska 		assertEqualInt(0, stat(files[i].name, &st));
87caf54c4fSMartin Matuska 		files[i].atime_sec = st.st_atime;
88caf54c4fSMartin Matuska 	}
89caf54c4fSMartin Matuska 
90caf54c4fSMartin Matuska 	/* Wait until the atime on the last file is actually in the past. */
91caf54c4fSMartin Matuska 	sleepUntilAfter(files[numfiles - 1].atime_sec);
92caf54c4fSMartin Matuska }
93caf54c4fSMartin Matuska 
94caf54c4fSMartin Matuska DEFINE_TEST(test_option_a)
95caf54c4fSMartin Matuska {
96caf54c4fSMartin Matuska 	struct stat st;
97caf54c4fSMartin Matuska 	int r;
98caf54c4fSMartin Matuska 	char *p;
99caf54c4fSMartin Matuska 
100caf54c4fSMartin Matuska 	/* Create all of the test files. */
101caf54c4fSMartin Matuska 	test_create();
102caf54c4fSMartin Matuska 
103caf54c4fSMartin Matuska 	/* Sanity check; verify that atimes really do get modified. */
104a8fc61d5SMartin Matuska 	p = slurpfile(NULL, "f0");
105a8fc61d5SMartin Matuska 	assert(p != NULL);
106caf54c4fSMartin Matuska 	free(p);
107caf54c4fSMartin Matuska 	assertEqualInt(0, stat("f0", &st));
108caf54c4fSMartin Matuska 	if (st.st_atime == files[0].atime_sec) {
109caf54c4fSMartin Matuska 		skipping("Cannot verify -a option\n"
110caf54c4fSMartin Matuska 		    "      Your system appears to not support atime.");
111caf54c4fSMartin Matuska 	}
112caf54c4fSMartin Matuska 	else
113caf54c4fSMartin Matuska 	{
114caf54c4fSMartin Matuska 		/*
115caf54c4fSMartin Matuska 		 * If this disk is mounted noatime, then we can't
116caf54c4fSMartin Matuska 		 * verify correct operation without -a.
117caf54c4fSMartin Matuska 		 */
118caf54c4fSMartin Matuska 
119caf54c4fSMartin Matuska 		/* Copy the file without -a; should change the atime. */
120caf54c4fSMartin Matuska 		r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog);
121caf54c4fSMartin Matuska 		assertEqualInt(r, 0);
122caf54c4fSMartin Matuska 		assertTextFileContents("1 block\n", "copy-no-a.err");
123caf54c4fSMartin Matuska 		assertEmptyFile("copy-no-a.out");
124caf54c4fSMartin Matuska 		assertEqualInt(0, stat(files[1].name, &st));
125caf54c4fSMartin Matuska 		failure("Copying file without -a should have changed atime.");
126caf54c4fSMartin Matuska 		assert(st.st_atime != files[1].atime_sec);
127caf54c4fSMartin Matuska 
128caf54c4fSMartin Matuska 		/* Archive the file without -a; should change the atime. */
129caf54c4fSMartin Matuska 		r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog);
130caf54c4fSMartin Matuska 		assertEqualInt(r, 0);
131caf54c4fSMartin Matuska 		assertTextFileContents("1 block\n", "copy-no-a.err");
132caf54c4fSMartin Matuska 		assertEqualInt(0, stat(files[2].name, &st));
133caf54c4fSMartin Matuska 		failure("Archiving file without -a should have changed atime.");
134caf54c4fSMartin Matuska 		assert(st.st_atime != files[2].atime_sec);
135caf54c4fSMartin Matuska 	}
136caf54c4fSMartin Matuska 
137caf54c4fSMartin Matuska 	/*
138caf54c4fSMartin Matuska 	 * We can, of course, still verify that the atime is unchanged
139caf54c4fSMartin Matuska 	 * when using the -a option.
140caf54c4fSMartin Matuska 	 */
141caf54c4fSMartin Matuska 
142caf54c4fSMartin Matuska 	/* Copy the file with -a; should not change the atime. */
143caf54c4fSMartin Matuska 	r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err",
144caf54c4fSMartin Matuska 	    files[3].name, testprog);
145caf54c4fSMartin Matuska 	assertEqualInt(r, 0);
146caf54c4fSMartin Matuska 	assertTextFileContents("1 block\n", "copy-a.err");
147caf54c4fSMartin Matuska 	assertEmptyFile("copy-a.out");
148caf54c4fSMartin Matuska 	assertEqualInt(0, stat(files[3].name, &st));
149caf54c4fSMartin Matuska 	failure("Copying file with -a should not have changed atime.");
150caf54c4fSMartin Matuska 	assertEqualInt(st.st_atime, files[3].atime_sec);
151caf54c4fSMartin Matuska 
152caf54c4fSMartin Matuska 	/* Archive the file with -a; should not change the atime. */
153caf54c4fSMartin Matuska 	r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err",
154caf54c4fSMartin Matuska 	    files[4].name, testprog);
155caf54c4fSMartin Matuska 	assertEqualInt(r, 0);
156caf54c4fSMartin Matuska 	assertTextFileContents("1 block\n", "copy-a.err");
157caf54c4fSMartin Matuska 	assertEqualInt(0, stat(files[4].name, &st));
158caf54c4fSMartin Matuska 	failure("Archiving file with -a should not have changed atime.");
159caf54c4fSMartin Matuska 	assertEqualInt(st.st_atime, files[4].atime_sec);
160caf54c4fSMartin Matuska }
161