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
75 #include <cyg/fileio/fileio.h>
77 #include <cyg/infra/testcase.h>
78 #include <cyg/infra/diag.h> // HAL polled output
80 //==========================================================================
83 MTAB_ENTRY( ramfs_mte1,
90 //==========================================================================
92 #define SHOW_RESULT( _fn, _res ) \
93 diag_printf("<FAIL>: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
95 //==========================================================================
99 #define LONGNAME1 "long_file_name_that_should_take_up_more_than_one_directory_entry_1"
100 #define LONGNAME2 "long_file_name_that_should_take_up_more_than_one_directory_entry_2"
103 //==========================================================================
105 #ifndef CYGPKG_LIBC_STRING
107 char *strcat( char *s1, const char *s2 )
111 while( (*s1++ = *s2++) != 0);
117 //==========================================================================
119 static void listdir( char *name, int statp, int numexpected, int *numgot )
125 diag_printf("<INFO>: reading directory %s\n",name);
127 dirp = opendir( name );
128 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
132 struct dirent *entry = readdir( dirp );
137 diag_printf("<INFO>: entry %14s",entry->d_name);
140 char fullname[PATH_MAX];
145 strcpy(fullname, name );
146 if( !(name[0] == '/' && name[1] == 0 ) )
147 strcat(fullname, "/" );
149 else fullname[0] = 0;
151 strcat(fullname, entry->d_name );
153 err = stat( fullname, &sbuf );
156 if( errno == ENOSYS )
157 diag_printf(" <no status available>");
158 else SHOW_RESULT( stat, err );
162 diag_printf(" [mode %08x ino %08x nlink %d size %d]",
163 sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size);
170 err = closedir( dirp );
171 if( err < 0 ) SHOW_RESULT( stat, err );
172 if (numexpected >= 0 && num != numexpected)
173 CYG_TEST_FAIL("Wrong number of dir entries\n");
174 if ( numgot != NULL )
178 //==========================================================================
180 static void createfile( char *name, size_t size )
188 diag_printf("<INFO>: create file %s size %d\n",name,size);
190 err = access( name, F_OK );
191 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
193 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
195 fd = open( name, O_WRONLY|O_CREAT );
196 if( fd < 0 ) SHOW_RESULT( open, fd );
201 if ( len > IOSIZE ) len = IOSIZE;
203 wrote = write( fd, buf, len );
204 if( wrote != len ) SHOW_RESULT( write, wrote );
210 if( err < 0 ) SHOW_RESULT( close, err );
213 //==========================================================================
216 static void maxfile( char *name )
225 diag_printf("<INFO>: create maximal file %s\n",name);
227 err = access( name, F_OK );
228 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
230 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
232 fd = open( name, O_WRONLY|O_CREAT );
233 if( fd < 0 ) SHOW_RESULT( open, fd );
237 wrote = write( fd, buf, IOSIZE );
238 if( wrote < 0 ) SHOW_RESULT( write, wrote );
242 } while( wrote == IOSIZE );
244 diag_printf("<INFO>: file size == %d\n",size);
247 if( err < 0 ) SHOW_RESULT( close, err );
251 //==========================================================================
253 static void checkfile( char *name )
262 diag_printf("<INFO>: check file %s\n",name);
264 err = access( name, F_OK );
265 if( err != 0 ) SHOW_RESULT( access, err );
267 fd = open( name, O_RDONLY );
268 if( fd < 0 ) SHOW_RESULT( open, fd );
272 done = read( fd, buf, IOSIZE );
273 if( done < 0 ) SHOW_RESULT( read, done );
275 if( done == 0 ) break;
277 for( i = 0; i < done; i++ )
278 if( buf[i] != i%256 )
280 diag_printf("buf[%d+%d](%02x) != %02x\n",pos,i,buf[i],i%256);
281 CYG_TEST_FAIL("Data read not equal to data written\n");
288 if( err < 0 ) SHOW_RESULT( close, err );
291 //==========================================================================
293 static void copyfile( char *name2, char *name1 )
301 diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
303 err = access( name1, F_OK );
304 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
306 err = access( name2, F_OK );
307 if( err != 0 ) SHOW_RESULT( access, err );
309 fd1 = open( name1, O_WRONLY|O_CREAT );
310 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
312 fd2 = open( name2, O_RDONLY );
313 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
317 done = read( fd2, buf, IOSIZE );
318 if( done < 0 ) SHOW_RESULT( read, done );
320 if( done == 0 ) break;
322 wrote = write( fd1, buf, done );
323 if( wrote != done ) SHOW_RESULT( write, wrote );
325 if( wrote != done ) break;
329 if( err < 0 ) SHOW_RESULT( close, err );
332 if( err < 0 ) SHOW_RESULT( close, err );
336 //==========================================================================
338 static void comparefiles( char *name2, char *name1 )
344 ssize_t done1, done2;
347 diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
349 err = access( name1, F_OK );
350 if( err != 0 ) SHOW_RESULT( access, err );
352 err = access( name1, F_OK );
353 if( err != 0 ) SHOW_RESULT( access, err );
355 fd1 = open( name1, O_RDONLY );
356 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
358 fd2 = open( name2, O_RDONLY );
359 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
363 done1 = read( fd1, buf1, IOSIZE );
364 if( done1 < 0 ) SHOW_RESULT( read, done1 );
366 done2 = read( fd2, buf2, IOSIZE );
367 if( done2 < 0 ) SHOW_RESULT( read, done2 );
370 diag_printf("Files different sizes\n");
372 if( done1 == 0 ) break;
374 for( i = 0; i < done1; i++ )
375 if( buf1[i] != buf2[i] )
377 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
378 CYG_TEST_FAIL("Data in files not equal\n");
383 if( err < 0 ) SHOW_RESULT( close, err );
386 if( err < 0 ) SHOW_RESULT( close, err );
390 //==========================================================================
392 void checkcwd( const char *cwd )
394 static char cwdbuf[PATH_MAX];
397 ret = getcwd( cwdbuf, sizeof(cwdbuf));
398 if( ret == NULL ) SHOW_RESULT( getcwd, ret );
400 if( strcmp( cwdbuf, cwd ) != 0 )
402 diag_printf( "cwdbuf %s cwd %s\n",cwdbuf, cwd );
403 CYG_TEST_FAIL( "Current directory mismatch");
407 //==========================================================================
410 int main( int argc, char **argv )
413 int existingdirents=-1;
417 // --------------------------------------------------------------
419 err = mount( "", "/", "ramfs" );
420 if( err < 0 ) SHOW_RESULT( mount, err );
423 if( err < 0 ) SHOW_RESULT( chdir, err );
427 listdir( "/", true, -1, &existingdirents );
428 if ( existingdirents < 2 )
429 CYG_TEST_FAIL("Not enough dir entries\n");
431 // --------------------------------------------------------------
433 createfile( "/foo", 202 );
435 copyfile( "foo", "fee");
437 comparefiles( "foo", "/fee" );
438 diag_printf("<INFO>: mkdir bar\n");
439 err = mkdir( "/bar", 0 );
440 if( err < 0 ) SHOW_RESULT( mkdir, err );
442 listdir( "/" , true, existingdirents+3, NULL );
444 copyfile( "fee", "/bar/fum" );
445 checkfile( "bar/fum" );
446 comparefiles( "/fee", "bar/fum" );
448 diag_printf("<INFO>: cd bar\n");
449 err = chdir( "bar" );
450 if( err < 0 ) SHOW_RESULT( chdir, err );
454 diag_printf("<INFO>: rename /foo bundy\n");
455 err = rename( "/foo", "bundy" );
456 if( err < 0 ) SHOW_RESULT( rename, err );
458 listdir( "/", true, existingdirents+2, NULL );
459 listdir( "" , true, 4, NULL );
461 checkfile( "/bar/bundy" );
462 comparefiles("/fee", "bundy" );
464 // --------------------------------------------------------------
466 createfile( LONGNAME1, 123 );
467 checkfile( LONGNAME1 );
468 copyfile( LONGNAME1, LONGNAME2 );
470 listdir( "", false, 6, NULL );
472 diag_printf("<INFO>: unlink " LONGNAME1 "\n");
473 err = unlink( LONGNAME1 );
474 if( err < 0 ) SHOW_RESULT( unlink, err );
476 diag_printf("<INFO>: unlink " LONGNAME2 "\n");
477 err = unlink( LONGNAME2 );
478 if( err < 0 ) SHOW_RESULT( unlink, err );
481 // --------------------------------------------------------------
483 diag_printf("<INFO>: unlink fee\n");
484 err = unlink( "/fee" );
485 if( err < 0 ) SHOW_RESULT( unlink, err );
487 diag_printf("<INFO>: unlink fum\n");
488 err = unlink( "fum" );
489 if( err < 0 ) SHOW_RESULT( unlink, err );
491 diag_printf("<INFO>: unlink /bar/bundy\n");
492 err = unlink( "/bar/bundy" );
493 if( err < 0 ) SHOW_RESULT( unlink, err );
495 diag_printf("<INFO>: cd /\n");
497 if( err < 0 ) SHOW_RESULT( chdir, err );
501 diag_printf("<INFO>: rmdir /bar\n");
502 err = rmdir( "/bar" );
503 if( err < 0 ) SHOW_RESULT( rmdir, err );
505 listdir( "/", false, existingdirents, NULL );
507 // --------------------------------------------------------------
509 diag_printf("<INFO>: mount /ram \n");
510 err = mount( "", "/ram", "ramfs" );
511 if( err < 0 ) SHOW_RESULT( mount, err );
513 createfile( "/ram/tinky", 456 );
514 copyfile( "/ram/tinky", "/ram/laalaa" );
515 checkfile( "/ram/tinky");
516 checkfile( "/ram/laalaa");
517 comparefiles( "/ram/tinky", "/ram/laalaa" );
519 diag_printf("<INFO>: cd /ram\n");
520 err = chdir( "/ram" );
521 if( err < 0 ) SHOW_RESULT( chdir, err );
525 diag_printf("<INFO>: mkdir noonoo\n");
526 err = mkdir( "noonoo", 0 );
527 if( err < 0 ) SHOW_RESULT( mkdir, err );
529 listdir( "." , true, existingdirents+3, NULL);
531 diag_printf("<INFO>: cd noonoo\n");
532 err = chdir( "noonoo" );
533 if( err < 0 ) SHOW_RESULT( chdir, err );
535 checkcwd( "/ram/noonoo" );
537 createfile( "tinky", 678 );
538 checkfile( "tinky" );
540 createfile( "dipsy", 3456 );
541 checkfile( "dipsy" );
542 copyfile( "dipsy", "po" );
544 comparefiles( "dipsy", "po" );
546 listdir( ".", true, 5, NULL );
547 listdir( "", true, 5, NULL );
548 listdir( "..", true, existingdirents+3, NULL );
550 // --------------------------------------------------------------
552 diag_printf("<INFO>: unlink tinky\n");
553 err = unlink( "tinky" );
554 if( err < 0 ) SHOW_RESULT( unlink, err );
556 diag_printf("<INFO>: unlink dipsy\n");
557 err = unlink( "dipsy" );
558 if( err < 0 ) SHOW_RESULT( unlink, err );
560 diag_printf("<INFO>: unlink po\n");
561 err = unlink( "po" );
562 if( err < 0 ) SHOW_RESULT( unlink, err );
564 diag_printf("<INFO>: cd ..\n");
566 if( err < 0 ) SHOW_RESULT( chdir, err );
569 diag_printf("<INFO>: rmdir noonoo\n");
570 err = rmdir( "noonoo" );
571 if( err < 0 ) SHOW_RESULT( rmdir, err );
573 // --------------------------------------------------------------
575 err = mkdir( "x", 0 );
576 if( err < 0 ) SHOW_RESULT( mkdir, err );
578 err = mkdir( "x/y", 0 );
579 if( err < 0 ) SHOW_RESULT( mkdir, err );
581 err = mkdir( "x/y/z", 0 );
582 if( err < 0 ) SHOW_RESULT( mkdir, err );
584 err = mkdir( "x/y/z/w", 0 );
585 if( err < 0 ) SHOW_RESULT( mkdir, err );
587 diag_printf("<INFO>: cd /ram/x/y/z/w\n");
588 err = chdir( "/ram/x/y/z/w" );
589 if( err < 0 ) SHOW_RESULT( chdir, err );
590 checkcwd( "/ram/x/y/z/w" );
592 diag_printf("<INFO>: cd ..\n");
594 if( err < 0 ) SHOW_RESULT( chdir, err );
595 checkcwd( "/ram/x/y/z" );
597 diag_printf("<INFO>: cd .\n");
599 if( err < 0 ) SHOW_RESULT( chdir, err );
600 checkcwd( "/ram/x/y/z" );
602 diag_printf("<INFO>: cd ../../y\n");
603 err = chdir( "../../y" );
604 if( err < 0 ) SHOW_RESULT( chdir, err );
605 checkcwd( "/ram/x/y" );
607 diag_printf("<INFO>: cd ../..\n");
608 err = chdir( "../.." );
609 if( err < 0 ) SHOW_RESULT( chdir, err );
612 diag_printf("<INFO>: rmdir x/y/z/w\n");
613 err = rmdir( "x/y/z/w" );
614 if( err < 0 ) SHOW_RESULT( rmdir, err );
616 diag_printf("<INFO>: rmdir x/y/z\n");
617 err = rmdir( "x/y/z" );
618 if( err < 0 ) SHOW_RESULT( rmdir, err );
620 diag_printf("<INFO>: rmdir x/y\n");
621 err = rmdir( "x/y" );
622 if( err < 0 ) SHOW_RESULT( rmdir, err );
624 diag_printf("<INFO>: rmdir x\n");
626 if( err < 0 ) SHOW_RESULT( rmdir, err );
628 // --------------------------------------------------------------
630 diag_printf("<INFO>: unlink tinky\n");
631 err = unlink( "tinky" );
632 if( err < 0 ) SHOW_RESULT( unlink, err );
634 diag_printf("<INFO>: unlink laalaa\n");
635 err = unlink( "laalaa" );
636 if( err < 0 ) SHOW_RESULT( unlink, err );
638 diag_printf("<INFO>: cd /\n");
640 if( err < 0 ) SHOW_RESULT( chdir, err );
643 diag_printf("<INFO>: umount /ram\n");
644 err = umount( "/ram" );
645 if( err < 0 ) SHOW_RESULT( umount, err );
647 CYG_TEST_PASS_FINISH("fileio1");
650 // -------------------------------------------------------------------------