1 1.1 joerg /*- 2 1.1.1.5 christos * SPDX-License-Identifier: BSD-2-Clause 3 1.1.1.5 christos * 4 1.1 joerg * Copyright (c) 2003-2008 Tim Kientzle 5 1.1 joerg * All rights reserved. 6 1.1 joerg */ 7 1.1 joerg #include "test.h" 8 1.1.1.2 joerg #if defined(HAVE_UTIME_H) 9 1.1 joerg #include <utime.h> 10 1.1.1.2 joerg #elif defined(HAVE_SYS_UTIME_H) 11 1.1.1.2 joerg #include <sys/utime.h> 12 1.1.1.2 joerg #endif 13 1.1 joerg 14 1.1 joerg static struct { 15 1.1 joerg const char *name; 16 1.1 joerg time_t atime_sec; 17 1.1 joerg } files[] = { 18 1.1 joerg { "f0", 0 }, 19 1.1 joerg { "f1", 0 }, 20 1.1 joerg { "f2", 0 }, 21 1.1 joerg { "f3", 0 }, 22 1.1 joerg { "f4", 0 }, 23 1.1 joerg { "f5", 0 } 24 1.1 joerg }; 25 1.1 joerg 26 1.1 joerg /* 27 1.1 joerg * Create a bunch of test files and record their atimes. 28 1.1 joerg * For the atime preserve/change tests, the files must have 29 1.1 joerg * atimes in the past. We can accomplish this by explicitly invoking 30 1.1 joerg * utime() on platforms that support it or by simply sleeping 31 1.1 joerg * for a second after creating the files. (Creating all of the files 32 1.1 joerg * at once means we only need to sleep once.) 33 1.1 joerg */ 34 1.1 joerg static void 35 1.1 joerg test_create(void) 36 1.1 joerg { 37 1.1 joerg struct stat st; 38 1.1 joerg struct utimbuf times; 39 1.1 joerg static const int numfiles = sizeof(files) / sizeof(files[0]); 40 1.1 joerg int i; 41 1.1 joerg 42 1.1 joerg for (i = 0; i < numfiles; ++i) { 43 1.1 joerg /* 44 1.1 joerg * Note: Have to write at least one byte to the file. 45 1.1 joerg * cpio doesn't bother reading the file if it's zero length, 46 1.1 joerg * so the atime never gets changed in that case, which 47 1.1 joerg * makes the tests below rather pointless. 48 1.1 joerg */ 49 1.1.1.2 joerg assertMakeFile(files[i].name, 0644, "a"); 50 1.1 joerg 51 1.1 joerg /* If utime() isn't supported on your platform, just 52 1.1 joerg * #ifdef this section out. Most of the test below is 53 1.1 joerg * still valid. */ 54 1.1 joerg memset(×, 0, sizeof(times)); 55 1.1.1.6 christos #if defined(_WIN32) && !defined(__CYGWIN__) 56 1.1.1.3 joerg times.actime = 86400; 57 1.1.1.3 joerg times.modtime = 86400; 58 1.1.1.3 joerg #else 59 1.1 joerg times.actime = 1; 60 1.1 joerg times.modtime = 3; 61 1.1.1.3 joerg #endif 62 1.1 joerg assertEqualInt(0, utime(files[i].name, ×)); 63 1.1 joerg 64 1.1 joerg /* Record whatever atime the file ended up with. */ 65 1.1 joerg /* If utime() is available, this should be 1, but there's 66 1.1 joerg * no harm in being careful. */ 67 1.1 joerg assertEqualInt(0, stat(files[i].name, &st)); 68 1.1 joerg files[i].atime_sec = st.st_atime; 69 1.1 joerg } 70 1.1 joerg 71 1.1 joerg /* Wait until the atime on the last file is actually in the past. */ 72 1.1.1.2 joerg sleepUntilAfter(files[numfiles - 1].atime_sec); 73 1.1 joerg } 74 1.1 joerg 75 1.1 joerg DEFINE_TEST(test_option_a) 76 1.1 joerg { 77 1.1 joerg struct stat st; 78 1.1 joerg int r; 79 1.1.1.2 joerg char *p; 80 1.1 joerg 81 1.1 joerg /* Create all of the test files. */ 82 1.1 joerg test_create(); 83 1.1 joerg 84 1.1 joerg /* Sanity check; verify that atimes really do get modified. */ 85 1.1.1.3 joerg p = slurpfile(NULL, "f0"); 86 1.1.1.3 joerg assert(p != NULL); 87 1.1.1.2 joerg free(p); 88 1.1 joerg assertEqualInt(0, stat("f0", &st)); 89 1.1 joerg if (st.st_atime == files[0].atime_sec) { 90 1.1 joerg skipping("Cannot verify -a option\n" 91 1.1 joerg " Your system appears to not support atime."); 92 1.1 joerg } 93 1.1 joerg else 94 1.1 joerg { 95 1.1 joerg /* 96 1.1 joerg * If this disk is mounted noatime, then we can't 97 1.1 joerg * verify correct operation without -a. 98 1.1 joerg */ 99 1.1 joerg 100 1.1 joerg /* Copy the file without -a; should change the atime. */ 101 1.1 joerg r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog); 102 1.1 joerg assertEqualInt(r, 0); 103 1.1.1.2 joerg assertTextFileContents("1 block\n", "copy-no-a.err"); 104 1.1 joerg assertEmptyFile("copy-no-a.out"); 105 1.1 joerg assertEqualInt(0, stat(files[1].name, &st)); 106 1.1 joerg failure("Copying file without -a should have changed atime."); 107 1.1 joerg assert(st.st_atime != files[1].atime_sec); 108 1.1 joerg 109 1.1 joerg /* Archive the file without -a; should change the atime. */ 110 1.1 joerg r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog); 111 1.1 joerg assertEqualInt(r, 0); 112 1.1.1.2 joerg assertTextFileContents("1 block\n", "copy-no-a.err"); 113 1.1 joerg assertEqualInt(0, stat(files[2].name, &st)); 114 1.1 joerg failure("Archiving file without -a should have changed atime."); 115 1.1 joerg assert(st.st_atime != files[2].atime_sec); 116 1.1 joerg } 117 1.1 joerg 118 1.1 joerg /* 119 1.1 joerg * We can, of course, still verify that the atime is unchanged 120 1.1 joerg * when using the -a option. 121 1.1 joerg */ 122 1.1 joerg 123 1.1 joerg /* Copy the file with -a; should not change the atime. */ 124 1.1 joerg r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err", 125 1.1 joerg files[3].name, testprog); 126 1.1 joerg assertEqualInt(r, 0); 127 1.1.1.2 joerg assertTextFileContents("1 block\n", "copy-a.err"); 128 1.1 joerg assertEmptyFile("copy-a.out"); 129 1.1 joerg assertEqualInt(0, stat(files[3].name, &st)); 130 1.1 joerg failure("Copying file with -a should not have changed atime."); 131 1.1 joerg assertEqualInt(st.st_atime, files[3].atime_sec); 132 1.1 joerg 133 1.1 joerg /* Archive the file with -a; should not change the atime. */ 134 1.1 joerg r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err", 135 1.1 joerg files[4].name, testprog); 136 1.1 joerg assertEqualInt(r, 0); 137 1.1.1.2 joerg assertTextFileContents("1 block\n", "copy-a.err"); 138 1.1 joerg assertEqualInt(0, stat(files[4].name, &st)); 139 1.1 joerg failure("Archiving file with -a should not have changed atime."); 140 1.1 joerg assertEqualInt(st.st_atime, files[4].atime_sec); 141 1.1 joerg } 142