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
19 #include <sys/types.h>
24 #include "et/com_err.h"
25 #include "ext2fs/ext2_err.h"
29 * For checking structure magic numbers...
32 #define EXT2_CHECK_MAGIC(struct, code) \
33 if ((struct)->magic != (code)) return (code)
35 struct test_private_data {
38 void (*read_blk)(unsigned long block, int count, errcode_t err);
39 void (*write_blk)(unsigned long block, int count, errcode_t err);
40 void (*set_blksize)(int blksize, errcode_t err);
43 static errcode_t test_open(const char *name, int flags, io_channel *channel);
44 static errcode_t test_close(io_channel channel);
45 static errcode_t test_set_blksize(io_channel channel, int blksize);
46 static errcode_t test_read_blk(io_channel channel, unsigned long block,
47 int count, void *data);
48 static errcode_t test_write_blk(io_channel channel, unsigned long block,
49 int count, const void *data);
50 static errcode_t test_flush(io_channel channel);
52 static struct struct_io_manager struct_test_manager = {
53 EXT2_ET_MAGIC_IO_MANAGER,
63 io_manager test_io_manager = &struct_test_manager;
66 * These global variable can be set by the test program as
67 * necessary *before* calling test_open
69 io_manager test_io_backing_manager = 0;
70 void (*test_io_cb_read_blk)
71 (unsigned long block, int count, errcode_t err) = 0;
72 void (*test_io_cb_write_blk)
73 (unsigned long block, int count, errcode_t err) = 0;
74 void (*test_io_cb_set_blksize)
75 (int blksize, errcode_t err) = 0;
77 static errcode_t test_open(const char *name, int flags, io_channel *channel)
80 struct test_private_data *data = NULL;
84 return EXT2_ET_BAD_DEVICE_NAME;
85 io = (io_channel) malloc(sizeof(struct struct_io_channel));
88 memset(io, 0, sizeof(struct struct_io_channel));
89 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
90 data = (struct test_private_data *)
91 malloc(sizeof(struct test_private_data));
96 io->manager = test_io_manager;
97 io->name = malloc(strlen(name)+1);
102 strcpy(io->name, name);
103 io->private_data = data;
104 io->block_size = 1024;
108 memset(data, 0, sizeof(struct test_private_data));
109 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
110 if (test_io_backing_manager) {
111 retval = test_io_backing_manager->open(name, flags,
117 data->read_blk = test_io_cb_read_blk;
118 data->write_blk = test_io_cb_write_blk;
119 data->set_blksize = test_io_cb_set_blksize;
132 static errcode_t test_close(io_channel channel)
134 struct test_private_data *data;
135 errcode_t retval = 0;
137 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
138 data = (struct test_private_data *) channel->private_data;
139 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
142 retval = io_channel_close(data->real);
144 if (channel->private_data)
145 free(channel->private_data);
152 static errcode_t test_set_blksize(io_channel channel, int blksize)
154 struct test_private_data *data;
155 errcode_t retval = 0;
157 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
158 data = (struct test_private_data *) channel->private_data;
159 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
162 retval = io_channel_set_blksize(data->real, blksize);
163 if (data->set_blksize)
164 data->set_blksize(blksize, retval);
166 printf("Test_io: set_blksize(%d) returned %s\n",
167 blksize, retval ? error_message(retval) : "OK");
172 static errcode_t test_read_blk(io_channel channel, unsigned long block,
173 int count, void *buf)
175 struct test_private_data *data;
176 errcode_t retval = 0;
178 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
179 data = (struct test_private_data *) channel->private_data;
180 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
183 retval = io_channel_read_blk(data->real, block, count, buf);
185 data->read_blk(block, count, retval);
187 printf("Test_io: read_blk(%lu, %d) returned %s\n",
188 block, count, retval ? error_message(retval) : "OK");
192 static errcode_t test_write_blk(io_channel channel, unsigned long block,
193 int count, const void *buf)
195 struct test_private_data *data;
196 errcode_t retval = 0;
198 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
199 data = (struct test_private_data *) channel->private_data;
200 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
203 retval = io_channel_write_blk(data->real, block, count, buf);
205 data->write_blk(block, count, retval);
207 printf("Test_io: write_blk(%lu, %d) returned %s\n",
208 block, count, retval ? error_message(retval) : "OK");
213 * Flush data buffers to disk.
215 static errcode_t test_flush(io_channel channel)
217 struct test_private_data *data;
219 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
220 data = (struct test_private_data *) channel->private_data;
221 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
224 return io_channel_flush(data->real);