File indexing completed on 2025-05-11 08:24:31
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032
0033 #include <sys/stat.h>
0034 #include <limits.h>
0035 #include <fcntl.h>
0036 #include <errno.h>
0037 #include <stdio.h>
0038 #include <stdint.h>
0039 #include <stdlib.h>
0040 #include <string.h>
0041 #include <unistd.h>
0042 #include <utime.h>
0043
0044 #ifdef __rtems__
0045 #include "fstest.h"
0046 #include "fs_config.h"
0047 #include <tmacros.h>
0048
0049 const char rtems_test_name[] = "FSTIME " FILESYSTEM;
0050 const RTEMS_TEST_STATE rtems_test_state = TEST_STATE;
0051 #else
0052 #include <assert.h>
0053 #define rtems_test_assert(x) assert(x)
0054 #define TIME_PRECISION (2)
0055 #define TIME_EQUAL(x,y) (abs((x)-(y))<TIME_PRECISION)
0056 #endif
0057
0058 static int do_create(const char *path, int oflag, mode_t mode)
0059 {
0060 int fd = open (path, O_CREAT | oflag, mode);
0061 rtems_test_assert (fd >= 0);
0062
0063 return fd;
0064 }
0065
0066 static int do_open(const char *path, int oflag)
0067 {
0068 int fd = open (path, O_CREAT | oflag);
0069 rtems_test_assert (fd >= 0);
0070
0071 return fd;
0072 }
0073
0074 static void time_test01 (void)
0075 {
0076 struct stat st;
0077 struct utimbuf timbuf;
0078 int status;
0079 int fd;
0080 time_t creation_time;
0081 time_t truncation_time;
0082 time_t dir01_creation_time;
0083 char databuf[] = "TEST";
0084 char readbuf[sizeof(databuf)];
0085 const char *file01 = "test01";
0086 const char *file02 = "test02";
0087 const char *file03 = "test03";
0088 const char *dir01 = "dir01";
0089
0090 int n;
0091 int len = strlen (databuf);
0092
0093 const char *wd = __func__;
0094 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0095
0096
0097
0098 status = mkdir (wd, mode);
0099 rtems_test_assert (status == 0);
0100 status = chdir (wd);
0101 rtems_test_assert (status == 0);
0102
0103
0104
0105
0106 puts ("Sleep a few seconds");
0107 sleep (3 * TIME_PRECISION);
0108
0109
0110
0111
0112 fd = do_create (file01, O_WRONLY, mode);
0113 n = write (fd, databuf, len);
0114 rtems_test_assert (n == len);
0115 status = close (fd);
0116 rtems_test_assert (status == 0);
0117
0118 fd = do_create (file02, O_WRONLY, mode);
0119 n = write (fd, databuf, len);
0120 rtems_test_assert (n == len);
0121 status = close (fd);
0122 rtems_test_assert (status == 0);
0123
0124
0125 fd = do_create (file03, O_WRONLY, mode);
0126 status = close (fd);
0127 rtems_test_assert (status == 0);
0128
0129
0130
0131
0132
0133
0134
0135 status = stat (file01, &st);
0136 rtems_test_assert (status == 0);
0137
0138
0139
0140
0141 rtems_test_assert (st.st_ctime == st.st_mtime);
0142
0143 creation_time = st.st_ctime;
0144
0145 status = stat (".", &st);
0146 rtems_test_assert (status == 0);
0147
0148
0149
0150
0151 rtems_test_assert (st.st_ctime == st.st_mtime);
0152 rtems_test_assert (TIME_EQUAL (creation_time, st.st_mtime));
0153 rtems_test_assert (TIME_EQUAL (creation_time, st.st_ctime));
0154
0155 status = stat (file02, &st);
0156 rtems_test_assert (status == 0);
0157
0158
0159
0160
0161 rtems_test_assert (st.st_ctime == st.st_mtime);
0162 rtems_test_assert (TIME_EQUAL (creation_time, st.st_mtime));
0163 rtems_test_assert (TIME_EQUAL (creation_time, st.st_ctime));
0164
0165 status = stat (file03, &st);
0166 rtems_test_assert (status == 0);
0167
0168
0169
0170
0171 rtems_test_assert (st.st_ctime == st.st_mtime);
0172 rtems_test_assert (TIME_EQUAL (creation_time, st.st_mtime));
0173 rtems_test_assert (TIME_EQUAL (creation_time, st.st_ctime));
0174
0175
0176
0177
0178 puts ("Sleep a few seconds");
0179 sleep (3 * TIME_PRECISION);
0180
0181
0182
0183
0184 status = mkdir (dir01, mode);
0185 rtems_test_assert (status == 0);
0186
0187
0188
0189
0190 status = truncate (file01, len);
0191 rtems_test_assert (status == 0);
0192
0193
0194
0195
0196 status = truncate (file02, len + 1);
0197 rtems_test_assert (status == 0);
0198
0199
0200
0201
0202 fd = do_open (file03, O_TRUNC | O_WRONLY);
0203 status = close (fd);
0204 rtems_test_assert (status == 0);
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226 status = stat (file01, &st);
0227 rtems_test_assert (status == 0);
0228
0229 rtems_test_assert (st.st_ctime == st.st_mtime);
0230 if (TIME_EQUAL (creation_time, st.st_ctime)) {
0231 puts ("WARNING: truncate() behaviour may violate future POSIX standard");
0232 }
0233
0234 truncation_time = st.st_ctime;
0235
0236
0237
0238
0239 status = stat (file02, &st);
0240 rtems_test_assert (status == 0);
0241
0242 rtems_test_assert (st.st_ctime == st.st_mtime);
0243 rtems_test_assert (!TIME_EQUAL (creation_time, st.st_ctime));
0244
0245
0246
0247
0248 status = stat (file03, &st);
0249 rtems_test_assert (status == 0);
0250
0251 rtems_test_assert (st.st_ctime == st.st_mtime);
0252 rtems_test_assert (!TIME_EQUAL (creation_time, st.st_ctime));
0253
0254
0255
0256
0257
0258
0259
0260 status = stat (dir01, &st);
0261 rtems_test_assert (status == 0);
0262
0263 rtems_test_assert (st.st_ctime == st.st_mtime);
0264
0265 dir01_creation_time = st.st_ctime;
0266
0267 status = stat (".", &st);
0268 rtems_test_assert (status == 0);
0269
0270 rtems_test_assert (st.st_ctime == st.st_mtime);
0271 rtems_test_assert (TIME_EQUAL (dir01_creation_time, st.st_mtime));
0272
0273
0274
0275
0276 puts ("Sleep a few seconds");
0277 sleep (3 * TIME_PRECISION);
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287 fd = do_open (file01, O_RDONLY);
0288 n = read (fd, readbuf, len);
0289 rtems_test_assert (n == len);
0290 status = fstat (fd, &st);
0291 rtems_test_assert (status == 0);
0292
0293 rtems_test_assert (st.st_ctime == st.st_mtime);
0294 rtems_test_assert (TIME_EQUAL (truncation_time, st.st_mtime));
0295
0296 status = close (fd);
0297 rtems_test_assert (status == 0);
0298
0299
0300
0301 fd = do_open (file01, O_WRONLY);
0302 n = write (fd, databuf, len);
0303 rtems_test_assert (n == len);
0304 status = fstat (fd, &st);
0305
0306 rtems_test_assert (st.st_ctime == st.st_mtime);
0307 rtems_test_assert (!TIME_EQUAL (truncation_time, st.st_mtime));
0308 status = close (fd);
0309 rtems_test_assert (status == 0);
0310
0311
0312
0313
0314
0315 timbuf.actime = creation_time;
0316 timbuf.modtime = creation_time;
0317
0318 status = utime (file01, &timbuf);
0319 rtems_test_assert (status == 0);
0320
0321 status = stat (file01, &st);
0322 rtems_test_assert (status == 0);
0323
0324 rtems_test_assert (st.st_atime == st.st_mtime);
0325 rtems_test_assert (TIME_EQUAL (creation_time, st.st_atime));
0326 rtems_test_assert (!TIME_EQUAL (creation_time, st.st_ctime));
0327
0328 status = utime (dir01, &timbuf);
0329 rtems_test_assert (status == 0);
0330
0331 status = stat (dir01, &st);
0332 rtems_test_assert (status == 0);
0333
0334 rtems_test_assert (st.st_atime == st.st_mtime);
0335 rtems_test_assert (TIME_EQUAL (creation_time, st.st_atime));
0336 rtems_test_assert (!TIME_EQUAL (creation_time, st.st_ctime));
0337 }
0338
0339
0340
0341
0342
0343 #ifdef __rtems__
0344 void test (void)
0345 #else
0346 int main(int argc, char **argv)
0347 #endif
0348 {
0349
0350 time_test01();
0351
0352 #ifndef __rtems__
0353 return 0;
0354 #endif
0355 }