1228753Smm/*-
2228753Smm * Copyright (c) 2003-2008 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26228753Smm#if defined(HAVE_UTIME_H)
27228753Smm#include <utime.h>
28228753Smm#elif defined(HAVE_SYS_UTIME_H)
29228753Smm#include <sys/utime.h>
30228753Smm#endif
31228763Smm__FBSDID("$FreeBSD$");
32228753Smm
33228753Smmstatic struct {
34228753Smm	const char *name;
35228753Smm	time_t atime_sec;
36228753Smm} files[] = {
37228753Smm	{ "f0", 0 },
38228753Smm	{ "f1", 0 },
39228753Smm	{ "f2", 0 },
40228753Smm	{ "f3", 0 },
41228753Smm	{ "f4", 0 },
42228753Smm	{ "f5", 0 }
43228753Smm};
44228753Smm
45228753Smm/*
46228753Smm * Create a bunch of test files and record their atimes.
47228753Smm * For the atime preserve/change tests, the files must have
48228753Smm * atimes in the past.  We can accomplish this by explicitly invoking
49228753Smm * utime() on platforms that support it or by simply sleeping
50228753Smm * for a second after creating the files.  (Creating all of the files
51228753Smm * at once means we only need to sleep once.)
52228753Smm */
53228753Smmstatic void
54228753Smmtest_create(void)
55228753Smm{
56228753Smm	struct stat st;
57228753Smm	struct utimbuf times;
58228753Smm	static const int numfiles = sizeof(files) / sizeof(files[0]);
59228753Smm	int i;
60228753Smm
61228753Smm	for (i = 0; i < numfiles; ++i) {
62228753Smm		/*
63228753Smm		 * Note: Have to write at least one byte to the file.
64228753Smm		 * cpio doesn't bother reading the file if it's zero length,
65228753Smm		 * so the atime never gets changed in that case, which
66228753Smm		 * makes the tests below rather pointless.
67228753Smm		 */
68228753Smm		assertMakeFile(files[i].name, 0644, "a");
69228753Smm
70228753Smm		/* If utime() isn't supported on your platform, just
71228753Smm		 * #ifdef this section out.  Most of the test below is
72228753Smm		 * still valid. */
73228753Smm		memset(&times, 0, sizeof(times));
74228753Smm		times.actime = 1;
75228753Smm		times.modtime = 3;
76228753Smm		assertEqualInt(0, utime(files[i].name, &times));
77228753Smm
78228753Smm		/* Record whatever atime the file ended up with. */
79228753Smm		/* If utime() is available, this should be 1, but there's
80228753Smm		 * no harm in being careful. */
81228753Smm		assertEqualInt(0, stat(files[i].name, &st));
82228753Smm		files[i].atime_sec = st.st_atime;
83228753Smm	}
84228753Smm
85228753Smm	/* Wait until the atime on the last file is actually in the past. */
86228753Smm	sleepUntilAfter(files[numfiles - 1].atime_sec);
87228753Smm}
88228753Smm
89228753SmmDEFINE_TEST(test_option_a)
90228753Smm{
91228753Smm	struct stat st;
92228753Smm	int r;
93228753Smm	char *p;
94228753Smm
95228753Smm	/* Create all of the test files. */
96228753Smm	test_create();
97228753Smm
98228753Smm	/* Sanity check; verify that atimes really do get modified. */
99228753Smm	assert((p = slurpfile(NULL, "f0")) != NULL);
100228753Smm	free(p);
101228753Smm	assertEqualInt(0, stat("f0", &st));
102228753Smm	if (st.st_atime == files[0].atime_sec) {
103228753Smm		skipping("Cannot verify -a option\n"
104228753Smm		    "      Your system appears to not support atime.");
105228753Smm	}
106228753Smm	else
107228753Smm	{
108228753Smm		/*
109228753Smm		 * If this disk is mounted noatime, then we can't
110228753Smm		 * verify correct operation without -a.
111228753Smm		 */
112228753Smm
113228753Smm		/* Copy the file without -a; should change the atime. */
114228753Smm		r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog);
115228753Smm		assertEqualInt(r, 0);
116228753Smm		assertTextFileContents("1 block\n", "copy-no-a.err");
117228753Smm		assertEmptyFile("copy-no-a.out");
118228753Smm		assertEqualInt(0, stat(files[1].name, &st));
119228753Smm		failure("Copying file without -a should have changed atime.");
120228753Smm		assert(st.st_atime != files[1].atime_sec);
121228753Smm
122228753Smm		/* Archive the file without -a; should change the atime. */
123228753Smm		r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog);
124228753Smm		assertEqualInt(r, 0);
125228753Smm		assertTextFileContents("1 block\n", "copy-no-a.err");
126228753Smm		assertEqualInt(0, stat(files[2].name, &st));
127228753Smm		failure("Archiving file without -a should have changed atime.");
128228753Smm		assert(st.st_atime != files[2].atime_sec);
129228753Smm	}
130228753Smm
131228753Smm	/*
132228753Smm	 * We can, of course, still verify that the atime is unchanged
133228753Smm	 * when using the -a option.
134228753Smm	 */
135228753Smm
136228753Smm	/* Copy the file with -a; should not change the atime. */
137228753Smm	r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err",
138228753Smm	    files[3].name, testprog);
139228753Smm	assertEqualInt(r, 0);
140228753Smm	assertTextFileContents("1 block\n", "copy-a.err");
141228753Smm	assertEmptyFile("copy-a.out");
142228753Smm	assertEqualInt(0, stat(files[3].name, &st));
143228753Smm	failure("Copying file with -a should not have changed atime.");
144228753Smm	assertEqualInt(st.st_atime, files[3].atime_sec);
145228753Smm
146228753Smm	/* Archive the file with -a; should not change the atime. */
147228753Smm	r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err",
148228753Smm	    files[4].name, testprog);
149228753Smm	assertEqualInt(r, 0);
150228753Smm	assertTextFileContents("1 block\n", "copy-a.err");
151228753Smm	assertEqualInt(0, stat(files[4].name, &st));
152228753Smm	failure("Archiving file with -a should not have changed atime.");
153228753Smm	assertEqualInt(st.st_atime, files[4].atime_sec);
154228753Smm}
155