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.
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.
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
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.
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.
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.
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####
44 // Contributors: nickg
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
56 //####DESCRIPTIONEND####
58 //==========================================================================
60 #include <pkgconf/hal.h>
61 #include <pkgconf/kernel.h>
62 #include <pkgconf/io_fileio.h>
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>
76 #include <cyg/fileio/fileio.h>
78 #include <cyg/infra/testcase.h>
79 #include <cyg/infra/diag.h> // HAL polled output
81 #include <pkgconf/fs_jffs2.h> // Address of JFFS2
83 //==========================================================================
86 MTAB_ENTRY( jffs2_mte1,
89 CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1,
93 //==========================================================================
95 #define SHOW_RESULT( _fn, _res ) \
96 diag_printf("<FAIL>: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
98 //==========================================================================
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"
106 //==========================================================================
108 #ifndef CYGPKG_LIBC_STRING
110 char *strcat( char *s1, const char *s2 )
114 while( (*s1++ = *s2++) != 0);
120 //==========================================================================
122 static void listdir( char *name, int statp, int numexpected, int *numgot )
128 diag_printf("<INFO>: reading directory %s\n",name);
130 dirp = opendir( name );
131 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
135 struct dirent *entry = readdir( dirp );
140 diag_printf("<INFO>: entry %14s",entry->d_name);
143 char fullname[PATH_MAX];
148 strcpy(fullname, name );
149 if( !(name[0] == '/' && name[1] == 0 ) )
150 strcat(fullname, "/" );
152 else fullname[0] = 0;
154 strcat(fullname, entry->d_name );
156 err = stat( fullname, &sbuf );
159 if( errno == ENOSYS )
160 diag_printf(" <no status available>");
161 else SHOW_RESULT( stat, err );
165 diag_printf(" [mode %08x ino %08x nlink %d size %d]",
166 sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size);
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 )
181 //==========================================================================
183 static void createfile( char *name, size_t size )
191 diag_printf("<INFO>: create file %s size %d\n",name,size);
193 err = access( name, F_OK );
194 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
196 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
198 fd = open( name, O_WRONLY|O_CREAT );
199 if( fd < 0 ) SHOW_RESULT( open, fd );
204 if ( len > IOSIZE ) len = IOSIZE;
206 wrote = write( fd, buf, len );
207 if( wrote != len ) SHOW_RESULT( write, wrote );
213 if( err < 0 ) SHOW_RESULT( close, err );
216 //==========================================================================
219 static void maxfile( char *name )
228 diag_printf("<INFO>: create maximal file %s\n",name);
230 err = access( name, F_OK );
231 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
233 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
235 fd = open( name, O_WRONLY|O_CREAT );
236 if( fd < 0 ) SHOW_RESULT( open, fd );
240 wrote = write( fd, buf, IOSIZE );
241 if( wrote < 0 ) SHOW_RESULT( write, wrote );
245 } while( wrote == IOSIZE );
247 diag_printf("<INFO>: file size == %d\n",size);
250 if( err < 0 ) SHOW_RESULT( close, err );
254 //==========================================================================
256 static void checkfile( char *name )
265 diag_printf("<INFO>: check file %s\n",name);
267 err = access( name, F_OK );
268 if( err != 0 ) SHOW_RESULT( access, err );
270 fd = open( name, O_RDONLY );
271 if( fd < 0 ) SHOW_RESULT( open, fd );
275 done = read( fd, buf, IOSIZE );
276 if( done < 0 ) SHOW_RESULT( read, done );
278 if( done == 0 ) break;
280 for( i = 0; i < done; i++ )
281 if( buf[i] != i%256 )
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");
291 if( err < 0 ) SHOW_RESULT( close, err );
294 //==========================================================================
296 static void copyfile( char *name2, char *name1 )
304 diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
306 err = access( name1, F_OK );
307 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
309 err = access( name2, F_OK );
310 if( err != 0 ) SHOW_RESULT( access, err );
312 fd1 = open( name1, O_WRONLY|O_CREAT );
313 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
315 fd2 = open( name2, O_RDONLY );
316 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
320 done = read( fd2, buf, IOSIZE );
321 if( done < 0 ) SHOW_RESULT( read, done );
323 if( done == 0 ) break;
325 wrote = write( fd1, buf, done );
326 if( wrote != done ) SHOW_RESULT( write, wrote );
328 if( wrote != done ) break;
332 if( err < 0 ) SHOW_RESULT( close, err );
335 if( err < 0 ) SHOW_RESULT( close, err );
339 //==========================================================================
341 static void comparefiles( char *name2, char *name1 )
347 ssize_t done1, done2;
350 diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
352 err = access( name1, F_OK );
353 if( err != 0 ) SHOW_RESULT( access, err );
355 err = access( name1, F_OK );
356 if( err != 0 ) SHOW_RESULT( access, err );
358 fd1 = open( name1, O_RDONLY );
359 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
361 fd2 = open( name2, O_RDONLY );
362 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
366 done1 = read( fd1, buf1, IOSIZE );
367 if( done1 < 0 ) SHOW_RESULT( read, done1 );
369 done2 = read( fd2, buf2, IOSIZE );
370 if( done2 < 0 ) SHOW_RESULT( read, done2 );
373 diag_printf("Files different sizes\n");
375 if( done1 == 0 ) break;
377 for( i = 0; i < done1; i++ )
378 if( buf1[i] != buf2[i] )
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");
386 if( err < 0 ) SHOW_RESULT( close, err );
389 if( err < 0 ) SHOW_RESULT( close, err );
393 //==========================================================================
395 void checkcwd( const char *cwd )
397 static char cwdbuf[PATH_MAX];
400 ret = getcwd( cwdbuf, sizeof(cwdbuf));
401 if( ret == NULL ) SHOW_RESULT( getcwd, ret );
403 if( strcmp( cwdbuf, cwd ) != 0 )
405 diag_printf( "cwdbuf %s cwd %s\n",cwdbuf, cwd );
406 CYG_TEST_FAIL( "Current directory mismatch");
410 //==========================================================================
413 int main( int argc, char **argv )
417 int existingdirents=-1;
421 // --------------------------------------------------------------
423 err = mount( CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, "/", "jffs2" );
424 if( err < 0 ) SHOW_RESULT( mount, err );
427 if( err < 0 ) SHOW_RESULT( chdir, err );
431 listdir( "/", true, -1, &existingdirents );
432 if ( existingdirents < 2 )
433 CYG_TEST_FAIL("Not enough dir entries\n");
435 // --------------------------------------------------------------
437 createfile( "/foo", 202 );
439 copyfile( "foo", "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 );
446 listdir( "/" , true, existingdirents+3, NULL );
448 copyfile( "fee", "/bar/fum" );
449 checkfile( "bar/fum" );
450 comparefiles( "/fee", "bar/fum" );
452 diag_printf("<INFO>: cd bar\n");
453 err = chdir( "bar" );
454 if( err < 0 ) SHOW_RESULT( chdir, err );
458 diag_printf("<INFO>: rename /foo bundy\n");
459 err = rename( "/foo", "bundy" );
460 if( err < 0 ) SHOW_RESULT( rename, err );
462 listdir( "/", true, existingdirents+2, NULL );
463 listdir( "" , true, 4, NULL );
465 checkfile( "/bar/bundy" );
466 comparefiles("/fee", "bundy" );
468 // --------------------------------------------------------------
470 createfile( LONGNAME1, 123 );
471 checkfile( LONGNAME1 );
472 copyfile( LONGNAME1, LONGNAME2 );
474 listdir( "", false, 6, NULL );
476 diag_printf("<INFO>: unlink " LONGNAME1 "\n");
477 err = unlink( LONGNAME1 );
478 if( err < 0 ) SHOW_RESULT( unlink, err );
480 diag_printf("<INFO>: unlink " LONGNAME2 "\n");
481 err = unlink( LONGNAME2 );
482 if( err < 0 ) SHOW_RESULT( unlink, err );
485 // --------------------------------------------------------------
487 diag_printf("<INFO>: unlink fee\n");
488 err = unlink( "/fee" );
489 if( err < 0 ) SHOW_RESULT( unlink, err );
491 diag_printf("<INFO>: unlink fum\n");
492 err = unlink( "fum" );
493 if( err < 0 ) SHOW_RESULT( unlink, err );
495 diag_printf("<INFO>: unlink /bar/bundy\n");
496 err = unlink( "/bar/bundy" );
497 if( err < 0 ) SHOW_RESULT( unlink, err );
499 diag_printf("<INFO>: cd /\n");
501 if( err < 0 ) SHOW_RESULT( chdir, err );
505 diag_printf("<INFO>: rmdir /bar\n");
506 err = rmdir( "/bar" );
507 if( err < 0 ) SHOW_RESULT( rmdir, err );
509 listdir( "/", false, existingdirents, NULL );
511 // --------------------------------------------------------------
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 );
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" );
523 diag_printf("<INFO>: cd /jffs2\n");
524 err = chdir( "/jffs2" );
525 if( err < 0 ) SHOW_RESULT( chdir, err );
527 checkcwd( "/jffs2" );
529 diag_printf("<INFO>: mkdir noonoo\n");
530 err = mkdir( "noonoo", 0 );
531 if( err < 0 ) SHOW_RESULT( mkdir, err );
533 listdir( "." , true, existingdirents+3, NULL);
535 diag_printf("<INFO>: cd noonoo\n");
536 err = chdir( "noonoo" );
537 if( err < 0 ) SHOW_RESULT( chdir, err );
539 checkcwd( "/jffs2/noonoo" );
541 createfile( "tinky", 678 );
542 checkfile( "tinky" );
544 createfile( "dipsy", 3456 );
545 checkfile( "dipsy" );
546 copyfile( "dipsy", "po" );
548 comparefiles( "dipsy", "po" );
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 );
560 listdir( ".", true, 5, NULL );
561 listdir( "", true, 5, NULL );
562 listdir( "..", true, existingdirents+3, NULL );
564 // --------------------------------------------------------------
566 diag_printf("<INFO>: unlink tinky\n");
567 err = unlink( "tinky" );
568 if( err < 0 ) SHOW_RESULT( unlink, err );
570 diag_printf("<INFO>: unlink dipsy\n");
571 err = unlink( "dipsy" );
572 if( err < 0 ) SHOW_RESULT( unlink, err );
574 diag_printf("<INFO>: unlink po\n");
575 err = unlink( "po" );
576 if( err < 0 ) SHOW_RESULT( unlink, err );
578 diag_printf("<INFO>: cd ..\n");
580 if( err < 0 ) SHOW_RESULT( chdir, err );
581 checkcwd( "/jffs2" );
583 diag_printf("<INFO>: rmdir noonoo\n");
584 err = rmdir( "noonoo" );
585 if( err < 0 ) SHOW_RESULT( rmdir, err );
587 // --------------------------------------------------------------
589 err = mkdir( "x", 0 );
590 if( err < 0 ) SHOW_RESULT( mkdir, err );
592 err = mkdir( "x/y", 0 );
593 if( err < 0 ) SHOW_RESULT( mkdir, err );
595 err = mkdir( "x/y/z", 0 );
596 if( err < 0 ) SHOW_RESULT( mkdir, err );
598 err = mkdir( "x/y/z/w", 0 );
599 if( err < 0 ) SHOW_RESULT( mkdir, err );
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" );
606 diag_printf("<INFO>: cd ..\n");
608 if( err < 0 ) SHOW_RESULT( chdir, err );
609 checkcwd( "/jffs2/x/y/z" );
611 diag_printf("<INFO>: cd .\n");
613 if( err < 0 ) SHOW_RESULT( chdir, err );
614 checkcwd( "/jffs2/x/y/z" );
616 diag_printf("<INFO>: cd ../../y\n");
617 err = chdir( "../../y" );
618 if( err < 0 ) SHOW_RESULT( chdir, err );
619 checkcwd( "/jffs2/x/y" );
621 diag_printf("<INFO>: cd ../..\n");
622 err = chdir( "../.." );
623 if( err < 0 ) SHOW_RESULT( chdir, err );
624 checkcwd( "/jffs2" );
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 );
630 diag_printf("<INFO>: rmdir x/y/z\n");
631 err = rmdir( "x/y/z" );
632 if( err < 0 ) SHOW_RESULT( rmdir, err );
634 diag_printf("<INFO>: rmdir x/y\n");
635 err = rmdir( "x/y" );
636 if( err < 0 ) SHOW_RESULT( rmdir, err );
638 diag_printf("<INFO>: rmdir x\n");
640 if( err < 0 ) SHOW_RESULT( rmdir, err );
642 // --------------------------------------------------------------
644 diag_printf("<INFO>: unlink tinky\n");
645 err = unlink( "tinky" );
646 if( err < 0 ) SHOW_RESULT( unlink, err );
648 diag_printf("<INFO>: unlink laalaa\n");
649 err = unlink( "laalaa" );
650 if( err < 0 ) SHOW_RESULT( unlink, err );
652 diag_printf("<INFO>: cd /\n");
654 if( err < 0 ) SHOW_RESULT( chdir, err );
657 diag_printf("<INFO>: umount /jffs2\n");
658 err = umount( "/jffs2" );
659 if( err < 0 ) SHOW_RESULT( umount, err );
661 diag_printf("<INFO>: umount /\n");
663 if( err < 0 ) SHOW_RESULT( umount, err );
665 CYG_TEST_PASS_FINISH("fileio1");
668 // -------------------------------------------------------------------------