]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/fs/jffs2/v2_0/tests/fileio1.c
RedBoot TX53 Release 2012-02-15
[karo-tx-redboot.git] / packages / fs / jffs2 / 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 #include <cyg/io/flash.h>
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 #include <pkgconf/fs_jffs2.h>   // Address of JFFS2
82
83 //==========================================================================
84
85 #if 0
86 MTAB_ENTRY( jffs2_mte1,
87                    "/",
88                    "jffs2",
89                    CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1,
90                    0);
91 #endif
92
93 //==========================================================================
94
95 #define SHOW_RESULT( _fn, _res ) \
96 diag_printf("<FAIL>: " #_fn "() returned %d %s\n", _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         if( statp )
142         {
143             char fullname[PATH_MAX];
144             struct stat sbuf;
145
146             if( name[0] )
147             {
148                 strcpy(fullname, name );
149                 if( !(name[0] == '/' && name[1] == 0 ) )
150                     strcat(fullname, "/" );
151             }
152             else fullname[0] = 0;
153             
154             strcat(fullname, entry->d_name );
155             
156             err = stat( fullname, &sbuf );
157             if( err < 0 )
158             {
159                 if( errno == ENOSYS )
160                     diag_printf(" <no status available>");
161                 else SHOW_RESULT( stat, err );
162             }
163             else
164             {
165                 diag_printf(" [mode %08x ino %08x nlink %d size %d]",
166                             sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size);
167             }
168         }
169
170         diag_printf("\n");
171     }
172
173     err = closedir( dirp );
174     if( err < 0 ) SHOW_RESULT( stat, err );
175     if (numexpected >= 0 && num != numexpected)
176         CYG_TEST_FAIL("Wrong number of dir entries\n");
177     if ( numgot != NULL )
178         *numgot = num;
179 }
180
181 //==========================================================================
182
183 static void createfile( char *name, size_t size )
184 {
185     char buf[IOSIZE];
186     int fd;
187     ssize_t wrote;
188     int i;
189     int err;
190
191     diag_printf("<INFO>: create file %s size %d\n",name,size);
192
193     err = access( name, F_OK );
194     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
195     
196     for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
197  
198     fd = open( name, O_WRONLY|O_CREAT );
199     if( fd < 0 ) SHOW_RESULT( open, fd );
200
201     while( size > 0 )
202     {
203         ssize_t len = size;
204         if ( len > IOSIZE ) len = IOSIZE;
205         
206         wrote = write( fd, buf, len );
207         if( wrote != len ) SHOW_RESULT( write, wrote );        
208
209         size -= wrote;
210     }
211
212     err = close( fd );
213     if( err < 0 ) SHOW_RESULT( close, err );
214 }
215
216 //==========================================================================
217
218 #if 0
219 static void maxfile( char *name )
220 {
221     char buf[IOSIZE];
222     int fd;
223     ssize_t wrote;
224     int i;
225     int err;
226     size_t size = 0;
227     
228     diag_printf("<INFO>: create maximal file %s\n",name);
229
230     err = access( name, F_OK );
231     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
232     
233     for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
234  
235     fd = open( name, O_WRONLY|O_CREAT );
236     if( fd < 0 ) SHOW_RESULT( open, fd );
237
238     do
239     {
240         wrote = write( fd, buf, IOSIZE );
241         if( wrote < 0 ) SHOW_RESULT( write, wrote );        
242
243         size += wrote;
244         
245     } while( wrote == IOSIZE );
246
247     diag_printf("<INFO>: file size == %d\n",size);
248
249     err = close( fd );
250     if( err < 0 ) SHOW_RESULT( close, err );
251 }
252 #endif
253
254 //==========================================================================
255
256 static void checkfile( char *name )
257 {
258     char buf[IOSIZE];
259     int fd;
260     ssize_t done;
261     int i;
262     int err;
263     off_t pos = 0;
264
265     diag_printf("<INFO>: check file %s\n",name);
266     
267     err = access( name, F_OK );
268     if( err != 0 ) SHOW_RESULT( access, err );
269
270     fd = open( name, O_RDONLY );
271     if( fd < 0 ) SHOW_RESULT( open, fd );
272
273     for(;;)
274     {
275         done = read( fd, buf, IOSIZE );
276         if( done < 0 ) SHOW_RESULT( read, done );
277
278         if( done == 0 ) break;
279
280         for( i = 0; i < done; i++ )
281             if( buf[i] != i%256 )
282             {
283                 diag_printf("buf[%d+%d](%02x) != %02x\n",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 i;
417     int existingdirents=-1;
418
419     CYG_TEST_INIT();
420
421     // --------------------------------------------------------------
422
423     err = mount( CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, "/", "jffs2" );
424     if( err < 0 ) SHOW_RESULT( mount, err );    
425
426     err = chdir( "/" );
427     if( err < 0 ) SHOW_RESULT( chdir, err );
428
429     checkcwd( "/" );
430     
431     listdir( "/", true, -1, &existingdirents );
432     if ( existingdirents < 2 )
433         CYG_TEST_FAIL("Not enough dir entries\n");
434
435     // --------------------------------------------------------------
436
437     createfile( "/foo", 202 );
438     checkfile( "foo" );
439     copyfile( "foo", "fee");
440     checkfile( "fee" );
441     comparefiles( "foo", "/fee" );
442     diag_printf("<INFO>: mkdir bar\n");
443     err = mkdir( "/bar", 0 );
444     if( err < 0 ) SHOW_RESULT( mkdir, err );
445
446     listdir( "/" , true, existingdirents+3, NULL );
447
448     copyfile( "fee", "/bar/fum" );
449     checkfile( "bar/fum" );
450     comparefiles( "/fee", "bar/fum" );
451
452     diag_printf("<INFO>: cd bar\n");
453     err = chdir( "bar" );
454     if( err < 0 ) SHOW_RESULT( chdir, err );
455
456     checkcwd( "/bar" );
457     
458     diag_printf("<INFO>: rename /foo bundy\n");    
459     err = rename( "/foo", "bundy" );
460     if( err < 0 ) SHOW_RESULT( rename, err );
461     
462     listdir( "/", true, existingdirents+2, NULL );
463     listdir( "" , true, 4, NULL );
464
465     checkfile( "/bar/bundy" );
466     comparefiles("/fee", "bundy" );
467
468     // --------------------------------------------------------------
469
470     createfile( LONGNAME1, 123 );
471     checkfile( LONGNAME1 );
472     copyfile( LONGNAME1, LONGNAME2 );
473
474     listdir( "", false, 6, NULL );
475     
476     diag_printf("<INFO>: unlink " LONGNAME1 "\n");    
477     err = unlink( LONGNAME1 );
478     if( err < 0 ) SHOW_RESULT( unlink, err );
479
480     diag_printf("<INFO>: unlink " LONGNAME2 "\n");    
481     err = unlink( LONGNAME2 );
482     if( err < 0 ) SHOW_RESULT( unlink, err );
483     
484     
485     // --------------------------------------------------------------
486
487     diag_printf("<INFO>: unlink fee\n");    
488     err = unlink( "/fee" );
489     if( err < 0 ) SHOW_RESULT( unlink, err );
490
491     diag_printf("<INFO>: unlink fum\n");        
492     err = unlink( "fum" );
493     if( err < 0 ) SHOW_RESULT( unlink, err );
494
495     diag_printf("<INFO>: unlink /bar/bundy\n");        
496     err = unlink( "/bar/bundy" );
497     if( err < 0 ) SHOW_RESULT( unlink, err );
498
499     diag_printf("<INFO>: cd /\n");        
500     err = chdir( "/" );
501     if( err < 0 ) SHOW_RESULT( chdir, err );
502
503     checkcwd( "/" );
504     
505     diag_printf("<INFO>: rmdir /bar\n");        
506     err = rmdir( "/bar" );
507     if( err < 0 ) SHOW_RESULT( rmdir, err );
508     
509     listdir( "/", false, existingdirents, NULL );
510
511     // --------------------------------------------------------------
512
513     diag_printf("<INFO>: mount /jffs2 \n");
514     err = mount( CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, "/jffs2", "jffs2" );
515     if( err < 0 ) SHOW_RESULT( mount, err );    
516
517     createfile( "/jffs2/tinky", 456 );
518     copyfile( "/jffs2/tinky", "/jffs2/laalaa" );
519     checkfile( "/jffs2/tinky");
520     checkfile( "/jffs2/laalaa");
521     comparefiles( "/jffs2/tinky", "/jffs2/laalaa" );
522
523     diag_printf("<INFO>: cd /jffs2\n");    
524     err = chdir( "/jffs2" );
525     if( err < 0 ) SHOW_RESULT( chdir, err );
526
527     checkcwd( "/jffs2" );
528         
529     diag_printf("<INFO>: mkdir noonoo\n");    
530     err = mkdir( "noonoo", 0 );
531     if( err < 0 ) SHOW_RESULT( mkdir, err );
532
533     listdir( "." , true, existingdirents+3, NULL);
534
535     diag_printf("<INFO>: cd noonoo\n");
536     err = chdir( "noonoo" );
537     if( err < 0 ) SHOW_RESULT( chdir, err );
538
539     checkcwd( "/jffs2/noonoo" );
540     
541     createfile( "tinky", 678 );
542     checkfile( "tinky" );
543
544     createfile( "dipsy", 3456 );
545     checkfile( "dipsy" );
546     copyfile( "dipsy", "po" );
547     checkfile( "po" );
548     comparefiles( "dipsy", "po" );
549
550
551     /*for(i=0;i<2048;i++) {
552         diag_printf("<INFO>: churningchurningchurning................................ITERATION = %d\n", i);    
553         createfile( "churningchurningchurning", 4096 );
554         diag_printf("<INFO>: unlink churningchurningchurning\n");    
555         err = unlink( "churningchurningchurning" );
556         if( err < 0 ) SHOW_RESULT( unlink, err );
557     }*/
558
559
560     listdir( ".", true, 5, NULL );
561     listdir( "", true, 5, NULL );
562     listdir( "..", true, existingdirents+3, NULL );
563
564     // --------------------------------------------------------------
565
566     diag_printf("<INFO>: unlink tinky\n");    
567     err = unlink( "tinky" );
568     if( err < 0 ) SHOW_RESULT( unlink, err );
569
570     diag_printf("<INFO>: unlink dipsy\n");    
571     err = unlink( "dipsy" );
572     if( err < 0 ) SHOW_RESULT( unlink, err );
573
574     diag_printf("<INFO>: unlink po\n");    
575     err = unlink( "po" );
576     if( err < 0 ) SHOW_RESULT( unlink, err );
577
578     diag_printf("<INFO>: cd ..\n"); 
579     err = chdir( ".." );
580     if( err < 0 ) SHOW_RESULT( chdir, err );
581     checkcwd( "/jffs2" );
582     
583     diag_printf("<INFO>: rmdir noonoo\n"); 
584     err = rmdir( "noonoo" );
585     if( err < 0 ) SHOW_RESULT( rmdir, err );
586
587     // --------------------------------------------------------------
588
589     err = mkdir( "x", 0 );
590     if( err < 0 ) SHOW_RESULT( mkdir, err );
591     
592     err = mkdir( "x/y", 0 );
593     if( err < 0 ) SHOW_RESULT( mkdir, err );
594     
595     err = mkdir( "x/y/z", 0 );
596     if( err < 0 ) SHOW_RESULT( mkdir, err );
597
598     err = mkdir( "x/y/z/w", 0 );
599     if( err < 0 ) SHOW_RESULT( mkdir, err );
600     
601     diag_printf("<INFO>: cd /jffs2/x/y/z/w\n");
602     err = chdir( "/jffs2/x/y/z/w" );
603     if( err < 0 ) SHOW_RESULT( chdir, err );
604     checkcwd( "/jffs2/x/y/z/w" );
605
606     diag_printf("<INFO>: cd ..\n");
607     err = chdir( ".." );
608     if( err < 0 ) SHOW_RESULT( chdir, err );
609     checkcwd( "/jffs2/x/y/z" );
610     
611     diag_printf("<INFO>: cd .\n");
612     err = chdir( "." );
613     if( err < 0 ) SHOW_RESULT( chdir, err );
614     checkcwd( "/jffs2/x/y/z" );
615
616     diag_printf("<INFO>: cd ../../y\n");
617     err = chdir( "../../y" );
618     if( err < 0 ) SHOW_RESULT( chdir, err );
619     checkcwd( "/jffs2/x/y" );
620
621     diag_printf("<INFO>: cd ../..\n");
622     err = chdir( "../.." );
623     if( err < 0 ) SHOW_RESULT( chdir, err );
624     checkcwd( "/jffs2" );
625
626     diag_printf("<INFO>: rmdir x/y/z/w\n"); 
627     err = rmdir( "x/y/z/w" );
628     if( err < 0 ) SHOW_RESULT( rmdir, err );
629
630     diag_printf("<INFO>: rmdir x/y/z\n"); 
631     err = rmdir( "x/y/z" );
632     if( err < 0 ) SHOW_RESULT( rmdir, err );
633
634     diag_printf("<INFO>: rmdir x/y\n"); 
635     err = rmdir( "x/y" );
636     if( err < 0 ) SHOW_RESULT( rmdir, err );
637
638     diag_printf("<INFO>: rmdir x\n"); 
639     err = rmdir( "x" );
640     if( err < 0 ) SHOW_RESULT( rmdir, err );
641     
642     // --------------------------------------------------------------
643     
644     diag_printf("<INFO>: unlink tinky\n");    
645     err = unlink( "tinky" );
646     if( err < 0 ) SHOW_RESULT( unlink, err );
647
648     diag_printf("<INFO>: unlink laalaa\n");    
649     err = unlink( "laalaa" );
650     if( err < 0 ) SHOW_RESULT( unlink, err );
651
652     diag_printf("<INFO>: cd /\n");    
653     err = chdir( "/" );
654     if( err < 0 ) SHOW_RESULT( chdir, err );
655     checkcwd( "/" );
656     
657     diag_printf("<INFO>: umount /jffs2\n");    
658     err = umount( "/jffs2" );
659     if( err < 0 ) SHOW_RESULT( umount, err );    
660     
661     diag_printf("<INFO>: umount /\n");    
662     err = umount( "/" );
663     if( err < 0 ) SHOW_RESULT( umount, err );    
664     
665     CYG_TEST_PASS_FINISH("fileio1");
666 }
667
668 // -------------------------------------------------------------------------
669 // EOF fileio1.c