source: rtems/testsuites/psxtests/psxreaddir/test.c @ 0fdaca2

4.104.114.84.9
Last change on this file since 0fdaca2 was 0fdaca2, checked in by Ralf Corsepius <ralf.corsepius@…>, on Apr 1, 2004 at 10:14:37 AM

2004-04-01 Ralf Corsepius <ralf_corsepius@…>

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