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/io_fileio.h>
63 #include <cyg/infra/cyg_trac.h> // tracing macros
64 #include <cyg/infra/cyg_ass.h> // assertion macros
73 #include <cyg/infra/testcase.h>
74 #include <cyg/infra/diag.h> // HAL polled output
76 //==========================================================================
77 // Include the test filesystem.
78 // If we could make tests out of multiple files, then we could just link
79 // against the object file for this rather than including it.
83 //==========================================================================
85 #define SHOW_RESULT( _fn, _res ) \
86 diag_printf("<INFO>: " #_fn "() returned %ld %s\n", (long)_res, _res<0?strerror(errno):"");
88 //==========================================================================
92 //==========================================================================
94 static void listdir( char *name, int statp )
99 diag_printf("<INFO>: reading directory %s\n",name);
101 dirp = opendir( name );
102 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
106 struct dirent *entry = readdir( dirp );
111 diag_printf("<INFO>: entry %14s",entry->d_name);
114 char fullname[PATH_MAX];
119 strcpy(fullname, name );
120 if( !(name[0] == '/' && name[1] == 0 ) )
121 strcat(fullname, "/" );
123 else fullname[0] = 0;
125 strcat(fullname, entry->d_name );
127 err = stat( fullname, &sbuf );
130 if( errno == ENOSYS )
131 diag_printf(" <no status available>");
132 else SHOW_RESULT( stat, err );
136 diag_printf(" [mode %08x nlink %d size %ld]",
137 sbuf.st_mode,sbuf.st_nlink,sbuf.st_size);
144 err = closedir( dirp );
145 if( err < 0 ) SHOW_RESULT( stat, err );
148 //==========================================================================
150 static void createfile( char *name, size_t size )
158 diag_printf("<INFO>: create file %s size %d\n",name,size);
160 err = access( name, F_OK );
161 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
163 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
165 fd = open( name, O_WRONLY|O_CREAT );
166 if( fd < 0 ) SHOW_RESULT( open, fd );
171 if ( len > IOSIZE ) len = IOSIZE;
173 wrote = write( fd, buf, len );
174 if( wrote != len ) SHOW_RESULT( write, wrote );
180 if( err < 0 ) SHOW_RESULT( close, err );
183 //==========================================================================
185 static void maxfile( char *name )
194 diag_printf("<INFO>: create maximal file %s\n",name);
196 err = access( name, F_OK );
197 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
199 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
201 fd = open( name, O_WRONLY|O_CREAT );
202 if( fd < 0 ) SHOW_RESULT( open, fd );
206 wrote = write( fd, buf, IOSIZE );
207 if( wrote < 0 ) SHOW_RESULT( write, wrote );
211 } while( wrote == IOSIZE );
213 diag_printf("<INFO>: file size == %d\n",size);
216 if( err < 0 ) SHOW_RESULT( close, err );
219 //==========================================================================
221 static void checkfile( char *name )
229 diag_printf("<INFO>: check file %s\n",name);
231 err = access( name, F_OK );
232 if( err != 0 ) SHOW_RESULT( access, err );
234 fd = open( name, O_RDONLY );
235 if( fd < 0 ) SHOW_RESULT( open, fd );
239 done = read( fd, buf, IOSIZE );
240 if( done < 0 ) SHOW_RESULT( read, done );
242 if( done == 0 ) break;
244 for( i = 0; i < done; i++ )
245 if( buf[i] != i%256 )
247 diag_printf("buf[%d](%02x) != %02x\n",i,buf[i],i%256);
248 CYG_TEST_FAIL("Data read not equal to data written\n");
253 if( err < 0 ) SHOW_RESULT( close, err );
256 //==========================================================================
258 static void copyfile( char *name2, char *name1 )
266 diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
268 err = access( name1, F_OK );
269 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
271 err = access( name2, F_OK );
272 if( err != 0 ) SHOW_RESULT( access, err );
274 fd1 = open( name1, O_WRONLY|O_CREAT );
275 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
277 fd2 = open( name2, O_RDONLY );
278 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
282 done = read( fd2, buf, IOSIZE );
283 if( done < 0 ) SHOW_RESULT( read, done );
285 if( done == 0 ) break;
287 wrote = write( fd1, buf, done );
288 if( wrote != done ) SHOW_RESULT( write, wrote );
290 if( wrote != done ) break;
294 if( err < 0 ) SHOW_RESULT( close, err );
297 if( err < 0 ) SHOW_RESULT( close, err );
301 //==========================================================================
303 static void comparefiles( char *name2, char *name1 )
309 ssize_t done1, done2;
312 diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
314 err = access( name1, F_OK );
315 if( err != 0 ) SHOW_RESULT( access, err );
317 err = access( name1, F_OK );
318 if( err != 0 ) SHOW_RESULT( access, err );
320 fd1 = open( name1, O_RDONLY );
321 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
323 fd2 = open( name2, O_RDONLY );
324 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
328 done1 = read( fd1, buf1, IOSIZE );
329 if( done1 < 0 ) SHOW_RESULT( read, done1 );
331 done2 = read( fd2, buf2, IOSIZE );
332 if( done2 < 0 ) SHOW_RESULT( read, done2 );
335 diag_printf("Files different sizes\n");
337 if( done1 == 0 ) break;
339 for( i = 0; i < done1; i++ )
340 if( buf1[i] != buf2[i] )
342 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
343 CYG_TEST_FAIL("Data in files not equal\n");
348 if( err < 0 ) SHOW_RESULT( close, err );
351 if( err < 0 ) SHOW_RESULT( close, err );
355 //==========================================================================
358 int cyg_user_start(void)
364 // --------------------------------------------------------------
366 createfile( "/foo", 202 );
368 copyfile( "foo", "fee");
370 comparefiles( "foo", "/fee" );
372 err = mkdir( "/bar", 0 );
373 if( err < 0 ) SHOW_RESULT( mkdir, err );
375 listdir( "/" , false);
377 copyfile( "fee", "/bar/fum" );
378 checkfile( "bar/fum" );
379 comparefiles( "/fee", "bar/fum" );
382 err = chdir( "bar" );
383 if( err < 0 ) SHOW_RESULT( chdir, err );
385 err = rename( "/foo", "bundy" );
386 if( err < 0 ) SHOW_RESULT( rename, err );
388 listdir( "/", true );
389 listdir( "" , true );
391 checkfile( "/bar/bundy" );
392 comparefiles("/fee", "bundy" );
396 // --------------------------------------------------------------
398 err = unlink( "/fee" );
399 if( err < 0 ) SHOW_RESULT( unlink, err );
401 err = unlink( "fum" );
402 if( err < 0 ) SHOW_RESULT( unlink, err );
404 err = unlink( "/bar/bundy" );
405 if( err < 0 ) SHOW_RESULT( unlink, err );
408 if( err < 0 ) SHOW_RESULT( chdir, err );
410 err = rmdir( "/bar" );
411 if( err < 0 ) SHOW_RESULT( rmdir, err );
413 listdir( "/", false );
415 // --------------------------------------------------------------
417 err = mount( "", "/ram", "testfs" );
418 if( err < 0 ) SHOW_RESULT( mount, err );
420 createfile( "/ram/tinky", 456 );
421 copyfile( "/ram/tinky", "/ram/laalaa" );
422 checkfile( "/ram/tinky");
423 checkfile( "/ram/laalaa");
424 comparefiles( "/ram/tinky", "/ram/laalaa" );
426 err = chdir( "/ram" );
427 if( err < 0 ) SHOW_RESULT( chdir, err );
429 createfile( "tinky", 678 );
430 checkfile( "tinky" );
433 checkfile( "dipsy" );
434 copyfile( "dipsy", "po" );
436 comparefiles( "dipsy", "po" );
440 // --------------------------------------------------------------
442 err = unlink( "tinky" );
443 if( err < 0 ) SHOW_RESULT( unlink, err );
445 err = unlink( "dipsy" );
446 if( err < 0 ) SHOW_RESULT( unlink, err );
448 err = unlink( "po" );
449 if( err < 0 ) SHOW_RESULT( unlink, err );
451 err = unlink( "laalaa" );
452 if( err < 0 ) SHOW_RESULT( unlink, err );
455 if( err < 0 ) SHOW_RESULT( chdir, err );
457 err = umount( "/ram" );
458 if( err < 0 ) SHOW_RESULT( umount, err );
460 CYG_TEST_PASS_FINISH("fileio1");
463 // -------------------------------------------------------------------------