]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/fs/ram/v2_0/tests/ramfs1.c
unified MX27, MX25, MX37 trees
[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 #include <pkgconf/fs_ram.h>
65
66 #include <cyg/kernel/ktypes.h>         // base kernel types
67 #include <cyg/infra/cyg_trac.h>        // tracing macros
68 #include <cyg/infra/cyg_ass.h>         // assertion macros
69
70 #include <unistd.h>
71 #include <fcntl.h>
72 #include <sys/stat.h>
73 #include <errno.h>
74 #include <string.h>
75 #include <dirent.h>
76
77 #include <stdio.h>
78 #include <cyg/fileio/fileio.h>
79
80 #include <cyg/infra/testcase.h>
81 #include <cyg/infra/diag.h>            // HAL polled output
82
83 //==========================================================================
84
85 #if 0
86 MTAB_ENTRY( ramfs_mte1,
87                    "/",
88                    "ramfs",
89                    "",
90                    0);
91 #endif
92
93 //==========================================================================
94
95 #define SHOW_RESULT( _fn, _res ) \
96 diag_printf("<FAIL>: " #_fn "() returned %ld %s\n", (long)_res, _res<0?strerror(errno):"");
97
98 //==========================================================================
99
100 #define IOSIZE  100
101
102 #define LONGNAME1       "long_file_name_that_should_take_up_more_than_one_directory_entry_1"
103 #define LONGNAME2       "long_file_name_that_should_take_up_more_than_one_directory_entry_2"
104
105
106 //==========================================================================
107
108 #ifndef CYGPKG_LIBC_STRING
109
110 char *strcat( char *s1, const char *s2 )
111 {
112     char *s = s1;
113     while( *s1 ) s1++;
114     while( (*s1++ = *s2++) != 0);
115     return s;
116 }
117
118 #endif
119
120 //==========================================================================
121
122 static void listdir( char *name, int statp, int numexpected, int *numgot )
123 {
124     int err;
125     DIR *dirp;
126     int num=0;
127     
128     diag_printf("<INFO>: reading directory %s\n",name);
129     
130     dirp = opendir( name );
131     if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
132
133     for(;;)
134     {
135         struct dirent *entry = readdir( dirp );
136         
137         if( entry == NULL )
138             break;
139         num++;
140         diag_printf("<INFO>: entry %14s",entry->d_name);
141 #ifdef CYGPKG_FS_RAM_RET_DIRENT_DTYPE
142         diag_printf(" d_type %2x", entry->d_type);
143 #endif
144         if( statp )
145         {
146             char fullname[PATH_MAX];
147             struct stat sbuf;
148
149             if( name[0] )
150             {
151                 strcpy(fullname, name );
152                 if( !(name[0] == '/' && name[1] == 0 ) )
153                     strcat(fullname, "/" );
154             }
155             else fullname[0] = 0;
156             
157             strcat(fullname, entry->d_name );
158             
159             err = stat( fullname, &sbuf );
160             if( err < 0 )
161             {
162                 if( errno == ENOSYS )
163                     diag_printf(" <no status available>");
164                 else SHOW_RESULT( stat, err );
165             }
166             else
167             {
168                 diag_printf(" [mode %08x ino %08x nlink %d size %ld]",
169                             sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,
170                             (unsigned long) sbuf.st_size);
171             }
172 #ifdef CYGPKG_FS_RAM_RET_DIRENT_DTYPE
173             if ((entry->d_type & S_IFMT) != (sbuf.st_mode & S_IFMT))
174               CYG_TEST_FAIL("File mode's don't match between dirent and stat");
175 #endif
176         }
177
178         diag_printf("\n");
179     }
180
181     err = closedir( dirp );
182     if( err < 0 ) SHOW_RESULT( stat, err );
183     if (numexpected >= 0 && num != numexpected)
184         CYG_TEST_FAIL("Wrong number of dir entries\n");
185     if ( numgot != NULL )
186         *numgot = num;
187 }
188
189 //==========================================================================
190
191 static void createfile( char *name, size_t size )
192 {
193     char buf[IOSIZE];
194     int fd;
195     ssize_t wrote;
196     int i;
197     int err;
198
199     diag_printf("<INFO>: create file %s size %zd\n",name,size);
200
201     err = access( name, F_OK );
202     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
203     
204     for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
205  
206     fd = open( name, O_WRONLY|O_CREAT );
207     if( fd < 0 ) SHOW_RESULT( open, fd );
208
209     while( size > 0 )
210     {
211         ssize_t len = size;
212         if ( len > IOSIZE ) len = IOSIZE;
213         
214         wrote = write( fd, buf, len );
215         if( wrote != len ) SHOW_RESULT( write, wrote );        
216
217         size -= wrote;
218     }
219
220     err = close( fd );
221     if( err < 0 ) SHOW_RESULT( close, err );
222 }
223
224 //==========================================================================
225
226 #if 0
227 static void maxfile( char *name )
228 {
229     char buf[IOSIZE];
230     int fd;
231     ssize_t wrote;
232     int i;
233     int err;
234     size_t size = 0;
235     
236     diag_printf("<INFO>: create maximal file %s\n",name);
237
238     err = access( name, F_OK );
239     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
240     
241     for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
242  
243     fd = open( name, O_WRONLY|O_CREAT );
244     if( fd < 0 ) SHOW_RESULT( open, fd );
245
246     do
247     {
248         wrote = write( fd, buf, IOSIZE );
249         if( wrote < 0 ) SHOW_RESULT( write, wrote );        
250
251         size += wrote;
252         
253     } while( wrote == IOSIZE );
254
255     diag_printf("<INFO>: file size == %d\n",size);
256
257     err = close( fd );
258     if( err < 0 ) SHOW_RESULT( close, err );
259 }
260 #endif
261
262 //==========================================================================
263
264 static void checkfile( char *name )
265 {
266     char buf[IOSIZE];
267     int fd;
268     ssize_t done;
269     int i;
270     int err;
271     off_t pos = 0;
272
273     diag_printf("<INFO>: check file %s\n",name);
274     
275     err = access( name, F_OK );
276     if( err != 0 ) SHOW_RESULT( access, err );
277
278     fd = open( name, O_RDONLY );
279     if( fd < 0 ) SHOW_RESULT( open, fd );
280
281     for(;;)
282     {
283         done = read( fd, buf, IOSIZE );
284         if( done < 0 ) SHOW_RESULT( read, done );
285
286         if( done == 0 ) break;
287
288         for( i = 0; i < done; i++ )
289             if( buf[i] != i%256 )
290             {
291                 diag_printf("buf[%ld+%d](%02x) != %02x\n",
292                             (unsigned long)pos,i,buf[i],i%256);
293                 CYG_TEST_FAIL("Data read not equal to data written\n");
294             }
295         
296         pos += done;
297     }
298
299     err = close( fd );
300     if( err < 0 ) SHOW_RESULT( close, err );
301 }
302
303 //==========================================================================
304
305 static void copyfile( char *name2, char *name1 )
306 {
307
308     int err;
309     char buf[IOSIZE];
310     int fd1, fd2;
311     ssize_t done, wrote;
312
313     diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
314
315     err = access( name1, F_OK );
316     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
317
318     err = access( name2, F_OK );
319     if( err != 0 ) SHOW_RESULT( access, err );
320     
321     fd1 = open( name1, O_WRONLY|O_CREAT );
322     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
323
324     fd2 = open( name2, O_RDONLY );
325     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
326     
327     for(;;)
328     {
329         done = read( fd2, buf, IOSIZE );
330         if( done < 0 ) SHOW_RESULT( read, done );
331
332         if( done == 0 ) break;
333
334         wrote = write( fd1, buf, done );
335         if( wrote != done ) SHOW_RESULT( write, wrote );
336
337         if( wrote != done ) break;
338     }
339
340     err = close( fd1 );
341     if( err < 0 ) SHOW_RESULT( close, err );
342
343     err = close( fd2 );
344     if( err < 0 ) SHOW_RESULT( close, err );
345     
346 }
347
348 //==========================================================================
349
350 static void comparefiles( char *name2, char *name1 )
351 {
352     int err;
353     char buf1[IOSIZE];
354     char buf2[IOSIZE];
355     int fd1, fd2;
356     ssize_t done1, done2;
357     int i;
358
359     diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
360
361     err = access( name1, F_OK );
362     if( err != 0 ) SHOW_RESULT( access, err );
363
364     err = access( name1, F_OK );
365     if( err != 0 ) SHOW_RESULT( access, err );
366     
367     fd1 = open( name1, O_RDONLY );
368     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
369
370     fd2 = open( name2, O_RDONLY );
371     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
372     
373     for(;;)
374     {
375         done1 = read( fd1, buf1, IOSIZE );
376         if( done1 < 0 ) SHOW_RESULT( read, done1 );
377
378         done2 = read( fd2, buf2, IOSIZE );
379         if( done2 < 0 ) SHOW_RESULT( read, done2 );
380
381         if( done1 != done2 )
382             diag_printf("Files different sizes\n");
383         
384         if( done1 == 0 ) break;
385
386         for( i = 0; i < done1; i++ )
387             if( buf1[i] != buf2[i] )
388             {
389                 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
390                 CYG_TEST_FAIL("Data in files not equal\n");
391             }
392     }
393
394     err = close( fd1 );
395     if( err < 0 ) SHOW_RESULT( close, err );
396
397     err = close( fd2 );
398     if( err < 0 ) SHOW_RESULT( close, err );
399     
400 }
401
402 //==========================================================================
403
404 void checkcwd( const char *cwd )
405 {
406     static char cwdbuf[PATH_MAX];
407     char *ret;
408
409     ret = getcwd( cwdbuf, sizeof(cwdbuf));
410     if( ret == NULL ) SHOW_RESULT( getcwd, ret );    
411
412     if( strcmp( cwdbuf, cwd ) != 0 )
413     {
414         diag_printf( "cwdbuf %s cwd %s\n",cwdbuf, cwd );
415         CYG_TEST_FAIL( "Current directory mismatch");
416     }
417 }
418
419 //==========================================================================
420 // main
421
422 int main( int argc, char **argv )
423 {
424     int err;
425     int existingdirents=-1;
426 #if defined(CYGSEM_FILEIO_BLOCK_USAGE) && defined(CYGPKG_FS_RAM_BLOCKS_ARRAY)
427     struct cyg_fs_block_usage usage;
428 #endif
429
430     CYG_TEST_INIT();
431
432     // --------------------------------------------------------------
433
434     err = mount( "", "/", "ramfs" );
435     if( err < 0 ) SHOW_RESULT( mount, err );    
436
437     err = chdir( "/" );
438     if( err < 0 ) SHOW_RESULT( chdir, err );
439
440     checkcwd( "/" );
441     
442     listdir( "/", true, -1, &existingdirents );
443     if ( existingdirents < 2 )
444         CYG_TEST_FAIL("Not enough dir entries\n");
445     // --------------------------------------------------------------
446 #if defined(CYGSEM_FILEIO_BLOCK_USAGE) && defined(CYGPKG_FS_RAM_BLOCKS_ARRAY)
447     err = cyg_fs_getinfo("/", FS_INFO_BLOCK_USAGE, &usage, sizeof(usage));
448     if( err < 0 ) SHOW_RESULT( cyg_fs_getinfo, err );
449     diag_printf("<INFO>: total size: %6lld blocks, %10lld bytes\n",
450                 usage.total_blocks, usage.total_blocks * usage.block_size); 
451     diag_printf("<INFO>: free size:  %6lld blocks, %10lld bytes\n",
452                 usage.free_blocks, usage.free_blocks * usage.block_size); 
453     diag_printf("<INFO>: block size: %6u bytes\n", usage.block_size);
454 #endif
455     // --------------------------------------------------------------
456
457     createfile( "/foo", 202 );
458     checkfile( "foo" );
459     copyfile( "foo", "fee");
460     checkfile( "fee" );
461     comparefiles( "foo", "/fee" );
462     diag_printf("<INFO>: mkdir bar\n");
463     err = mkdir( "/bar", 0 );
464     if( err < 0 ) SHOW_RESULT( mkdir, err );
465
466     listdir( "/" , true, existingdirents+3, NULL );
467
468     copyfile( "fee", "/bar/fum" );
469     checkfile( "bar/fum" );
470     comparefiles( "/fee", "bar/fum" );
471
472     diag_printf("<INFO>: cd bar\n");
473     err = chdir( "bar" );
474     if( err < 0 ) SHOW_RESULT( chdir, err );
475
476     checkcwd( "/bar" );
477     
478     diag_printf("<INFO>: rename /foo bundy\n");    
479     err = rename( "/foo", "bundy" );
480     if( err < 0 ) SHOW_RESULT( rename, err );
481     
482     listdir( "/", true, existingdirents+2, NULL );
483     listdir( "" , true, 4, NULL );
484
485     checkfile( "/bar/bundy" );
486     comparefiles("/fee", "bundy" );
487
488     // --------------------------------------------------------------
489
490     createfile( LONGNAME1, 123 );
491     checkfile( LONGNAME1 );
492     copyfile( LONGNAME1, LONGNAME2 );
493
494     listdir( "", false, 6, NULL );
495     
496     diag_printf("<INFO>: unlink " LONGNAME1 "\n");    
497     err = unlink( LONGNAME1 );
498     if( err < 0 ) SHOW_RESULT( unlink, err );
499
500     diag_printf("<INFO>: unlink " LONGNAME2 "\n");    
501     err = unlink( LONGNAME2 );
502     if( err < 0 ) SHOW_RESULT( unlink, err );
503     
504     
505     // --------------------------------------------------------------
506
507     diag_printf("<INFO>: unlink fee\n");    
508     err = unlink( "/fee" );
509     if( err < 0 ) SHOW_RESULT( unlink, err );
510
511     diag_printf("<INFO>: unlink fum\n");        
512     err = unlink( "fum" );
513     if( err < 0 ) SHOW_RESULT( unlink, err );
514
515     diag_printf("<INFO>: unlink /bar/bundy\n");        
516     err = unlink( "/bar/bundy" );
517     if( err < 0 ) SHOW_RESULT( unlink, err );
518
519     diag_printf("<INFO>: cd /\n");        
520     err = chdir( "/" );
521     if( err < 0 ) SHOW_RESULT( chdir, err );
522
523     checkcwd( "/" );
524     
525     diag_printf("<INFO>: rmdir /bar\n");        
526     err = rmdir( "/bar" );
527     if( err < 0 ) SHOW_RESULT( rmdir, err );
528     
529     listdir( "/", false, existingdirents, NULL );
530
531     // --------------------------------------------------------------
532
533     diag_printf("<INFO>: mount /ram \n");
534     err = mount( "", "/ram", "ramfs" );
535     if( err < 0 ) SHOW_RESULT( mount, err );    
536
537     createfile( "/ram/tinky", 456 );
538     copyfile( "/ram/tinky", "/ram/laalaa" );
539     checkfile( "/ram/tinky");
540     checkfile( "/ram/laalaa");
541     comparefiles( "/ram/tinky", "/ram/laalaa" );
542
543     diag_printf("<INFO>: cd /ram\n");    
544     err = chdir( "/ram" );
545     if( err < 0 ) SHOW_RESULT( chdir, err );
546
547     checkcwd( "/ram" );
548         
549     diag_printf("<INFO>: mkdir noonoo\n");    
550     err = mkdir( "noonoo", 0 );
551     if( err < 0 ) SHOW_RESULT( mkdir, err );
552
553     listdir( "." , true, existingdirents+3, NULL);
554
555     diag_printf("<INFO>: cd noonoo\n");
556     err = chdir( "noonoo" );
557     if( err < 0 ) SHOW_RESULT( chdir, err );
558
559     checkcwd( "/ram/noonoo" );
560     
561     createfile( "tinky", 678 );
562     checkfile( "tinky" );
563
564     createfile( "dipsy", 3456 );
565     checkfile( "dipsy" );
566     copyfile( "dipsy", "po" );
567     checkfile( "po" );
568     comparefiles( "dipsy", "po" );
569
570     listdir( ".", true, 5, NULL );
571     listdir( "", true, 5, NULL );
572     listdir( "..", true, existingdirents+3, NULL );
573
574     // --------------------------------------------------------------
575 #if defined(CYGSEM_FILEIO_BLOCK_USAGE) && defined(CYGPKG_FS_RAM_BLOCKS_ARRAY)
576     err = cyg_fs_getinfo("/", FS_INFO_BLOCK_USAGE, &usage, sizeof(usage));
577     if( err < 0 ) SHOW_RESULT( cyg_fs_getinfo, err );
578     diag_printf("<INFO>: total size: %6lld blocks, %10lld bytes\n",
579                 usage.total_blocks, usage.total_blocks * usage.block_size); 
580     diag_printf("<INFO>: free size:  %6lld blocks, %10lld bytes\n",
581                 usage.free_blocks, usage.free_blocks * usage.block_size); 
582     diag_printf("<INFO>: block size: %6u bytes\n", usage.block_size);
583 #endif
584     // --------------------------------------------------------------
585
586     diag_printf("<INFO>: unlink tinky\n");    
587     err = unlink( "tinky" );
588     if( err < 0 ) SHOW_RESULT( unlink, err );
589
590     diag_printf("<INFO>: unlink dipsy\n");    
591     err = unlink( "dipsy" );
592     if( err < 0 ) SHOW_RESULT( unlink, err );
593
594     diag_printf("<INFO>: unlink po\n");    
595     err = unlink( "po" );
596     if( err < 0 ) SHOW_RESULT( unlink, err );
597
598     diag_printf("<INFO>: cd ..\n"); 
599     err = chdir( ".." );
600     if( err < 0 ) SHOW_RESULT( chdir, err );
601     checkcwd( "/ram" );
602     
603     diag_printf("<INFO>: rmdir noonoo\n"); 
604     err = rmdir( "noonoo" );
605     if( err < 0 ) SHOW_RESULT( rmdir, err );
606
607     // --------------------------------------------------------------
608
609     err = mkdir( "x", 0 );
610     if( err < 0 ) SHOW_RESULT( mkdir, err );
611     
612     err = mkdir( "x/y", 0 );
613     if( err < 0 ) SHOW_RESULT( mkdir, err );
614     
615     err = mkdir( "x/y/z", 0 );
616     if( err < 0 ) SHOW_RESULT( mkdir, err );
617
618     err = mkdir( "x/y/z/w", 0 );
619     if( err < 0 ) SHOW_RESULT( mkdir, err );
620     
621     diag_printf("<INFO>: cd /ram/x/y/z/w\n");
622     err = chdir( "/ram/x/y/z/w" );
623     if( err < 0 ) SHOW_RESULT( chdir, err );
624     checkcwd( "/ram/x/y/z/w" );
625
626     diag_printf("<INFO>: cd ..\n");
627     err = chdir( ".." );
628     if( err < 0 ) SHOW_RESULT( chdir, err );
629     checkcwd( "/ram/x/y/z" );
630     
631     diag_printf("<INFO>: cd .\n");
632     err = chdir( "." );
633     if( err < 0 ) SHOW_RESULT( chdir, err );
634     checkcwd( "/ram/x/y/z" );
635
636     diag_printf("<INFO>: cd ../../y\n");
637     err = chdir( "../../y" );
638     if( err < 0 ) SHOW_RESULT( chdir, err );
639     checkcwd( "/ram/x/y" );
640
641     diag_printf("<INFO>: cd ../..\n");
642     err = chdir( "../.." );
643     if( err < 0 ) SHOW_RESULT( chdir, err );
644     checkcwd( "/ram" );
645
646     diag_printf("<INFO>: rmdir x/y/z/w\n"); 
647     err = rmdir( "x/y/z/w" );
648     if( err < 0 ) SHOW_RESULT( rmdir, err );
649
650     diag_printf("<INFO>: rmdir x/y/z\n"); 
651     err = rmdir( "x/y/z" );
652     if( err < 0 ) SHOW_RESULT( rmdir, err );
653
654     diag_printf("<INFO>: rmdir x/y\n"); 
655     err = rmdir( "x/y" );
656     if( err < 0 ) SHOW_RESULT( rmdir, err );
657
658     diag_printf("<INFO>: rmdir x\n"); 
659     err = rmdir( "x" );
660     if( err < 0 ) SHOW_RESULT( rmdir, err );
661     
662     // --------------------------------------------------------------
663     
664     diag_printf("<INFO>: unlink tinky\n");    
665     err = unlink( "tinky" );
666     if( err < 0 ) SHOW_RESULT( unlink, err );
667
668     diag_printf("<INFO>: unlink laalaa\n");    
669     err = unlink( "laalaa" );
670     if( err < 0 ) SHOW_RESULT( unlink, err );
671
672     diag_printf("<INFO>: cd /\n");    
673     err = chdir( "/" );
674     if( err < 0 ) SHOW_RESULT( chdir, err );
675     checkcwd( "/" );
676     
677     diag_printf("<INFO>: umount /ram\n");    
678     err = umount( "/ram" );
679     if( err < 0 ) SHOW_RESULT( umount, err );    
680     
681     CYG_TEST_PASS_FINISH("ramfs1");
682 }
683
684 // -------------------------------------------------------------------------
685 // EOF ramfs1.c