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);
146 char fullname[PATH_MAX];
151 strcpy(fullname, name );
152 if( !(name[0] == '/' && name[1] == 0 ) )
153 strcat(fullname, "/" );
155 else fullname[0] = 0;
157 strcat(fullname, entry->d_name );
159 err = stat( fullname, &sbuf );
162 if( errno == ENOSYS )
163 diag_printf(" <no status available>");
164 else SHOW_RESULT( stat, err );
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);
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 )
185 //==========================================================================
187 static void createfile( char *name, size_t size )
189 unsigned char buf[IOSIZE];
195 diag_printf("<INFO>: create file %s size %d\n",name,size);
197 err = access( name, F_OK );
198 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
200 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
202 fd = open( name, O_WRONLY|O_CREAT );
203 if( fd < 0 ) SHOW_RESULT( open, fd );
208 if ( len > IOSIZE ) len = IOSIZE;
210 wrote = write( fd, buf, len );
211 if( wrote != len ) SHOW_RESULT( write, wrote );
217 if( err < 0 ) SHOW_RESULT( close, err );
220 //==========================================================================
223 static void maxfile( char *name )
232 diag_printf("<INFO>: create maximal file %s\n",name);
234 err = access( name, F_OK );
235 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
237 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
239 fd = open( name, O_WRONLY|O_CREAT );
240 if( fd < 0 ) SHOW_RESULT( open, fd );
244 wrote = write( fd, buf, IOSIZE );
245 if( wrote < 0 ) SHOW_RESULT( write, wrote );
249 } while( wrote == IOSIZE );
251 diag_printf("<INFO>: file size == %d\n",size);
254 if( err < 0 ) SHOW_RESULT( close, err );
258 //==========================================================================
260 static void checkfile( char *name )
262 unsigned char buf[IOSIZE];
269 diag_printf("<INFO>: check file %s\n",name);
271 err = access( name, F_OK );
272 if( err != 0 ) SHOW_RESULT( access, err );
274 fd = open( name, O_RDONLY );
275 if( fd < 0 ) SHOW_RESULT( open, fd );
279 done = read( fd, buf, IOSIZE );
280 if( done < 0 ) SHOW_RESULT( read, done );
282 if( done == 0 ) break;
284 for( i = 0; i < done; i++ )
285 if( buf[i] != i%256 )
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");
296 if( err < 0 ) SHOW_RESULT( close, err );
299 //==========================================================================
301 static void copyfile( char *name2, char *name1 )
309 diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
311 err = access( name1, F_OK );
312 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
314 err = access( name2, F_OK );
315 if( err != 0 ) SHOW_RESULT( access, err );
317 fd1 = open( name1, O_WRONLY|O_CREAT );
318 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
320 fd2 = open( name2, O_RDONLY );
321 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
325 done = read( fd2, buf, IOSIZE );
326 if( done < 0 ) SHOW_RESULT( read, done );
328 if( done == 0 ) break;
330 wrote = write( fd1, buf, done );
331 if( wrote != done ) SHOW_RESULT( write, wrote );
333 if( wrote != done ) break;
337 if( err < 0 ) SHOW_RESULT( close, err );
340 if( err < 0 ) SHOW_RESULT( close, err );
344 //==========================================================================
346 static void comparefiles( char *name2, char *name1 )
352 ssize_t done1, done2;
355 diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
357 err = access( name1, F_OK );
358 if( err != 0 ) SHOW_RESULT( access, err );
360 err = access( name1, F_OK );
361 if( err != 0 ) SHOW_RESULT( access, err );
363 fd1 = open( name1, O_RDONLY );
364 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
366 fd2 = open( name2, O_RDONLY );
367 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
371 done1 = read( fd1, buf1, IOSIZE );
372 if( done1 < 0 ) SHOW_RESULT( read, done1 );
374 done2 = read( fd2, buf2, IOSIZE );
375 if( done2 < 0 ) SHOW_RESULT( read, done2 );
378 diag_printf("Files different sizes\n");
380 if( done1 == 0 ) break;
382 for( i = 0; i < done1; i++ )
383 if( buf1[i] != buf2[i] )
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");
391 if( err < 0 ) SHOW_RESULT( close, err );
394 if( err < 0 ) SHOW_RESULT( close, err );
398 //==========================================================================
400 void checkcwd( const char *cwd )
402 static char cwdbuf[PATH_MAX];
405 ret = getcwd( cwdbuf, sizeof(cwdbuf));
406 if( ret == NULL ) SHOW_RESULT( getcwd, ret );
408 if( strcmp( cwdbuf, cwd ) != 0 )
410 diag_printf( "cwdbuf %s cwd %s\n",cwdbuf, cwd );
411 CYG_TEST_FAIL( "Current directory mismatch");
415 //==========================================================================
418 int main( int argc, char **argv )
422 int existingdirents=-1;
426 // --------------------------------------------------------------
428 err = mount( CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, "/", "jffs2" );
429 if( err < 0 ) SHOW_RESULT( mount, err );
432 if( err < 0 ) SHOW_RESULT( chdir, err );
436 listdir( "/", true, -1, &existingdirents );
437 if ( existingdirents < 2 )
438 CYG_TEST_FAIL("Not enough dir entries\n");
440 // --------------------------------------------------------------
442 createfile( "/foo", 202 );
444 copyfile( "foo", "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 );
451 listdir( "/" , true, existingdirents+3, NULL );
453 copyfile( "fee", "/bar/fum" );
454 checkfile( "bar/fum" );
455 comparefiles( "/fee", "bar/fum" );
457 diag_printf("<INFO>: cd bar\n");
458 err = chdir( "bar" );
459 if( err < 0 ) SHOW_RESULT( chdir, err );
463 diag_printf("<INFO>: rename /foo bundy\n");
464 err = rename( "/foo", "bundy" );
465 if( err < 0 ) SHOW_RESULT( rename, err );
467 listdir( "/", true, existingdirents+2, NULL );
468 listdir( "" , true, 4, NULL );
470 checkfile( "/bar/bundy" );
471 comparefiles("/fee", "bundy" );
473 // --------------------------------------------------------------
475 createfile( LONGNAME1, 123 );
476 checkfile( LONGNAME1 );
477 copyfile( LONGNAME1, LONGNAME2 );
479 listdir( "", false, 6, NULL );
481 diag_printf("<INFO>: unlink " LONGNAME1 "\n");
482 err = unlink( LONGNAME1 );
483 if( err < 0 ) SHOW_RESULT( unlink, err );
485 diag_printf("<INFO>: unlink " LONGNAME2 "\n");
486 err = unlink( LONGNAME2 );
487 if( err < 0 ) SHOW_RESULT( unlink, err );
490 // --------------------------------------------------------------
492 diag_printf("<INFO>: unlink fee\n");
493 err = unlink( "/fee" );
494 if( err < 0 ) SHOW_RESULT( unlink, err );
496 diag_printf("<INFO>: unlink fum\n");
497 err = unlink( "fum" );
498 if( err < 0 ) SHOW_RESULT( unlink, err );
500 diag_printf("<INFO>: unlink /bar/bundy\n");
501 err = unlink( "/bar/bundy" );
502 if( err < 0 ) SHOW_RESULT( unlink, err );
504 diag_printf("<INFO>: cd /\n");
506 if( err < 0 ) SHOW_RESULT( chdir, err );
510 diag_printf("<INFO>: rmdir /bar\n");
511 err = rmdir( "/bar" );
512 if( err < 0 ) SHOW_RESULT( rmdir, err );
514 listdir( "/", false, existingdirents, NULL );
516 // --------------------------------------------------------------
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 );
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" );
528 diag_printf("<INFO>: cd /jffs2\n");
529 err = chdir( "/jffs2" );
530 if( err < 0 ) SHOW_RESULT( chdir, err );
532 checkcwd( "/jffs2" );
534 diag_printf("<INFO>: mkdir noonoo\n");
535 err = mkdir( "noonoo", 0 );
536 if( err < 0 ) SHOW_RESULT( mkdir, err );
538 listdir( "." , true, existingdirents+3, NULL);
540 diag_printf("<INFO>: cd noonoo\n");
541 err = chdir( "noonoo" );
542 if( err < 0 ) SHOW_RESULT( chdir, err );
544 checkcwd( "/jffs2/noonoo" );
546 createfile( "tinky", 678 );
547 checkfile( "tinky" );
549 createfile( "dipsy", 3456 );
550 checkfile( "dipsy" );
551 copyfile( "dipsy", "po" );
553 comparefiles( "dipsy", "po" );
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 );
565 listdir( ".", true, 5, NULL );
566 listdir( "", true, 5, NULL );
567 listdir( "..", true, existingdirents+3, NULL );
569 // --------------------------------------------------------------
571 diag_printf("<INFO>: unlink tinky\n");
572 err = unlink( "tinky" );
573 if( err < 0 ) SHOW_RESULT( unlink, err );
575 diag_printf("<INFO>: unlink dipsy\n");
576 err = unlink( "dipsy" );
577 if( err < 0 ) SHOW_RESULT( unlink, err );
579 diag_printf("<INFO>: unlink po\n");
580 err = unlink( "po" );
581 if( err < 0 ) SHOW_RESULT( unlink, err );
583 diag_printf("<INFO>: cd ..\n");
585 if( err < 0 ) SHOW_RESULT( chdir, err );
586 checkcwd( "/jffs2" );
588 diag_printf("<INFO>: rmdir noonoo\n");
589 err = rmdir( "noonoo" );
590 if( err < 0 ) SHOW_RESULT( rmdir, err );
592 // --------------------------------------------------------------
594 err = mkdir( "x", 0 );
595 if( err < 0 ) SHOW_RESULT( mkdir, err );
597 err = mkdir( "x/y", 0 );
598 if( err < 0 ) SHOW_RESULT( mkdir, err );
600 err = mkdir( "x/y/z", 0 );
601 if( err < 0 ) SHOW_RESULT( mkdir, err );
603 err = mkdir( "x/y/z/w", 0 );
604 if( err < 0 ) SHOW_RESULT( mkdir, err );
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" );
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 .\n");
618 if( err < 0 ) SHOW_RESULT( chdir, err );
619 checkcwd( "/jffs2/x/y/z" );
621 diag_printf("<INFO>: cd ../../y\n");
622 err = chdir( "../../y" );
623 if( err < 0 ) SHOW_RESULT( chdir, err );
624 checkcwd( "/jffs2/x/y" );
626 diag_printf("<INFO>: cd ../..\n");
627 err = chdir( "../.." );
628 if( err < 0 ) SHOW_RESULT( chdir, err );
629 checkcwd( "/jffs2" );
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 );
635 diag_printf("<INFO>: rmdir x/y/z\n");
636 err = rmdir( "x/y/z" );
637 if( err < 0 ) SHOW_RESULT( rmdir, err );
639 diag_printf("<INFO>: rmdir x/y\n");
640 err = rmdir( "x/y" );
641 if( err < 0 ) SHOW_RESULT( rmdir, err );
643 diag_printf("<INFO>: rmdir x\n");
645 if( err < 0 ) SHOW_RESULT( rmdir, err );
647 // --------------------------------------------------------------
649 diag_printf("<INFO>: unlink tinky\n");
650 err = unlink( "tinky" );
651 if( err < 0 ) SHOW_RESULT( unlink, err );
653 diag_printf("<INFO>: unlink laalaa\n");
654 err = unlink( "laalaa" );
655 if( err < 0 ) SHOW_RESULT( unlink, err );
657 diag_printf("<INFO>: cd /\n");
659 if( err < 0 ) SHOW_RESULT( chdir, err );
662 diag_printf("<INFO>: umount /jffs2\n");
663 err = umount( "/jffs2" );
664 if( err < 0 ) SHOW_RESULT( umount, err );
666 diag_printf("<INFO>: umount /\n");
668 if( err < 0 ) SHOW_RESULT( umount, err );
670 CYG_TEST_PASS_FINISH("jffs2_1");
673 // -------------------------------------------------------------------------