]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/fs/rom/v2_0/tests/romfs1.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / fs / rom / v2_0 / tests / romfs1.c
1 //==========================================================================
2 //
3 //      romfs1.c
4 //
5 //      Test fileio system
6 //
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
13 //
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.
17 //
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
21 // for more details.
22 //
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.
26 //
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.
33 //
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.
36 //
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####
43 //
44 // Author(s):           nickg
45 // Contributors:        nickg, richard.panton@3glab.com, jlarmour
46 // Date:                2000-05-25
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
50 //
51 //####DESCRIPTIONEND####
52 //
53 //==========================================================================
54
55 #include <pkgconf/hal.h>
56 #include <pkgconf/io_fileio.h>
57 #include <pkgconf/isoinfra.h>
58 #include <pkgconf/system.h>
59 #include <pkgconf/fs_rom.h>
60
61 #include <unistd.h>
62 #include <fcntl.h>
63 #include <sys/stat.h>
64 #include <errno.h>
65 #include <string.h>
66 #include <dirent.h>
67 #include <stdio.h>
68
69 #include <cyg/fileio/fileio.h>
70
71 #include <cyg/infra/cyg_type.h>
72 #include <cyg/infra/testcase.h>
73 #include <cyg/infra/diag.h>            // HAL polled output
74
75 // Test ROMFS data. Two example data files are generated so that
76 // the test will work on both big-endian and little-endian targets.
77 #if (CYG_BYTEORDER == CYG_LSBFIRST)
78 # include <cyg/romfs/testromfs_le.h>
79 #else
80 # include <cyg/romfs/testromfs_be.h>
81 #endif
82
83 //==========================================================================
84
85 MTAB_ENTRY( romfs_mte1,
86                    "/",
87                    "romfs",
88                    "",
89                    (CYG_ADDRWORD) &filedata[0] );
90
91
92 //==========================================================================
93
94 #define SHOW_RESULT( _fn, _res ) \
95   diag_printf("<FAIL>: " #_fn "() returned %d %s\n", (int)_res, _res<0?strerror(errno):"");
96
97 #define CHKFAIL_TYPE( _fn, _res, _type ) { \
98 if ( _res != -1 ) \
99   diag_printf("<FAIL>: " #_fn "() returned %d (expected -1)\n", (int)_res); \
100 else if ( errno != _type ) \
101     diag_printf("<FAIL>: " #_fn "() failed with errno %d (%s),\n    expected %d (%s)\n", errno, strerror(errno), _type, strerror(_type) ); \
102 }
103
104 //==========================================================================
105
106 #define IOSIZE  100
107
108 #define LONGNAME1       "long_file_name_that_should_take_up_more_than_one_directory_entry_1"
109 #define LONGNAME2       "long_file_name_that_should_take_up_more_than_one_directory_entry_2"
110
111
112 //==========================================================================
113
114 #ifndef CYGINT_ISO_STRING_STRFUNCS
115
116 char *strcat( char *s1, const char *s2 )
117 {
118     char *s = s1;
119     while( *s1 ) s1++;
120     while( (*s1++ = *s2++) != 0);
121     return s;
122 }
123
124 #endif
125
126 //==========================================================================
127
128 static void listdir( char *name, int statp )
129 {
130     int err;
131     DIR *dirp;
132     
133     diag_printf("<INFO>: reading directory %s\n",name);
134     
135     dirp = opendir( name );
136     if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
137
138     for(;;)
139     {
140         struct dirent *entry = readdir( dirp );
141         
142         if( entry == NULL )
143             break;
144
145         diag_printf("<INFO>: entry %14s",entry->d_name);
146 #ifdef CYGPKG_FS_ROM_RET_DIRENT_DTYPE
147         diag_printf(" d_type %2x", entry->d_type);
148 #endif
149         if( statp )
150         {
151             char fullname[PATH_MAX];
152             struct stat sbuf;
153
154             if( name[0] )
155             {
156                 strcpy(fullname, name );
157                 if( !(name[0] == '/' && name[1] == 0 ) )
158                     strcat(fullname, "/" );
159             }
160             else fullname[0] = 0;
161             
162             strcat(fullname, entry->d_name );
163             
164             err = stat( fullname, &sbuf );
165             if( err < 0 )
166             {
167                 if( errno == ENOSYS )
168                     diag_printf(" <no status available>");
169                 else SHOW_RESULT( stat, err );
170             }
171             else
172             {
173                 diag_printf(" [mode %08x ino %08x nlink %d size %ld]",
174                             sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size);
175             }
176 #ifdef CYGPKG_FS_ROM_RET_DIRENT_DTYPE
177             if ((entry->d_type & S_IFMT) != (sbuf.st_mode & S_IFMT))
178               CYG_TEST_FAIL("File mode's don't match between dirent and stat");
179 #endif
180         }
181
182         diag_printf("\n");
183     }
184
185     err = closedir( dirp );
186     if( err < 0 ) SHOW_RESULT( stat, err );
187 }
188
189 //==========================================================================
190
191 #ifdef CYGPKG_FS_RAM
192 static void copyfile( char *name2, char *name1 )
193 {
194
195     int err;
196     char buf[IOSIZE];
197     int fd1, fd2;
198     ssize_t done, wrote;
199
200     diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
201
202     err = access( name1, F_OK );
203     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
204
205     err = access( name2, F_OK );
206     if( err != 0 ) SHOW_RESULT( access, err );
207     
208     fd1 = open( name1, O_WRONLY|O_CREAT );
209     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
210
211     fd2 = open( name2, O_RDONLY );
212     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
213     
214     for(;;)
215     {
216         done = read( fd2, buf, IOSIZE );
217         if( done < 0 ) SHOW_RESULT( read, done );
218
219         if( done == 0 ) break;
220
221         wrote = write( fd1, buf, done );
222         if( wrote != done ) SHOW_RESULT( write, wrote );
223
224         if( wrote != done ) break;
225     }
226
227     err = close( fd1 );
228     if( err < 0 ) SHOW_RESULT( close, err );
229
230     err = close( fd2 );
231     if( err < 0 ) SHOW_RESULT( close, err );
232     
233 }
234 #endif
235
236 //==========================================================================
237
238 static void comparefiles( char *name2, char *name1 )
239 {
240     int err;
241     char buf1[IOSIZE];
242     char buf2[IOSIZE];
243     int fd1, fd2;
244     ssize_t done1, done2;
245     int i;
246
247     diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
248
249     err = access( name1, F_OK );
250     if( err != 0 ) SHOW_RESULT( access, err );
251
252     err = access( name1, F_OK );
253     if( err != 0 ) SHOW_RESULT( access, err );
254     
255     fd1 = open( name1, O_RDONLY );
256     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
257
258     fd2 = open( name2, O_RDONLY );
259     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
260     
261     for(;;)
262     {
263         done1 = read( fd1, buf1, IOSIZE );
264         if( done1 < 0 ) SHOW_RESULT( read, done1 );
265
266         done2 = read( fd2, buf2, IOSIZE );
267         if( done2 < 0 ) SHOW_RESULT( read, done2 );
268
269         if( done1 != done2 )
270             diag_printf("Files different sizes\n");
271         
272         if( done1 == 0 ) break;
273
274         for( i = 0; i < done1; i++ )
275             if( buf1[i] != buf2[i] )
276             {
277                 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
278                 CYG_TEST_FAIL("Data in files not equal\n");
279             }
280     }
281
282     err = close( fd1 );
283     if( err < 0 ) SHOW_RESULT( close, err );
284
285     err = close( fd2 );
286     if( err < 0 ) SHOW_RESULT( close, err );
287     
288 }
289
290 //==========================================================================
291 // main
292
293 int main( int argc, char **argv )
294 {
295     int err;
296     char address[16];
297 #if defined(CYGSEM_FILEIO_BLOCK_USAGE)
298     struct cyg_fs_block_usage usage;
299 #endif
300
301     CYG_TEST_INIT();
302
303     // --------------------------------------------------------------
304
305     diag_printf("<INFO>: ROMFS root follows\n");
306     listdir( "/", true );
307
308     diag_printf("<INFO>: cd /etc\n" );
309     err = chdir( "/etc" );
310     if ( err < 0 ) {
311         SHOW_RESULT( chdir, err );
312         CYG_TEST_FAIL_FINISH("romfs1");
313     }
314
315     diag_printf("<INFO>: ROMFS list of '' follows\n");
316     listdir( "", true );
317
318     diag_printf("<INFO>: ROMFS list of /etc follows\n");
319     listdir( "/etc", true );
320
321     diag_printf("<INFO>: ROMFS list of . follows\n");
322     listdir( ".", true );
323     
324 #ifdef CYGPKG_FS_RAM
325     err = mount( "", "/var", "ramfs" );
326     if( err < 0 ) SHOW_RESULT( mount, err );
327
328     copyfile( "/etc/passwd", "/var/passwd_copy" );
329
330     comparefiles( "/etc/passwd", "/var/passwd_copy" );
331 #endif
332     
333     diag_printf("<INFO>: ROMFS list of / follows\n");
334 #ifdef CYGPKG_FS_RAM
335     diag_printf("<INFO>: Note that /var now gives stat() info for RAMFS\n");
336 #endif
337     listdir( "/", true );
338
339     diag_printf("<INFO>: Mount ROMFS again onto /mnt\n");
340     sprintf( address, "%p", (void*)&filedata[0] );
341     err = mount( address, "/mnt", "romfs" );
342     if( err < 0 ) SHOW_RESULT( mount, err );    
343
344     comparefiles( "/etc/passwd", "/mnt/etc/passwd" );
345
346
347     err = mkdir( "/foo", 0 );
348     CHKFAIL_TYPE( mkdir, err, EROFS );
349
350     err = rename( "/var", "/tmp" );     // RAMFS is mounted here
351 #ifdef CYGPKG_FS_RAM
352     CHKFAIL_TYPE( rename, err, EXDEV );
353 #else
354     CHKFAIL_TYPE( rename, err, EROFS );
355 #endif
356
357     err = rename( "/var/passwd_copy", "/mnt/etc/passwd_copy" );
358     CHKFAIL_TYPE( rename, err, EXDEV );
359
360     err = rename( "/etc", "/tmp" );
361     CHKFAIL_TYPE( rename, err, EROFS );
362
363     diag_printf("<INFO>: cd /etc\n");
364     err = chdir( "/etc" );
365     if( err < 0 ) SHOW_RESULT( chdir, err );
366
367     err = chdir( "/mnt/etc" );
368     if( err < 0 ) SHOW_RESULT( chdir, err );
369
370     listdir( ".", true );
371
372     diag_printf("<INFO>: unlink /tmp\n");        
373     err = unlink( "/tmp" );
374     CHKFAIL_TYPE( unlink, err, EROFS );
375
376     diag_printf("<INFO>: mount random area\n");
377     sprintf(address, "%p", (void*)(&filedata[0] + 0x100));
378     err = mount( address, "/tmp", "romfs" );
379     CHKFAIL_TYPE( mount, err, ENOENT );
380
381     err = umount( "/mnt" );
382     if( err < 0 ) SHOW_RESULT( umount, err );    
383
384     err = umount( "/var" );
385 #ifdef CYGPKG_FS_RAM
386     if( err < 0 ) SHOW_RESULT( umount, err );    
387 #else
388     CHKFAIL_TYPE( umount, err, EINVAL );
389 #endif
390
391 #if defined(CYGSEM_FILEIO_BLOCK_USAGE)
392     err = cyg_fs_getinfo("/", FS_INFO_BLOCK_USAGE, &usage, sizeof(usage));
393     if( err < 0 ) SHOW_RESULT( cyg_fs_getinfo, err );
394     diag_printf("<INFO>: total size: %6lld blocks, %10lld bytes\n",
395                 usage.total_blocks, usage.total_blocks * usage.block_size); 
396     diag_printf("<INFO>: free size:  %6lld blocks, %10lld bytes\n",
397                 usage.free_blocks, usage.free_blocks * usage.block_size); 
398     diag_printf("<INFO>: block size: %6u bytes\n", usage.block_size);
399 #endif
400     // --------------------------------------------------------------
401
402     err = umount( "/" );
403     if( err < 0 ) SHOW_RESULT( umount, err );    
404
405
406     CYG_TEST_PASS_FINISH("romfs1");
407 }
408
409 // -------------------------------------------------------------------------
410 // EOF romfs1.c