Blame cpio/test/test_option_a.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2008 Tim Kientzle
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
#include "test.h"
Packit Service 1d0348
#if defined(HAVE_UTIME_H)
Packit Service 1d0348
#include <utime.h>
Packit Service 1d0348
#elif defined(HAVE_SYS_UTIME_H)
Packit Service 1d0348
#include <sys/utime.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_option_a.c,v 1.3 2008/08/24 06:21:00 kientzle Exp $");
Packit Service 1d0348
Packit Service 1d0348
static struct {
Packit Service 1d0348
	const char *name;
Packit Service 1d0348
	time_t atime_sec;
Packit Service 1d0348
} files[] = {
Packit Service 1d0348
	{ "f0", 0 },
Packit Service 1d0348
	{ "f1", 0 },
Packit Service 1d0348
	{ "f2", 0 },
Packit Service 1d0348
	{ "f3", 0 },
Packit Service 1d0348
	{ "f4", 0 },
Packit Service 1d0348
	{ "f5", 0 }
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Create a bunch of test files and record their atimes.
Packit Service 1d0348
 * For the atime preserve/change tests, the files must have
Packit Service 1d0348
 * atimes in the past.  We can accomplish this by explicitly invoking
Packit Service 1d0348
 * utime() on platforms that support it or by simply sleeping
Packit Service 1d0348
 * for a second after creating the files.  (Creating all of the files
Packit Service 1d0348
 * at once means we only need to sleep once.)
Packit Service 1d0348
 */
Packit Service 1d0348
static void
Packit Service 1d0348
test_create(void)
Packit Service 1d0348
{
Packit Service 1d0348
	struct stat st;
Packit Service 1d0348
	struct utimbuf times;
Packit Service 1d0348
	static const int numfiles = sizeof(files) / sizeof(files[0]);
Packit Service 1d0348
	int i;
Packit Service 1d0348
Packit Service 1d0348
	for (i = 0; i < numfiles; ++i) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Note: Have to write at least one byte to the file.
Packit Service 1d0348
		 * cpio doesn't bother reading the file if it's zero length,
Packit Service 1d0348
		 * so the atime never gets changed in that case, which
Packit Service 1d0348
		 * makes the tests below rather pointless.
Packit Service 1d0348
		 */
Packit Service 1d0348
		assertMakeFile(files[i].name, 0644, "a");
Packit Service 1d0348
Packit Service 1d0348
		/* If utime() isn't supported on your platform, just
Packit Service 1d0348
		 * #ifdef this section out.  Most of the test below is
Packit Service 1d0348
		 * still valid. */
Packit Service 1d0348
		memset(&times, 0, sizeof(times));
Packit Service 1d0348
		times.actime = 1;
Packit Service 1d0348
		times.modtime = 3;
Packit Service 1d0348
		assertEqualInt(0, utime(files[i].name, ×);;
Packit Service 1d0348
Packit Service 1d0348
		/* Record whatever atime the file ended up with. */
Packit Service 1d0348
		/* If utime() is available, this should be 1, but there's
Packit Service 1d0348
		 * no harm in being careful. */
Packit Service 1d0348
		assertEqualInt(0, stat(files[i].name, &st);;
Packit Service 1d0348
		files[i].atime_sec = st.st_atime;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Wait until the atime on the last file is actually in the past. */
Packit Service 1d0348
	sleepUntilAfter(files[numfiles - 1].atime_sec);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
DEFINE_TEST(test_option_a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct stat st;
Packit Service 1d0348
	int r;
Packit Service 1d0348
	char *p;
Packit Service 1d0348
Packit Service 1d0348
	/* Create all of the test files. */
Packit Service 1d0348
	test_create();
Packit Service 1d0348
Packit Service 1d0348
	/* Sanity check; verify that atimes really do get modified. */
Packit Service 1d0348
	p = slurpfile(NULL, "f0");
Packit Service 1d0348
	assert(p != NULL);
Packit Service 1d0348
	free(p);
Packit Service 1d0348
	assertEqualInt(0, stat("f0", &st);;
Packit Service 1d0348
	if (st.st_atime == files[0].atime_sec) {
Packit Service 1d0348
		skipping("Cannot verify -a option\n"
Packit Service 1d0348
		    "      Your system appears to not support atime.");
Packit Service 1d0348
	}
Packit Service 1d0348
	else
Packit Service 1d0348
	{
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * If this disk is mounted noatime, then we can't
Packit Service 1d0348
		 * verify correct operation without -a.
Packit Service 1d0348
		 */
Packit Service 1d0348
Packit Service 1d0348
		/* Copy the file without -a; should change the atime. */
Packit Service 1d0348
		r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog);
Packit Service 1d0348
		assertEqualInt(r, 0);
Packit Service 1d0348
		assertTextFileContents("1 block\n", "copy-no-a.err");
Packit Service 1d0348
		assertEmptyFile("copy-no-a.out");
Packit Service 1d0348
		assertEqualInt(0, stat(files[1].name, &st);;
Packit Service 1d0348
		failure("Copying file without -a should have changed atime.");
Packit Service 1d0348
		assert(st.st_atime != files[1].atime_sec);
Packit Service 1d0348
Packit Service 1d0348
		/* Archive the file without -a; should change the atime. */
Packit Service 1d0348
		r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog);
Packit Service 1d0348
		assertEqualInt(r, 0);
Packit Service 1d0348
		assertTextFileContents("1 block\n", "copy-no-a.err");
Packit Service 1d0348
		assertEqualInt(0, stat(files[2].name, &st);;
Packit Service 1d0348
		failure("Archiving file without -a should have changed atime.");
Packit Service 1d0348
		assert(st.st_atime != files[2].atime_sec);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * We can, of course, still verify that the atime is unchanged
Packit Service 1d0348
	 * when using the -a option.
Packit Service 1d0348
	 */
Packit Service 1d0348
Packit Service 1d0348
	/* Copy the file with -a; should not change the atime. */
Packit Service 1d0348
	r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err",
Packit Service 1d0348
	    files[3].name, testprog);
Packit Service 1d0348
	assertEqualInt(r, 0);
Packit Service 1d0348
	assertTextFileContents("1 block\n", "copy-a.err");
Packit Service 1d0348
	assertEmptyFile("copy-a.out");
Packit Service 1d0348
	assertEqualInt(0, stat(files[3].name, &st);;
Packit Service 1d0348
	failure("Copying file with -a should not have changed atime.");
Packit Service 1d0348
	assertEqualInt(st.st_atime, files[3].atime_sec);
Packit Service 1d0348
Packit Service 1d0348
	/* Archive the file with -a; should not change the atime. */
Packit Service 1d0348
	r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err",
Packit Service 1d0348
	    files[4].name, testprog);
Packit Service 1d0348
	assertEqualInt(r, 0);
Packit Service 1d0348
	assertTextFileContents("1 block\n", "copy-a.err");
Packit Service 1d0348
	assertEqualInt(0, stat(files[4].name, &st);;
Packit Service 1d0348
	failure("Archiving file with -a should not have changed atime.");
Packit Service 1d0348
	assertEqualInt(st.st_atime, files[4].atime_sec);
Packit Service 1d0348
}