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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [fileio/] [current/] [tests/] [fileio1.c] - Blame information for rev 786

Details | Compare with Previous | View Log

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