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, richard.panton@3glab.com, jlarmour
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
51 //####DESCRIPTIONEND####
53 //==========================================================================
55 #include <pkgconf/hal.h>
56 #include <pkgconf/io_fileio.h>
57 #include <pkgconf/isoinfra.h>
58 #include <pkgconf/system.h>
68 #include <cyg/fileio/fileio.h>
70 #include <cyg/infra/cyg_type.h>
71 #include <cyg/infra/testcase.h>
72 #include <cyg/infra/diag.h> // HAL polled output
74 // Test ROMFS data. Two example data files are generated so that
75 // the test will work on both big-endian and little-endian targets.
76 #if (CYG_BYTEORDER == CYG_LSBFIRST)
77 # include <cyg/romfs/testromfs_le.h>
79 # include <cyg/romfs/testromfs_be.h>
82 //==========================================================================
84 MTAB_ENTRY( romfs_mte1,
88 (CYG_ADDRWORD) &filedata[0] );
91 //==========================================================================
93 #define SHOW_RESULT( _fn, _res ) \
94 diag_printf("<FAIL>: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
96 #define CHKFAIL_TYPE( _fn, _res, _type ) { \
98 diag_printf("<FAIL>: " #_fn "() returned %d (expected -1)\n", _res); \
99 else if ( errno != _type ) \
100 diag_printf("<FAIL>: " #_fn "() failed with errno %d (%s),\n expected %d (%s)\n", errno, strerror(errno), _type, strerror(_type) ); \
103 //==========================================================================
107 #define LONGNAME1 "long_file_name_that_should_take_up_more_than_one_directory_entry_1"
108 #define LONGNAME2 "long_file_name_that_should_take_up_more_than_one_directory_entry_2"
111 //==========================================================================
113 #ifndef CYGINT_ISO_STRING_STRFUNCS
115 char *strcat( char *s1, const char *s2 )
119 while( (*s1++ = *s2++) != 0);
125 //==========================================================================
127 static void listdir( char *name, int statp )
132 diag_printf("<INFO>: reading directory %s\n",name);
134 dirp = opendir( name );
135 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
139 struct dirent *entry = readdir( dirp );
144 diag_printf("<INFO>: entry %14s",entry->d_name);
147 char fullname[PATH_MAX];
152 strcpy(fullname, name );
153 if( !(name[0] == '/' && name[1] == 0 ) )
154 strcat(fullname, "/" );
156 else fullname[0] = 0;
158 strcat(fullname, entry->d_name );
160 err = stat( fullname, &sbuf );
163 if( errno == ENOSYS )
164 diag_printf(" <no status available>");
165 else SHOW_RESULT( stat, err );
169 diag_printf(" [mode %08x ino %08x nlink %d size %d]",
170 sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size);
177 err = closedir( dirp );
178 if( err < 0 ) SHOW_RESULT( stat, err );
181 //==========================================================================
184 static void copyfile( char *name2, char *name1 )
192 diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
194 err = access( name1, F_OK );
195 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
197 err = access( name2, F_OK );
198 if( err != 0 ) SHOW_RESULT( access, err );
200 fd1 = open( name1, O_WRONLY|O_CREAT );
201 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
203 fd2 = open( name2, O_RDONLY );
204 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
208 done = read( fd2, buf, IOSIZE );
209 if( done < 0 ) SHOW_RESULT( read, done );
211 if( done == 0 ) break;
213 wrote = write( fd1, buf, done );
214 if( wrote != done ) SHOW_RESULT( write, wrote );
216 if( wrote != done ) break;
220 if( err < 0 ) SHOW_RESULT( close, err );
223 if( err < 0 ) SHOW_RESULT( close, err );
228 //==========================================================================
230 static void comparefiles( char *name2, char *name1 )
236 ssize_t done1, done2;
239 diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
241 err = access( name1, F_OK );
242 if( err != 0 ) SHOW_RESULT( access, err );
244 err = access( name1, F_OK );
245 if( err != 0 ) SHOW_RESULT( access, err );
247 fd1 = open( name1, O_RDONLY );
248 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
250 fd2 = open( name2, O_RDONLY );
251 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
255 done1 = read( fd1, buf1, IOSIZE );
256 if( done1 < 0 ) SHOW_RESULT( read, done1 );
258 done2 = read( fd2, buf2, IOSIZE );
259 if( done2 < 0 ) SHOW_RESULT( read, done2 );
262 diag_printf("Files different sizes\n");
264 if( done1 == 0 ) break;
266 for( i = 0; i < done1; i++ )
267 if( buf1[i] != buf2[i] )
269 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
270 CYG_TEST_FAIL("Data in files not equal\n");
275 if( err < 0 ) SHOW_RESULT( close, err );
278 if( err < 0 ) SHOW_RESULT( close, err );
282 //==========================================================================
285 int main( int argc, char **argv )
292 // --------------------------------------------------------------
294 diag_printf("<INFO>: ROMFS root follows\n");
295 listdir( "/", true );
297 diag_printf("<INFO>: cd /etc\n" );
298 err = chdir( "/etc" );
300 SHOW_RESULT( chdir, err );
301 CYG_TEST_FAIL_FINISH("romfs1");
304 diag_printf("<INFO>: ROMFS list of '' follows\n");
307 diag_printf("<INFO>: ROMFS list of /etc follows\n");
308 listdir( "/etc", true );
310 diag_printf("<INFO>: ROMFS list of . follows\n");
311 listdir( ".", true );
314 err = mount( "", "/var", "ramfs" );
315 if( err < 0 ) SHOW_RESULT( mount, err );
317 copyfile( "/etc/passwd", "/var/passwd_copy" );
319 comparefiles( "/etc/passwd", "/var/passwd_copy" );
322 diag_printf("<INFO>: ROMFS list of / follows\n");
324 diag_printf("<INFO>: Note that /var now gives stat() info for RAMFS\n");
326 listdir( "/", true );
328 diag_printf("<INFO>: Mount ROMFS again onto /mnt\n");
329 sprintf( address, "%p", (void*)&filedata[0] );
330 err = mount( address, "/mnt", "romfs" );
331 if( err < 0 ) SHOW_RESULT( mount, err );
333 comparefiles( "/etc/passwd", "/mnt/etc/passwd" );
336 err = mkdir( "/foo", 0 );
337 CHKFAIL_TYPE( mkdir, err, EROFS );
339 err = rename( "/var", "/tmp" ); // RAMFS is mounted here
341 CHKFAIL_TYPE( rename, err, EXDEV );
343 CHKFAIL_TYPE( rename, err, EROFS );
346 err = rename( "/var/passwd_copy", "/mnt/etc/passwd_copy" );
347 CHKFAIL_TYPE( rename, err, EXDEV );
349 err = rename( "/etc", "/tmp" );
350 CHKFAIL_TYPE( rename, err, EROFS );
352 diag_printf("<INFO>: cd /etc\n");
353 err = chdir( "/etc" );
354 if( err < 0 ) SHOW_RESULT( chdir, err );
356 err = chdir( "/mnt/etc" );
357 if( err < 0 ) SHOW_RESULT( chdir, err );
359 listdir( ".", true );
361 diag_printf("<INFO>: unlink /tmp\n");
362 err = unlink( "/tmp" );
363 CHKFAIL_TYPE( unlink, err, EROFS );
365 diag_printf("<INFO>: mount random area\n");
366 sprintf(address, "%p", (void*)(&filedata[0] + 0x100));
367 err = mount( address, "/tmp", "romfs" );
368 CHKFAIL_TYPE( mount, err, ENOENT );
370 err = umount( "/mnt" );
371 if( err < 0 ) SHOW_RESULT( umount, err );
373 err = umount( "/var" );
375 if( err < 0 ) SHOW_RESULT( umount, err );
377 CHKFAIL_TYPE( umount, err, EINVAL );
381 if( err < 0 ) SHOW_RESULT( umount, err );
384 CYG_TEST_PASS_FINISH("romfs1");
387 // -------------------------------------------------------------------------