]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/fs/ram/v2_0/tests/ramfs1.c
Initial revision
[karo-tx-redboot.git] / packages / fs / ram / v2_0 / tests / ramfs1.c
1 //==========================================================================
2 //
3 //      ramfs1.c
4 //
5 //      Test fileio system
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 // Copyright (C) 2004 eCosCentric Limited
13 //
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
17 //
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 //
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
33 //
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
36 //
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
43 //
44 // Author(s):           nickg
45 // Contributors:        nickg
46 // Date:                2000-05-25
47 // Purpose:             Test fileio system
48 // Description:         This test uses the testfs to check out the initialization
49 //                      and basic operation of the fileio system
50 //                      
51 //                      
52 //                      
53 //                      
54 //                      
55 //              
56 //
57 //####DESCRIPTIONEND####
58 //
59 //==========================================================================
60
61 #include <pkgconf/hal.h>
62 #include <pkgconf/kernel.h>
63 #include <pkgconf/io_fileio.h>
64
65 #include <cyg/kernel/ktypes.h>         // base kernel types
66 #include <cyg/infra/cyg_trac.h>        // tracing macros
67 #include <cyg/infra/cyg_ass.h>         // assertion macros
68
69 #include <unistd.h>
70 #include <fcntl.h>
71 #include <sys/stat.h>
72 #include <errno.h>
73 #include <string.h>
74 #include <dirent.h>
75
76 #include <cyg/fileio/fileio.h>
77
78 #include <cyg/infra/testcase.h>
79 #include <cyg/infra/diag.h>            // HAL polled output
80
81 //==========================================================================
82
83 #if 0
84 MTAB_ENTRY( ramfs_mte1,
85                    "/",
86                    "ramfs",
87                    "",
88                    0);
89 #endif
90
91 //==========================================================================
92
93 #define SHOW_RESULT( _fn, _res ) \
94 diag_printf("<FAIL>: " #_fn "() returned %ld %s\n", (long)_res, _res<0?strerror(errno):"");
95
96 //==========================================================================
97
98 #define IOSIZE  100
99
100 #define LONGNAME1       "long_file_name_that_should_take_up_more_than_one_directory_entry_1"
101 #define LONGNAME2       "long_file_name_that_should_take_up_more_than_one_directory_entry_2"
102
103
104 //==========================================================================
105
106 #ifndef CYGPKG_LIBC_STRING
107
108 char *strcat( char *s1, const char *s2 )
109 {
110     char *s = s1;
111     while( *s1 ) s1++;
112     while( (*s1++ = *s2++) != 0);
113     return s;
114 }
115
116 #endif
117
118 //==========================================================================
119
120 static void listdir( char *name, int statp, int numexpected, int *numgot )
121 {
122     int err;
123     DIR *dirp;
124     int num=0;
125     
126     diag_printf("<INFO>: reading directory %s\n",name);
127     
128     dirp = opendir( name );
129     if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
130
131     for(;;)
132     {
133         struct dirent *entry = readdir( dirp );
134         
135         if( entry == NULL )
136             break;
137         num++;
138         diag_printf("<INFO>: entry %14s",entry->d_name);
139         if( statp )
140         {
141             char fullname[PATH_MAX];
142             struct stat sbuf;
143
144             if( name[0] )
145             {
146                 strcpy(fullname, name );
147                 if( !(name[0] == '/' && name[1] == 0 ) )
148                     strcat(fullname, "/" );
149             }
150             else fullname[0] = 0;
151             
152             strcat(fullname, entry->d_name );
153             
154             err = stat( fullname, &sbuf );
155             if( err < 0 )
156             {
157                 if( errno == ENOSYS )
158                     diag_printf(" <no status available>");
159                 else SHOW_RESULT( stat, err );
160             }
161             else
162             {
163                 diag_printf(" [mode %08x ino %08x nlink %d size %ld]",
164                             sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,
165                             (unsigned long) sbuf.st_size);
166             }
167         }
168
169         diag_printf("\n");
170     }
171
172     err = closedir( dirp );
173     if( err < 0 ) SHOW_RESULT( stat, err );
174     if (numexpected >= 0 && num != numexpected)
175         CYG_TEST_FAIL("Wrong number of dir entries\n");
176     if ( numgot != NULL )
177         *numgot = num;
178 }
179
180 //==========================================================================
181
182 static void createfile( char *name, size_t size )
183 {
184     char buf[IOSIZE];
185     int fd;
186     ssize_t wrote;
187     int i;
188     int err;
189
190     diag_printf("<INFO>: create file %s size %d\n",name,size);
191
192     err = access( name, F_OK );
193     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
194     
195     for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
196  
197     fd = open( name, O_WRONLY|O_CREAT );
198     if( fd < 0 ) SHOW_RESULT( open, fd );
199
200     while( size > 0 )
201     {
202         ssize_t len = size;
203         if ( len > IOSIZE ) len = IOSIZE;
204         
205         wrote = write( fd, buf, len );
206         if( wrote != len ) SHOW_RESULT( write, wrote );        
207
208         size -= wrote;
209     }
210
211     err = close( fd );
212     if( err < 0 ) SHOW_RESULT( close, err );
213 }
214
215 //==========================================================================
216
217 #if 0
218 static void maxfile( char *name )
219 {
220     char buf[IOSIZE];
221     int fd;
222     ssize_t wrote;
223     int i;
224     int err;
225     size_t size = 0;
226     
227     diag_printf("<INFO>: create maximal file %s\n",name);
228
229     err = access( name, F_OK );
230     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
231     
232     for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
233  
234     fd = open( name, O_WRONLY|O_CREAT );
235     if( fd < 0 ) SHOW_RESULT( open, fd );
236
237     do
238     {
239         wrote = write( fd, buf, IOSIZE );
240         if( wrote < 0 ) SHOW_RESULT( write, wrote );        
241
242         size += wrote;
243         
244     } while( wrote == IOSIZE );
245
246     diag_printf("<INFO>: file size == %d\n",size);
247
248     err = close( fd );
249     if( err < 0 ) SHOW_RESULT( close, err );
250 }
251 #endif
252
253 //==========================================================================
254
255 static void checkfile( char *name )
256 {
257     char buf[IOSIZE];
258     int fd;
259     ssize_t done;
260     int i;
261     int err;
262     off_t pos = 0;
263
264     diag_printf("<INFO>: check file %s\n",name);
265     
266     err = access( name, F_OK );
267     if( err != 0 ) SHOW_RESULT( access, err );
268
269     fd = open( name, O_RDONLY );
270     if( fd < 0 ) SHOW_RESULT( open, fd );
271
272     for(;;)
273     {
274         done = read( fd, buf, IOSIZE );
275         if( done < 0 ) SHOW_RESULT( read, done );
276
277         if( done == 0 ) break;
278
279         for( i = 0; i < done; i++ )
280             if( buf[i] != i%256 )
281             {
282                 diag_printf("buf[%ld+%d](%02x) != %02x\n",
283                             (unsigned long)pos,i,buf[i],i%256);
284                 CYG_TEST_FAIL("Data read not equal to data written\n");
285             }
286         
287         pos += done;
288     }
289
290     err = close( fd );
291     if( err < 0 ) SHOW_RESULT( close, err );
292 }
293
294 //==========================================================================
295
296 static void copyfile( char *name2, char *name1 )
297 {
298
299     int err;
300     char buf[IOSIZE];
301     int fd1, fd2;
302     ssize_t done, wrote;
303
304     diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
305
306     err = access( name1, F_OK );
307     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
308
309     err = access( name2, F_OK );
310     if( err != 0 ) SHOW_RESULT( access, err );
311     
312     fd1 = open( name1, O_WRONLY|O_CREAT );
313     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
314
315     fd2 = open( name2, O_RDONLY );
316     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
317     
318     for(;;)
319     {
320         done = read( fd2, buf, IOSIZE );
321         if( done < 0 ) SHOW_RESULT( read, done );
322
323         if( done == 0 ) break;
324
325         wrote = write( fd1, buf, done );
326         if( wrote != done ) SHOW_RESULT( write, wrote );
327
328         if( wrote != done ) break;
329     }
330
331     err = close( fd1 );
332     if( err < 0 ) SHOW_RESULT( close, err );
333
334     err = close( fd2 );
335     if( err < 0 ) SHOW_RESULT( close, err );
336     
337 }
338
339 //==========================================================================
340
341 static void comparefiles( char *name2, char *name1 )
342 {
343     int err;
344     char buf1[IOSIZE];
345     char buf2[IOSIZE];
346     int fd1, fd2;
347     ssize_t done1, done2;
348     int i;
349
350     diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
351
352     err = access( name1, F_OK );
353     if( err != 0 ) SHOW_RESULT( access, err );
354
355     err = access( name1, F_OK );
356     if( err != 0 ) SHOW_RESULT( access, err );
357     
358     fd1 = open( name1, O_RDONLY );
359     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
360
361     fd2 = open( name2, O_RDONLY );
362     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
363     
364     for(;;)
365     {
366         done1 = read( fd1, buf1, IOSIZE );
367         if( done1 < 0 ) SHOW_RESULT( read, done1 );
368
369         done2 = read( fd2, buf2, IOSIZE );
370         if( done2 < 0 ) SHOW_RESULT( read, done2 );
371
372         if( done1 != done2 )
373             diag_printf("Files different sizes\n");
374         
375         if( done1 == 0 ) break;
376
377         for( i = 0; i < done1; i++ )
378             if( buf1[i] != buf2[i] )
379             {
380                 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
381                 CYG_TEST_FAIL("Data in files not equal\n");
382             }
383     }
384
385     err = close( fd1 );
386     if( err < 0 ) SHOW_RESULT( close, err );
387
388     err = close( fd2 );
389     if( err < 0 ) SHOW_RESULT( close, err );
390     
391 }
392
393 //==========================================================================
394
395 void checkcwd( const char *cwd )
396 {
397     static char cwdbuf[PATH_MAX];
398     char *ret;
399
400     ret = getcwd( cwdbuf, sizeof(cwdbuf));
401     if( ret == NULL ) SHOW_RESULT( getcwd, ret );    
402
403     if( strcmp( cwdbuf, cwd ) != 0 )
404     {
405         diag_printf( "cwdbuf %s cwd %s\n",cwdbuf, cwd );
406         CYG_TEST_FAIL( "Current directory mismatch");
407     }
408 }
409
410 //==========================================================================
411 // main
412
413 int main( int argc, char **argv )
414 {
415     int err;
416     int existingdirents=-1;
417
418     CYG_TEST_INIT();
419
420     // --------------------------------------------------------------
421
422     err = mount( "", "/", "ramfs" );
423     if( err < 0 ) SHOW_RESULT( mount, err );    
424
425     err = chdir( "/" );
426     if( err < 0 ) SHOW_RESULT( chdir, err );
427
428     checkcwd( "/" );
429     
430     listdir( "/", true, -1, &existingdirents );
431     if ( existingdirents < 2 )
432         CYG_TEST_FAIL("Not enough dir entries\n");
433
434     // --------------------------------------------------------------
435
436     createfile( "/foo", 202 );
437     checkfile( "foo" );
438     copyfile( "foo", "fee");
439     checkfile( "fee" );
440     comparefiles( "foo", "/fee" );
441     diag_printf("<INFO>: mkdir bar\n");
442     err = mkdir( "/bar", 0 );
443     if( err < 0 ) SHOW_RESULT( mkdir, err );
444
445     listdir( "/" , true, existingdirents+3, NULL );
446
447     copyfile( "fee", "/bar/fum" );
448     checkfile( "bar/fum" );
449     comparefiles( "/fee", "bar/fum" );
450
451     diag_printf("<INFO>: cd bar\n");
452     err = chdir( "bar" );
453     if( err < 0 ) SHOW_RESULT( chdir, err );
454
455     checkcwd( "/bar" );
456     
457     diag_printf("<INFO>: rename /foo bundy\n");    
458     err = rename( "/foo", "bundy" );
459     if( err < 0 ) SHOW_RESULT( rename, err );
460     
461     listdir( "/", true, existingdirents+2, NULL );
462     listdir( "" , true, 4, NULL );
463
464     checkfile( "/bar/bundy" );
465     comparefiles("/fee", "bundy" );
466
467     // --------------------------------------------------------------
468
469     createfile( LONGNAME1, 123 );
470     checkfile( LONGNAME1 );
471     copyfile( LONGNAME1, LONGNAME2 );
472
473     listdir( "", false, 6, NULL );
474     
475     diag_printf("<INFO>: unlink " LONGNAME1 "\n");    
476     err = unlink( LONGNAME1 );
477     if( err < 0 ) SHOW_RESULT( unlink, err );
478
479     diag_printf("<INFO>: unlink " LONGNAME2 "\n");    
480     err = unlink( LONGNAME2 );
481     if( err < 0 ) SHOW_RESULT( unlink, err );
482     
483     
484     // --------------------------------------------------------------
485
486     diag_printf("<INFO>: unlink fee\n");    
487     err = unlink( "/fee" );
488     if( err < 0 ) SHOW_RESULT( unlink, err );
489
490     diag_printf("<INFO>: unlink fum\n");        
491     err = unlink( "fum" );
492     if( err < 0 ) SHOW_RESULT( unlink, err );
493
494     diag_printf("<INFO>: unlink /bar/bundy\n");        
495     err = unlink( "/bar/bundy" );
496     if( err < 0 ) SHOW_RESULT( unlink, err );
497
498     diag_printf("<INFO>: cd /\n");        
499     err = chdir( "/" );
500     if( err < 0 ) SHOW_RESULT( chdir, err );
501
502     checkcwd( "/" );
503     
504     diag_printf("<INFO>: rmdir /bar\n");        
505     err = rmdir( "/bar" );
506     if( err < 0 ) SHOW_RESULT( rmdir, err );
507     
508     listdir( "/", false, existingdirents, NULL );
509
510     // --------------------------------------------------------------
511
512     diag_printf("<INFO>: mount /ram \n");
513     err = mount( "", "/ram", "ramfs" );
514     if( err < 0 ) SHOW_RESULT( mount, err );    
515
516     createfile( "/ram/tinky", 456 );
517     copyfile( "/ram/tinky", "/ram/laalaa" );
518     checkfile( "/ram/tinky");
519     checkfile( "/ram/laalaa");
520     comparefiles( "/ram/tinky", "/ram/laalaa" );
521
522     diag_printf("<INFO>: cd /ram\n");    
523     err = chdir( "/ram" );
524     if( err < 0 ) SHOW_RESULT( chdir, err );
525
526     checkcwd( "/ram" );
527         
528     diag_printf("<INFO>: mkdir noonoo\n");    
529     err = mkdir( "noonoo", 0 );
530     if( err < 0 ) SHOW_RESULT( mkdir, err );
531
532     listdir( "." , true, existingdirents+3, NULL);
533
534     diag_printf("<INFO>: cd noonoo\n");
535     err = chdir( "noonoo" );
536     if( err < 0 ) SHOW_RESULT( chdir, err );
537
538     checkcwd( "/ram/noonoo" );
539     
540     createfile( "tinky", 678 );
541     checkfile( "tinky" );
542
543     createfile( "dipsy", 3456 );
544     checkfile( "dipsy" );
545     copyfile( "dipsy", "po" );
546     checkfile( "po" );
547     comparefiles( "dipsy", "po" );
548
549     listdir( ".", true, 5, NULL );
550     listdir( "", true, 5, NULL );
551     listdir( "..", true, existingdirents+3, NULL );
552
553     // --------------------------------------------------------------
554
555     diag_printf("<INFO>: unlink tinky\n");    
556     err = unlink( "tinky" );
557     if( err < 0 ) SHOW_RESULT( unlink, err );
558
559     diag_printf("<INFO>: unlink dipsy\n");    
560     err = unlink( "dipsy" );
561     if( err < 0 ) SHOW_RESULT( unlink, err );
562
563     diag_printf("<INFO>: unlink po\n");    
564     err = unlink( "po" );
565     if( err < 0 ) SHOW_RESULT( unlink, err );
566
567     diag_printf("<INFO>: cd ..\n"); 
568     err = chdir( ".." );
569     if( err < 0 ) SHOW_RESULT( chdir, err );
570     checkcwd( "/ram" );
571     
572     diag_printf("<INFO>: rmdir noonoo\n"); 
573     err = rmdir( "noonoo" );
574     if( err < 0 ) SHOW_RESULT( rmdir, err );
575
576     // --------------------------------------------------------------
577
578     err = mkdir( "x", 0 );
579     if( err < 0 ) SHOW_RESULT( mkdir, err );
580     
581     err = mkdir( "x/y", 0 );
582     if( err < 0 ) SHOW_RESULT( mkdir, err );
583     
584     err = mkdir( "x/y/z", 0 );
585     if( err < 0 ) SHOW_RESULT( mkdir, err );
586
587     err = mkdir( "x/y/z/w", 0 );
588     if( err < 0 ) SHOW_RESULT( mkdir, err );
589     
590     diag_printf("<INFO>: cd /ram/x/y/z/w\n");
591     err = chdir( "/ram/x/y/z/w" );
592     if( err < 0 ) SHOW_RESULT( chdir, err );
593     checkcwd( "/ram/x/y/z/w" );
594
595     diag_printf("<INFO>: cd ..\n");
596     err = chdir( ".." );
597     if( err < 0 ) SHOW_RESULT( chdir, err );
598     checkcwd( "/ram/x/y/z" );
599     
600     diag_printf("<INFO>: cd .\n");
601     err = chdir( "." );
602     if( err < 0 ) SHOW_RESULT( chdir, err );
603     checkcwd( "/ram/x/y/z" );
604
605     diag_printf("<INFO>: cd ../../y\n");
606     err = chdir( "../../y" );
607     if( err < 0 ) SHOW_RESULT( chdir, err );
608     checkcwd( "/ram/x/y" );
609
610     diag_printf("<INFO>: cd ../..\n");
611     err = chdir( "../.." );
612     if( err < 0 ) SHOW_RESULT( chdir, err );
613     checkcwd( "/ram" );
614
615     diag_printf("<INFO>: rmdir x/y/z/w\n"); 
616     err = rmdir( "x/y/z/w" );
617     if( err < 0 ) SHOW_RESULT( rmdir, err );
618
619     diag_printf("<INFO>: rmdir x/y/z\n"); 
620     err = rmdir( "x/y/z" );
621     if( err < 0 ) SHOW_RESULT( rmdir, err );
622
623     diag_printf("<INFO>: rmdir x/y\n"); 
624     err = rmdir( "x/y" );
625     if( err < 0 ) SHOW_RESULT( rmdir, err );
626
627     diag_printf("<INFO>: rmdir x\n"); 
628     err = rmdir( "x" );
629     if( err < 0 ) SHOW_RESULT( rmdir, err );
630     
631     // --------------------------------------------------------------
632     
633     diag_printf("<INFO>: unlink tinky\n");    
634     err = unlink( "tinky" );
635     if( err < 0 ) SHOW_RESULT( unlink, err );
636
637     diag_printf("<INFO>: unlink laalaa\n");    
638     err = unlink( "laalaa" );
639     if( err < 0 ) SHOW_RESULT( unlink, err );
640
641     diag_printf("<INFO>: cd /\n");    
642     err = chdir( "/" );
643     if( err < 0 ) SHOW_RESULT( chdir, err );
644     checkcwd( "/" );
645     
646     diag_printf("<INFO>: umount /ram\n");    
647     err = umount( "/ram" );
648     if( err < 0 ) SHOW_RESULT( umount, err );    
649     
650     CYG_TEST_PASS_FINISH("ramfs1");
651 }
652
653 // -------------------------------------------------------------------------
654 // EOF ramfs1.c