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: stable/10/contrib/libarchive/cpio/test/test_option_a.c 348608 2019-06-04 10:36:26Z mm $");
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));
74348608Smm#if defined(_WIN32) && !defined(CYGWIN)
75348608Smm		times.actime = 86400;
76348608Smm		times.modtime = 86400;
77348608Smm#else
78228753Smm		times.actime = 1;
79228753Smm		times.modtime = 3;
80348608Smm#endif
81228753Smm		assertEqualInt(0, utime(files[i].name, &times));
82228753Smm
83228753Smm		/* Record whatever atime the file ended up with. */
84228753Smm		/* If utime() is available, this should be 1, but there's
85228753Smm		 * no harm in being careful. */
86228753Smm		assertEqualInt(0, stat(files[i].name, &st));
87228753Smm		files[i].atime_sec = st.st_atime;
88228753Smm	}
89228753Smm
90228753Smm	/* Wait until the atime on the last file is actually in the past. */
91228753Smm	sleepUntilAfter(files[numfiles - 1].atime_sec);
92228753Smm}
93228753Smm
94228753SmmDEFINE_TEST(test_option_a)
95228753Smm{
96228753Smm	struct stat st;
97228753Smm	int r;
98228753Smm	char *p;
99228753Smm
100228753Smm	/* Create all of the test files. */
101228753Smm	test_create();
102228753Smm
103228753Smm	/* Sanity check; verify that atimes really do get modified. */
104318483Smm	p = slurpfile(NULL, "f0");
105318483Smm	assert(p != NULL);
106228753Smm	free(p);
107228753Smm	assertEqualInt(0, stat("f0", &st));
108228753Smm	if (st.st_atime == files[0].atime_sec) {
109228753Smm		skipping("Cannot verify -a option\n"
110228753Smm		    "      Your system appears to not support atime.");
111228753Smm	}
112228753Smm	else
113228753Smm	{
114228753Smm		/*
115228753Smm		 * If this disk is mounted noatime, then we can't
116228753Smm		 * verify correct operation without -a.
117228753Smm		 */
118228753Smm
119228753Smm		/* Copy the file without -a; should change the atime. */
120228753Smm		r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog);
121228753Smm		assertEqualInt(r, 0);
122228753Smm		assertTextFileContents("1 block\n", "copy-no-a.err");
123228753Smm		assertEmptyFile("copy-no-a.out");
124228753Smm		assertEqualInt(0, stat(files[1].name, &st));
125228753Smm		failure("Copying file without -a should have changed atime.");
126228753Smm		assert(st.st_atime != files[1].atime_sec);
127228753Smm
128228753Smm		/* Archive the file without -a; should change the atime. */
129228753Smm		r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog);
130228753Smm		assertEqualInt(r, 0);
131228753Smm		assertTextFileContents("1 block\n", "copy-no-a.err");
132228753Smm		assertEqualInt(0, stat(files[2].name, &st));
133228753Smm		failure("Archiving file without -a should have changed atime.");
134228753Smm		assert(st.st_atime != files[2].atime_sec);
135228753Smm	}
136228753Smm
137228753Smm	/*
138228753Smm	 * We can, of course, still verify that the atime is unchanged
139228753Smm	 * when using the -a option.
140228753Smm	 */
141228753Smm
142228753Smm	/* Copy the file with -a; should not change the atime. */
143228753Smm	r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err",
144228753Smm	    files[3].name, testprog);
145228753Smm	assertEqualInt(r, 0);
146228753Smm	assertTextFileContents("1 block\n", "copy-a.err");
147228753Smm	assertEmptyFile("copy-a.out");
148228753Smm	assertEqualInt(0, stat(files[3].name, &st));
149228753Smm	failure("Copying file with -a should not have changed atime.");
150228753Smm	assertEqualInt(st.st_atime, files[3].atime_sec);
151228753Smm
152228753Smm	/* Archive the file with -a; should not change the atime. */
153228753Smm	r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err",
154228753Smm	    files[4].name, testprog);
155228753Smm	assertEqualInt(r, 0);
156228753Smm	assertTextFileContents("1 block\n", "copy-a.err");
157228753Smm	assertEqualInt(0, stat(files[4].name, &st));
158228753Smm	failure("Archiving file with -a should not have changed atime.");
159228753Smm	assertEqualInt(st.st_atime, files[4].atime_sec);
160228753Smm}
161