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