OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [fileio/] [v2_0/] [tests/] [fileio1.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
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
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
//                      
51
//                      
52
//                      
53
//                      
54
//              
55
//
56
//####DESCRIPTIONEND####
57
//
58
//==========================================================================
59
 
60
#include <pkgconf/hal.h>
61
#include <pkgconf/kernel.h>
62
#include <pkgconf/io_fileio.h>
63
 
64
#include <cyg/kernel/ktypes.h>         // base kernel types
65
#include <cyg/infra/cyg_trac.h>        // tracing macros
66
#include <cyg/infra/cyg_ass.h>         // assertion macros
67
 
68
#include <unistd.h>
69
#include <fcntl.h>
70
#include <sys/stat.h>
71
#include <errno.h>
72
#include <string.h>
73
 
74
#include <cyg/infra/testcase.h>
75
#include <cyg/infra/diag.h>            // HAL polled output
76
 
77
//==========================================================================
78
// Include the test filesystem.
79
// If we could make tests out of multiple files, then we could just link
80
// against the object file for this rather than including it.
81
 
82
#include "testfs.c"
83
 
84
//==========================================================================
85
 
86
#define SHOW_RESULT( _fn, _res ) \
87
diag_printf("<INFO>: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
88
 
89
//==========================================================================
90
 
91
#define IOSIZE  100
92
 
93
//==========================================================================
94
 
95
static void listdir( char *name, int statp )
96
{
97
    int err;
98
    DIR *dirp;
99
 
100
    diag_printf("<INFO>: reading directory %s\n",name);
101
 
102
    dirp = opendir( name );
103
    if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
104
 
105
    for(;;)
106
    {
107
        struct dirent *entry = readdir( dirp );
108
 
109
        if( entry == NULL )
110
            break;
111
 
112
        diag_printf("<INFO>: entry %14s",entry->d_name);
113
        if( statp )
114
        {
115
            char fullname[PATH_MAX];
116
            struct stat sbuf;
117
 
118
            if( name[0] )
119
            {
120
                strcpy(fullname, name );
121
                if( !(name[0] == '/' && name[1] == 0 ) )
122
                    strcat(fullname, "/" );
123
            }
124
            else fullname[0] = 0;
125
 
126
            strcat(fullname, entry->d_name );
127
 
128
            err = stat( fullname, &sbuf );
129
            if( err < 0 )
130
            {
131
                if( errno == ENOSYS )
132
                    diag_printf(" <no status available>");
133
                else SHOW_RESULT( stat, err );
134
            }
135
            else
136
            {
137
                diag_printf(" [mode %08x nlink %d size %d]",
138
                            sbuf.st_mode,sbuf.st_nlink,sbuf.st_size);
139
            }
140
        }
141
 
142
        diag_printf("\n");
143
    }
144
 
145
    err = closedir( dirp );
146
    if( err < 0 ) SHOW_RESULT( stat, err );
147
}
148
 
149
//==========================================================================
150
 
151
static void createfile( char *name, size_t size )
152
{
153
    char buf[IOSIZE];
154
    int fd;
155
    ssize_t wrote;
156
    int i;
157
    int err;
158
 
159
    diag_printf("<INFO>: create file %s size %d\n",name,size);
160
 
161
    err = access( name, F_OK );
162
    if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
163
 
164
    for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
165
 
166
    fd = open( name, O_WRONLY|O_CREAT );
167
    if( fd < 0 ) SHOW_RESULT( open, fd );
168
 
169
    while( size > 0 )
170
    {
171
        ssize_t len = size;
172
        if ( len > IOSIZE ) len = IOSIZE;
173
 
174
        wrote = write( fd, buf, len );
175
        if( wrote != len ) SHOW_RESULT( write, wrote );
176
 
177
        size -= wrote;
178
    }
179
 
180
    err = close( fd );
181
    if( err < 0 ) SHOW_RESULT( close, err );
182
}
183
 
184
//==========================================================================
185
 
186
static void maxfile( char *name )
187
{
188
    char buf[IOSIZE];
189
    int fd;
190
    ssize_t wrote;
191
    int i;
192
    int err;
193
    size_t size = 0;
194
 
195
    diag_printf("<INFO>: create maximal file %s\n",name);
196
 
197
    err = access( name, F_OK );
198
    if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
199
 
200
    for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
201
 
202
    fd = open( name, O_WRONLY|O_CREAT );
203
    if( fd < 0 ) SHOW_RESULT( open, fd );
204
 
205
    do
206
    {
207
        wrote = write( fd, buf, IOSIZE );
208
        if( wrote < 0 ) SHOW_RESULT( write, wrote );
209
 
210
        size += wrote;
211
 
212
    } while( wrote == IOSIZE );
213
 
214
    diag_printf("<INFO>: file size == %d\n",size);
215
 
216
    err = close( fd );
217
    if( err < 0 ) SHOW_RESULT( close, err );
218
}
219
 
220
//==========================================================================
221
 
222
static void checkfile( char *name )
223
{
224
    char buf[IOSIZE];
225
    int fd;
226
    ssize_t done;
227
    int i;
228
    int err;
229
 
230
    diag_printf("<INFO>: check file %s\n",name);
231
 
232
    err = access( name, F_OK );
233
    if( err != 0 ) SHOW_RESULT( access, err );
234
 
235
    fd = open( name, O_RDONLY );
236
    if( fd < 0 ) SHOW_RESULT( open, fd );
237
 
238
    for(;;)
239
    {
240
        done = read( fd, buf, IOSIZE );
241
        if( done < 0 ) SHOW_RESULT( read, done );
242
 
243
        if( done == 0 ) break;
244
 
245
        for( i = 0; i < done; i++ )
246
            if( buf[i] != i%256 )
247
            {
248
                diag_printf("buf[%d](%02x) != %02x\n",i,buf[i],i%256);
249
                CYG_TEST_FAIL("Data read not equal to data written\n");
250
            }
251
    }
252
 
253
    err = close( fd );
254
    if( err < 0 ) SHOW_RESULT( close, err );
255
}
256
 
257
//==========================================================================
258
 
259
static void copyfile( char *name2, char *name1 )
260
{
261
 
262
    int err;
263
    char buf[IOSIZE];
264
    int fd1, fd2;
265
    ssize_t done, wrote;
266
 
267
    diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
268
 
269
    err = access( name1, F_OK );
270
    if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
271
 
272
    err = access( name2, F_OK );
273
    if( err != 0 ) SHOW_RESULT( access, err );
274
 
275
    fd1 = open( name1, O_WRONLY|O_CREAT );
276
    if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
277
 
278
    fd2 = open( name2, O_RDONLY );
279
    if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
280
 
281
    for(;;)
282
    {
283
        done = read( fd2, buf, IOSIZE );
284
        if( done < 0 ) SHOW_RESULT( read, done );
285
 
286
        if( done == 0 ) break;
287
 
288
        wrote = write( fd1, buf, done );
289
        if( wrote != done ) SHOW_RESULT( write, wrote );
290
 
291
        if( wrote != done ) break;
292
    }
293
 
294
    err = close( fd1 );
295
    if( err < 0 ) SHOW_RESULT( close, err );
296
 
297
    err = close( fd2 );
298
    if( err < 0 ) SHOW_RESULT( close, err );
299
 
300
}
301
 
302
//==========================================================================
303
 
304
static void comparefiles( char *name2, char *name1 )
305
{
306
    int err;
307
    char buf1[IOSIZE];
308
    char buf2[IOSIZE];
309
    int fd1, fd2;
310
    ssize_t done1, done2;
311
    int i;
312
 
313
    diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
314
 
315
    err = access( name1, F_OK );
316
    if( err != 0 ) SHOW_RESULT( access, err );
317
 
318
    err = access( name1, F_OK );
319
    if( err != 0 ) SHOW_RESULT( access, err );
320
 
321
    fd1 = open( name1, O_RDONLY );
322
    if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
323
 
324
    fd2 = open( name2, O_RDONLY );
325
    if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
326
 
327
    for(;;)
328
    {
329
        done1 = read( fd1, buf1, IOSIZE );
330
        if( done1 < 0 ) SHOW_RESULT( read, done1 );
331
 
332
        done2 = read( fd2, buf2, IOSIZE );
333
        if( done2 < 0 ) SHOW_RESULT( read, done2 );
334
 
335
        if( done1 != done2 )
336
            diag_printf("Files different sizes\n");
337
 
338
        if( done1 == 0 ) break;
339
 
340
        for( i = 0; i < done1; i++ )
341
            if( buf1[i] != buf2[i] )
342
            {
343
                diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
344
                CYG_TEST_FAIL("Data in files not equal\n");
345
            }
346
    }
347
 
348
    err = close( fd1 );
349
    if( err < 0 ) SHOW_RESULT( close, err );
350
 
351
    err = close( fd2 );
352
    if( err < 0 ) SHOW_RESULT( close, err );
353
 
354
}
355
 
356
//==========================================================================
357
// main
358
 
359
int main( int argc, char **argv )
360
{
361
    int err;
362
 
363
    CYG_TEST_INIT();
364
 
365
    // --------------------------------------------------------------
366
 
367
    createfile( "/foo", 202 );
368
    checkfile( "foo" );
369
    copyfile( "foo", "fee");
370
    checkfile( "fee" );
371
    comparefiles( "foo", "/fee" );
372
 
373
    err = mkdir( "/bar", 0 );
374
    if( err < 0 ) SHOW_RESULT( mkdir, err );
375
 
376
    listdir( "/" , false);
377
 
378
    copyfile( "fee", "/bar/fum" );
379
    checkfile( "bar/fum" );
380
    comparefiles( "/fee", "bar/fum" );
381
 
382
 
383
    err = chdir( "bar" );
384
    if( err < 0 ) SHOW_RESULT( chdir, err );
385
 
386
    err = rename( "/foo", "bundy" );
387
    if( err < 0 ) SHOW_RESULT( rename, err );
388
 
389
    listdir( "/", true );
390
    listdir( "" , true );
391
 
392
    checkfile( "/bar/bundy" );
393
    comparefiles("/fee", "bundy" );
394
 
395
    testfs_dump();
396
 
397
    // --------------------------------------------------------------
398
 
399
    err = unlink( "/fee" );
400
    if( err < 0 ) SHOW_RESULT( unlink, err );
401
 
402
    err = unlink( "fum" );
403
    if( err < 0 ) SHOW_RESULT( unlink, err );
404
 
405
    err = unlink( "/bar/bundy" );
406
    if( err < 0 ) SHOW_RESULT( unlink, err );
407
 
408
    err = chdir( "/" );
409
    if( err < 0 ) SHOW_RESULT( chdir, err );
410
 
411
    err = rmdir( "/bar" );
412
    if( err < 0 ) SHOW_RESULT( rmdir, err );
413
 
414
    listdir( "/", false );
415
 
416
    // --------------------------------------------------------------
417
 
418
    err = mount( "", "/ram", "testfs" );
419
    if( err < 0 ) SHOW_RESULT( mount, err );
420
 
421
    createfile( "/ram/tinky", 456 );
422
    copyfile( "/ram/tinky", "/ram/laalaa" );
423
    checkfile( "/ram/tinky");
424
    checkfile( "/ram/laalaa");
425
    comparefiles( "/ram/tinky", "/ram/laalaa" );
426
 
427
    err = chdir( "/ram" );
428
    if( err < 0 ) SHOW_RESULT( chdir, err );
429
 
430
    createfile( "tinky", 678 );
431
    checkfile( "tinky" );
432
 
433
    maxfile( "dipsy" );
434
    checkfile( "dipsy" );
435
    copyfile( "dipsy", "po" );
436
    checkfile( "po" );
437
    comparefiles( "dipsy", "po" );
438
 
439
    testfs_dump();
440
 
441
    // --------------------------------------------------------------
442
 
443
    err = unlink( "tinky" );
444
    if( err < 0 ) SHOW_RESULT( unlink, err );
445
 
446
    err = unlink( "dipsy" );
447
    if( err < 0 ) SHOW_RESULT( unlink, err );
448
 
449
    err = unlink( "po" );
450
    if( err < 0 ) SHOW_RESULT( unlink, err );
451
 
452
    err = unlink( "laalaa" );
453
    if( err < 0 ) SHOW_RESULT( unlink, err );
454
 
455
    err = chdir( "/" );
456
    if( err < 0 ) SHOW_RESULT( chdir, err );
457
 
458
    err = umount( "/ram" );
459
    if( err < 0 ) SHOW_RESULT( umount, err );
460
 
461
    CYG_TEST_PASS_FINISH("fileio1");
462
}
463
 
464
// -------------------------------------------------------------------------
465
// EOF fileio1.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.