1 /*- 2 * Copyright (c) 2003-2008 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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. 24 */ 25 #include "test.h" 26 #include <utime.h> 27 __FBSDID("$FreeBSD$"); 28 29 static struct { 30 const char *name; 31 time_t atime_sec; 32 } files[] = { 33 { "f0", 0 }, 34 { "f1", 0 }, 35 { "f2", 0 }, 36 { "f3", 0 }, 37 { "f4", 0 }, 38 { "f5", 0 } 39 }; 40 41 /* 42 * Create a bunch of test files and record their atimes. 43 * For the atime preserve/change tests, the files must have 44 * atimes in the past. We can accomplish this by explicitly invoking 45 * utime() on platforms that support it or by simply sleeping 46 * for a second after creating the files. (Creating all of the files 47 * at once means we only need to sleep once.) 48 */ 49 static void 50 test_create(void) 51 { 52 struct stat st; 53 struct utimbuf times; 54 static const int numfiles = sizeof(files) / sizeof(files[0]); 55 int i; 56 int fd; 57 58 for (i = 0; i < numfiles; ++i) { 59 fd = open(files[i].name, O_CREAT | O_WRONLY, 0644); 60 assert(fd >= 0); 61 /* 62 * Note: Have to write at least one byte to the file. 63 * cpio doesn't bother reading the file if it's zero length, 64 * so the atime never gets changed in that case, which 65 * makes the tests below rather pointless. 66 */ 67 assertEqualInt(1, write(fd, "a", 1)); 68 close(fd); 69 70 /* If utime() isn't supported on your platform, just 71 * #ifdef this section out. Most of the test below is 72 * still valid. */ 73 memset(×, 0, sizeof(times)); 74 times.actime = 1; 75 times.modtime = 3; 76 assertEqualInt(0, utime(files[i].name, ×)); 77 78 /* Record whatever atime the file ended up with. */ 79 /* If utime() is available, this should be 1, but there's 80 * no harm in being careful. */ 81 assertEqualInt(0, stat(files[i].name, &st)); 82 files[i].atime_sec = st.st_atime; 83 } 84 85 /* Wait until the atime on the last file is actually in the past. */ 86 /* If utime() is supported above, there's no sleep here which 87 * makes the test faster. */ 88 while (files[numfiles - 1].atime_sec >= time(NULL)) 89 sleep(1); 90 } 91 92 DEFINE_TEST(test_option_a) 93 { 94 struct stat st; 95 int r; 96 int f; 97 char buff[64]; 98 99 /* Create all of the test files. */ 100 test_create(); 101 102 /* Sanity check; verify that atimes really do get modified. */ 103 f = open(files[0].name, O_RDONLY); 104 assertEqualInt(1, read(f,buff, 1)); 105 assertEqualInt(0, close(f)); 106 assertEqualInt(0, stat("f0", &st)); 107 if (st.st_atime == files[0].atime_sec) { 108 skipping("Cannot verify -a option\n" 109 " Your system appears to not support atime."); 110 } 111 else 112 { 113 /* 114 * If this disk is mounted noatime, then we can't 115 * verify correct operation without -a. 116 */ 117 118 /* Copy the file without -a; should change the atime. */ 119 r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog); 120 assertEqualInt(r, 0); 121 assertEmptyFile("copy-no-a.err"); 122 assertEmptyFile("copy-no-a.out"); 123 assertEqualInt(0, stat(files[1].name, &st)); 124 failure("Copying file without -a should have changed atime."); 125 assert(st.st_atime != files[1].atime_sec); 126 127 /* Archive the file without -a; should change the atime. */ 128 r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog); 129 assertEqualInt(r, 0); 130 assertEmptyFile("copy-no-a.err"); 131 assertEqualInt(0, stat(files[2].name, &st)); 132 failure("Archiving file without -a should have changed atime."); 133 assert(st.st_atime != files[2].atime_sec); 134 } 135 136 /* 137 * We can, of course, still verify that the atime is unchanged 138 * when using the -a option. 139 */ 140 141 /* Copy the file with -a; should not change the atime. */ 142 r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err", 143 files[3].name, testprog); 144 assertEqualInt(r, 0); 145 assertEmptyFile("copy-a.err"); 146 assertEmptyFile("copy-a.out"); 147 assertEqualInt(0, stat(files[3].name, &st)); 148 failure("Copying file with -a should not have changed atime."); 149 assertEqualInt(st.st_atime, files[3].atime_sec); 150 151 /* Archive the file with -a; should not change the atime. */ 152 r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err", 153 files[4].name, testprog); 154 assertEqualInt(r, 0); 155 assertEmptyFile("copy-a.err"); 156 assertEqualInt(0, stat(files[4].name, &st)); 157 failure("Archiving file with -a should not have changed atime."); 158 assertEqualInt(st.st_atime, files[4].atime_sec); 159 } 160