2 * Copyright (c) 2003-2008 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #if defined(HAVE_UTIME_H)
28 #elif defined(HAVE_SYS_UTIME_H)
29 #include <sys/utime.h>
31 __FBSDID("$FreeBSD$");
46 * Create a bunch of test files and record their atimes.
47 * For the atime preserve/change tests, the files must have
48 * atimes in the past. We can accomplish this by explicitly invoking
49 * utime() on platforms that support it or by simply sleeping
50 * for a second after creating the files. (Creating all of the files
51 * at once means we only need to sleep once.)
58 static const int numfiles = sizeof(files) / sizeof(files[0]);
61 for (i = 0; i < numfiles; ++i) {
63 * Note: Have to write at least one byte to the file.
64 * cpio doesn't bother reading the file if it's zero length,
65 * so the atime never gets changed in that case, which
66 * makes the tests below rather pointless.
68 assertMakeFile(files[i].name, 0644, "a");
70 /* If utime() isn't supported on your platform, just
71 * #ifdef this section out. Most of the test below is
73 memset(×, 0, sizeof(times));
74 #if defined(_WIN32) && !defined(CYGWIN)
76 times.modtime = 86400;
81 assertEqualInt(0, utime(files[i].name, ×));
83 /* Record whatever atime the file ended up with. */
84 /* If utime() is available, this should be 1, but there's
85 * no harm in being careful. */
86 assertEqualInt(0, stat(files[i].name, &st));
87 files[i].atime_sec = st.st_atime;
90 /* Wait until the atime on the last file is actually in the past. */
91 sleepUntilAfter(files[numfiles - 1].atime_sec);
94 DEFINE_TEST(test_option_a)
100 /* Create all of the test files. */
103 /* Sanity check; verify that atimes really do get modified. */
104 p = slurpfile(NULL, "f0");
107 assertEqualInt(0, stat("f0", &st));
108 if (st.st_atime == files[0].atime_sec) {
109 skipping("Cannot verify -a option\n"
110 " Your system appears to not support atime.");
115 * If this disk is mounted noatime, then we can't
116 * verify correct operation without -a.
119 /* Copy the file without -a; should change the atime. */
120 r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog);
121 assertEqualInt(r, 0);
122 assertTextFileContents("1 block\n", "copy-no-a.err");
123 assertEmptyFile("copy-no-a.out");
124 assertEqualInt(0, stat(files[1].name, &st));
125 failure("Copying file without -a should have changed atime.");
126 assert(st.st_atime != files[1].atime_sec);
128 /* Archive the file without -a; should change the atime. */
129 r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog);
130 assertEqualInt(r, 0);
131 assertTextFileContents("1 block\n", "copy-no-a.err");
132 assertEqualInt(0, stat(files[2].name, &st));
133 failure("Archiving file without -a should have changed atime.");
134 assert(st.st_atime != files[2].atime_sec);
138 * We can, of course, still verify that the atime is unchanged
139 * when using the -a option.
142 /* Copy the file with -a; should not change the atime. */
143 r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err",
144 files[3].name, testprog);
145 assertEqualInt(r, 0);
146 assertTextFileContents("1 block\n", "copy-a.err");
147 assertEmptyFile("copy-a.out");
148 assertEqualInt(0, stat(files[3].name, &st));
149 failure("Copying file with -a should not have changed atime.");
150 assertEqualInt(st.st_atime, files[3].atime_sec);
152 /* Archive the file with -a; should not change the atime. */
153 r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err",
154 files[4].name, testprog);
155 assertEqualInt(r, 0);
156 assertTextFileContents("1 block\n", "copy-a.err");
157 assertEqualInt(0, stat(files[4].name, &st));
158 failure("Archiving file with -a should not have changed atime.");
159 assertEqualInt(st.st_atime, files[4].atime_sec);