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/io_fileio.h>
63 #include <pkgconf/fs_fat.h>
65 #include <cyg/infra/cyg_trac.h> // tracing macros
66 #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
80 #include <cyg/fs/fatfs.h>
84 //==========================================================================
86 #define SHOW_RESULT( _fn, _res ) \
87 diag_printf("<FAIL>: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
89 //==========================================================================
93 #define LONGNAME1 "long_file_name_that_should_take_up_more_than_one_directory_entry_1"
94 #define LONGNAME2 "long_file_name_that_should_take_up_more_than_one_directory_entry_2"
97 //==========================================================================
99 #ifndef CYGPKG_LIBC_STRING
101 char *strcat( char *s1, const char *s2 )
105 while( (*s1++ = *s2++) != 0);
111 //==========================================================================
113 static void listdir( char *name, int statp, int numexpected, int *numgot )
119 diag_printf("<INFO>: reading directory %s\n",name);
121 dirp = opendir( name );
122 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
126 struct dirent *entry = readdir( dirp );
131 diag_printf("<INFO>: entry %14s",entry->d_name);
132 #ifdef CYGPKG_FS_FAT_RET_DIRENT_DTYPE
133 diag_printf(" d_type %2x", entry->d_type);
137 char fullname[PATH_MAX];
142 strcpy(fullname, name );
143 if( !(name[0] == '/' && name[1] == 0 ) )
144 strcat(fullname, "/" );
146 else fullname[0] = 0;
148 strcat(fullname, entry->d_name );
150 err = stat( fullname, &sbuf );
153 if( errno == ENOSYS )
154 diag_printf(" <no status available>");
155 else SHOW_RESULT( stat, err );
159 diag_printf(" [mode %08x ino %08x nlink %d size %ld]",
160 sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,(long)sbuf.st_size);
162 #ifdef CYGPKG_FS_FAT_RET_DIRENT_DTYPE
163 if ((entry->d_type & S_IFMT) != (sbuf.st_mode & S_IFMT))
164 CYG_TEST_FAIL("File mode's don't match between dirent and stat");
171 err = closedir( dirp );
172 if( err < 0 ) SHOW_RESULT( stat, err );
173 if (numexpected >= 0 && num != numexpected)
174 CYG_TEST_FAIL("Wrong number of dir entries\n");
175 if ( numgot != NULL )
179 //==========================================================================
181 static void createfile( char *name, size_t size )
189 diag_printf("<INFO>: create file %s size %zd \n",name,size);
191 err = access( name, F_OK );
192 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
194 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
196 fd = open( name, O_WRONLY|O_CREAT );
197 if( fd < 0 ) SHOW_RESULT( open, fd );
202 if ( len > IOSIZE ) len = IOSIZE;
204 wrote = write( fd, buf, len );
205 if( wrote != len ) SHOW_RESULT( write, (int)wrote );
211 if( err < 0 ) SHOW_RESULT( close, err );
214 //==========================================================================
216 static void maxfile( char *name )
226 diag_printf("<INFO>: create maximal file %s\n",name);
227 diag_printf("<INFO>: This may take a few minutes\n");
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 );
245 if( (size-prevsize) > 100000 )
247 diag_printf("<INFO>: size = %zd \n", size);
251 } while( wrote == IOSIZE );
253 diag_printf("<INFO>: file size == %zd\n",size);
256 if( err < 0 ) SHOW_RESULT( close, err );
259 //==========================================================================
261 static void checkfile( char *name )
270 diag_printf("<INFO>: check file %s\n",name);
272 err = access( name, F_OK );
273 if( err != 0 ) SHOW_RESULT( access, err );
275 fd = open( name, O_RDONLY );
276 if( fd < 0 ) SHOW_RESULT( open, fd );
280 done = read( fd, buf, IOSIZE );
281 if( done < 0 ) SHOW_RESULT( read, (int)done );
283 if( done == 0 ) break;
285 for( i = 0; i < done; i++ )
286 if( buf[i] != i%256 )
288 diag_printf("buf[%ld+%d](%02x) != %02x\n",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 #ifdef CYGCFG_FS_FAT_USE_ATTRIBUTES
300 //==========================================================================
302 static void checkattrib(const char *name,
303 const cyg_fs_attrib_t test_attrib )
306 cyg_fs_attrib_t file_attrib;
308 diag_printf("<INFO>: check attrib %s\n",name);
310 err = cyg_fs_get_attrib(name, &file_attrib);
311 if( err != 0 ) SHOW_RESULT( stat, err );
313 if ( (file_attrib & S_FATFS_ATTRIB) != test_attrib )
314 diag_printf("<FAIL>: attrib %s incorrect\n\tExpected %x Was %x\n",
315 name,test_attrib,(file_attrib & S_FATFS_ATTRIB));
317 #endif // CYGCFG_FS_FAT_USE_ATTRIBUTES
319 //==========================================================================
321 static void copyfile( char *name2, char *name1 )
329 diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
331 err = access( name1, F_OK );
332 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
334 err = access( name2, F_OK );
335 if( err != 0 ) SHOW_RESULT( access, err );
337 fd1 = open( name1, O_WRONLY|O_CREAT );
338 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
340 fd2 = open( name2, O_RDONLY );
341 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
345 done = read( fd2, buf, IOSIZE );
346 if( done < 0 ) SHOW_RESULT( read, (int)done );
348 if( done == 0 ) break;
350 wrote = write( fd1, buf, done );
351 if( wrote != done ) SHOW_RESULT( write, (int) wrote );
353 if( wrote != done ) break;
357 if( err < 0 ) SHOW_RESULT( close, err );
360 if( err < 0 ) SHOW_RESULT( close, err );
364 //==========================================================================
366 static void comparefiles( char *name2, char *name1 )
372 ssize_t done1, done2;
375 diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
377 err = access( name1, F_OK );
378 if( err != 0 ) SHOW_RESULT( access, err );
380 err = access( name1, F_OK );
381 if( err != 0 ) SHOW_RESULT( access, err );
383 fd1 = open( name1, O_RDONLY );
384 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
386 fd2 = open( name2, O_RDONLY );
387 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
391 done1 = read( fd1, buf1, IOSIZE );
392 if( done1 < 0 ) SHOW_RESULT( read, (int)done1 );
394 done2 = read( fd2, buf2, IOSIZE );
395 if( done2 < 0 ) SHOW_RESULT( read, (int)done2 );
398 diag_printf("Files different sizes\n");
400 if( done1 == 0 ) break;
402 for( i = 0; i < done1; i++ )
403 if( buf1[i] != buf2[i] )
405 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
406 CYG_TEST_FAIL("Data in files not equal\n");
411 if( err < 0 ) SHOW_RESULT( close, err );
414 if( err < 0 ) SHOW_RESULT( close, err );
418 //==========================================================================
420 void checkcwd( const char *cwd )
422 static char cwdbuf[PATH_MAX];
425 ret = getcwd( cwdbuf, sizeof(cwdbuf));
426 if( ret == NULL ) SHOW_RESULT( getcwd, (int)ret );
428 if( strcmp( cwdbuf, cwd ) != 0 )
430 diag_printf( "cwdbuf %s cwd %s\n",cwdbuf, cwd );
431 CYG_TEST_FAIL( "Current directory mismatch");
435 //==========================================================================
438 int main( int argc, char **argv )
441 int existingdirents=-1;
442 #if defined(CYGSEM_FILEIO_BLOCK_USAGE)
443 struct cyg_fs_block_usage usage;
448 // --------------------------------------------------------------
450 err = mount( "/dev/disk0/1", "/", "fatfs" );
451 if( err < 0 ) SHOW_RESULT( mount, err );
454 if( err < 0 ) SHOW_RESULT( chdir, err );
458 listdir( "/", true, -1, &existingdirents );
460 // --------------------------------------------------------------
461 #if defined(CYGSEM_FILEIO_BLOCK_USAGE)
462 err = cyg_fs_getinfo("/", FS_INFO_BLOCK_USAGE, &usage, sizeof(usage));
463 if( err < 0 ) SHOW_RESULT( cyg_fs_getinfo, err );
464 diag_printf("<INFO>: total size: %6lld blocks, %10lld bytes\n",
465 usage.total_blocks, usage.total_blocks * usage.block_size);
466 diag_printf("<INFO>: free size: %6lld blocks, %10lld bytes\n",
467 usage.free_blocks, usage.free_blocks * usage.block_size);
468 diag_printf("<INFO>: block size: %6u bytes\n", usage.block_size);
470 // --------------------------------------------------------------
472 createfile( "/foo", 20257 );
474 copyfile( "foo", "fee");
476 comparefiles( "foo", "/fee" );
477 diag_printf("<INFO>: mkdir bar\n");
478 err = mkdir( "/bar", 0 );
479 if( err < 0 ) SHOW_RESULT( mkdir, err );
481 listdir( "/" , true, existingdirents+3, NULL );
483 copyfile( "fee", "/bar/fum" );
484 checkfile( "bar/fum" );
485 comparefiles( "/fee", "bar/fum" );
487 diag_printf("<INFO>: cd bar\n");
488 err = chdir( "bar" );
489 if( err < 0 ) SHOW_RESULT( chdir, err );
493 diag_printf("<INFO>: rename /foo bundy\n");
494 err = rename( "/foo", "bundy" );
495 if( err < 0 ) SHOW_RESULT( rename, err );
497 listdir( "/", true, existingdirents+2, NULL );
498 listdir( "" , true, 4, NULL );
500 checkfile( "/bar/bundy" );
501 comparefiles("/fee", "bundy" );
503 #if defined(CYGSEM_FILEIO_BLOCK_USAGE)
504 err = cyg_fs_getinfo("/", FS_INFO_BLOCK_USAGE, &usage, sizeof(usage));
505 if( err < 0 ) SHOW_RESULT( cyg_fs_getinfo, err );
506 diag_printf("<INFO>: total size: %6lld blocks, %10lld bytes\n",
507 usage.total_blocks, usage.total_blocks * usage.block_size);
508 diag_printf("<INFO>: free size: %6lld blocks, %10lld bytes\n",
509 usage.free_blocks, usage.free_blocks * usage.block_size);
510 diag_printf("<INFO>: block size: %6u bytes\n", usage.block_size);
512 // --------------------------------------------------------------
514 diag_printf("<INFO>: unlink fee\n");
515 err = unlink( "/fee" );
516 if( err < 0 ) SHOW_RESULT( unlink, err );
518 diag_printf("<INFO>: unlink fum\n");
519 err = unlink( "fum" );
520 if( err < 0 ) SHOW_RESULT( unlink, err );
522 diag_printf("<INFO>: unlink /bar/bundy\n");
523 err = unlink( "/bar/bundy" );
524 if( err < 0 ) SHOW_RESULT( unlink, err );
526 diag_printf("<INFO>: cd /\n");
528 if( err < 0 ) SHOW_RESULT( chdir, err );
532 diag_printf("<INFO>: rmdir /bar\n");
533 err = rmdir( "/bar" );
534 if( err < 0 ) SHOW_RESULT( rmdir, err );
536 listdir( "/", false, existingdirents, NULL );
538 // --------------------------------------------------------------
541 diag_printf("<INFO>: mkdir disk2\n");
542 err = mkdir( "/disk2", 0 );
543 if( err < 0 ) SHOW_RESULT( mkdir, err );
545 diag_printf("<INFO>: mount /disk2\n");
546 err = mount( "/dev/disk0/2", "/disk2", "fatfs" );
547 if( err < 0 ) SHOW_RESULT( mount, err );
550 listdir( "/disk2" , true, -1, &existingdirents);
552 createfile( "/disk2/tinky", 4567 );
553 copyfile( "/disk2/tinky", "/disk2/laalaa" );
554 checkfile( "/disk2/tinky");
555 checkfile( "/disk2/laalaa");
556 comparefiles( "/disk2/tinky", "/disk2/laalaa" );
558 diag_printf("<INFO>: cd /disk2\n");
559 err = chdir( "/disk2" );
560 if( err < 0 ) SHOW_RESULT( chdir, err );
562 checkcwd( "/disk2" );
564 diag_printf("<INFO>: mkdir noonoo\n");
565 err = mkdir( "noonoo", 0 );
566 if( err < 0 ) SHOW_RESULT( mkdir, err );
568 listdir( "/disk2" , true, existingdirents+3, NULL);
570 diag_printf("<INFO>: cd noonoo\n");
571 err = chdir( "noonoo" );
572 if( err < 0 ) SHOW_RESULT( chdir, err );
574 checkcwd( "/disk2/noonoo" );
576 createfile( "tinky", 6789 );
577 checkfile( "tinky" );
579 createfile( "dipsy", 34567 );
580 checkfile( "dipsy" );
581 copyfile( "dipsy", "po" );
583 comparefiles( "dipsy", "po" );
585 listdir( ".", true, 5, NULL );
586 listdir( "", true, 5, NULL );
587 listdir( "..", true, existingdirents+3, NULL );
589 // --------------------------------------------------------------
591 diag_printf("<INFO>: unlink tinky\n");
592 err = unlink( "tinky" );
593 if( err < 0 ) SHOW_RESULT( unlink, err );
595 diag_printf("<INFO>: unlink dipsy\n");
596 err = unlink( "dipsy" );
597 if( err < 0 ) SHOW_RESULT( unlink, err );
599 diag_printf("<INFO>: unlink po\n");
600 err = unlink( "po" );
601 if( err < 0 ) SHOW_RESULT( unlink, err );
603 diag_printf("<INFO>: cd ..\n");
605 if( err < 0 ) SHOW_RESULT( chdir, err );
606 checkcwd( "/disk2" );
608 diag_printf("<INFO>: rmdir noonoo\n");
609 err = rmdir( "noonoo" );
610 if( err < 0 ) SHOW_RESULT( rmdir, err );
612 // --------------------------------------------------------------
614 err = mkdir( "x", 0 );
615 if( err < 0 ) SHOW_RESULT( mkdir, err );
617 err = mkdir( "x/y", 0 );
618 if( err < 0 ) SHOW_RESULT( mkdir, err );
620 err = mkdir( "x/y/z", 0 );
621 if( err < 0 ) SHOW_RESULT( mkdir, err );
623 err = mkdir( "x/y/z/w", 0 );
624 if( err < 0 ) SHOW_RESULT( mkdir, err );
626 diag_printf("<INFO>: cd /disk2/x/y/z/w\n");
627 err = chdir( "/disk2/x/y/z/w" );
628 if( err < 0 ) SHOW_RESULT( chdir, err );
629 checkcwd( "/disk2/x/y/z/w" );
631 diag_printf("<INFO>: cd ..\n");
633 if( err < 0 ) SHOW_RESULT( chdir, err );
634 checkcwd( "/disk2/x/y/z" );
636 diag_printf("<INFO>: cd .\n");
638 if( err < 0 ) SHOW_RESULT( chdir, err );
639 checkcwd( "/disk2/x/y/z" );
641 diag_printf("<INFO>: cd ../../y\n");
642 err = chdir( "../../y" );
643 if( err < 0 ) SHOW_RESULT( chdir, err );
644 checkcwd( "/disk2/x/y" );
646 diag_printf("<INFO>: cd ../..\n");
647 err = chdir( "../.." );
648 if( err < 0 ) SHOW_RESULT( chdir, err );
649 checkcwd( "/disk2" );
651 diag_printf("<INFO>: rmdir x/y/z/w\n");
652 err = rmdir( "x/y/z/w" );
653 if( err < 0 ) SHOW_RESULT( rmdir, err );
655 diag_printf("<INFO>: rmdir x/y/z\n");
656 err = rmdir( "x/y/z" );
657 if( err < 0 ) SHOW_RESULT( rmdir, err );
659 diag_printf("<INFO>: rmdir x/y\n");
660 err = rmdir( "x/y" );
661 if( err < 0 ) SHOW_RESULT( rmdir, err );
663 diag_printf("<INFO>: rmdir x\n");
665 if( err < 0 ) SHOW_RESULT( rmdir, err );
667 // --------------------------------------------------------------
669 checkcwd( "/disk2" );
671 diag_printf("<INFO>: unlink tinky\n");
672 err = unlink( "tinky" );
673 if( err < 0 ) SHOW_RESULT( unlink, err );
675 diag_printf("<INFO>: unlink laalaa\n");
676 err = unlink( "laalaa" );
677 if( err < 0 ) SHOW_RESULT( unlink, err );
679 diag_printf("<INFO>: cd /\n");
681 if( err < 0 ) SHOW_RESULT( chdir, err );
684 listdir( "/disk2", true, -1, NULL );
687 diag_printf("<INFO>: rmdir dir\n");
688 err = rmdir( "disk2" );
689 if( err < 0 ) SHOW_RESULT( rmdir, err );
691 diag_printf("<INFO>: umount /disk2\n");
692 err = umount( "/disk2" );
693 if( err < 0 ) SHOW_RESULT( umount, err );
696 #ifdef CYGCFG_FS_FAT_USE_ATTRIBUTES
698 diag_printf("<INFO>: create /foo\n");
699 createfile( "/foo", 20257 );
701 // Verify it is created with archive bit set
702 checkattrib( "/foo", S_FATFS_ARCHIVE );
705 diag_printf("<INFO>: attrib -A+S /foo\n");
706 err = cyg_fs_set_attrib( "/foo", S_FATFS_SYSTEM );
707 if( err < 0 ) SHOW_RESULT( chmod system , err );
709 // Verify it is now System
710 checkattrib( "/foo", S_FATFS_SYSTEM );
713 diag_printf("<INFO>: attrib -S+H /foo\n");
714 err = cyg_fs_set_attrib( "/foo", S_FATFS_HIDDEN );
715 if( err < 0 ) SHOW_RESULT( chmod system , err );
717 // Verify it is now Hidden
718 checkattrib( "/foo", S_FATFS_HIDDEN );
721 diag_printf("<INFO>: attrib -H+R /foo\n");
722 err = cyg_fs_set_attrib( "/foo", S_FATFS_RDONLY );
723 if( err < 0 ) SHOW_RESULT( chmod system , err );
725 // Verify it is now Read-only
726 checkattrib( "/foo", S_FATFS_RDONLY );
728 // Verify we cannot unlink a read-only file
729 diag_printf("<INFO>: unlink /foo\n");
730 err = unlink( "/foo" );
731 if( (err != -1) || (errno != EPERM) ) SHOW_RESULT( unlink, err );
733 // Verify we cannot rename a read-only file
734 diag_printf("<INFO>: rename /foo bundy\n");
735 err = rename( "/foo", "bundy" );
736 if( (err != -1) || (errno != EPERM) ) SHOW_RESULT( rename, err );
738 // Verify we cannot open read-only file for writing
740 diag_printf("<INFO>: create file /foo\n");
741 fd = open( "/foo", O_WRONLY );
742 if( (err != -1) || (errno != EACCES) ) SHOW_RESULT( open, err );
743 if( err > 0 ) close(fd);
746 diag_printf("<INFO>: attrib -H /foo\n");
747 err = cyg_fs_set_attrib( "/foo", 0 );
748 if( err < 0 ) SHOW_RESULT( chmod none , err );
750 // Verify it is now nothing
751 checkattrib( "/foo", 0 );
753 // Now delete our test file
754 diag_printf("<INFO>: unlink /foo\n");
755 err = unlink( "/foo" );
756 if( err < 0 ) SHOW_RESULT( unlink, err );
758 #endif // CYGCFG_FS_FAT_USE_ATTRIBUTES
762 listdir( "/", true, -1, NULL );
764 diag_printf("<INFO>: unlink file.max\n");
765 err = unlink( "file.max" );
766 if( err < 0 ) SHOW_RESULT( unlink, err );
767 diag_printf("<INFO>: umount /\n");
769 if( err < 0 ) SHOW_RESULT( umount, err );
771 CYG_TEST_PASS_FINISH("fatfs1");
774 // -------------------------------------------------------------------------