2 * test_io.c --- This is the Test I/O interface.
4 * Copyright (C) 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
23 #include <sys/types.h>
30 * For checking structure magic numbers...
33 #define EXT2_CHECK_MAGIC(struct, code) \
34 if ((struct)->magic != (code)) return (code)
36 struct test_private_data {
42 void (*read_blk)(unsigned long block, int count, errcode_t err);
43 void (*write_blk)(unsigned long block, int count, errcode_t err);
44 void (*set_blksize)(int blksize, errcode_t err);
45 void (*write_byte)(unsigned long block, int count, errcode_t err);
48 static errcode_t test_open(const char *name, int flags, io_channel *channel);
49 static errcode_t test_close(io_channel channel);
50 static errcode_t test_set_blksize(io_channel channel, int blksize);
51 static errcode_t test_read_blk(io_channel channel, unsigned long block,
52 int count, void *data);
53 static errcode_t test_write_blk(io_channel channel, unsigned long block,
54 int count, const void *data);
55 static errcode_t test_flush(io_channel channel);
56 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
57 int count, const void *buf);
59 static struct struct_io_manager struct_test_manager = {
60 EXT2_ET_MAGIC_IO_MANAGER,
72 io_manager test_io_manager = &struct_test_manager;
75 * These global variable can be set by the test program as
76 * necessary *before* calling test_open
78 io_manager test_io_backing_manager = 0;
79 void (*test_io_cb_read_blk)
80 (unsigned long block, int count, errcode_t err) = 0;
81 void (*test_io_cb_write_blk)
82 (unsigned long block, int count, errcode_t err) = 0;
83 void (*test_io_cb_set_blksize)
84 (int blksize, errcode_t err) = 0;
85 void (*test_io_cb_write_byte)
86 (unsigned long block, int count, errcode_t err) = 0;
91 #define TEST_FLAG_READ 0x01
92 #define TEST_FLAG_WRITE 0x02
93 #define TEST_FLAG_SET_BLKSIZE 0x04
94 #define TEST_FLAG_FLUSH 0x08
96 static void test_dump_block(io_channel channel,
97 struct test_private_data *data,
98 unsigned long block, const void *buf)
100 const unsigned char *cp;
101 FILE *f = data->outfile;
103 unsigned long cksum = 0;
105 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
108 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
109 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
111 fprintf(f, "%04x: ", i);
112 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
116 static errcode_t test_open(const char *name, int flags, io_channel *channel)
118 io_channel io = NULL;
119 struct test_private_data *data = NULL;
123 return EXT2_ET_BAD_DEVICE_NAME;
124 retval = ext2fs_get_mem(sizeof(struct struct_io_channel),
128 memset(io, 0, sizeof(struct struct_io_channel));
129 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
130 retval = ext2fs_get_mem(sizeof(struct test_private_data),
133 retval = EXT2_ET_NO_MEMORY;
136 io->manager = test_io_manager;
137 retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name);
141 strcpy(io->name, name);
142 io->private_data = data;
143 io->block_size = 1024;
148 memset(data, 0, sizeof(struct test_private_data));
149 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
150 if (test_io_backing_manager) {
151 retval = test_io_backing_manager->open(name, flags,
157 data->read_blk = test_io_cb_read_blk;
158 data->write_blk = test_io_cb_write_blk;
159 data->set_blksize = test_io_cb_set_blksize;
160 data->write_byte = test_io_cb_write_byte;
162 data->outfile = NULL;
163 if (getenv("TEST_IO_LOGFILE"))
164 data->outfile = fopen(getenv("TEST_IO_LOGFILE"), "w");
166 data->outfile = stderr;
169 if (getenv("TEST_IO_FLAGS"))
170 data->flags = strtoul(getenv("TEST_IO_FLAGS"), NULL, 0);
173 if (getenv("TEST_IO_BLOCK"))
174 data->block = strtoul(getenv("TEST_IO_BLOCK"), NULL, 0);
181 ext2fs_free_mem((void **) &io);
183 ext2fs_free_mem((void **) &data);
187 static errcode_t test_close(io_channel channel)
189 struct test_private_data *data;
190 errcode_t retval = 0;
192 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
193 data = (struct test_private_data *) channel->private_data;
194 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
196 if (--channel->refcount > 0)
200 retval = io_channel_close(data->real);
202 if (data->outfile && data->outfile != stderr)
203 fclose(data->outfile);
205 ext2fs_free_mem((void **) &channel->private_data);
207 ext2fs_free_mem((void **) &channel->name);
208 ext2fs_free_mem((void **) &channel);
212 static errcode_t test_set_blksize(io_channel channel, int blksize)
214 struct test_private_data *data;
215 errcode_t retval = 0;
217 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
218 data = (struct test_private_data *) channel->private_data;
219 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
222 retval = io_channel_set_blksize(data->real, blksize);
223 if (data->set_blksize)
224 data->set_blksize(blksize, retval);
225 if (data->flags & TEST_FLAG_SET_BLKSIZE)
226 fprintf(data->outfile,
227 "Test_io: set_blksize(%d) returned %s\n",
228 blksize, retval ? error_message(retval) : "OK");
229 channel->block_size = blksize;
234 static errcode_t test_read_blk(io_channel channel, unsigned long block,
235 int count, void *buf)
237 struct test_private_data *data;
238 errcode_t retval = 0;
240 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
241 data = (struct test_private_data *) channel->private_data;
242 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
245 retval = io_channel_read_blk(data->real, block, count, buf);
247 data->read_blk(block, count, retval);
248 if (data->flags & TEST_FLAG_READ)
249 fprintf(data->outfile,
250 "Test_io: read_blk(%lu, %d) returned %s\n",
251 block, count, retval ? error_message(retval) : "OK");
252 if (data->block && data->block == block)
253 test_dump_block(channel, data, block, buf);
257 static errcode_t test_write_blk(io_channel channel, unsigned long block,
258 int count, const void *buf)
260 struct test_private_data *data;
261 errcode_t retval = 0;
263 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
264 data = (struct test_private_data *) channel->private_data;
265 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
268 retval = io_channel_write_blk(data->real, block, count, buf);
270 data->write_blk(block, count, retval);
271 if (data->flags & TEST_FLAG_WRITE)
272 fprintf(data->outfile,
273 "Test_io: write_blk(%lu, %d) returned %s\n",
274 block, count, retval ? error_message(retval) : "OK");
275 if (data->block && data->block == block)
276 test_dump_block(channel, data, block, buf);
280 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
281 int count, const void *buf)
283 struct test_private_data *data;
284 errcode_t retval = 0;
286 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
287 data = (struct test_private_data *) channel->private_data;
288 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
290 if (data->real && data->real->manager->write_byte)
291 retval = io_channel_write_byte(data->real, offset, count, buf);
292 if (data->write_byte)
293 data->write_byte(offset, count, retval);
294 if (data->flags & TEST_FLAG_WRITE)
295 fprintf(data->outfile,
296 "Test_io: write_byte(%lu, %d) returned %s\n",
297 offset, count, retval ? error_message(retval) : "OK");
302 * Flush data buffers to disk.
304 static errcode_t test_flush(io_channel channel)
306 struct test_private_data *data;
307 errcode_t retval = 0;
309 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
310 data = (struct test_private_data *) channel->private_data;
311 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
314 retval = io_channel_flush(data->real);
316 if (data->flags & TEST_FLAG_FLUSH)
317 fprintf(data->outfile, "Test_io: flush() returned %s\n",
318 retval ? error_message(retval) : "OK");