1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// jffs2_3.c
|
4 |
|
|
//
|
5 |
|
|
// Test garbage collect on a filesystem
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 2005 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): asl
|
43 |
|
|
// Contributors: asl
|
44 |
|
|
// Date: 2005-01-16
|
45 |
|
|
// Purpose: Test garbage collect on a filesystem
|
46 |
|
|
// Description: This test creates and deletes files in order
|
47 |
|
|
// to test the garbage collection code.
|
48 |
|
|
//
|
49 |
|
|
//####DESCRIPTIONEND####
|
50 |
|
|
//
|
51 |
|
|
//==========================================================================
|
52 |
|
|
|
53 |
|
|
#include <pkgconf/io_flash.h>
|
54 |
|
|
#include <stdio.h>
|
55 |
|
|
#include <unistd.h>
|
56 |
|
|
#include <fcntl.h>
|
57 |
|
|
#include <errno.h>
|
58 |
|
|
#include <string.h>
|
59 |
|
|
#include <stdlib.h>
|
60 |
|
|
|
61 |
|
|
#include <cyg/fileio/fileio.h>
|
62 |
|
|
#include <cyg/io/flash.h>
|
63 |
|
|
#include <cyg/crc/crc.h>
|
64 |
|
|
|
65 |
|
|
#include <cyg/infra/testcase.h>
|
66 |
|
|
#include <cyg/infra/diag.h> // HAL polled output
|
67 |
|
|
|
68 |
|
|
#include <pkgconf/fs_jffs2.h> // Address of JFFS2
|
69 |
|
|
|
70 |
|
|
//==========================================================================
|
71 |
|
|
|
72 |
|
|
#define stringify2(_x_) #_x_
|
73 |
|
|
#define stringify(_x_) stringify2(_x_)
|
74 |
|
|
|
75 |
|
|
#if defined(CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1)
|
76 |
|
|
# define JFFS2_TEST_DEV CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1
|
77 |
|
|
#elif defined(CYGFUN_IO_FLASH_BLOCK_FROM_FIS)
|
78 |
|
|
# define JFFS2_TEST_DEV "/dev/flash/fis/jffs2test"
|
79 |
|
|
#else
|
80 |
|
|
// fall back to using a user set area in the first device (only)
|
81 |
|
|
# define JFFS2_TEST_DEV "/dev/flash/0/" stringify(CYGNUM_FS_JFFS2_TEST_OFFSET) "," stringify(CYGNUM_FS_JFFS2_TEST_LENGTH)
|
82 |
|
|
#endif
|
83 |
|
|
|
84 |
|
|
//==========================================================================
|
85 |
|
|
|
86 |
|
|
#define ITERATIONS 1000000
|
87 |
|
|
#define NELEM(_x_) (sizeof(_x_)/sizeof(*(_x_)))
|
88 |
|
|
|
89 |
|
|
#define SHOW_RESULT( _fn, _res ) \
|
90 |
|
|
diag_printf("FAIL: " #_fn "() returned %d %s\n", _res, \
|
91 |
|
|
(unsigned long) _res<0?strerror(errno):"");
|
92 |
|
|
|
93 |
|
|
//==========================================================================
|
94 |
|
|
// file creation, deletion and testing functions
|
95 |
|
|
|
96 |
|
|
static void create_file(int i)
|
97 |
|
|
{
|
98 |
|
|
cyg_int32 buffer[1020];
|
99 |
|
|
char name[16];
|
100 |
|
|
cyg_uint32 j;
|
101 |
|
|
int fd, err;
|
102 |
|
|
|
103 |
|
|
sprintf(name,"test%07d",i);
|
104 |
|
|
|
105 |
|
|
fd = creat(name, S_IRWXU);
|
106 |
|
|
if (fd == -1) SHOW_RESULT( creat, fd );
|
107 |
|
|
|
108 |
|
|
for (j=1; j < NELEM(buffer); j++) {
|
109 |
|
|
buffer[j] = rand();
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
buffer[0] = 0;
|
113 |
|
|
buffer[0] = cyg_posix_crc32((unsigned char *)buffer, sizeof(buffer));
|
114 |
|
|
|
115 |
|
|
err = write(fd, buffer, sizeof(buffer));
|
116 |
|
|
if (err == -1) SHOW_RESULT( write, err );
|
117 |
|
|
|
118 |
|
|
err = close(fd);
|
119 |
|
|
if (err == -1) SHOW_RESULT( close, err );
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
static void delete_file(int i)
|
123 |
|
|
{
|
124 |
|
|
char name[16];
|
125 |
|
|
int err;
|
126 |
|
|
|
127 |
|
|
sprintf(name,"test%07d",i);
|
128 |
|
|
|
129 |
|
|
err = unlink(name);
|
130 |
|
|
if (err == -1) SHOW_RESULT( unlink, err );
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
static void check_file(int i)
|
134 |
|
|
{
|
135 |
|
|
char name[16];
|
136 |
|
|
int err, fd;
|
137 |
|
|
cyg_int32 buffer[1020];
|
138 |
|
|
cyg_uint32 crc;
|
139 |
|
|
|
140 |
|
|
sprintf(name,"test%07d",i);
|
141 |
|
|
|
142 |
|
|
fd = open(name, O_RDONLY);
|
143 |
|
|
if (fd == -1) SHOW_RESULT( open, fd );
|
144 |
|
|
|
145 |
|
|
err = read(fd, buffer, sizeof(buffer));
|
146 |
|
|
if (err == -1) SHOW_RESULT( read, fd );
|
147 |
|
|
|
148 |
|
|
crc = buffer[0];
|
149 |
|
|
buffer[0] = 0;
|
150 |
|
|
|
151 |
|
|
if (crc != cyg_posix_crc32((unsigned char *)buffer, sizeof(buffer))) {
|
152 |
|
|
CYG_TEST_FAIL("File corrupt");
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
err = close(fd);
|
156 |
|
|
if (err == -1) SHOW_RESULT( read, fd );
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
//==========================================================================
|
161 |
|
|
// main
|
162 |
|
|
|
163 |
|
|
int main( int argc, char **argv )
|
164 |
|
|
{
|
165 |
|
|
int err, iteration;
|
166 |
|
|
struct mallinfo minfo
|
167 |
|
|
;
|
168 |
|
|
CYG_TEST_INIT();
|
169 |
|
|
|
170 |
|
|
// --------------------------------------------------------------
|
171 |
|
|
|
172 |
|
|
CYG_TEST_INFO("mount /");
|
173 |
|
|
err = mount( JFFS2_TEST_DEV, "/", "jffs2" );
|
174 |
|
|
if( err < 0 ) SHOW_RESULT( mount, err );
|
175 |
|
|
|
176 |
|
|
chdir ("/");
|
177 |
|
|
|
178 |
|
|
iteration=0;
|
179 |
|
|
create_file(iteration);
|
180 |
|
|
while (iteration < ITERATIONS) {
|
181 |
|
|
if (!(iteration % 1000)) {
|
182 |
|
|
minfo = mallinfo();
|
183 |
|
|
diag_printf("<INFO> Iteration %07d fordblks = %7d\n",
|
184 |
|
|
iteration, minfo.fordblks);
|
185 |
|
|
}
|
186 |
|
|
iteration++;
|
187 |
|
|
create_file(iteration);
|
188 |
|
|
check_file(iteration-1);
|
189 |
|
|
delete_file(iteration-1);
|
190 |
|
|
check_file(iteration);
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
CYG_TEST_INFO("umount /");
|
194 |
|
|
err = umount( "/" );
|
195 |
|
|
if( err < 0 ) SHOW_RESULT( umount, err );
|
196 |
|
|
|
197 |
|
|
CYG_TEST_PASS_FINISH("jffs2_3");
|
198 |
|
|
}
|