comparison packages/io/fileio/current/tests/fileio1.c @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children e0c0827131d1
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //==========================================================================
2 //
3 // fileio1.c
4 //
5 // Test fileio system
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): nickg
35 // Contributors: nickg
36 // Date: 2000-05-25
37 // Purpose: Test fileio system
38 // Description: This test uses the testfs to check out the initialization
39 // and basic operation of the fileio system
40 //
41 //
42 //
43 //
44 //
45 //
46 //
47 //####DESCRIPTIONEND####
48 //
49 //==========================================================================
50
51 #include <pkgconf/hal.h>
52 #include <pkgconf/kernel.h>
53 #include <pkgconf/io_fileio.h>
54
55 #include <cyg/kernel/ktypes.h> // base kernel types
56 #include <cyg/infra/cyg_trac.h> // tracing macros
57 #include <cyg/infra/cyg_ass.h> // assertion macros
58
59 #include <unistd.h>
60 #include <fcntl.h>
61 #include <sys/stat.h>
62 #include <errno.h>
63 #include <string.h>
64
65 #include <cyg/infra/testcase.h>
66 #include <cyg/infra/diag.h> // HAL polled output
67
68 //==========================================================================
69 // Include the test filesystem.
70 // If we could make tests out of multiple files, then we could just link
71 // against the object file for this rather than including it.
72
73 #include "testfs.c"
74
75 //==========================================================================
76
77 #define SHOW_RESULT( _fn, _res ) \
78 diag_printf("<INFO>: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
79
80 //==========================================================================
81
82 #define IOSIZE 100
83
84 //==========================================================================
85
86 static char *strcat( char *s1, const char *s2 )
87 {
88 char *s = s1;
89 while( *s1 ) s1++;
90 while( (*s1++ = *s2++) != 0);
91 return s;
92 }
93
94 //==========================================================================
95
96 static void listdir( char *name, int statp )
97 {
98 int err;
99 DIR *dirp;
100
101 diag_printf("<INFO>: reading directory %s\n",name);
102
103 dirp = opendir( name );
104 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
105
106 for(;;)
107 {
108 struct dirent *entry = readdir( dirp );
109
110 if( entry == NULL )
111 break;
112
113 diag_printf("<INFO>: entry %14s",entry->d_name);
114 if( statp )
115 {
116 char fullname[PATH_MAX];
117 struct stat sbuf;
118
119 if( name[0] )
120 {
121 strcpy(fullname, name );
122 if( !(name[0] == '/' && name[1] == 0 ) )
123 strcat(fullname, "/" );
124 }
125 else fullname[0] = 0;
126
127 strcat(fullname, entry->d_name );
128
129 err = stat( fullname, &sbuf );
130 if( err < 0 )
131 {
132 if( errno == ENOSYS )
133 diag_printf(" <no status available>");
134 else SHOW_RESULT( stat, err );
135 }
136 else
137 {
138 diag_printf(" [mode %08x nlink %d size %d]",
139 sbuf.st_mode,sbuf.st_nlink,sbuf.st_size);
140 }
141 }
142
143 diag_printf("\n");
144 }
145
146 err = closedir( dirp );
147 if( err < 0 ) SHOW_RESULT( stat, err );
148 }
149
150 //==========================================================================
151
152 static void createfile( char *name, size_t size )
153 {
154 char buf[IOSIZE];
155 int fd;
156 ssize_t wrote;
157 int i;
158 int err;
159
160 diag_printf("<INFO>: create file %s size %d\n",name,size);
161
162 err = access( name, F_OK );
163 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
164
165 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
166
167 fd = open( name, O_WRONLY|O_CREAT );
168 if( fd < 0 ) SHOW_RESULT( open, fd );
169
170 while( size > 0 )
171 {
172 ssize_t len = size;
173 if ( len > IOSIZE ) len = IOSIZE;
174
175 wrote = write( fd, buf, len );
176 if( wrote != len ) SHOW_RESULT( write, wrote );
177
178 size -= wrote;
179 }
180
181 err = close( fd );
182 if( err < 0 ) SHOW_RESULT( close, err );
183 }
184
185 //==========================================================================
186
187 static void maxfile( char *name )
188 {
189 char buf[IOSIZE];
190 int fd;
191 ssize_t wrote;
192 int i;
193 int err;
194 size_t size = 0;
195
196 diag_printf("<INFO>: create maximal file %s\n",name);
197
198 err = access( name, F_OK );
199 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
200
201 for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
202
203 fd = open( name, O_WRONLY|O_CREAT );
204 if( fd < 0 ) SHOW_RESULT( open, fd );
205
206 do
207 {
208 wrote = write( fd, buf, IOSIZE );
209 if( wrote < 0 ) SHOW_RESULT( write, wrote );
210
211 size += wrote;
212
213 } while( wrote == IOSIZE );
214
215 diag_printf("<INFO>: file size == %d\n",size);
216
217 err = close( fd );
218 if( err < 0 ) SHOW_RESULT( close, err );
219 }
220
221 //==========================================================================
222
223 static void checkfile( char *name )
224 {
225 char buf[IOSIZE];
226 int fd;
227 ssize_t done;
228 int i;
229 int err;
230
231 diag_printf("<INFO>: check file %s\n",name);
232
233 err = access( name, F_OK );
234 if( err != 0 ) SHOW_RESULT( access, err );
235
236 fd = open( name, O_RDONLY );
237 if( fd < 0 ) SHOW_RESULT( open, fd );
238
239 for(;;)
240 {
241 done = read( fd, buf, IOSIZE );
242 if( done < 0 ) SHOW_RESULT( read, done );
243
244 if( done == 0 ) break;
245
246 for( i = 0; i < done; i++ )
247 if( buf[i] != i%256 )
248 {
249 diag_printf("buf[%d](%02x) != %02x\n",i,buf[i],i%256);
250 CYG_TEST_FAIL("Data read not equal to data written\n");
251 }
252 }
253
254 err = close( fd );
255 if( err < 0 ) SHOW_RESULT( close, err );
256 }
257
258 //==========================================================================
259
260 static void copyfile( char *name2, char *name1 )
261 {
262
263 int err;
264 char buf[IOSIZE];
265 int fd1, fd2;
266 ssize_t done, wrote;
267
268 diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
269
270 err = access( name1, F_OK );
271 if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
272
273 err = access( name2, F_OK );
274 if( err != 0 ) SHOW_RESULT( access, err );
275
276 fd1 = open( name1, O_WRONLY|O_CREAT );
277 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
278
279 fd2 = open( name2, O_RDONLY );
280 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
281
282 for(;;)
283 {
284 done = read( fd2, buf, IOSIZE );
285 if( done < 0 ) SHOW_RESULT( read, done );
286
287 if( done == 0 ) break;
288
289 wrote = write( fd1, buf, done );
290 if( wrote != done ) SHOW_RESULT( write, wrote );
291
292 if( wrote != done ) break;
293 }
294
295 err = close( fd1 );
296 if( err < 0 ) SHOW_RESULT( close, err );
297
298 err = close( fd2 );
299 if( err < 0 ) SHOW_RESULT( close, err );
300
301 }
302
303 //==========================================================================
304
305 static void comparefiles( char *name2, char *name1 )
306 {
307 int err;
308 char buf1[IOSIZE];
309 char buf2[IOSIZE];
310 int fd1, fd2;
311 ssize_t done1, done2;
312 int i;
313
314 diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
315
316 err = access( name1, F_OK );
317 if( err != 0 ) SHOW_RESULT( access, err );
318
319 err = access( name1, F_OK );
320 if( err != 0 ) SHOW_RESULT( access, err );
321
322 fd1 = open( name1, O_RDONLY );
323 if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
324
325 fd2 = open( name2, O_RDONLY );
326 if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
327
328 for(;;)
329 {
330 done1 = read( fd1, buf1, IOSIZE );
331 if( done1 < 0 ) SHOW_RESULT( read, done1 );
332
333 done2 = read( fd2, buf2, IOSIZE );
334 if( done2 < 0 ) SHOW_RESULT( read, done2 );
335
336 if( done1 != done2 )
337 diag_printf("Files different sizes\n");
338
339 if( done1 == 0 ) break;
340
341 for( i = 0; i < done1; i++ )
342 if( buf1[i] != buf2[i] )
343 {
344 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
345 CYG_TEST_FAIL("Data in files not equal\n");
346 }
347 }
348
349 err = close( fd1 );
350 if( err < 0 ) SHOW_RESULT( close, err );
351
352 err = close( fd2 );
353 if( err < 0 ) SHOW_RESULT( close, err );
354
355 }
356
357 //==========================================================================
358 // main
359
360 int main( int argc, char **argv )
361 {
362 int err;
363
364 CYG_TEST_INIT();
365
366 // --------------------------------------------------------------
367
368 createfile( "/foo", 202 );
369 checkfile( "foo" );
370 copyfile( "foo", "fee");
371 checkfile( "fee" );
372 comparefiles( "foo", "/fee" );
373
374 err = mkdir( "/bar", 0 );
375 if( err < 0 ) SHOW_RESULT( mkdir, err );
376
377 listdir( "/" , false);
378
379 copyfile( "fee", "/bar/fum" );
380 checkfile( "bar/fum" );
381 comparefiles( "/fee", "bar/fum" );
382
383
384 err = chdir( "bar" );
385 if( err < 0 ) SHOW_RESULT( chdir, err );
386
387 err = rename( "/foo", "bundy" );
388 if( err < 0 ) SHOW_RESULT( rename, err );
389
390 listdir( "/", true );
391 listdir( "" , true );
392
393 checkfile( "/bar/bundy" );
394 comparefiles("/fee", "bundy" );
395
396 testfs_dump();
397
398 // --------------------------------------------------------------
399
400 err = unlink( "/fee" );
401 if( err < 0 ) SHOW_RESULT( unlink, err );
402
403 err = unlink( "fum" );
404 if( err < 0 ) SHOW_RESULT( unlink, err );
405
406 err = unlink( "/bar/bundy" );
407 if( err < 0 ) SHOW_RESULT( unlink, err );
408
409 err = chdir( "/" );
410 if( err < 0 ) SHOW_RESULT( chdir, err );
411
412 err = rmdir( "/bar" );
413 if( err < 0 ) SHOW_RESULT( rmdir, err );
414
415 listdir( "/", false );
416
417 // --------------------------------------------------------------
418
419 err = mount( "", "/ram", "testfs" );
420 if( err < 0 ) SHOW_RESULT( mount, err );
421
422 createfile( "/ram/tinky", 456 );
423 copyfile( "/ram/tinky", "/ram/laalaa" );
424 checkfile( "/ram/tinky");
425 checkfile( "/ram/laalaa");
426 comparefiles( "/ram/tinky", "/ram/laalaa" );
427
428 err = chdir( "/ram" );
429 if( err < 0 ) SHOW_RESULT( chdir, err );
430
431 createfile( "tinky", 678 );
432 checkfile( "tinky" );
433
434 maxfile( "dipsy" );
435 checkfile( "dipsy" );
436 copyfile( "dipsy", "po" );
437 checkfile( "po" );
438 comparefiles( "dipsy", "po" );
439
440 testfs_dump();
441
442 // --------------------------------------------------------------
443
444 err = unlink( "tinky" );
445 if( err < 0 ) SHOW_RESULT( unlink, err );
446
447 err = unlink( "dipsy" );
448 if( err < 0 ) SHOW_RESULT( unlink, err );
449
450 err = unlink( "po" );
451 if( err < 0 ) SHOW_RESULT( unlink, err );
452
453 err = unlink( "laalaa" );
454 if( err < 0 ) SHOW_RESULT( unlink, err );
455
456 err = chdir( "/" );
457 if( err < 0 ) SHOW_RESULT( chdir, err );
458
459 err = umount( "/ram" );
460 if( err < 0 ) SHOW_RESULT( umount, err );
461
462 CYG_TEST_PASS_FINISH("fileio1");
463 }
464
465 // -------------------------------------------------------------------------
466 // EOF fileio1.c