[a7c39d3] | 1 | |
---|
| 2 | /* |
---|
| 3 | * COPYRIGHT (c) 1989-2011. |
---|
| 4 | * On-Line Applications Research Corporation (OAR). |
---|
| 5 | * |
---|
| 6 | * The license and distribution terms for this file may be |
---|
| 7 | * found in the file LICENSE in this distribution or at |
---|
| 8 | * http://www.rtems.com/license/LICENSE. |
---|
| 9 | * |
---|
[db6fbdf] | 10 | * $Id$ |
---|
[a7c39d3] | 11 | */ |
---|
| 12 | |
---|
| 13 | #include <sys/stat.h> |
---|
| 14 | #include <limits.h> |
---|
| 15 | #include <fcntl.h> |
---|
| 16 | #include <errno.h> |
---|
| 17 | #include <stdio.h> |
---|
| 18 | #include <stdint.h> |
---|
| 19 | #include <stdlib.h> |
---|
| 20 | #include <string.h> |
---|
| 21 | #include <unistd.h> |
---|
| 22 | #include <utime.h> |
---|
| 23 | #include "fstest.h" |
---|
| 24 | |
---|
| 25 | void time_test01 (void) |
---|
| 26 | { |
---|
| 27 | struct stat statbuf; |
---|
| 28 | struct utimbuf timbuf; |
---|
| 29 | int status = 0; |
---|
| 30 | int fd; |
---|
| 31 | time_t ctime1, mtime1; |
---|
| 32 | time_t ctime2, mtime2; |
---|
| 33 | char *readbuf; |
---|
| 34 | char *databuf = "TEST"; |
---|
| 35 | char *file01 = "test01"; |
---|
| 36 | char *file02 = "test02"; |
---|
| 37 | char *dir01 = "dir01"; |
---|
| 38 | |
---|
| 39 | int n; |
---|
| 40 | int len = strlen (databuf); |
---|
| 41 | |
---|
| 42 | const char *wd = __func__; |
---|
| 43 | mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; |
---|
| 44 | /* |
---|
| 45 | * Create a new directory and change to this |
---|
| 46 | */ |
---|
| 47 | status = mkdir (wd, mode); |
---|
| 48 | rtems_test_assert (status == 0); |
---|
| 49 | status = chdir (wd); |
---|
| 50 | rtems_test_assert (status == 0); |
---|
| 51 | |
---|
| 52 | /* |
---|
| 53 | * Create two files |
---|
| 54 | */ |
---|
| 55 | fd = open (file01, O_CREAT | O_WRONLY, mode); |
---|
| 56 | n = write (fd, databuf, len); |
---|
| 57 | rtems_test_assert (n == len); |
---|
| 58 | status = close (fd); |
---|
| 59 | rtems_test_assert (status == 0); |
---|
| 60 | |
---|
| 61 | fd = open (file02, O_CREAT | O_WRONLY, mode); |
---|
| 62 | n = write (fd, databuf, len); |
---|
| 63 | rtems_test_assert (n == len); |
---|
| 64 | status = close (fd); |
---|
| 65 | rtems_test_assert (status == 0); |
---|
| 66 | /* |
---|
| 67 | * If O_CREAT is set and the file did not previously exist, upon |
---|
| 68 | * successful completion, open() shall mark for update the st_atime, |
---|
| 69 | * st_ctime, and st_mtime fields of the file and the st_ctime and |
---|
| 70 | * st_mtime fields of the parent directory. |
---|
| 71 | */ |
---|
| 72 | status = stat (file01, &statbuf); |
---|
| 73 | rtems_test_assert (status == 0); |
---|
| 74 | ctime1 = statbuf.st_ctime; |
---|
| 75 | mtime1 = statbuf.st_mtime; |
---|
| 76 | |
---|
| 77 | status = stat (".", &statbuf); |
---|
| 78 | rtems_test_assert (status == 0); |
---|
| 79 | ctime2 = statbuf.st_ctime; |
---|
| 80 | mtime2 = statbuf.st_mtime; |
---|
| 81 | |
---|
| 82 | /* |
---|
[6fed43e] | 83 | * Make sure they are the same |
---|
[a7c39d3] | 84 | */ |
---|
| 85 | |
---|
| 86 | rtems_test_assert (TIME_EQUAL (ctime1, mtime1)); |
---|
| 87 | rtems_test_assert (TIME_EQUAL (ctime1, mtime2)); |
---|
| 88 | rtems_test_assert (TIME_EQUAL (ctime1, ctime2)); |
---|
| 89 | |
---|
| 90 | status = stat (file02, &statbuf); |
---|
| 91 | rtems_test_assert (status == 0); |
---|
| 92 | ctime1 = statbuf.st_ctime; |
---|
| 93 | mtime1 = statbuf.st_mtime; |
---|
| 94 | |
---|
| 95 | status = stat (".", &statbuf); |
---|
| 96 | rtems_test_assert (status == 0); |
---|
| 97 | ctime2 = statbuf.st_ctime; |
---|
| 98 | mtime2 = statbuf.st_mtime; |
---|
| 99 | |
---|
| 100 | /* |
---|
[6fed43e] | 101 | * Make sure they are the same |
---|
[a7c39d3] | 102 | */ |
---|
| 103 | rtems_test_assert (TIME_EQUAL (ctime1, mtime1)); |
---|
| 104 | rtems_test_assert (TIME_EQUAL (ctime1, mtime2)); |
---|
| 105 | rtems_test_assert (TIME_EQUAL (ctime1, ctime2)); |
---|
| 106 | |
---|
| 107 | /* |
---|
| 108 | * Sleep a few seconds |
---|
| 109 | */ |
---|
| 110 | puts ("Sleep a few seconds"); |
---|
| 111 | |
---|
| 112 | sleep (TIME_PRECISION); |
---|
| 113 | |
---|
| 114 | /* |
---|
[6fed43e] | 115 | * Create an empty directory |
---|
[a7c39d3] | 116 | */ |
---|
| 117 | status = mkdir (dir01, mode); |
---|
| 118 | rtems_test_assert (status == 0); |
---|
| 119 | /* |
---|
| 120 | * truncate file01 to len, so it does not changes the file size |
---|
| 121 | */ |
---|
| 122 | status = truncate (file01, len); |
---|
| 123 | rtems_test_assert (status == 0); |
---|
| 124 | |
---|
| 125 | /* |
---|
| 126 | *truncate file02 to len+1, it changes the file size |
---|
| 127 | */ |
---|
| 128 | status = truncate (file02, len + 1); |
---|
| 129 | rtems_test_assert (status == 0); |
---|
| 130 | |
---|
| 131 | /* |
---|
| 132 | * |
---|
| 133 | * truncate shall not modify the file offset for any open file |
---|
| 134 | * descriptions associated with the file. Upon successful completion, |
---|
| 135 | * if the file size is changed, this function shall mark for update |
---|
[6fed43e] | 136 | * the st_ctime and st_mtime fields of the file |
---|
[a7c39d3] | 137 | */ |
---|
| 138 | |
---|
| 139 | /* |
---|
[6fed43e] | 140 | * file01 shall not update |
---|
[a7c39d3] | 141 | */ |
---|
| 142 | status = stat (file01, &statbuf); |
---|
| 143 | rtems_test_assert (status == 0); |
---|
| 144 | ctime2 = statbuf.st_ctime; |
---|
| 145 | mtime2 = statbuf.st_mtime; |
---|
| 146 | |
---|
| 147 | rtems_test_assert (TIME_EQUAL (ctime1, mtime2)); |
---|
| 148 | rtems_test_assert (TIME_EQUAL (ctime1, ctime2)); |
---|
| 149 | |
---|
| 150 | /* |
---|
| 151 | * file02 shall update |
---|
| 152 | */ |
---|
| 153 | status = stat (file02, &statbuf); |
---|
| 154 | rtems_test_assert (status == 0); |
---|
| 155 | ctime2 = statbuf.st_ctime; |
---|
| 156 | mtime2 = statbuf.st_mtime; |
---|
| 157 | |
---|
| 158 | rtems_test_assert (TIME_EQUAL (ctime2, mtime2)); |
---|
| 159 | rtems_test_assert (!TIME_EQUAL (ctime1, mtime2)); |
---|
| 160 | rtems_test_assert (!TIME_EQUAL (ctime1, ctime2)); |
---|
| 161 | |
---|
| 162 | /* |
---|
[6fed43e] | 163 | * Upon successful completion, mkdir() shall mark for update the |
---|
[a7c39d3] | 164 | * 5st_atime, st_ctime, and st_mtime fields of the directory. |
---|
| 165 | * Also, the st_ctime and st_mtime fields of the directory that |
---|
| 166 | * contains the new entry shall be marked for update. |
---|
| 167 | */ |
---|
| 168 | status = stat (dir01, &statbuf); |
---|
| 169 | rtems_test_assert (status == 0); |
---|
| 170 | ctime2 = statbuf.st_ctime; |
---|
| 171 | mtime2 = statbuf.st_mtime; |
---|
| 172 | rtems_test_assert (TIME_EQUAL (ctime2, mtime2)); |
---|
| 173 | rtems_test_assert (!TIME_EQUAL (ctime1, mtime2)); |
---|
| 174 | rtems_test_assert (!TIME_EQUAL (ctime1, ctime2)); |
---|
| 175 | |
---|
| 176 | status = stat (".", &statbuf); |
---|
| 177 | rtems_test_assert (status == 0); |
---|
| 178 | ctime2 = statbuf.st_ctime; |
---|
| 179 | mtime2 = statbuf.st_mtime; |
---|
| 180 | |
---|
| 181 | rtems_test_assert (!TIME_EQUAL (ctime1, ctime2)); |
---|
| 182 | rtems_test_assert (!TIME_EQUAL (ctime1, mtime2)); |
---|
| 183 | |
---|
| 184 | /* |
---|
| 185 | * Upon successful completion, where nbyte is greater than 0, |
---|
| 186 | * write() shall mark for update the st_ctime and st_mtime fields of the file |
---|
| 187 | */ |
---|
| 188 | |
---|
| 189 | /* |
---|
| 190 | * read file01, and this should not uptate st_mtime and st_ctime |
---|
| 191 | */ |
---|
| 192 | readbuf = (char *) malloc (len + 1); |
---|
| 193 | fd = open (file01, O_RDONLY); |
---|
| 194 | rtems_test_assert (fd != -1); |
---|
| 195 | n = read (fd, readbuf, len); |
---|
| 196 | rtems_test_assert (n == len); |
---|
| 197 | status = fstat (fd, &statbuf); |
---|
| 198 | |
---|
| 199 | ctime2 = statbuf.st_ctime; |
---|
| 200 | mtime2 = statbuf.st_mtime; |
---|
| 201 | |
---|
| 202 | rtems_test_assert (TIME_EQUAL (ctime1, ctime2)); |
---|
| 203 | rtems_test_assert (TIME_EQUAL (ctime1, mtime2)); |
---|
| 204 | |
---|
| 205 | status = close (fd); |
---|
| 206 | rtems_test_assert (status == 0); |
---|
| 207 | /* |
---|
| 208 | * write file01, and this should uptate st_mtime st_ctime |
---|
| 209 | */ |
---|
| 210 | readbuf = (char *) malloc (len + 1); |
---|
| 211 | fd = open (file01, O_WRONLY); |
---|
| 212 | rtems_test_assert (fd != -1); |
---|
| 213 | n = write (fd, databuf, len); |
---|
| 214 | rtems_test_assert (n == len); |
---|
| 215 | status = fstat (fd, &statbuf); |
---|
| 216 | |
---|
| 217 | ctime2 = statbuf.st_ctime; |
---|
| 218 | mtime2 = statbuf.st_mtime; |
---|
| 219 | |
---|
| 220 | rtems_test_assert (!TIME_EQUAL (ctime1, ctime2)); |
---|
| 221 | rtems_test_assert (!TIME_EQUAL (ctime1, mtime2)); |
---|
| 222 | status = close (fd); |
---|
| 223 | rtems_test_assert (status == 0); |
---|
| 224 | |
---|
| 225 | /* |
---|
| 226 | * The utime() function shall set the access and modification times |
---|
| 227 | * of the file named by the path argument. |
---|
| 228 | */ |
---|
| 229 | timbuf.actime = ctime1; |
---|
| 230 | timbuf.modtime = mtime1; |
---|
| 231 | |
---|
| 232 | status = utime (file01, &timbuf); |
---|
| 233 | rtems_test_assert (status == 0); |
---|
| 234 | |
---|
| 235 | status = stat (file01, &statbuf); |
---|
| 236 | ctime2 = statbuf.st_atime; |
---|
| 237 | mtime2 = statbuf.st_mtime; |
---|
| 238 | |
---|
| 239 | rtems_test_assert (TIME_EQUAL (ctime1, ctime2)); |
---|
| 240 | rtems_test_assert (TIME_EQUAL (ctime1, mtime2)); |
---|
| 241 | |
---|
| 242 | status = utime (dir01, &timbuf); |
---|
| 243 | rtems_test_assert (status == 0); |
---|
| 244 | |
---|
| 245 | status = stat (dir01, &statbuf); |
---|
| 246 | ctime2 = statbuf.st_atime; |
---|
| 247 | mtime2 = statbuf.st_mtime; |
---|
| 248 | |
---|
| 249 | rtems_test_assert (TIME_EQUAL (ctime1, ctime2)); |
---|
| 250 | rtems_test_assert (TIME_EQUAL (ctime1, mtime2)); |
---|
| 251 | |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | /* |
---|
[6fed43e] | 255 | * These tests only get time_t value, and test |
---|
[a7c39d3] | 256 | * if they are changed. Thest tests don't check atime |
---|
| 257 | */ |
---|
| 258 | void test (void) |
---|
| 259 | { |
---|
| 260 | |
---|
| 261 | puts( "\n\n*** TIME TEST ***" ); |
---|
| 262 | time_test01(); |
---|
| 263 | puts( "*** END OF TIME TEST ***" ); |
---|
| 264 | } |
---|