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