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, richard.panton@3glab.com, jlarmour
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
50 //####DESCRIPTIONEND####
52 //==========================================================================
54 #include <pkgconf/hal.h>
55 #include <pkgconf/io_fileio.h>
56 #include <pkgconf/isoinfra.h>
57 #include <pkgconf/system.h>
67 #include <cyg/fileio/fileio.h>
69 #include <cyg/infra/testcase.h>
70 #include <cyg/infra/diag.h> // HAL polled output
72 #include <cyg/romfs/testromfs.h> // Test ROMFS data
74 //==========================================================================
76 MTAB_ENTRY( romfs_mte1,
80 (CYG_ADDRWORD) &filedata[0] );
83 //==========================================================================
85 #define SHOW_RESULT( _fn, _res ) \
86 diag_printf("<FAIL>: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
88 #define CHKFAIL_TYPE( _fn, _res, _type ) { \
90 diag_printf("<FAIL>: " #_fn "() returned %d (expected -1)\n", _res); \
91 else if ( errno != _type ) \
92 diag_printf("<FAIL>: " #_fn "() failed with errno %d (%s),\n expected %d (%s)\n", errno, strerror(errno), _type, strerror(_type) ); \
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 CYGINT_ISO_STRING_STRFUNCS
107 char *strcat( char *s1, const char *s2 )
111 while( (*s1++ = *s2++) != 0);
117 //==========================================================================
119 static void listdir( char *name, int statp )
124 diag_printf("<INFO>: reading directory %s\n",name);
126 dirp = opendir( name );
127 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
131 struct dirent *entry = readdir( dirp );
136 diag_printf("<INFO>: entry %14s",entry->d_name);
139 char fullname[PATH_MAX];
144 strcpy(fullname, name );
145 if( !(name[0] == '/' && name[1] == 0 ) )
146 strcat(fullname, "/" );
148 else fullname[0] = 0;
150 strcat(fullname, entry->d_name );
152 err = stat( fullname, &sbuf );
155 if( errno == ENOSYS )
156 diag_printf(" <no status available>");
157 else SHOW_RESULT( stat, err );
161 diag_printf(" [mode %08x ino %08x nlink %d size %d]",
162 sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size);
169 err = closedir( dirp );
170 if( err < 0 ) SHOW_RESULT( stat, err );
173 //==========================================================================
176 static void copyfile( char *name2, char *name1 )
184 diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
186 err = access( name1, F_OK );
187 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
189 err = access( name2, F_OK );
190 if( err != 0 ) SHOW_RESULT( access, err );
192 fd1 = open( name1, O_WRONLY|O_CREAT );
193 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
195 fd2 = open( name2, O_RDONLY );
196 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
200 done = read( fd2, buf, IOSIZE );
201 if( done < 0 ) SHOW_RESULT( read, done );
203 if( done == 0 ) break;
205 wrote = write( fd1, buf, done );
206 if( wrote != done ) SHOW_RESULT( write, wrote );
208 if( wrote != done ) break;
212 if( err < 0 ) SHOW_RESULT( close, err );
215 if( err < 0 ) SHOW_RESULT( close, err );
220 //==========================================================================
222 static void comparefiles( char *name2, char *name1 )
228 ssize_t done1, done2;
231 diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
233 err = access( name1, F_OK );
234 if( err != 0 ) SHOW_RESULT( access, err );
236 err = access( name1, F_OK );
237 if( err != 0 ) SHOW_RESULT( access, err );
239 fd1 = open( name1, O_RDONLY );
240 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
242 fd2 = open( name2, O_RDONLY );
243 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
247 done1 = read( fd1, buf1, IOSIZE );
248 if( done1 < 0 ) SHOW_RESULT( read, done1 );
250 done2 = read( fd2, buf2, IOSIZE );
251 if( done2 < 0 ) SHOW_RESULT( read, done2 );
254 diag_printf("Files different sizes\n");
256 if( done1 == 0 ) break;
258 for( i = 0; i < done1; i++ )
259 if( buf1[i] != buf2[i] )
261 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
262 CYG_TEST_FAIL("Data in files not equal\n");
267 if( err < 0 ) SHOW_RESULT( close, err );
270 if( err < 0 ) SHOW_RESULT( close, err );
274 //==========================================================================
277 int main( int argc, char **argv )
284 // --------------------------------------------------------------
286 diag_printf("<INFO>: ROMFS root follows\n");
287 listdir( "/", true );
289 diag_printf("<INFO>: cd /etc\n" );
290 err = chdir( "/etc" );
291 if ( err < 0 ) SHOW_RESULT( chdir, err );
293 diag_printf("<INFO>: ROMFS list of '' follows\n");
296 diag_printf("<INFO>: ROMFS list of /etc follows\n");
297 listdir( "/etc", true );
299 diag_printf("<INFO>: ROMFS list of . follows\n");
300 listdir( ".", true );
303 err = mount( "", "/var", "ramfs" );
304 if( err < 0 ) SHOW_RESULT( mount, err );
306 copyfile( "/etc/passwd", "/var/passwd_copy" );
308 comparefiles( "/etc/passwd", "/var/passwd_copy" );
311 diag_printf("<INFO>: ROMFS list of / follows\n");
313 diag_printf("<INFO>: Note that /var now gives stat() info for RAMFS\n");
315 listdir( "/", true );
317 diag_printf("<INFO>: Mount ROMFS again onto /mnt\n");
318 sprintf( address, "%p", (void*)&filedata[0] );
319 err = mount( address, "/mnt", "romfs" );
320 if( err < 0 ) SHOW_RESULT( mount, err );
322 comparefiles( "/etc/passwd", "/mnt/etc/passwd" );
325 err = mkdir( "/foo", 0 );
326 CHKFAIL_TYPE( mkdir, err, EROFS );
328 err = rename( "/var", "/tmp" ); // RAMFS is mounted here
330 CHKFAIL_TYPE( rename, err, EXDEV );
332 CHKFAIL_TYPE( rename, err, EROFS );
335 err = rename( "/var/passwd_copy", "/mnt/etc/passwd_copy" );
336 CHKFAIL_TYPE( rename, err, EXDEV );
338 err = rename( "/etc", "/tmp" );
339 CHKFAIL_TYPE( rename, err, EROFS );
341 diag_printf("<INFO>: cd /etc\n");
342 err = chdir( "/etc" );
343 if( err < 0 ) SHOW_RESULT( chdir, err );
345 err = chdir( "/mnt/etc" );
346 if( err < 0 ) SHOW_RESULT( chdir, err );
348 listdir( ".", true );
350 diag_printf("<INFO>: unlink /tmp\n");
351 err = unlink( "/tmp" );
352 CHKFAIL_TYPE( unlink, err, EROFS );
354 diag_printf("<INFO>: mount random area\n");
355 sprintf(address, "%p", (void*)(&filedata[0] + 0x100));
356 err = mount( address, "/tmp", "romfs" );
357 CHKFAIL_TYPE( mount, err, ENOENT );
359 err = umount( "/mnt" );
360 if( err < 0 ) SHOW_RESULT( umount, err );
362 err = umount( "/var" );
364 if( err < 0 ) SHOW_RESULT( umount, err );
366 CHKFAIL_TYPE( umount, err, EINVAL );
370 if( err < 0 ) SHOW_RESULT( umount, err );
373 CYG_TEST_PASS_FINISH("fileio1");
376 // -------------------------------------------------------------------------