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
76 #include <cyg/fileio/fileio.h>
78 #include <cyg/infra/testcase.h>
79 #include <cyg/infra/diag.h> // HAL polled output
81 //==========================================================================
84 MTAB_ENTRY( ramfs_mte1,
91 //==========================================================================
93 #define SHOW_RESULT( _fn, _res ) \
94 diag_printf("<FAIL>: " #_fn "() returned %ld %s\n", (long)_res, _res<0?strerror(errno):"");
96 //==========================================================================
100 #define LONGNAME1 "long_file_name_that_should_take_up_more_than_one_directory_entry_1"
101 #define LONGNAME2 "long_file_name_that_should_take_up_more_than_one_directory_entry_2"
104 //==========================================================================
106 #ifndef CYGPKG_LIBC_STRING
108 char *strcat( char *s1, const char *s2 )
112 while( (*s1++ = *s2++) != 0);
118 //==========================================================================
120 static void listdir( char *name, int statp, int numexpected, int *numgot )
126 diag_printf("<INFO>: reading directory %s\n",name);
128 dirp = opendir( name );
129 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
133 struct dirent *entry = readdir( dirp );
138 diag_printf("<INFO>: entry %14s",entry->d_name);
141 char fullname[PATH_MAX];
146 strcpy(fullname, name );
147 if( !(name[0] == '/' && name[1] == 0 ) )
148 strcat(fullname, "/" );
150 else fullname[0] = 0;
152 strcat(fullname, entry->d_name );
154 err = stat( fullname, &sbuf );
157 if( errno == ENOSYS )
158 diag_printf(" <no status available>");
159 else SHOW_RESULT( stat, err );
163 diag_printf(" [mode %08x ino %08x nlink %d size %ld]",
164 sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,
165 (unsigned long) sbuf.st_size);
172 err = closedir( dirp );
173 if( err < 0 ) SHOW_RESULT( stat, err );
174 if (numexpected >= 0 && num != numexpected)
175 CYG_TEST_FAIL("Wrong number of dir entries\n");
176 if ( numgot != NULL )
180 //==========================================================================
182 static void createfile( char *name, size_t size )
190 diag_printf("<INFO>: create file %s size %d\n",name,size);
192 err = access( name, F_OK );
193 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
195 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
197 fd = open( name, O_WRONLY|O_CREAT );
198 if( fd < 0 ) SHOW_RESULT( open, fd );
203 if ( len > IOSIZE ) len = IOSIZE;
205 wrote = write( fd, buf, len );
206 if( wrote != len ) SHOW_RESULT( write, wrote );
212 if( err < 0 ) SHOW_RESULT( close, err );
215 //==========================================================================
218 static void maxfile( char *name )
227 diag_printf("<INFO>: create maximal file %s\n",name);
229 err = access( name, F_OK );
230 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
232 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
234 fd = open( name, O_WRONLY|O_CREAT );
235 if( fd < 0 ) SHOW_RESULT( open, fd );
239 wrote = write( fd, buf, IOSIZE );
240 if( wrote < 0 ) SHOW_RESULT( write, wrote );
244 } while( wrote == IOSIZE );
246 diag_printf("<INFO>: file size == %d\n",size);
249 if( err < 0 ) SHOW_RESULT( close, err );
253 //==========================================================================
255 static void checkfile( char *name )
264 diag_printf("<INFO>: check file %s\n",name);
266 err = access( name, F_OK );
267 if( err != 0 ) SHOW_RESULT( access, err );
269 fd = open( name, O_RDONLY );
270 if( fd < 0 ) SHOW_RESULT( open, fd );
274 done = read( fd, buf, IOSIZE );
275 if( done < 0 ) SHOW_RESULT( read, done );
277 if( done == 0 ) break;
279 for( i = 0; i < done; i++ )
280 if( buf[i] != i%256 )
282 diag_printf("buf[%ld+%d](%02x) != %02x\n",
283 (unsigned long)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 )
416 int existingdirents=-1;
420 // --------------------------------------------------------------
422 err = mount( "", "/", "ramfs" );
423 if( err < 0 ) SHOW_RESULT( mount, err );
426 if( err < 0 ) SHOW_RESULT( chdir, err );
430 listdir( "/", true, -1, &existingdirents );
431 if ( existingdirents < 2 )
432 CYG_TEST_FAIL("Not enough dir entries\n");
434 // --------------------------------------------------------------
436 createfile( "/foo", 202 );
438 copyfile( "foo", "fee");
440 comparefiles( "foo", "/fee" );
441 diag_printf("<INFO>: mkdir bar\n");
442 err = mkdir( "/bar", 0 );
443 if( err < 0 ) SHOW_RESULT( mkdir, err );
445 listdir( "/" , true, existingdirents+3, NULL );
447 copyfile( "fee", "/bar/fum" );
448 checkfile( "bar/fum" );
449 comparefiles( "/fee", "bar/fum" );
451 diag_printf("<INFO>: cd bar\n");
452 err = chdir( "bar" );
453 if( err < 0 ) SHOW_RESULT( chdir, err );
457 diag_printf("<INFO>: rename /foo bundy\n");
458 err = rename( "/foo", "bundy" );
459 if( err < 0 ) SHOW_RESULT( rename, err );
461 listdir( "/", true, existingdirents+2, NULL );
462 listdir( "" , true, 4, NULL );
464 checkfile( "/bar/bundy" );
465 comparefiles("/fee", "bundy" );
467 // --------------------------------------------------------------
469 createfile( LONGNAME1, 123 );
470 checkfile( LONGNAME1 );
471 copyfile( LONGNAME1, LONGNAME2 );
473 listdir( "", false, 6, NULL );
475 diag_printf("<INFO>: unlink " LONGNAME1 "\n");
476 err = unlink( LONGNAME1 );
477 if( err < 0 ) SHOW_RESULT( unlink, err );
479 diag_printf("<INFO>: unlink " LONGNAME2 "\n");
480 err = unlink( LONGNAME2 );
481 if( err < 0 ) SHOW_RESULT( unlink, err );
484 // --------------------------------------------------------------
486 diag_printf("<INFO>: unlink fee\n");
487 err = unlink( "/fee" );
488 if( err < 0 ) SHOW_RESULT( unlink, err );
490 diag_printf("<INFO>: unlink fum\n");
491 err = unlink( "fum" );
492 if( err < 0 ) SHOW_RESULT( unlink, err );
494 diag_printf("<INFO>: unlink /bar/bundy\n");
495 err = unlink( "/bar/bundy" );
496 if( err < 0 ) SHOW_RESULT( unlink, err );
498 diag_printf("<INFO>: cd /\n");
500 if( err < 0 ) SHOW_RESULT( chdir, err );
504 diag_printf("<INFO>: rmdir /bar\n");
505 err = rmdir( "/bar" );
506 if( err < 0 ) SHOW_RESULT( rmdir, err );
508 listdir( "/", false, existingdirents, NULL );
510 // --------------------------------------------------------------
512 diag_printf("<INFO>: mount /ram \n");
513 err = mount( "", "/ram", "ramfs" );
514 if( err < 0 ) SHOW_RESULT( mount, err );
516 createfile( "/ram/tinky", 456 );
517 copyfile( "/ram/tinky", "/ram/laalaa" );
518 checkfile( "/ram/tinky");
519 checkfile( "/ram/laalaa");
520 comparefiles( "/ram/tinky", "/ram/laalaa" );
522 diag_printf("<INFO>: cd /ram\n");
523 err = chdir( "/ram" );
524 if( err < 0 ) SHOW_RESULT( chdir, err );
528 diag_printf("<INFO>: mkdir noonoo\n");
529 err = mkdir( "noonoo", 0 );
530 if( err < 0 ) SHOW_RESULT( mkdir, err );
532 listdir( "." , true, existingdirents+3, NULL);
534 diag_printf("<INFO>: cd noonoo\n");
535 err = chdir( "noonoo" );
536 if( err < 0 ) SHOW_RESULT( chdir, err );
538 checkcwd( "/ram/noonoo" );
540 createfile( "tinky", 678 );
541 checkfile( "tinky" );
543 createfile( "dipsy", 3456 );
544 checkfile( "dipsy" );
545 copyfile( "dipsy", "po" );
547 comparefiles( "dipsy", "po" );
549 listdir( ".", true, 5, NULL );
550 listdir( "", true, 5, NULL );
551 listdir( "..", true, existingdirents+3, NULL );
553 // --------------------------------------------------------------
555 diag_printf("<INFO>: unlink tinky\n");
556 err = unlink( "tinky" );
557 if( err < 0 ) SHOW_RESULT( unlink, err );
559 diag_printf("<INFO>: unlink dipsy\n");
560 err = unlink( "dipsy" );
561 if( err < 0 ) SHOW_RESULT( unlink, err );
563 diag_printf("<INFO>: unlink po\n");
564 err = unlink( "po" );
565 if( err < 0 ) SHOW_RESULT( unlink, err );
567 diag_printf("<INFO>: cd ..\n");
569 if( err < 0 ) SHOW_RESULT( chdir, err );
572 diag_printf("<INFO>: rmdir noonoo\n");
573 err = rmdir( "noonoo" );
574 if( err < 0 ) SHOW_RESULT( rmdir, err );
576 // --------------------------------------------------------------
578 err = mkdir( "x", 0 );
579 if( err < 0 ) SHOW_RESULT( mkdir, err );
581 err = mkdir( "x/y", 0 );
582 if( err < 0 ) SHOW_RESULT( mkdir, err );
584 err = mkdir( "x/y/z", 0 );
585 if( err < 0 ) SHOW_RESULT( mkdir, err );
587 err = mkdir( "x/y/z/w", 0 );
588 if( err < 0 ) SHOW_RESULT( mkdir, err );
590 diag_printf("<INFO>: cd /ram/x/y/z/w\n");
591 err = chdir( "/ram/x/y/z/w" );
592 if( err < 0 ) SHOW_RESULT( chdir, err );
593 checkcwd( "/ram/x/y/z/w" );
595 diag_printf("<INFO>: cd ..\n");
597 if( err < 0 ) SHOW_RESULT( chdir, err );
598 checkcwd( "/ram/x/y/z" );
600 diag_printf("<INFO>: cd .\n");
602 if( err < 0 ) SHOW_RESULT( chdir, err );
603 checkcwd( "/ram/x/y/z" );
605 diag_printf("<INFO>: cd ../../y\n");
606 err = chdir( "../../y" );
607 if( err < 0 ) SHOW_RESULT( chdir, err );
608 checkcwd( "/ram/x/y" );
610 diag_printf("<INFO>: cd ../..\n");
611 err = chdir( "../.." );
612 if( err < 0 ) SHOW_RESULT( chdir, err );
615 diag_printf("<INFO>: rmdir x/y/z/w\n");
616 err = rmdir( "x/y/z/w" );
617 if( err < 0 ) SHOW_RESULT( rmdir, err );
619 diag_printf("<INFO>: rmdir x/y/z\n");
620 err = rmdir( "x/y/z" );
621 if( err < 0 ) SHOW_RESULT( rmdir, err );
623 diag_printf("<INFO>: rmdir x/y\n");
624 err = rmdir( "x/y" );
625 if( err < 0 ) SHOW_RESULT( rmdir, err );
627 diag_printf("<INFO>: rmdir x\n");
629 if( err < 0 ) SHOW_RESULT( rmdir, err );
631 // --------------------------------------------------------------
633 diag_printf("<INFO>: unlink tinky\n");
634 err = unlink( "tinky" );
635 if( err < 0 ) SHOW_RESULT( unlink, err );
637 diag_printf("<INFO>: unlink laalaa\n");
638 err = unlink( "laalaa" );
639 if( err < 0 ) SHOW_RESULT( unlink, err );
641 diag_printf("<INFO>: cd /\n");
643 if( err < 0 ) SHOW_RESULT( chdir, err );
646 diag_printf("<INFO>: umount /ram\n");
647 err = umount( "/ram" );
648 if( err < 0 ) SHOW_RESULT( umount, err );
650 CYG_TEST_PASS_FINISH("ramfs1");
653 // -------------------------------------------------------------------------