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