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