Path: blob/main/contrib/libarchive/cpio/test/test_option_a.c
39507 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2003-2008 Tim Kientzle4* All rights reserved.5*/6#include "test.h"7#if defined(HAVE_UTIME_H)8#include <utime.h>9#elif defined(HAVE_SYS_UTIME_H)10#include <sys/utime.h>11#endif1213static struct {14const char *name;15time_t atime_sec;16} files[] = {17{ "f0", 0 },18{ "f1", 0 },19{ "f2", 0 },20{ "f3", 0 },21{ "f4", 0 },22{ "f5", 0 }23};2425/*26* Create a bunch of test files and record their atimes.27* For the atime preserve/change tests, the files must have28* atimes in the past. We can accomplish this by explicitly invoking29* utime() on platforms that support it or by simply sleeping30* for a second after creating the files. (Creating all of the files31* at once means we only need to sleep once.)32*/33static void34test_create(void)35{36struct stat st;37struct utimbuf times;38static const int numfiles = sizeof(files) / sizeof(files[0]);39int i;4041for (i = 0; i < numfiles; ++i) {42/*43* Note: Have to write at least one byte to the file.44* cpio doesn't bother reading the file if it's zero length,45* so the atime never gets changed in that case, which46* makes the tests below rather pointless.47*/48assertMakeFile(files[i].name, 0644, "a");4950/* If utime() isn't supported on your platform, just51* #ifdef this section out. Most of the test below is52* still valid. */53memset(×, 0, sizeof(times));54#if defined(_WIN32) && !defined(__CYGWIN__)55times.actime = 86400;56times.modtime = 86400;57#else58times.actime = 1;59times.modtime = 3;60#endif61assertEqualInt(0, utime(files[i].name, ×));6263/* Record whatever atime the file ended up with. */64/* If utime() is available, this should be 1, but there's65* no harm in being careful. */66assertEqualInt(0, stat(files[i].name, &st));67files[i].atime_sec = st.st_atime;68}6970/* Wait until the atime on the last file is actually in the past. */71sleepUntilAfter(files[numfiles - 1].atime_sec);72}7374DEFINE_TEST(test_option_a)75{76struct stat st;77int r;78char *p;7980/* Create all of the test files. */81test_create();8283/* Sanity check; verify that atimes really do get modified. */84p = slurpfile(NULL, "f0");85assert(p != NULL);86free(p);87assertEqualInt(0, stat("f0", &st));88if (st.st_atime == files[0].atime_sec) {89skipping("Cannot verify -a option\n"90" Your system appears to not support atime.");91}92else93{94/*95* If this disk is mounted noatime, then we can't96* verify correct operation without -a.97*/9899/* Copy the file without -a; should change the atime. */100r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog);101assertEqualInt(r, 0);102assertTextFileContents("1 block\n", "copy-no-a.err");103assertEmptyFile("copy-no-a.out");104assertEqualInt(0, stat(files[1].name, &st));105failure("Copying file without -a should have changed atime.");106assert(st.st_atime != files[1].atime_sec);107108/* Archive the file without -a; should change the atime. */109r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog);110assertEqualInt(r, 0);111assertTextFileContents("1 block\n", "copy-no-a.err");112assertEqualInt(0, stat(files[2].name, &st));113failure("Archiving file without -a should have changed atime.");114assert(st.st_atime != files[2].atime_sec);115}116117/*118* We can, of course, still verify that the atime is unchanged119* when using the -a option.120*/121122/* Copy the file with -a; should not change the atime. */123r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err",124files[3].name, testprog);125assertEqualInt(r, 0);126assertTextFileContents("1 block\n", "copy-a.err");127assertEmptyFile("copy-a.out");128assertEqualInt(0, stat(files[3].name, &st));129failure("Copying file with -a should not have changed atime.");130assertEqualInt(st.st_atime, files[3].atime_sec);131132/* Archive the file with -a; should not change the atime. */133r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err",134files[4].name, testprog);135assertEqualInt(r, 0);136assertTextFileContents("1 block\n", "copy-a.err");137assertEqualInt(0, stat(files[4].name, &st));138failure("Archiving file with -a should not have changed atime.");139assertEqualInt(st.st_atime, files[4].atime_sec);140}141142143