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>
64 #include <pkgconf/fs_ram.h>
66 #include <cyg/kernel/ktypes.h> // base kernel types
67 #include <cyg/infra/cyg_trac.h> // tracing macros
68 #include <cyg/infra/cyg_ass.h> // assertion macros
78 #include <cyg/fileio/fileio.h>
80 #include <cyg/infra/testcase.h>
81 #include <cyg/infra/diag.h> // HAL polled output
83 //==========================================================================
86 MTAB_ENTRY( ramfs_mte1,
93 //==========================================================================
95 #define SHOW_RESULT( _fn, _res ) \
96 diag_printf("<FAIL>: " #_fn "() returned %ld %s\n", (long)_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);
141 #ifdef CYGPKG_FS_RAM_RET_DIRENT_DTYPE
142 diag_printf(" d_type %2x", entry->d_type);
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);
172 #ifdef CYGPKG_FS_RAM_RET_DIRENT_DTYPE
173 if ((entry->d_type & S_IFMT) != (sbuf.st_mode & S_IFMT))
174 CYG_TEST_FAIL("File mode's don't match between dirent and stat");
181 err = closedir( dirp );
182 if( err < 0 ) SHOW_RESULT( stat, err );
183 if (numexpected >= 0 && num != numexpected)
184 CYG_TEST_FAIL("Wrong number of dir entries\n");
185 if ( numgot != NULL )
189 //==========================================================================
191 static void createfile( char *name, size_t size )
199 diag_printf("<INFO>: create file %s size %zd\n",name,size);
201 err = access( name, F_OK );
202 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
204 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
206 fd = open( name, O_WRONLY|O_CREAT );
207 if( fd < 0 ) SHOW_RESULT( open, fd );
212 if ( len > IOSIZE ) len = IOSIZE;
214 wrote = write( fd, buf, len );
215 if( wrote != len ) SHOW_RESULT( write, wrote );
221 if( err < 0 ) SHOW_RESULT( close, err );
224 //==========================================================================
227 static void maxfile( char *name )
236 diag_printf("<INFO>: create maximal file %s\n",name);
238 err = access( name, F_OK );
239 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
241 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
243 fd = open( name, O_WRONLY|O_CREAT );
244 if( fd < 0 ) SHOW_RESULT( open, fd );
248 wrote = write( fd, buf, IOSIZE );
249 if( wrote < 0 ) SHOW_RESULT( write, wrote );
253 } while( wrote == IOSIZE );
255 diag_printf("<INFO>: file size == %d\n",size);
258 if( err < 0 ) SHOW_RESULT( close, err );
262 //==========================================================================
264 static void checkfile( char *name )
273 diag_printf("<INFO>: check file %s\n",name);
275 err = access( name, F_OK );
276 if( err != 0 ) SHOW_RESULT( access, err );
278 fd = open( name, O_RDONLY );
279 if( fd < 0 ) SHOW_RESULT( open, fd );
283 done = read( fd, buf, IOSIZE );
284 if( done < 0 ) SHOW_RESULT( read, done );
286 if( done == 0 ) break;
288 for( i = 0; i < done; i++ )
289 if( buf[i] != i%256 )
291 diag_printf("buf[%ld+%d](%02x) != %02x\n",
292 (unsigned long)pos,i,buf[i],i%256);
293 CYG_TEST_FAIL("Data read not equal to data written\n");
300 if( err < 0 ) SHOW_RESULT( close, err );
303 //==========================================================================
305 static void copyfile( char *name2, char *name1 )
313 diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
315 err = access( name1, F_OK );
316 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
318 err = access( name2, F_OK );
319 if( err != 0 ) SHOW_RESULT( access, err );
321 fd1 = open( name1, O_WRONLY|O_CREAT );
322 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
324 fd2 = open( name2, O_RDONLY );
325 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
329 done = read( fd2, buf, IOSIZE );
330 if( done < 0 ) SHOW_RESULT( read, done );
332 if( done == 0 ) break;
334 wrote = write( fd1, buf, done );
335 if( wrote != done ) SHOW_RESULT( write, wrote );
337 if( wrote != done ) break;
341 if( err < 0 ) SHOW_RESULT( close, err );
344 if( err < 0 ) SHOW_RESULT( close, err );
348 //==========================================================================
350 static void comparefiles( char *name2, char *name1 )
356 ssize_t done1, done2;
359 diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
361 err = access( name1, F_OK );
362 if( err != 0 ) SHOW_RESULT( access, err );
364 err = access( name1, F_OK );
365 if( err != 0 ) SHOW_RESULT( access, err );
367 fd1 = open( name1, O_RDONLY );
368 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
370 fd2 = open( name2, O_RDONLY );
371 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
375 done1 = read( fd1, buf1, IOSIZE );
376 if( done1 < 0 ) SHOW_RESULT( read, done1 );
378 done2 = read( fd2, buf2, IOSIZE );
379 if( done2 < 0 ) SHOW_RESULT( read, done2 );
382 diag_printf("Files different sizes\n");
384 if( done1 == 0 ) break;
386 for( i = 0; i < done1; i++ )
387 if( buf1[i] != buf2[i] )
389 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
390 CYG_TEST_FAIL("Data in files not equal\n");
395 if( err < 0 ) SHOW_RESULT( close, err );
398 if( err < 0 ) SHOW_RESULT( close, err );
402 //==========================================================================
404 void checkcwd( const char *cwd )
406 static char cwdbuf[PATH_MAX];
409 ret = getcwd( cwdbuf, sizeof(cwdbuf));
410 if( ret == NULL ) SHOW_RESULT( getcwd, ret );
412 if( strcmp( cwdbuf, cwd ) != 0 )
414 diag_printf( "cwdbuf %s cwd %s\n",cwdbuf, cwd );
415 CYG_TEST_FAIL( "Current directory mismatch");
419 //==========================================================================
422 int main( int argc, char **argv )
425 int existingdirents=-1;
426 #if defined(CYGSEM_FILEIO_BLOCK_USAGE) && defined(CYGPKG_FS_RAM_BLOCKS_ARRAY)
427 struct cyg_fs_block_usage usage;
432 // --------------------------------------------------------------
434 err = mount( "", "/", "ramfs" );
435 if( err < 0 ) SHOW_RESULT( mount, err );
438 if( err < 0 ) SHOW_RESULT( chdir, err );
442 listdir( "/", true, -1, &existingdirents );
443 if ( existingdirents < 2 )
444 CYG_TEST_FAIL("Not enough dir entries\n");
445 // --------------------------------------------------------------
446 #if defined(CYGSEM_FILEIO_BLOCK_USAGE) && defined(CYGPKG_FS_RAM_BLOCKS_ARRAY)
447 err = cyg_fs_getinfo("/", FS_INFO_BLOCK_USAGE, &usage, sizeof(usage));
448 if( err < 0 ) SHOW_RESULT( cyg_fs_getinfo, err );
449 diag_printf("<INFO>: total size: %6lld blocks, %10lld bytes\n",
450 usage.total_blocks, usage.total_blocks * usage.block_size);
451 diag_printf("<INFO>: free size: %6lld blocks, %10lld bytes\n",
452 usage.free_blocks, usage.free_blocks * usage.block_size);
453 diag_printf("<INFO>: block size: %6u bytes\n", usage.block_size);
455 // --------------------------------------------------------------
457 createfile( "/foo", 202 );
459 copyfile( "foo", "fee");
461 comparefiles( "foo", "/fee" );
462 diag_printf("<INFO>: mkdir bar\n");
463 err = mkdir( "/bar", 0 );
464 if( err < 0 ) SHOW_RESULT( mkdir, err );
466 listdir( "/" , true, existingdirents+3, NULL );
468 copyfile( "fee", "/bar/fum" );
469 checkfile( "bar/fum" );
470 comparefiles( "/fee", "bar/fum" );
472 diag_printf("<INFO>: cd bar\n");
473 err = chdir( "bar" );
474 if( err < 0 ) SHOW_RESULT( chdir, err );
478 diag_printf("<INFO>: rename /foo bundy\n");
479 err = rename( "/foo", "bundy" );
480 if( err < 0 ) SHOW_RESULT( rename, err );
482 listdir( "/", true, existingdirents+2, NULL );
483 listdir( "" , true, 4, NULL );
485 checkfile( "/bar/bundy" );
486 comparefiles("/fee", "bundy" );
488 // --------------------------------------------------------------
490 createfile( LONGNAME1, 123 );
491 checkfile( LONGNAME1 );
492 copyfile( LONGNAME1, LONGNAME2 );
494 listdir( "", false, 6, NULL );
496 diag_printf("<INFO>: unlink " LONGNAME1 "\n");
497 err = unlink( LONGNAME1 );
498 if( err < 0 ) SHOW_RESULT( unlink, err );
500 diag_printf("<INFO>: unlink " LONGNAME2 "\n");
501 err = unlink( LONGNAME2 );
502 if( err < 0 ) SHOW_RESULT( unlink, err );
505 // --------------------------------------------------------------
507 diag_printf("<INFO>: unlink fee\n");
508 err = unlink( "/fee" );
509 if( err < 0 ) SHOW_RESULT( unlink, err );
511 diag_printf("<INFO>: unlink fum\n");
512 err = unlink( "fum" );
513 if( err < 0 ) SHOW_RESULT( unlink, err );
515 diag_printf("<INFO>: unlink /bar/bundy\n");
516 err = unlink( "/bar/bundy" );
517 if( err < 0 ) SHOW_RESULT( unlink, err );
519 diag_printf("<INFO>: cd /\n");
521 if( err < 0 ) SHOW_RESULT( chdir, err );
525 diag_printf("<INFO>: rmdir /bar\n");
526 err = rmdir( "/bar" );
527 if( err < 0 ) SHOW_RESULT( rmdir, err );
529 listdir( "/", false, existingdirents, NULL );
531 // --------------------------------------------------------------
533 diag_printf("<INFO>: mount /ram \n");
534 err = mount( "", "/ram", "ramfs" );
535 if( err < 0 ) SHOW_RESULT( mount, err );
537 createfile( "/ram/tinky", 456 );
538 copyfile( "/ram/tinky", "/ram/laalaa" );
539 checkfile( "/ram/tinky");
540 checkfile( "/ram/laalaa");
541 comparefiles( "/ram/tinky", "/ram/laalaa" );
543 diag_printf("<INFO>: cd /ram\n");
544 err = chdir( "/ram" );
545 if( err < 0 ) SHOW_RESULT( chdir, err );
549 diag_printf("<INFO>: mkdir noonoo\n");
550 err = mkdir( "noonoo", 0 );
551 if( err < 0 ) SHOW_RESULT( mkdir, err );
553 listdir( "." , true, existingdirents+3, NULL);
555 diag_printf("<INFO>: cd noonoo\n");
556 err = chdir( "noonoo" );
557 if( err < 0 ) SHOW_RESULT( chdir, err );
559 checkcwd( "/ram/noonoo" );
561 createfile( "tinky", 678 );
562 checkfile( "tinky" );
564 createfile( "dipsy", 3456 );
565 checkfile( "dipsy" );
566 copyfile( "dipsy", "po" );
568 comparefiles( "dipsy", "po" );
570 listdir( ".", true, 5, NULL );
571 listdir( "", true, 5, NULL );
572 listdir( "..", true, existingdirents+3, NULL );
574 // --------------------------------------------------------------
575 #if defined(CYGSEM_FILEIO_BLOCK_USAGE) && defined(CYGPKG_FS_RAM_BLOCKS_ARRAY)
576 err = cyg_fs_getinfo("/", FS_INFO_BLOCK_USAGE, &usage, sizeof(usage));
577 if( err < 0 ) SHOW_RESULT( cyg_fs_getinfo, err );
578 diag_printf("<INFO>: total size: %6lld blocks, %10lld bytes\n",
579 usage.total_blocks, usage.total_blocks * usage.block_size);
580 diag_printf("<INFO>: free size: %6lld blocks, %10lld bytes\n",
581 usage.free_blocks, usage.free_blocks * usage.block_size);
582 diag_printf("<INFO>: block size: %6u bytes\n", usage.block_size);
584 // --------------------------------------------------------------
586 diag_printf("<INFO>: unlink tinky\n");
587 err = unlink( "tinky" );
588 if( err < 0 ) SHOW_RESULT( unlink, err );
590 diag_printf("<INFO>: unlink dipsy\n");
591 err = unlink( "dipsy" );
592 if( err < 0 ) SHOW_RESULT( unlink, err );
594 diag_printf("<INFO>: unlink po\n");
595 err = unlink( "po" );
596 if( err < 0 ) SHOW_RESULT( unlink, err );
598 diag_printf("<INFO>: cd ..\n");
600 if( err < 0 ) SHOW_RESULT( chdir, err );
603 diag_printf("<INFO>: rmdir noonoo\n");
604 err = rmdir( "noonoo" );
605 if( err < 0 ) SHOW_RESULT( rmdir, err );
607 // --------------------------------------------------------------
609 err = mkdir( "x", 0 );
610 if( err < 0 ) SHOW_RESULT( mkdir, err );
612 err = mkdir( "x/y", 0 );
613 if( err < 0 ) SHOW_RESULT( mkdir, err );
615 err = mkdir( "x/y/z", 0 );
616 if( err < 0 ) SHOW_RESULT( mkdir, err );
618 err = mkdir( "x/y/z/w", 0 );
619 if( err < 0 ) SHOW_RESULT( mkdir, err );
621 diag_printf("<INFO>: cd /ram/x/y/z/w\n");
622 err = chdir( "/ram/x/y/z/w" );
623 if( err < 0 ) SHOW_RESULT( chdir, err );
624 checkcwd( "/ram/x/y/z/w" );
626 diag_printf("<INFO>: cd ..\n");
628 if( err < 0 ) SHOW_RESULT( chdir, err );
629 checkcwd( "/ram/x/y/z" );
631 diag_printf("<INFO>: cd .\n");
633 if( err < 0 ) SHOW_RESULT( chdir, err );
634 checkcwd( "/ram/x/y/z" );
636 diag_printf("<INFO>: cd ../../y\n");
637 err = chdir( "../../y" );
638 if( err < 0 ) SHOW_RESULT( chdir, err );
639 checkcwd( "/ram/x/y" );
641 diag_printf("<INFO>: cd ../..\n");
642 err = chdir( "../.." );
643 if( err < 0 ) SHOW_RESULT( chdir, err );
646 diag_printf("<INFO>: rmdir x/y/z/w\n");
647 err = rmdir( "x/y/z/w" );
648 if( err < 0 ) SHOW_RESULT( rmdir, err );
650 diag_printf("<INFO>: rmdir x/y/z\n");
651 err = rmdir( "x/y/z" );
652 if( err < 0 ) SHOW_RESULT( rmdir, err );
654 diag_printf("<INFO>: rmdir x/y\n");
655 err = rmdir( "x/y" );
656 if( err < 0 ) SHOW_RESULT( rmdir, err );
658 diag_printf("<INFO>: rmdir x\n");
660 if( err < 0 ) SHOW_RESULT( rmdir, err );
662 // --------------------------------------------------------------
664 diag_printf("<INFO>: unlink tinky\n");
665 err = unlink( "tinky" );
666 if( err < 0 ) SHOW_RESULT( unlink, err );
668 diag_printf("<INFO>: unlink laalaa\n");
669 err = unlink( "laalaa" );
670 if( err < 0 ) SHOW_RESULT( unlink, err );
672 diag_printf("<INFO>: cd /\n");
674 if( err < 0 ) SHOW_RESULT( chdir, err );
677 diag_printf("<INFO>: umount /ram\n");
678 err = umount( "/ram" );
679 if( err < 0 ) SHOW_RESULT( umount, err );
681 CYG_TEST_PASS_FINISH("ramfs1");
684 // -------------------------------------------------------------------------