1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// ramfs2.c
|
4 |
|
|
//
|
5 |
|
|
// Test fseek on a filesystem
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 2004 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: 2004-03-29
|
45 |
|
|
// Purpose: Test fseek on a filesystem
|
46 |
|
|
// Description: This test uses the ramfs to check out the fseek
|
47 |
|
|
// operation on a filesystem.
|
48 |
|
|
//
|
49 |
|
|
//
|
50 |
|
|
//
|
51 |
|
|
//
|
52 |
|
|
//
|
53 |
|
|
//
|
54 |
|
|
//
|
55 |
|
|
//####DESCRIPTIONEND####
|
56 |
|
|
//
|
57 |
|
|
//==========================================================================
|
58 |
|
|
#include <stdio.h>
|
59 |
|
|
#include <unistd.h>
|
60 |
|
|
#include <fcntl.h>
|
61 |
|
|
#include <errno.h>
|
62 |
|
|
#include <string.h>
|
63 |
|
|
|
64 |
|
|
#include <cyg/fileio/fileio.h>
|
65 |
|
|
|
66 |
|
|
#include <cyg/infra/testcase.h>
|
67 |
|
|
#include <cyg/infra/diag.h> // HAL polled output
|
68 |
|
|
//==========================================================================
|
69 |
|
|
|
70 |
|
|
#define SHOW_RESULT( _fn, _res ) \
|
71 |
|
|
diag_printf("FAIL: " #_fn "() returned %ld %s\n", (long)_res, _res<0?strerror(errno):"");
|
72 |
|
|
|
73 |
|
|
//==========================================================================
|
74 |
|
|
|
75 |
|
|
char buf[1024];
|
76 |
|
|
char buf1[1024];
|
77 |
|
|
|
78 |
|
|
//==========================================================================
|
79 |
|
|
// main
|
80 |
|
|
|
81 |
|
|
int main( int argc, char **argv )
|
82 |
|
|
{
|
83 |
|
|
int err;
|
84 |
|
|
FILE *stream;
|
85 |
|
|
long pos;
|
86 |
|
|
int i;
|
87 |
|
|
|
88 |
|
|
CYG_TEST_INIT();
|
89 |
|
|
|
90 |
|
|
// --------------------------------------------------------------
|
91 |
|
|
|
92 |
|
|
CYG_TEST_INFO("mount /");
|
93 |
|
|
err = mount( "", "/", "ramfs" );
|
94 |
|
|
if( err < 0 ) SHOW_RESULT( mount, err );
|
95 |
|
|
|
96 |
|
|
CYG_TEST_INFO("creating /fseek");
|
97 |
|
|
stream = fopen("/fseek","w+");
|
98 |
|
|
if (!stream) {
|
99 |
|
|
diag_printf("FAIL: fopen() returned NULL, %s\n", strerror(errno));
|
100 |
|
|
CYG_TEST_FINISH("done"); \
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
/* Write a buffer full of cyclic numbers */
|
104 |
|
|
for (i = 0; i < sizeof(buf); i++) {
|
105 |
|
|
buf[i] = i % 256;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
CYG_TEST_INFO("writing test pattern");
|
109 |
|
|
err=fwrite(buf,sizeof(buf), 1, stream);
|
110 |
|
|
if ( err < 0 ) SHOW_RESULT( fwrite, err );
|
111 |
|
|
|
112 |
|
|
/* The current position should be the same size as the buffer */
|
113 |
|
|
pos = ftell(stream);
|
114 |
|
|
|
115 |
|
|
if (pos < 0) SHOW_RESULT( ftell, pos );
|
116 |
|
|
if (pos != sizeof(buf))
|
117 |
|
|
diag_printf("<FAIL>: ftell is not telling the truth.");
|
118 |
|
|
|
119 |
|
|
CYG_TEST_INFO("fseek()ing to beginning and writing");
|
120 |
|
|
|
121 |
|
|
/* Seek back to the beginning of the file */
|
122 |
|
|
err = fseek(stream, 0, SEEK_SET);
|
123 |
|
|
if ( err < 0 ) SHOW_RESULT( fwrite, err );
|
124 |
|
|
|
125 |
|
|
pos = ftell(stream);
|
126 |
|
|
|
127 |
|
|
if (pos < 0) SHOW_RESULT( ftell, pos );
|
128 |
|
|
if (pos != 0) CYG_TEST_FAIL("ftell is not telling the truth");
|
129 |
|
|
|
130 |
|
|
/* Write 4 zeros to the beginning of the file */
|
131 |
|
|
for (i = 0; i < 4; i++) {
|
132 |
|
|
buf[i] = 0;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
err = fwrite(buf, 4, 1, stream);
|
136 |
|
|
if ( err < 0 ) SHOW_RESULT( fwrite, err );
|
137 |
|
|
|
138 |
|
|
/* Check the pointer is at 4 */
|
139 |
|
|
pos = ftell(stream);
|
140 |
|
|
|
141 |
|
|
if (pos < 0) SHOW_RESULT( ftell, pos );
|
142 |
|
|
if (pos != 4) CYG_TEST_FAIL("ftell is not telling the truth");
|
143 |
|
|
|
144 |
|
|
CYG_TEST_INFO("closing file");
|
145 |
|
|
|
146 |
|
|
/* Close the file, open it up again and read it back */
|
147 |
|
|
err = fclose(stream);
|
148 |
|
|
if (err != 0) SHOW_RESULT( fclose, err );
|
149 |
|
|
|
150 |
|
|
CYG_TEST_INFO("open file /fseek");
|
151 |
|
|
stream = fopen("/fseek", "r+");
|
152 |
|
|
if (!stream) {
|
153 |
|
|
diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
err = fread(buf1,sizeof(buf1),1, stream);
|
157 |
|
|
if (err != 1) SHOW_RESULT( fread, err );
|
158 |
|
|
|
159 |
|
|
CYG_TEST_INFO("Comparing contents");
|
160 |
|
|
if (memcmp(buf, buf1, sizeof(buf1))) {
|
161 |
|
|
CYG_TEST_FAIL("File contents inconsistent");
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
err = fclose(stream);
|
165 |
|
|
if (err != 0) SHOW_RESULT( fclose, err );
|
166 |
|
|
|
167 |
|
|
CYG_TEST_INFO("open file /fseek");
|
168 |
|
|
stream = fopen("/fseek", "r+");
|
169 |
|
|
if (!stream) {
|
170 |
|
|
diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
CYG_TEST_INFO("fseek()ing past the end to create a hole");
|
174 |
|
|
/* Seek 1K after the end of the file */
|
175 |
|
|
err = fseek(stream, sizeof(buf), SEEK_END);
|
176 |
|
|
if ( err < 0 ) SHOW_RESULT( fseek, err );
|
177 |
|
|
|
178 |
|
|
pos = ftell(stream);
|
179 |
|
|
|
180 |
|
|
if (pos < 0) SHOW_RESULT( ftell, pos );
|
181 |
|
|
if (pos != (2*sizeof(buf))) CYG_TEST_FAIL("ftell is not telling the truth");
|
182 |
|
|
|
183 |
|
|
CYG_TEST_INFO("writing test pattern");
|
184 |
|
|
err=fwrite(buf,sizeof(buf), 1, stream);
|
185 |
|
|
if ( err < 0 ) SHOW_RESULT( fwrite, err );
|
186 |
|
|
|
187 |
|
|
pos = ftell(stream);
|
188 |
|
|
|
189 |
|
|
if (pos < 0) SHOW_RESULT( ftell, pos );
|
190 |
|
|
if (pos != (3*sizeof(buf))) CYG_TEST_FAIL("ftell is not telling the truth");
|
191 |
|
|
|
192 |
|
|
CYG_TEST_INFO("closing file");
|
193 |
|
|
err = fclose(stream);
|
194 |
|
|
if (err != 0) SHOW_RESULT( fclose, err );
|
195 |
|
|
|
196 |
|
|
CYG_TEST_INFO("open file /fseek");
|
197 |
|
|
stream = fopen("/fseek", "r+");
|
198 |
|
|
if (!stream) {
|
199 |
|
|
diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
err = fread(buf1,sizeof(buf1),1, stream);
|
203 |
|
|
if (err != 1) SHOW_RESULT( fread, err );
|
204 |
|
|
|
205 |
|
|
CYG_TEST_INFO("Comparing contents");
|
206 |
|
|
if (memcmp(buf, buf1, sizeof(buf1))) {
|
207 |
|
|
CYG_TEST_FAIL("File contents inconsistent");
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
err = fread(buf1,sizeof(buf1),1, stream);
|
211 |
|
|
if (err != 1) SHOW_RESULT( fread, err );
|
212 |
|
|
|
213 |
|
|
for (i = 0; i< sizeof(buf); i++) {
|
214 |
|
|
if (buf1[i] != 0)
|
215 |
|
|
CYG_TEST_FAIL("Hole does not contain zeros");
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
err = fread(buf1,sizeof(buf1),1, stream);
|
219 |
|
|
if (err != 1) SHOW_RESULT( fread, err );
|
220 |
|
|
|
221 |
|
|
if (memcmp(buf, buf1, sizeof(buf1))) {
|
222 |
|
|
CYG_TEST_FAIL("File contents inconsistent");
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
CYG_TEST_INFO("closing file");
|
226 |
|
|
|
227 |
|
|
/* Close the file */
|
228 |
|
|
err = fclose(stream);
|
229 |
|
|
if (err != 0) SHOW_RESULT( fclose, err );
|
230 |
|
|
|
231 |
|
|
CYG_TEST_INFO("umount /");
|
232 |
|
|
err = umount( "/" );
|
233 |
|
|
if( err < 0 ) SHOW_RESULT( umount, err );
|
234 |
|
|
|
235 |
|
|
CYG_TEST_PASS_FINISH("ramfs2");
|
236 |
|
|
|
237 |
|
|
|
238 |
|
|
}
|