1 | /* |
---|
2 | * This is a native test to explore how the readdir() family works. |
---|
3 | * Newlib supports the following readdir() family members: |
---|
4 | * |
---|
5 | * closedir() - |
---|
6 | * readdir() - |
---|
7 | * scandir() - |
---|
8 | * opendir() - |
---|
9 | * rewinddir() - |
---|
10 | * telldir() - BSD not in POSIX |
---|
11 | * seekdir() - BSD not in POSIX |
---|
12 | * |
---|
13 | * |
---|
14 | * seekdir() takes an offset which is a byte offset. The Linux |
---|
15 | * implementation of this appears to seek to the ((off/DIRENT_SIZE) + 1) |
---|
16 | * record where DIRENT_SIZE seems to be 12 bytes. |
---|
17 | * |
---|
18 | * |
---|
19 | * |
---|
20 | * $Id$ |
---|
21 | */ |
---|
22 | |
---|
23 | #include <stdio.h> |
---|
24 | #include <sys/types.h> |
---|
25 | #include <fcntl.h> |
---|
26 | #include <dirent.h> |
---|
27 | #include <string.h> |
---|
28 | #include <assert.h> |
---|
29 | #include <unistd.h> |
---|
30 | #include <errno.h> |
---|
31 | #include <imfs.h> |
---|
32 | #include <assert.h> |
---|
33 | |
---|
34 | DIR *directory; |
---|
35 | DIR *directory2; |
---|
36 | DIR *directory3; |
---|
37 | DIR *directory_not; |
---|
38 | |
---|
39 | #ifndef __P |
---|
40 | #define __P(args)() |
---|
41 | #endif |
---|
42 | |
---|
43 | int scandir ( const char *dirname, |
---|
44 | struct dirent *** namelist, |
---|
45 | int (*select) __P((struct dirent *)), |
---|
46 | int (*dcomp) __P((const void *, const void *)) |
---|
47 | ); |
---|
48 | |
---|
49 | #if defined(__rtems__) |
---|
50 | #define d_type d_reclen |
---|
51 | #include <pmacros.h> |
---|
52 | #endif |
---|
53 | |
---|
54 | void printdir( DIR *directory ) |
---|
55 | { |
---|
56 | struct dirent *d; |
---|
57 | |
---|
58 | printf( "%-20s %8s %8s %8s %4s\n", |
---|
59 | "name", " inode", " offset", "reclen", " type" ); |
---|
60 | d = readdir(directory); |
---|
61 | |
---|
62 | while (d) { |
---|
63 | printf( "%-20s %8d %8d %6d 0x%04x\n", |
---|
64 | d->d_name, (int)d->d_ino, (int)d->d_off, d->d_reclen, d->d_type ); |
---|
65 | d = readdir(directory); |
---|
66 | |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | void complete_printdir( char *path ) |
---|
71 | { |
---|
72 | DIR *the_dir; |
---|
73 | int status; |
---|
74 | |
---|
75 | the_dir = opendir( path ); |
---|
76 | assert( the_dir ); |
---|
77 | printdir( the_dir ); |
---|
78 | status = closedir( the_dir ); |
---|
79 | } |
---|
80 | |
---|
81 | char *many_files[] = { |
---|
82 | "a", |
---|
83 | "b", |
---|
84 | "c", |
---|
85 | "d", |
---|
86 | "e", |
---|
87 | "f", |
---|
88 | "g", |
---|
89 | "h", |
---|
90 | "i", |
---|
91 | "j", |
---|
92 | "k", |
---|
93 | "l", |
---|
94 | "m", |
---|
95 | "n", |
---|
96 | "o", |
---|
97 | "p", |
---|
98 | "q", |
---|
99 | "r", |
---|
100 | "s", |
---|
101 | "t", |
---|
102 | "u", |
---|
103 | "v", |
---|
104 | "w", |
---|
105 | "x", |
---|
106 | "y", |
---|
107 | "z", |
---|
108 | "aa", |
---|
109 | "ab", |
---|
110 | "ac", |
---|
111 | "ad", |
---|
112 | "ae", |
---|
113 | "af", |
---|
114 | "ag", |
---|
115 | "ah", |
---|
116 | "ai", |
---|
117 | "aj", |
---|
118 | "ak", |
---|
119 | "al", |
---|
120 | "am", |
---|
121 | "an", |
---|
122 | "ao", |
---|
123 | "ap", |
---|
124 | "aq", |
---|
125 | "ar" |
---|
126 | }; |
---|
127 | |
---|
128 | char *dnames[] = { |
---|
129 | "a", |
---|
130 | "b", |
---|
131 | "c", |
---|
132 | "d", |
---|
133 | "e", |
---|
134 | "f", |
---|
135 | "c/y", |
---|
136 | "c/z", |
---|
137 | "c/x", |
---|
138 | "c/y/a3333", |
---|
139 | "c/y/j123", |
---|
140 | "END" |
---|
141 | }; |
---|
142 | |
---|
143 | int select1 ( struct dirent *entry ) |
---|
144 | { |
---|
145 | printf("SCANDIR SELECT1 accepts nodename: %s\n", entry->d_name ); |
---|
146 | return 1; |
---|
147 | } |
---|
148 | |
---|
149 | int select2 ( struct dirent *entry ) |
---|
150 | { |
---|
151 | if( strcmp( entry->d_name, "y") == 0 ) { |
---|
152 | printf("SCANDIR SELECT accepted nodename: %s\n", entry->d_name ); |
---|
153 | return 1; |
---|
154 | } |
---|
155 | printf("SCANDIR SELECT rejected nodename: %s\n", entry->d_name ); |
---|
156 | return 0; |
---|
157 | } |
---|
158 | |
---|
159 | int compare_ascending(const struct dirent **a, const struct dirent **b ) |
---|
160 | { |
---|
161 | int i; |
---|
162 | |
---|
163 | i = strcmp ( |
---|
164 | (char *)((struct dirent *)(*a)->d_name), |
---|
165 | (char *)((struct dirent *)(*b)->d_name) |
---|
166 | ); |
---|
167 | return i; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | int compare_descending( struct dirent **a, struct dirent **b ) |
---|
172 | { |
---|
173 | int i; |
---|
174 | |
---|
175 | i = strcmp ( |
---|
176 | (char *)((struct dirent *)(*b)->d_name), |
---|
177 | (char *)((struct dirent *)(*a)->d_name) |
---|
178 | ); |
---|
179 | |
---|
180 | return i; |
---|
181 | } |
---|
182 | |
---|
183 | void test_across_mount() |
---|
184 | { |
---|
185 | rtems_filesystem_mount_table_entry_t *mt_entry; |
---|
186 | int status; |
---|
187 | |
---|
188 | /* |
---|
189 | * Verify Readdir works correctly over mount points. |
---|
190 | */ |
---|
191 | |
---|
192 | printf("Validate readdir across mount point\n"); |
---|
193 | assert( mkdir( "/imfs", 0777 ) == 0 ); |
---|
194 | assert( mkdir( "/imfs/should_be_hidden", 0777 ) == 0 ); |
---|
195 | complete_printdir("/imfs" ); |
---|
196 | printf("Attempting to mount IMFS file system at /imfs \n"); |
---|
197 | status = mount( |
---|
198 | &mt_entry, |
---|
199 | &IMFS_ops, |
---|
200 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
201 | NULL, |
---|
202 | "/imfs" ); |
---|
203 | assert( status == 0 ); |
---|
204 | if( mt_entry == NULL ){ |
---|
205 | printf(" NULL mount table entry was returned\n"); |
---|
206 | } |
---|
207 | printf( "create /imfs/testdir and /imfs/testdir/testsubdir\n"); |
---|
208 | |
---|
209 | status = mkdir( "/imfs/testdir", 0777 ); |
---|
210 | assert( status == 0 ); |
---|
211 | status = mkdir( "/imfs/testdir/testsubdir", 0777 ); |
---|
212 | assert( status == 0 ); |
---|
213 | |
---|
214 | complete_printdir("/imfs" ); |
---|
215 | complete_printdir("/imfs/" ); |
---|
216 | complete_printdir("/imfs/." ); |
---|
217 | complete_printdir("/imfs/testdir" ); |
---|
218 | complete_printdir("/imfs/testdir/.." ); |
---|
219 | } |
---|
220 | |
---|
221 | #if defined(__rtems__) |
---|
222 | int test_main(void) |
---|
223 | #else |
---|
224 | int main( |
---|
225 | int argc, |
---|
226 | char **argv |
---|
227 | ) |
---|
228 | #endif |
---|
229 | { |
---|
230 | int fd; |
---|
231 | int i; |
---|
232 | int status; |
---|
233 | off_t off; |
---|
234 | struct dirent *d_not; |
---|
235 | struct dirent **namelist; |
---|
236 | struct stat s; |
---|
237 | |
---|
238 | |
---|
239 | printf( "\n\n*** READDIR TEST ***\n" ); |
---|
240 | |
---|
241 | printf( "\nchdir to the root directory\n" ); |
---|
242 | status = chdir( "/" ); |
---|
243 | printf( "chdir() status : %d\n\n", status ); |
---|
244 | |
---|
245 | printf( "\nCreating a series of directories under /\n" ); |
---|
246 | i=0; |
---|
247 | while ( strcmp(dnames[i], "END") != 0 ) |
---|
248 | { |
---|
249 | status = mkdir( dnames[i], 0x1c0 ); |
---|
250 | printf("Creating directory: %s %d %d ", dnames[i], status, errno ); |
---|
251 | if ( errno == 0 ) |
---|
252 | printf(" Success\n"); |
---|
253 | else |
---|
254 | printf(" Failure\n"); |
---|
255 | |
---|
256 | i++; |
---|
257 | } |
---|
258 | |
---|
259 | /* |
---|
260 | * Create files under many and open the directory. |
---|
261 | */ |
---|
262 | |
---|
263 | printf("Create a lot of files\n"); |
---|
264 | status = mkdir( "/many", 0x1c0 ); |
---|
265 | status = chdir( "/many" ); |
---|
266 | for (i = 0; i<44; i++) { |
---|
267 | printf("Create %s\n", many_files[i]); |
---|
268 | fd = open (many_files[i], O_CREAT, S_IRWXU); |
---|
269 | close (fd); |
---|
270 | } |
---|
271 | printf("Open /many and print the directory\n"); |
---|
272 | directory_not = opendir( "/many" ); |
---|
273 | printdir ( directory_not ); |
---|
274 | d_not = readdir( directory_not ); |
---|
275 | |
---|
276 | printf("open /b/myfile\n"); |
---|
277 | fd = open ("/b/my_file", O_CREAT, S_IRWXU); |
---|
278 | assert( fd != -1 ); |
---|
279 | close (fd); |
---|
280 | |
---|
281 | printf("scandir a file status: "); |
---|
282 | status = scandir( |
---|
283 | "/b/my_file", |
---|
284 | &namelist, |
---|
285 | select1, |
---|
286 | NULL |
---|
287 | ); |
---|
288 | printf("%d\n", status); |
---|
289 | |
---|
290 | printf("Open /b/new_file\n"); |
---|
291 | fd = open( "/b/new_file", O_CREAT, S_IRWXU ); |
---|
292 | assert( fd != -1 ); |
---|
293 | |
---|
294 | printf("fcntl F_SETFD should return 0\n"); |
---|
295 | status = fcntl( fd, F_SETFD, 1 ); |
---|
296 | assert( status == 0 ); |
---|
297 | |
---|
298 | printf("fcntl F_SETFD should return 1\n"); |
---|
299 | status = fcntl( fd, F_GETFD, 1 ); |
---|
300 | assert( status == 1 ); |
---|
301 | |
---|
302 | #if 0 |
---|
303 | printf("fcntl F_DUPFD should return 0\n"); |
---|
304 | status = fcntl( fd, F_DUPFD, 0 ); |
---|
305 | assert ( status == 0 ); |
---|
306 | #else |
---|
307 | printf("fcntl F_DUPFD should return 0 -- skip until implemented\n"); |
---|
308 | #endif |
---|
309 | |
---|
310 | printf("fcntl F_GETFL returns current flags\n"); |
---|
311 | status = fcntl( fd, F_GETFL, 1 ); |
---|
312 | printf("fcntl F_GETFL returned 0x%x\n", status ); |
---|
313 | assert( status != -1 ); |
---|
314 | |
---|
315 | printf("fcntl F_SETFL to add O_APPEND and O_NONBLOCK\n"); |
---|
316 | status = fcntl( fd, F_SETFL, O_APPEND|O_NONBLOCK ); |
---|
317 | assert ( status != -1 ); |
---|
318 | |
---|
319 | printf("fcntl F_GETFL return current flags to see changes\n"); |
---|
320 | status = fcntl( fd, F_GETFL, 1 ); |
---|
321 | printf("fcntl F_GETFL returned 0x%x\n", status ); |
---|
322 | assert( status != -1 ); |
---|
323 | |
---|
324 | printf("fcntl F_GETLK should return -1\n"); |
---|
325 | status = fcntl( fd, F_GETLK, 1 ); |
---|
326 | assert ( status == -1 ); |
---|
327 | |
---|
328 | printf("fcntl F_SETLK should return -1\n"); |
---|
329 | status = fcntl( fd, F_SETLK, 1 ); |
---|
330 | assert ( status == -1 ); |
---|
331 | |
---|
332 | printf("fcntl F_SETLKW should return -1\n"); |
---|
333 | status = fcntl( fd, F_SETLKW, 1 ); |
---|
334 | assert ( status == -1 ); |
---|
335 | |
---|
336 | printf("fcntl F_SETOWN should return -1\n"); |
---|
337 | status = fcntl( fd, F_SETOWN, 1 ); |
---|
338 | assert ( status == -1 ); |
---|
339 | |
---|
340 | printf("fcntl F_GETOWN should return -1\n"); |
---|
341 | status = fcntl( fd, F_GETOWN, 1 ); |
---|
342 | assert ( status == -1 ); |
---|
343 | |
---|
344 | printf("fcntl invalid argument should return -1\n"); |
---|
345 | status = fcntl( fd, 0xb, 1 ); |
---|
346 | printf("Status %d\n",status); |
---|
347 | assert( status == -1 ); |
---|
348 | |
---|
349 | printf("opendir and readdir /b/myfile\n"); |
---|
350 | directory_not = opendir ("/b/my_file"); |
---|
351 | d_not = readdir(directory_not); |
---|
352 | |
---|
353 | printf("opendir and readdir\n"); |
---|
354 | directory_not = opendir ("/a"); |
---|
355 | d_not = readdir (directory_not); |
---|
356 | |
---|
357 | printf("chdir to /b/myfile\n"); |
---|
358 | status = chdir ("/b/my_file"); |
---|
359 | assert (status == -1); |
---|
360 | |
---|
361 | printf( "\nPerforming stat of directory /\n"); |
---|
362 | status = stat( "/", &s ); |
---|
363 | printf("status for stat : %d, size of directory: %d\n\n", |
---|
364 | status,(int)s.st_size); |
---|
365 | |
---|
366 | puts( "\nOpen and print directory /" ); |
---|
367 | directory = opendir("/"); |
---|
368 | assert( directory ); |
---|
369 | printdir(directory); |
---|
370 | |
---|
371 | printf("\nmkdir /d/my_dir\n"); |
---|
372 | status = mkdir( "/d/my_dir", 0x1c0 ); |
---|
373 | printf("Open /d/my_dir\n"); |
---|
374 | directory_not = opendir( "/d/my_dir" ); |
---|
375 | assert( directory_not ); |
---|
376 | |
---|
377 | printf( "remove /d/my_dir.\n" ); |
---|
378 | status = rmdir( "/d/my_dir" ); |
---|
379 | assert( status == 0 ); |
---|
380 | |
---|
381 | printf( "close /d/my_dir.\n" ); |
---|
382 | closedir( directory_not ); |
---|
383 | |
---|
384 | printf( "\nOpening directory /c\n" ); |
---|
385 | directory2 = opendir("/c"); |
---|
386 | |
---|
387 | assert( directory2 ); |
---|
388 | |
---|
389 | printdir(directory2); |
---|
390 | status = closedir( directory2 ); |
---|
391 | |
---|
392 | printf( "\nOpening directory /c/y\n" ); |
---|
393 | directory3 = opendir("/c/y"); |
---|
394 | assert( directory3 ); |
---|
395 | printdir(directory3); |
---|
396 | status = closedir( directory3 ); |
---|
397 | |
---|
398 | printf( "\nLSEEK to the start of the open directory\n" ); |
---|
399 | lseek( directory->dd_fd, 0, SEEK_SET ); |
---|
400 | printdir(directory); |
---|
401 | |
---|
402 | lseek( directory->dd_fd, 0, SEEK_CUR ); |
---|
403 | |
---|
404 | lseek( directory->dd_fd, 0, SEEK_END ); |
---|
405 | |
---|
406 | lseek( directory->dd_fd, 0, -99 ); |
---|
407 | |
---|
408 | printf( "\nRewinding directory\n" ); |
---|
409 | rewinddir( directory ); |
---|
410 | printdir(directory); |
---|
411 | |
---|
412 | /* Don't know how to check this one automatically. */ |
---|
413 | printf( "Send rewinddir a NULL pointer\n"); |
---|
414 | rewinddir( NULL ); |
---|
415 | |
---|
416 | printf( "\nSeek directory\n" ); |
---|
417 | printf( "telldir() should report only sizeof(struct dirent) increments \n" ); |
---|
418 | printf( "in position. Sizeof(struct dirent): %d\n", sizeof(struct dirent) ); |
---|
419 | rewinddir( directory ); |
---|
420 | for( off=0 ; off<=200 ; off=off + sizeof(struct dirent) / 4 ) { |
---|
421 | seekdir( directory, off ); |
---|
422 | printf( |
---|
423 | "seeked to %2d -- currently at %2d\n", |
---|
424 | (int)off, |
---|
425 | (int)telldir(directory) |
---|
426 | ); |
---|
427 | } |
---|
428 | |
---|
429 | printf( "Send seekdir a NULL pointer\n"); |
---|
430 | seekdir( NULL, off ); |
---|
431 | |
---|
432 | printf( "\nClosing directory\n" ); |
---|
433 | status = closedir( directory ); |
---|
434 | |
---|
435 | printf( "\nSCANDIR TEST\n"); |
---|
436 | printf( "\nselection rule 1\n"); |
---|
437 | printf( "scanning for any entry under directory /c\n\n"); |
---|
438 | status = scandir( |
---|
439 | "/c", |
---|
440 | &namelist, |
---|
441 | select1, |
---|
442 | NULL |
---|
443 | ); |
---|
444 | printf("\nscandir status: %d\n", status ); |
---|
445 | for ( i=0; i<status; i++) |
---|
446 | { |
---|
447 | printf("Selected Node Name: %s\n", namelist[i]->d_name ); |
---|
448 | } |
---|
449 | |
---|
450 | printf( "\nselection rule 2\n"); |
---|
451 | printf( "scanning for any entry under directory /c whose name = y\n\n"); |
---|
452 | status = scandir( |
---|
453 | "/c", |
---|
454 | &namelist, |
---|
455 | select2, |
---|
456 | NULL |
---|
457 | ); |
---|
458 | printf("\nscandir status: %d\n", status ); |
---|
459 | for ( i=0; i<status; i++) |
---|
460 | { |
---|
461 | printf("Selected Node Name: %s\n", namelist[i]->d_name ); |
---|
462 | } |
---|
463 | |
---|
464 | printf( "\nSCANDIR with sorting\n" ); |
---|
465 | printf( "\nselection rule 1\n"); |
---|
466 | printf( "scanning for any entry under directory /c\n"); |
---|
467 | printf( "sort in ascending order\n\n"); |
---|
468 | status = scandir( |
---|
469 | "/c", |
---|
470 | &namelist, |
---|
471 | select1, |
---|
472 | compare_ascending |
---|
473 | ); |
---|
474 | printf("\nscandir status: %d\n", status ); |
---|
475 | for ( i=0; i<status; i++) |
---|
476 | { |
---|
477 | printf("Selected and Sorted Node Name: %s\n", namelist[i]->d_name ); |
---|
478 | } |
---|
479 | |
---|
480 | |
---|
481 | printf( "\nSCANDIR with sorting\n" ); |
---|
482 | printf( "\nselection rule 1\n"); |
---|
483 | printf( "scanning for any entry under directory /c\n"); |
---|
484 | printf( "sort in descending order\n\n"); |
---|
485 | status = scandir( |
---|
486 | "/c", |
---|
487 | &namelist, |
---|
488 | select1, |
---|
489 | compare_descending |
---|
490 | ); |
---|
491 | printf("scandir status: %d\n", status ); |
---|
492 | for ( i=0; i<status; i++) |
---|
493 | { |
---|
494 | printf("Selected and Sorted Node Name: %s\n", namelist[i]->d_name ); |
---|
495 | } |
---|
496 | |
---|
497 | test_across_mount(); |
---|
498 | printf( "\n\n*** END OF READDIR TEST ***\n" ); |
---|
499 | rtems_test_exit(0); |
---|
500 | } |
---|
501 | |
---|