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