source: rtems/testsuites/psxtests/psxreaddir/test.c @ 3cefd8f0

4.115
Last change on this file since 3cefd8f0 was 3cefd8f0, checked in by Joel Sherrill <joel.sherrill@…>, on 05/06/11 at 17:29:29

2011-05-06 Joel Sherrill <joel.sherrill@…>

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