]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/fs/rom/v2_0/tests/fileio1.c
Initial revision
[karo-tx-redboot.git] / packages / fs / rom / v2_0 / tests / fileio1.c
1 //==========================================================================
2 //
3 //      fileio1.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 //
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.
16 //
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
20 // for more details.
21 //
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.
25 //
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.
32 //
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.
35 //
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####
42 //
43 // Author(s):           nickg
44 // Contributors:        nickg, richard.panton@3glab.com, jlarmour
45 // Date:                2000-05-25
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
49 //
50 //####DESCRIPTIONEND####
51 //
52 //==========================================================================
53
54 #include <pkgconf/hal.h>
55 #include <pkgconf/io_fileio.h>
56 #include <pkgconf/isoinfra.h>
57 #include <pkgconf/system.h>
58
59 #include <unistd.h>
60 #include <fcntl.h>
61 #include <sys/stat.h>
62 #include <errno.h>
63 #include <string.h>
64 #include <dirent.h>
65 #include <stdio.h>
66
67 #include <cyg/fileio/fileio.h>
68
69 #include <cyg/infra/testcase.h>
70 #include <cyg/infra/diag.h>            // HAL polled output
71
72 #include <cyg/romfs/testromfs.h>  // Test ROMFS data
73
74 //==========================================================================
75
76 MTAB_ENTRY( romfs_mte1,
77                    "/",
78                    "romfs",
79                    "",
80                    (CYG_ADDRWORD) &filedata[0] );
81
82
83 //==========================================================================
84
85 #define SHOW_RESULT( _fn, _res ) \
86 diag_printf("<FAIL>: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
87
88 #define CHKFAIL_TYPE( _fn, _res, _type ) { \
89 if ( _res != -1 ) \
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) ); \
93 }
94
95 //==========================================================================
96
97 #define IOSIZE  100
98
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"
101
102
103 //==========================================================================
104
105 #ifndef CYGINT_ISO_STRING_STRFUNCS
106
107 char *strcat( char *s1, const char *s2 )
108 {
109     char *s = s1;
110     while( *s1 ) s1++;
111     while( (*s1++ = *s2++) != 0);
112     return s;
113 }
114
115 #endif
116
117 //==========================================================================
118
119 static void listdir( char *name, int statp )
120 {
121     int err;
122     DIR *dirp;
123     
124     diag_printf("<INFO>: reading directory %s\n",name);
125     
126     dirp = opendir( name );
127     if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
128
129     for(;;)
130     {
131         struct dirent *entry = readdir( dirp );
132         
133         if( entry == NULL )
134             break;
135
136         diag_printf("<INFO>: entry %14s",entry->d_name);
137         if( statp )
138         {
139             char fullname[PATH_MAX];
140             struct stat sbuf;
141
142             if( name[0] )
143             {
144                 strcpy(fullname, name );
145                 if( !(name[0] == '/' && name[1] == 0 ) )
146                     strcat(fullname, "/" );
147             }
148             else fullname[0] = 0;
149             
150             strcat(fullname, entry->d_name );
151             
152             err = stat( fullname, &sbuf );
153             if( err < 0 )
154             {
155                 if( errno == ENOSYS )
156                     diag_printf(" <no status available>");
157                 else SHOW_RESULT( stat, err );
158             }
159             else
160             {
161                 diag_printf(" [mode %08x ino %08x nlink %d size %d]",
162                             sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size);
163             }
164         }
165
166         diag_printf("\n");
167     }
168
169     err = closedir( dirp );
170     if( err < 0 ) SHOW_RESULT( stat, err );
171 }
172
173 //==========================================================================
174
175 #ifdef CYGPKG_FS_RAM
176 static void copyfile( char *name2, char *name1 )
177 {
178
179     int err;
180     char buf[IOSIZE];
181     int fd1, fd2;
182     ssize_t done, wrote;
183
184     diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
185
186     err = access( name1, F_OK );
187     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
188
189     err = access( name2, F_OK );
190     if( err != 0 ) SHOW_RESULT( access, err );
191     
192     fd1 = open( name1, O_WRONLY|O_CREAT );
193     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
194
195     fd2 = open( name2, O_RDONLY );
196     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
197     
198     for(;;)
199     {
200         done = read( fd2, buf, IOSIZE );
201         if( done < 0 ) SHOW_RESULT( read, done );
202
203         if( done == 0 ) break;
204
205         wrote = write( fd1, buf, done );
206         if( wrote != done ) SHOW_RESULT( write, wrote );
207
208         if( wrote != done ) break;
209     }
210
211     err = close( fd1 );
212     if( err < 0 ) SHOW_RESULT( close, err );
213
214     err = close( fd2 );
215     if( err < 0 ) SHOW_RESULT( close, err );
216     
217 }
218 #endif
219
220 //==========================================================================
221
222 static void comparefiles( char *name2, char *name1 )
223 {
224     int err;
225     char buf1[IOSIZE];
226     char buf2[IOSIZE];
227     int fd1, fd2;
228     ssize_t done1, done2;
229     int i;
230
231     diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
232
233     err = access( name1, F_OK );
234     if( err != 0 ) SHOW_RESULT( access, err );
235
236     err = access( name1, F_OK );
237     if( err != 0 ) SHOW_RESULT( access, err );
238     
239     fd1 = open( name1, O_RDONLY );
240     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
241
242     fd2 = open( name2, O_RDONLY );
243     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
244     
245     for(;;)
246     {
247         done1 = read( fd1, buf1, IOSIZE );
248         if( done1 < 0 ) SHOW_RESULT( read, done1 );
249
250         done2 = read( fd2, buf2, IOSIZE );
251         if( done2 < 0 ) SHOW_RESULT( read, done2 );
252
253         if( done1 != done2 )
254             diag_printf("Files different sizes\n");
255         
256         if( done1 == 0 ) break;
257
258         for( i = 0; i < done1; i++ )
259             if( buf1[i] != buf2[i] )
260             {
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");
263             }
264     }
265
266     err = close( fd1 );
267     if( err < 0 ) SHOW_RESULT( close, err );
268
269     err = close( fd2 );
270     if( err < 0 ) SHOW_RESULT( close, err );
271     
272 }
273
274 //==========================================================================
275 // main
276
277 int main( int argc, char **argv )
278 {
279     int err;
280     char address[16];
281
282     CYG_TEST_INIT();
283
284     // --------------------------------------------------------------
285
286     diag_printf("<INFO>: ROMFS root follows\n");
287     listdir( "/", true );
288
289     diag_printf("<INFO>: cd /etc\n" );
290     err = chdir( "/etc" );
291     if ( err < 0 ) SHOW_RESULT( chdir, err );
292
293     diag_printf("<INFO>: ROMFS list of '' follows\n");
294     listdir( "", true );
295
296     diag_printf("<INFO>: ROMFS list of /etc follows\n");
297     listdir( "/etc", true );
298
299     diag_printf("<INFO>: ROMFS list of . follows\n");
300     listdir( ".", true );
301     
302 #ifdef CYGPKG_FS_RAM
303     err = mount( "", "/var", "ramfs" );
304     if( err < 0 ) SHOW_RESULT( mount, err );
305
306     copyfile( "/etc/passwd", "/var/passwd_copy" );
307
308     comparefiles( "/etc/passwd", "/var/passwd_copy" );
309 #endif
310     
311     diag_printf("<INFO>: ROMFS list of / follows\n");
312 #ifdef CYGPKG_FS_RAM
313     diag_printf("<INFO>: Note that /var now gives stat() info for RAMFS\n");
314 #endif
315     listdir( "/", true );
316
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 );    
321
322     comparefiles( "/etc/passwd", "/mnt/etc/passwd" );
323
324
325     err = mkdir( "/foo", 0 );
326     CHKFAIL_TYPE( mkdir, err, EROFS );
327
328     err = rename( "/var", "/tmp" );     // RAMFS is mounted here
329 #ifdef CYGPKG_FS_RAM
330     CHKFAIL_TYPE( rename, err, EXDEV );
331 #else
332     CHKFAIL_TYPE( rename, err, EROFS );
333 #endif
334
335     err = rename( "/var/passwd_copy", "/mnt/etc/passwd_copy" );
336     CHKFAIL_TYPE( rename, err, EXDEV );
337
338     err = rename( "/etc", "/tmp" );
339     CHKFAIL_TYPE( rename, err, EROFS );
340
341     diag_printf("<INFO>: cd /etc\n");
342     err = chdir( "/etc" );
343     if( err < 0 ) SHOW_RESULT( chdir, err );
344
345     err = chdir( "/mnt/etc" );
346     if( err < 0 ) SHOW_RESULT( chdir, err );
347
348     listdir( ".", true );
349
350     diag_printf("<INFO>: unlink /tmp\n");        
351     err = unlink( "/tmp" );
352     CHKFAIL_TYPE( unlink, err, EROFS );
353
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 );
358
359     err = umount( "/mnt" );
360     if( err < 0 ) SHOW_RESULT( umount, err );    
361
362     err = umount( "/var" );
363 #ifdef CYGPKG_FS_RAM
364     if( err < 0 ) SHOW_RESULT( umount, err );    
365 #else
366     CHKFAIL_TYPE( umount, err, EINVAL );
367 #endif
368
369     err = umount( "/" );
370     if( err < 0 ) SHOW_RESULT( umount, err );    
371
372
373     CYG_TEST_PASS_FINISH("fileio1");
374 }
375
376 // -------------------------------------------------------------------------
377 // EOF fileio1.c