1 //==========================================================================
5 // Test garbage collect on a filesystem
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2005 Andrew Lunn
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.
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
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.
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.
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.
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //==========================================================================
39 //#####DESCRIPTIONBEGIN####
44 // Purpose: Test garbage collect on a filesystem
45 // Description: This test creates and deletes files in order
46 // to test the garbage collection code.
48 //####DESCRIPTIONEND####
50 //==========================================================================
58 #include <cyg/fileio/fileio.h>
59 #include <cyg/io/flash.h>
60 #include <cyg/crc/crc.h>
62 #include <cyg/infra/testcase.h>
63 #include <cyg/infra/diag.h> // HAL polled output
65 //==========================================================================
67 #define ITERATIONS 1000000
68 #define NELEM(_x_) (sizeof(_x_)/sizeof(*(_x_)))
70 #define SHOW_RESULT( _fn, _res ) \
71 diag_printf("FAIL: " #_fn "() returned %d %s\n", _res, \
72 (unsigned long) _res<0?strerror(errno):"");
74 //==========================================================================
75 // file creation, deletion and testing functions
77 static void create_file(int i)
79 cyg_int32 buffer[1020];
84 sprintf(name,"test%07d",i);
86 fd = creat(name, S_IRWXU);
87 if (fd == -1) SHOW_RESULT( creat, fd );
89 for (j=1; j < NELEM(buffer); j++) {
94 buffer[0] = cyg_posix_crc32((unsigned char *)buffer, sizeof(buffer));
96 err = write(fd, buffer, sizeof(buffer));
97 if (err == -1) SHOW_RESULT( write, err );
100 if (err == -1) SHOW_RESULT( close, err );
103 static void delete_file(int i)
108 sprintf(name,"test%07d",i);
111 if (err == -1) SHOW_RESULT( unlink, err );
114 static void check_file(int i)
118 cyg_int32 buffer[1020];
121 sprintf(name,"test%07d",i);
123 fd = open(name, O_RDONLY);
124 if (fd == -1) SHOW_RESULT( open, fd );
126 err = read(fd, buffer, sizeof(buffer));
127 if (err == -1) SHOW_RESULT( read, fd );
132 if (crc != cyg_posix_crc32((unsigned char *)buffer, sizeof(buffer))) {
133 CYG_TEST_FAIL("File corrupt");
137 if (err == -1) SHOW_RESULT( read, fd );
141 //==========================================================================
144 int main( int argc, char **argv )
147 struct mallinfo minfo
151 // --------------------------------------------------------------
153 CYG_TEST_INFO("mount /");
154 err = mount( CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, "/", "jffs2" );
155 if( err < 0 ) SHOW_RESULT( mount, err );
160 create_file(iteration);
161 while (iteration < ITERATIONS) {
162 if (!(iteration % 1000)) {
164 diag_printf("<INFO> Iteration %07d fordblks = %7d\n",
165 iteration, minfo.fordblks);
168 create_file(iteration);
169 check_file(iteration-1);
170 delete_file(iteration-1);
171 check_file(iteration);
174 CYG_TEST_INFO("umount /");
176 if( err < 0 ) SHOW_RESULT( umount, err );
178 CYG_TEST_PASS_FINISH("jffs2_3");