1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2011. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id Exp $ |
---|
10 | */ |
---|
11 | #include <sys/stat.h> |
---|
12 | #include <string.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <fcntl.h> |
---|
15 | #include <stdio.h> |
---|
16 | #include <stdlib.h> |
---|
17 | #include <stdint.h> |
---|
18 | #include <memory.h> |
---|
19 | #include <unistd.h> |
---|
20 | |
---|
21 | #include "fstest.h" |
---|
22 | |
---|
23 | const char *databuf = |
---|
24 | "Happy days are here again. Happy days are here again.1Happy " |
---|
25 | "days are here again.2Happy days are here again.3Happy days are here again." |
---|
26 | "4Happy days are here again.5Happy days are here again.6Happy days are here " |
---|
27 | "again.7Happy days are here again."; |
---|
28 | |
---|
29 | void read_write_test (void) |
---|
30 | { |
---|
31 | |
---|
32 | int fd; |
---|
33 | int status; |
---|
34 | char *name01 = "name01"; |
---|
35 | char *name02 = "name02"; |
---|
36 | struct stat statbuf; |
---|
37 | char *readbuf; |
---|
38 | size_t len = strlen (databuf); |
---|
39 | off_t pos = 0; |
---|
40 | |
---|
41 | int n; |
---|
42 | mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; |
---|
43 | |
---|
44 | |
---|
45 | const char* wd=__func__; |
---|
46 | |
---|
47 | /* |
---|
48 | * Create a new directory and change the current directory to this |
---|
49 | */ |
---|
50 | status=mkdir(wd,mode); |
---|
51 | rtems_test_assert(status==0); |
---|
52 | status=chdir(wd); |
---|
53 | rtems_test_assert(status==0); |
---|
54 | /* |
---|
55 | * Create an empty file |
---|
56 | */ |
---|
57 | fd = open (name01, O_CREAT | O_WRONLY, mode); |
---|
58 | status = close (fd); |
---|
59 | rtems_test_assert (status == 0); |
---|
60 | /* |
---|
61 | * Verify the empty file |
---|
62 | */ |
---|
63 | status = stat (name01, &statbuf); |
---|
64 | rtems_test_assert (status == 0); |
---|
65 | |
---|
66 | rtems_test_assert (S_ISREG (statbuf.st_mode)); |
---|
67 | rtems_test_assert (statbuf.st_size == 0); |
---|
68 | |
---|
69 | /* |
---|
70 | * Write data to the empty file |
---|
71 | */ |
---|
72 | fd = open (name01, O_WRONLY); |
---|
73 | rtems_test_assert (fd != -1); |
---|
74 | |
---|
75 | n = write (fd, databuf, len); |
---|
76 | rtems_test_assert (n == len); |
---|
77 | status = close (fd); |
---|
78 | rtems_test_assert (status == 0); |
---|
79 | |
---|
80 | status = stat (name01, &statbuf); |
---|
81 | rtems_test_assert (status == 0); |
---|
82 | rtems_test_assert (S_ISREG (statbuf.st_mode)); |
---|
83 | rtems_test_assert (statbuf.st_size == len); |
---|
84 | |
---|
85 | /* |
---|
86 | * Read the data from the file |
---|
87 | */ |
---|
88 | readbuf = (char *) malloc (len + 1); |
---|
89 | rtems_test_assert (readbuf); |
---|
90 | |
---|
91 | fd = open (name01, O_RDONLY); |
---|
92 | rtems_test_assert (fd != -1); |
---|
93 | n = read (fd, readbuf, len); |
---|
94 | rtems_test_assert (n == len); |
---|
95 | rtems_test_assert (!strncmp (databuf, readbuf, len)); |
---|
96 | status = close (fd); |
---|
97 | rtems_test_assert (status == 0); |
---|
98 | |
---|
99 | /* |
---|
100 | * Open the file using O_APPEND and write the data |
---|
101 | */ |
---|
102 | memset (readbuf, 0, len + 1); |
---|
103 | fd = open (name01, O_WRONLY | O_APPEND); |
---|
104 | n = write (fd, databuf, len); |
---|
105 | rtems_test_assert (n == len); |
---|
106 | pos = lseek (fd, 0, SEEK_CUR); |
---|
107 | rtems_test_assert (pos == 2 * len); |
---|
108 | status = close (fd); |
---|
109 | rtems_test_assert (status == 0); |
---|
110 | |
---|
111 | /* |
---|
112 | * Read the data and verify it |
---|
113 | */ |
---|
114 | fd = open (name01, O_RDONLY); |
---|
115 | rtems_test_assert (fd != -1); |
---|
116 | n = read (fd, readbuf, len); |
---|
117 | rtems_test_assert (n == len); |
---|
118 | rtems_test_assert (!strncmp (databuf, readbuf, len)); |
---|
119 | |
---|
120 | n = read (fd, readbuf, len); |
---|
121 | rtems_test_assert (n == len); |
---|
122 | rtems_test_assert (!strncmp (databuf, readbuf, len)); |
---|
123 | status = close (fd); |
---|
124 | rtems_test_assert (status == 0); |
---|
125 | |
---|
126 | /* |
---|
127 | * Open the file using O_RDWR |
---|
128 | */ |
---|
129 | memset (readbuf, 0, len + 1); |
---|
130 | |
---|
131 | fd = open (name01, O_RDWR); |
---|
132 | n = write (fd, databuf, len); |
---|
133 | rtems_test_assert (n == len); |
---|
134 | pos = lseek (fd, 0, SEEK_CUR); |
---|
135 | rtems_test_assert (pos == len); |
---|
136 | n = read (fd, readbuf, len); |
---|
137 | rtems_test_assert (n == len); |
---|
138 | rtems_test_assert (!strncmp (databuf, readbuf, len)); |
---|
139 | pos = lseek (fd, 0, SEEK_CUR); |
---|
140 | rtems_test_assert (pos == 2 * len); |
---|
141 | status = close (fd); |
---|
142 | rtems_test_assert (status == 0); |
---|
143 | |
---|
144 | /* |
---|
145 | * Open the file using O_TRUNC |
---|
146 | */ |
---|
147 | |
---|
148 | fd = open (name01, O_WRONLY | O_TRUNC); |
---|
149 | status = close (fd); |
---|
150 | rtems_test_assert (status == 0); |
---|
151 | |
---|
152 | /* |
---|
153 | * Verify if the length is zero |
---|
154 | */ |
---|
155 | status = stat (name01, &statbuf); |
---|
156 | rtems_test_assert (status == 0); |
---|
157 | rtems_test_assert (S_ISREG (statbuf.st_mode)); |
---|
158 | rtems_test_assert (statbuf.st_size == 0); |
---|
159 | |
---|
160 | /* |
---|
161 | * Open a directory |
---|
162 | */ |
---|
163 | status = mkdir (name02, mode); |
---|
164 | rtems_test_assert (status == 0); |
---|
165 | fd = open (name02, O_RDONLY); |
---|
166 | rtems_test_assert (fd != -1); |
---|
167 | |
---|
168 | status = close (fd); |
---|
169 | rtems_test_assert (status == 0); |
---|
170 | |
---|
171 | free (readbuf); |
---|
172 | |
---|
173 | /* |
---|
174 | * Go back to parent directory |
---|
175 | */ |
---|
176 | status=chdir(".."); |
---|
177 | rtems_test_assert(status==0); |
---|
178 | } |
---|
179 | |
---|
180 | void truncate_test03(void ) |
---|
181 | { |
---|
182 | |
---|
183 | int fd; |
---|
184 | int status; |
---|
185 | char *name01="name01"; |
---|
186 | struct stat statbuf; |
---|
187 | |
---|
188 | char data; |
---|
189 | int n; |
---|
190 | int i; |
---|
191 | |
---|
192 | size_t len = strlen(databuf); |
---|
193 | |
---|
194 | char *readbuf; |
---|
195 | off_t good_size=100; |
---|
196 | mode_t mode = S_IRWXU|S_IRWXG|S_IRWXO; |
---|
197 | |
---|
198 | |
---|
199 | const char* wd=__func__; |
---|
200 | |
---|
201 | /* |
---|
202 | * Create a new directory and change the current directory to this |
---|
203 | */ |
---|
204 | status=mkdir(wd,mode); |
---|
205 | rtems_test_assert(status==0); |
---|
206 | status=chdir(wd); |
---|
207 | rtems_test_assert(status==0); |
---|
208 | |
---|
209 | /* |
---|
210 | * Create an empty file |
---|
211 | */ |
---|
212 | fd=creat(name01,mode); |
---|
213 | status=close(fd); |
---|
214 | rtems_test_assert(status==0); |
---|
215 | |
---|
216 | |
---|
217 | /* |
---|
218 | * Truncate it to a valid size |
---|
219 | */ |
---|
220 | status=truncate(name01,good_size); |
---|
221 | rtems_test_assert(status==0); |
---|
222 | /* |
---|
223 | * Verify the size and the data |
---|
224 | */ |
---|
225 | status=stat(name01,&statbuf); |
---|
226 | rtems_test_assert(status==0); |
---|
227 | rtems_test_assert(good_size==statbuf.st_size); |
---|
228 | |
---|
229 | fd=open(name01,O_RDONLY); |
---|
230 | while ((n=read(fd,&data,1))>0) { |
---|
231 | rtems_test_assert(data==0); |
---|
232 | } |
---|
233 | |
---|
234 | status=close(fd); |
---|
235 | rtems_test_assert(status==0); |
---|
236 | |
---|
237 | /* |
---|
238 | * Fill a file with data |
---|
239 | */ |
---|
240 | fd=open(name01,O_WRONLY); |
---|
241 | rtems_test_assert(fd!=-1); |
---|
242 | n=write(fd,databuf,len); |
---|
243 | rtems_test_assert(n==len); |
---|
244 | |
---|
245 | /* |
---|
246 | * Truncate it to the half size |
---|
247 | */ |
---|
248 | |
---|
249 | status=truncate(name01,len/2); |
---|
250 | status=truncate(name01,len); |
---|
251 | |
---|
252 | /* |
---|
253 | * verify the data |
---|
254 | */ |
---|
255 | readbuf = (char *)malloc(len/2); |
---|
256 | rtems_test_assert( readbuf ); |
---|
257 | fd=open(name01,O_RDONLY); |
---|
258 | rtems_test_assert(fd!=-1); |
---|
259 | n=read(fd,readbuf,len/2); |
---|
260 | rtems_test_assert(n==len/2); |
---|
261 | rtems_test_assert(!strncmp(databuf,readbuf,len/2)); |
---|
262 | n=read(fd,readbuf,len/2); |
---|
263 | rtems_test_assert(n==len/2); |
---|
264 | for (i=0;i<len/2;i++){ |
---|
265 | rtems_test_assert(readbuf[i]==0); |
---|
266 | } |
---|
267 | status=close(fd); |
---|
268 | rtems_test_assert( status==0 ); |
---|
269 | |
---|
270 | /* |
---|
271 | * Go back to parent directory |
---|
272 | */ |
---|
273 | status=chdir(".."); |
---|
274 | rtems_test_assert(status==0); |
---|
275 | } |
---|
276 | |
---|
277 | void lseek_test(void) |
---|
278 | { |
---|
279 | int fd; |
---|
280 | int status; |
---|
281 | char *name01="test_name01"; |
---|
282 | struct stat statbuf; |
---|
283 | |
---|
284 | int n; |
---|
285 | int i; |
---|
286 | |
---|
287 | size_t len = strlen(databuf); |
---|
288 | off_t pos; |
---|
289 | int total_written=0; |
---|
290 | |
---|
291 | char *readbuf; |
---|
292 | mode_t mode = S_IRWXU|S_IRWXG|S_IRWXO; |
---|
293 | |
---|
294 | |
---|
295 | |
---|
296 | const char* wd=__func__; |
---|
297 | |
---|
298 | /* |
---|
299 | * Create a new directory and change the current directory to this |
---|
300 | */ |
---|
301 | status=mkdir(wd,mode); |
---|
302 | rtems_test_assert(status==0); |
---|
303 | status=chdir(wd); |
---|
304 | rtems_test_assert(status==0); |
---|
305 | |
---|
306 | /* |
---|
307 | * Create a file and fill with the data. |
---|
308 | */ |
---|
309 | puts("Create a new file"); |
---|
310 | fd=creat(name01,mode); |
---|
311 | rtems_test_assert(fd!=-1); |
---|
312 | |
---|
313 | pos=lseek(fd,0,SEEK_CUR); |
---|
314 | rtems_test_assert(pos==0); |
---|
315 | |
---|
316 | pos=lseek(fd,0,SEEK_END); |
---|
317 | rtems_test_assert(pos==0); |
---|
318 | |
---|
319 | pos=lseek(fd,0,SEEK_SET); |
---|
320 | rtems_test_assert(pos==0); |
---|
321 | |
---|
322 | |
---|
323 | printf( "Writing %d bytes to file\n", len * 10 ); |
---|
324 | for (i=0; i<10; i++) { |
---|
325 | n = write(fd, databuf, len); |
---|
326 | rtems_test_assert( n != -1 ); |
---|
327 | total_written += n; |
---|
328 | } |
---|
329 | printf("Successfully wrote %d\n", total_written); |
---|
330 | |
---|
331 | /* |
---|
332 | * Check the current position |
---|
333 | */ |
---|
334 | puts("Check the current position"); |
---|
335 | pos=lseek(fd,0,SEEK_CUR); |
---|
336 | rtems_test_assert(pos==total_written); |
---|
337 | |
---|
338 | pos=lseek(fd,0,SEEK_END); |
---|
339 | rtems_test_assert(pos==total_written); |
---|
340 | |
---|
341 | /* |
---|
342 | * ftruncate shall not change the posistion |
---|
343 | */ |
---|
344 | status=ftruncate(fd,total_written+1); |
---|
345 | rtems_test_assert(status==0); |
---|
346 | |
---|
347 | pos=lseek(fd,0,SEEK_CUR); |
---|
348 | rtems_test_assert(pos==total_written); |
---|
349 | |
---|
350 | pos=lseek(fd,0,SEEK_END); |
---|
351 | printf("%jd\n",(intmax_t)pos); |
---|
352 | rtems_test_assert(pos==total_written+1); |
---|
353 | |
---|
354 | status=ftruncate(fd,total_written); |
---|
355 | rtems_test_assert(status==0); |
---|
356 | |
---|
357 | pos=lseek(fd,0,SEEK_CUR); |
---|
358 | rtems_test_assert(pos==total_written+1); |
---|
359 | |
---|
360 | |
---|
361 | status=close(fd); |
---|
362 | rtems_test_assert(status==0); |
---|
363 | |
---|
364 | /* |
---|
365 | * Check the file size |
---|
366 | */ |
---|
367 | status=stat(name01,&statbuf); |
---|
368 | rtems_test_assert(statbuf.st_size==total_written); |
---|
369 | |
---|
370 | /* |
---|
371 | * Open the file with O_RDONLY and check the lseek |
---|
372 | */ |
---|
373 | readbuf=(char*)malloc(len); |
---|
374 | fd=open(name01,O_RDONLY); |
---|
375 | pos=lseek(fd,len,SEEK_CUR); |
---|
376 | rtems_test_assert(pos==len); |
---|
377 | n=read(fd,readbuf,len); |
---|
378 | rtems_test_assert(n==len); |
---|
379 | rtems_test_assert(!strncmp(databuf,readbuf,len)); |
---|
380 | |
---|
381 | pos=lseek(fd,len,SEEK_CUR); |
---|
382 | rtems_test_assert(pos==3*len); |
---|
383 | n=read(fd,readbuf,len); |
---|
384 | rtems_test_assert(n==len); |
---|
385 | rtems_test_assert(!strncmp(databuf,readbuf,len)); |
---|
386 | |
---|
387 | pos=lseek(fd,-len,SEEK_CUR); |
---|
388 | rtems_test_assert(pos==3*len); |
---|
389 | n=read(fd,readbuf,len); |
---|
390 | rtems_test_assert(n==len); |
---|
391 | rtems_test_assert(!strncmp(databuf,readbuf,len)); |
---|
392 | |
---|
393 | pos=lseek(fd,4*len,SEEK_SET); |
---|
394 | n=read(fd,readbuf,len); |
---|
395 | rtems_test_assert(n==len); |
---|
396 | rtems_test_assert(!strncmp(databuf,readbuf,len)); |
---|
397 | |
---|
398 | |
---|
399 | pos=lseek(fd,10,SEEK_SET); |
---|
400 | n=read(fd,readbuf,len); |
---|
401 | rtems_test_assert(n==len); |
---|
402 | rtems_test_assert(strncmp(databuf,readbuf,len)!=0); |
---|
403 | |
---|
404 | pos=lseek(fd,-len,SEEK_END); |
---|
405 | n=read(fd,readbuf,2*len); |
---|
406 | rtems_test_assert(n==len); |
---|
407 | rtems_test_assert(!strncmp(databuf,readbuf,len)); |
---|
408 | |
---|
409 | status=close(fd); |
---|
410 | rtems_test_assert(status==0); |
---|
411 | |
---|
412 | /* |
---|
413 | * Open the file withe O_RDWR and check the lseek |
---|
414 | */ |
---|
415 | |
---|
416 | fd=open(name01,O_RDWR); |
---|
417 | |
---|
418 | pos=lseek(fd,len,SEEK_CUR); |
---|
419 | rtems_test_assert(pos==len); |
---|
420 | n=read(fd,readbuf,len); |
---|
421 | rtems_test_assert(n==len); |
---|
422 | rtems_test_assert(!strncmp(databuf,readbuf,len)); |
---|
423 | |
---|
424 | pos=lseek(fd,len,SEEK_CUR); |
---|
425 | rtems_test_assert(pos==3*len); |
---|
426 | n=read(fd,readbuf,len); |
---|
427 | rtems_test_assert(n==len); |
---|
428 | rtems_test_assert(!strncmp(databuf,readbuf,len)); |
---|
429 | |
---|
430 | pos=lseek(fd,-len,SEEK_CUR); |
---|
431 | rtems_test_assert(pos==3*len); |
---|
432 | n=read(fd,readbuf,len); |
---|
433 | rtems_test_assert(n==len); |
---|
434 | rtems_test_assert(!strncmp(databuf,readbuf,len)); |
---|
435 | |
---|
436 | pos=lseek(fd,4*len,SEEK_SET); |
---|
437 | n=read(fd,readbuf,len); |
---|
438 | rtems_test_assert(n==len); |
---|
439 | rtems_test_assert(!strncmp(databuf,readbuf,len)); |
---|
440 | |
---|
441 | /* |
---|
442 | * Go to the wrong position, so the data is not the same |
---|
443 | */ |
---|
444 | pos=lseek(fd,10,SEEK_SET); |
---|
445 | n=read(fd,readbuf,len); |
---|
446 | rtems_test_assert(n==len); |
---|
447 | rtems_test_assert(strncmp(databuf,readbuf,len)!=0); |
---|
448 | |
---|
449 | /* |
---|
450 | * Use SEEK_END |
---|
451 | */ |
---|
452 | pos=lseek(fd,-len,SEEK_END); |
---|
453 | n=read(fd,readbuf,2*len); |
---|
454 | rtems_test_assert(n==len); |
---|
455 | rtems_test_assert(!strncmp(databuf,readbuf,len)); |
---|
456 | |
---|
457 | memset(readbuf,0,len); |
---|
458 | |
---|
459 | /* |
---|
460 | * Write the zero to the end of file. |
---|
461 | */ |
---|
462 | pos=lseek(fd,-len,SEEK_END); |
---|
463 | rtems_test_assert(pos==total_written-len); |
---|
464 | n=write(fd,readbuf,len); |
---|
465 | rtems_test_assert(n==len); |
---|
466 | /* |
---|
467 | * Verify it |
---|
468 | */ |
---|
469 | pos=lseek(fd,total_written-len,SEEK_SET); |
---|
470 | n=read(fd,readbuf,len); |
---|
471 | rtems_test_assert(n==len); |
---|
472 | for (i=0;i<n;i++){ |
---|
473 | rtems_test_assert(readbuf[i]==0); |
---|
474 | } |
---|
475 | |
---|
476 | /* |
---|
477 | * Write the zero to the beginning of file. |
---|
478 | */ |
---|
479 | pos=lseek(fd,-total_written,SEEK_END); |
---|
480 | rtems_test_assert(pos==0); |
---|
481 | n=write(fd,readbuf,len); |
---|
482 | rtems_test_assert(n==len); |
---|
483 | |
---|
484 | /* |
---|
485 | * Verify it |
---|
486 | */ |
---|
487 | |
---|
488 | pos=lseek(fd,0,SEEK_SET); |
---|
489 | n=read(fd,readbuf,len); |
---|
490 | rtems_test_assert(n==len); |
---|
491 | for (i=0;i<n;i++){ |
---|
492 | rtems_test_assert(readbuf[i]==0); |
---|
493 | } |
---|
494 | |
---|
495 | n=read(fd,readbuf,len); |
---|
496 | rtems_test_assert(n==len); |
---|
497 | rtems_test_assert(strncmp(databuf,readbuf,len)==0); |
---|
498 | /* |
---|
499 | * Call ftruncate to decrease the file and the position not change |
---|
500 | */ |
---|
501 | status=ftruncate(fd,len); |
---|
502 | rtems_test_assert(status==0); |
---|
503 | pos=lseek(fd,0,SEEK_CUR); |
---|
504 | rtems_test_assert(pos==len*2); |
---|
505 | |
---|
506 | status=close(fd); |
---|
507 | rtems_test_assert(status==0); |
---|
508 | /* |
---|
509 | * Go back to parent directory |
---|
510 | */ |
---|
511 | status=chdir(".."); |
---|
512 | rtems_test_assert(status==0); |
---|
513 | |
---|
514 | } |
---|
515 | void test(void ) |
---|
516 | { |
---|
517 | read_write_test(); |
---|
518 | lseek_test(); |
---|
519 | } |
---|