1 //==========================================================================
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
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.
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
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.
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.
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.
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####
45 // Contributors: nickg
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
57 //####DESCRIPTIONEND####
59 //==========================================================================
61 #include <pkgconf/hal.h>
62 #include <pkgconf/kernel.h>
63 #include <pkgconf/io_fileio.h>
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>
78 #include <cyg/fileio/fileio.h>
80 #include <cyg/infra/testcase.h>
81 #include <cyg/infra/diag.h> // HAL polled output
83 #include <pkgconf/fs_jffs2.h> // Address of JFFS2
85 //==========================================================================
88 MTAB_ENTRY( jffs2_mte1,
91 CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1,
95 //==========================================================================
97 #define SHOW_RESULT( _fn, _res ) \
98 diag_printf("<FAIL>: " #_fn "() returned %ld %s\n", \
99 (unsigned long)_res, _res<0?strerror(errno):"");
101 //==========================================================================
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"
109 //==========================================================================
111 #ifndef CYGPKG_LIBC_STRING
113 char *strcat( char *s1, const char *s2 )
117 while( (*s1++ = *s2++) != 0);
123 //==========================================================================
125 static void listdir( char *name, int statp, int numexpected, int *numgot )
131 diag_printf("<INFO>: reading directory %s\n",name);
133 dirp = opendir( name );
134 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
138 struct dirent *entry = readdir( dirp );
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);
149 char fullname[PATH_MAX];
154 strcpy(fullname, name );
155 if( !(name[0] == '/' && name[1] == 0 ) )
156 strcat(fullname, "/" );
158 else fullname[0] = 0;
160 strcat(fullname, entry->d_name );
162 err = stat( fullname, &sbuf );
165 if( errno == ENOSYS )
166 diag_printf(" <no status available>");
167 else SHOW_RESULT( stat, err );
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);
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");
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 )
192 //==========================================================================
194 static void createfile( char *name, size_t size )
196 unsigned char buf[IOSIZE];
202 diag_printf("<INFO>: create file %s size %d\n",name,size);
204 err = access( name, F_OK );
205 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
207 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
209 fd = open( name, O_WRONLY|O_CREAT );
210 if( fd < 0 ) SHOW_RESULT( open, fd );
215 if ( len > IOSIZE ) len = IOSIZE;
217 wrote = write( fd, buf, len );
218 if( wrote != len ) SHOW_RESULT( write, wrote );
224 if( err < 0 ) SHOW_RESULT( close, err );
227 //==========================================================================
230 static void maxfile( char *name )
239 diag_printf("<INFO>: create maximal file %s\n",name);
241 err = access( name, F_OK );
242 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
244 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
246 fd = open( name, O_WRONLY|O_CREAT );
247 if( fd < 0 ) SHOW_RESULT( open, fd );
251 wrote = write( fd, buf, IOSIZE );
252 if( wrote < 0 ) SHOW_RESULT( write, wrote );
256 } while( wrote == IOSIZE );
258 diag_printf("<INFO>: file size == %d\n",size);
261 if( err < 0 ) SHOW_RESULT( close, err );
265 //==========================================================================
267 static void checkfile( char *name )
269 unsigned char buf[IOSIZE];
276 diag_printf("<INFO>: check file %s\n",name);
278 err = access( name, F_OK );
279 if( err != 0 ) SHOW_RESULT( access, err );
281 fd = open( name, O_RDONLY );
282 if( fd < 0 ) SHOW_RESULT( open, fd );
286 done = read( fd, buf, IOSIZE );
287 if( done < 0 ) SHOW_RESULT( read, done );
289 if( done == 0 ) break;
291 for( i = 0; i < done; i++ )
292 if( buf[i] != i%256 )
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");
303 if( err < 0 ) SHOW_RESULT( close, err );
306 //==========================================================================
308 static void copyfile( char *name2, char *name1 )
316 diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
318 err = access( name1, F_OK );
319 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
321 err = access( name2, F_OK );
322 if( err != 0 ) SHOW_RESULT( access, err );
324 fd1 = open( name1, O_WRONLY|O_CREAT );
325 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
327 fd2 = open( name2, O_RDONLY );
328 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
332 done = read( fd2, buf, IOSIZE );
333 if( done < 0 ) SHOW_RESULT( read, done );
335 if( done == 0 ) break;
337 wrote = write( fd1, buf, done );
338 if( wrote != done ) SHOW_RESULT( write, wrote );
340 if( wrote != done ) break;
344 if( err < 0 ) SHOW_RESULT( close, err );
347 if( err < 0 ) SHOW_RESULT( close, err );
351 //==========================================================================
353 static void comparefiles( char *name2, char *name1 )
359 ssize_t done1, done2;
362 diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
364 err = access( name1, F_OK );
365 if( err != 0 ) SHOW_RESULT( access, err );
367 err = access( name1, F_OK );
368 if( err != 0 ) SHOW_RESULT( access, err );
370 fd1 = open( name1, O_RDONLY );
371 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
373 fd2 = open( name2, O_RDONLY );
374 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
378 done1 = read( fd1, buf1, IOSIZE );
379 if( done1 < 0 ) SHOW_RESULT( read, done1 );
381 done2 = read( fd2, buf2, IOSIZE );
382 if( done2 < 0 ) SHOW_RESULT( read, done2 );
385 diag_printf("Files different sizes\n");
387 if( done1 == 0 ) break;
389 for( i = 0; i < done1; i++ )
390 if( buf1[i] != buf2[i] )
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");
398 if( err < 0 ) SHOW_RESULT( close, err );
401 if( err < 0 ) SHOW_RESULT( close, err );
405 //==========================================================================
407 void checkcwd( const char *cwd )
409 static char cwdbuf[PATH_MAX];
412 ret = getcwd( cwdbuf, sizeof(cwdbuf));
413 if( ret == NULL ) SHOW_RESULT( getcwd, ret );
415 if( strcmp( cwdbuf, cwd ) != 0 )
417 diag_printf( "cwdbuf %s cwd %s\n",cwdbuf, cwd );
418 CYG_TEST_FAIL( "Current directory mismatch");
422 //==========================================================================
425 int main( int argc, char **argv )
429 int existingdirents=-1;
433 // --------------------------------------------------------------
435 err = mount( CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, "/", "jffs2" );
436 if( err < 0 ) SHOW_RESULT( mount, err );
439 if( err < 0 ) SHOW_RESULT( chdir, err );
443 listdir( "/", true, -1, &existingdirents );
444 if ( existingdirents < 2 )
445 CYG_TEST_FAIL("Not enough dir entries\n");
447 // --------------------------------------------------------------
449 createfile( "/foo", 202 );
451 copyfile( "foo", "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 );
458 listdir( "/" , true, existingdirents+3, NULL );
460 copyfile( "fee", "/bar/fum" );
461 checkfile( "bar/fum" );
462 comparefiles( "/fee", "bar/fum" );
464 diag_printf("<INFO>: cd bar\n");
465 err = chdir( "bar" );
466 if( err < 0 ) SHOW_RESULT( chdir, err );
470 diag_printf("<INFO>: rename /foo bundy\n");
471 err = rename( "/foo", "bundy" );
472 if( err < 0 ) SHOW_RESULT( rename, err );
474 listdir( "/", true, existingdirents+2, NULL );
475 listdir( "" , true, 4, NULL );
477 checkfile( "/bar/bundy" );
478 comparefiles("/fee", "bundy" );
480 // --------------------------------------------------------------
482 createfile( LONGNAME1, 123 );
483 checkfile( LONGNAME1 );
484 copyfile( LONGNAME1, LONGNAME2 );
486 listdir( "", false, 6, NULL );
488 diag_printf("<INFO>: unlink " LONGNAME1 "\n");
489 err = unlink( LONGNAME1 );
490 if( err < 0 ) SHOW_RESULT( unlink, err );
492 diag_printf("<INFO>: unlink " LONGNAME2 "\n");
493 err = unlink( LONGNAME2 );
494 if( err < 0 ) SHOW_RESULT( unlink, err );
497 // --------------------------------------------------------------
499 diag_printf("<INFO>: unlink fee\n");
500 err = unlink( "/fee" );
501 if( err < 0 ) SHOW_RESULT( unlink, err );
503 diag_printf("<INFO>: unlink fum\n");
504 err = unlink( "fum" );
505 if( err < 0 ) SHOW_RESULT( unlink, err );
507 diag_printf("<INFO>: unlink /bar/bundy\n");
508 err = unlink( "/bar/bundy" );
509 if( err < 0 ) SHOW_RESULT( unlink, err );
511 diag_printf("<INFO>: cd /\n");
513 if( err < 0 ) SHOW_RESULT( chdir, err );
517 diag_printf("<INFO>: rmdir /bar\n");
518 err = rmdir( "/bar" );
519 if( err < 0 ) SHOW_RESULT( rmdir, err );
521 listdir( "/", false, existingdirents, NULL );
523 // --------------------------------------------------------------
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 );
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" );
535 diag_printf("<INFO>: cd /jffs2\n");
536 err = chdir( "/jffs2" );
537 if( err < 0 ) SHOW_RESULT( chdir, err );
539 checkcwd( "/jffs2" );
541 diag_printf("<INFO>: mkdir noonoo\n");
542 err = mkdir( "noonoo", 0 );
543 if( err < 0 ) SHOW_RESULT( mkdir, err );
545 listdir( "." , true, existingdirents+3, NULL);
547 diag_printf("<INFO>: cd noonoo\n");
548 err = chdir( "noonoo" );
549 if( err < 0 ) SHOW_RESULT( chdir, err );
551 checkcwd( "/jffs2/noonoo" );
553 createfile( "tinky", 678 );
554 checkfile( "tinky" );
556 createfile( "dipsy", 3456 );
557 checkfile( "dipsy" );
558 copyfile( "dipsy", "po" );
560 comparefiles( "dipsy", "po" );
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 );
572 listdir( ".", true, 5, NULL );
573 listdir( "", true, 5, NULL );
574 listdir( "..", true, existingdirents+3, NULL );
576 // --------------------------------------------------------------
578 diag_printf("<INFO>: unlink tinky\n");
579 err = unlink( "tinky" );
580 if( err < 0 ) SHOW_RESULT( unlink, err );
582 diag_printf("<INFO>: unlink dipsy\n");
583 err = unlink( "dipsy" );
584 if( err < 0 ) SHOW_RESULT( unlink, err );
586 diag_printf("<INFO>: unlink po\n");
587 err = unlink( "po" );
588 if( err < 0 ) SHOW_RESULT( unlink, err );
590 diag_printf("<INFO>: cd ..\n");
592 if( err < 0 ) SHOW_RESULT( chdir, err );
593 checkcwd( "/jffs2" );
595 diag_printf("<INFO>: rmdir noonoo\n");
596 err = rmdir( "noonoo" );
597 if( err < 0 ) SHOW_RESULT( rmdir, err );
599 // --------------------------------------------------------------
601 err = mkdir( "x", 0 );
602 if( err < 0 ) SHOW_RESULT( mkdir, err );
604 err = mkdir( "x/y", 0 );
605 if( err < 0 ) SHOW_RESULT( mkdir, err );
607 err = mkdir( "x/y/z", 0 );
608 if( err < 0 ) SHOW_RESULT( mkdir, err );
610 err = mkdir( "x/y/z/w", 0 );
611 if( err < 0 ) SHOW_RESULT( mkdir, err );
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" );
618 diag_printf("<INFO>: cd ..\n");
620 if( err < 0 ) SHOW_RESULT( chdir, err );
621 checkcwd( "/jffs2/x/y/z" );
623 diag_printf("<INFO>: cd .\n");
625 if( err < 0 ) SHOW_RESULT( chdir, err );
626 checkcwd( "/jffs2/x/y/z" );
628 diag_printf("<INFO>: cd ../../y\n");
629 err = chdir( "../../y" );
630 if( err < 0 ) SHOW_RESULT( chdir, err );
631 checkcwd( "/jffs2/x/y" );
633 diag_printf("<INFO>: cd ../..\n");
634 err = chdir( "../.." );
635 if( err < 0 ) SHOW_RESULT( chdir, err );
636 checkcwd( "/jffs2" );
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 );
642 diag_printf("<INFO>: rmdir x/y/z\n");
643 err = rmdir( "x/y/z" );
644 if( err < 0 ) SHOW_RESULT( rmdir, err );
646 diag_printf("<INFO>: rmdir x/y\n");
647 err = rmdir( "x/y" );
648 if( err < 0 ) SHOW_RESULT( rmdir, err );
650 diag_printf("<INFO>: rmdir x\n");
652 if( err < 0 ) SHOW_RESULT( rmdir, err );
654 // --------------------------------------------------------------
656 diag_printf("<INFO>: unlink tinky\n");
657 err = unlink( "tinky" );
658 if( err < 0 ) SHOW_RESULT( unlink, err );
660 diag_printf("<INFO>: unlink laalaa\n");
661 err = unlink( "laalaa" );
662 if( err < 0 ) SHOW_RESULT( unlink, err );
664 diag_printf("<INFO>: cd /\n");
666 if( err < 0 ) SHOW_RESULT( chdir, err );
669 diag_printf("<INFO>: umount /jffs2\n");
670 err = umount( "/jffs2" );
671 if( err < 0 ) SHOW_RESULT( umount, err );
673 diag_printf("<INFO>: umount /\n");
675 if( err < 0 ) SHOW_RESULT( umount, err );
677 CYG_TEST_PASS_FINISH("jffs2_1");
680 // -------------------------------------------------------------------------