test_io.c revision 48e6e81362f264aee4f3945c14928efaf71a06c9
1/* 2 * test_io.c --- This is the Test I/O interface. 3 * 4 * Copyright (C) 1996 Theodore Ts'o. 5 * 6 * %Begin-Header% 7 * This file may be redistributed under the terms of the GNU Public 8 * License. 9 * %End-Header% 10 */ 11 12#include <stdio.h> 13#include <string.h> 14#if HAVE_UNISTD_H 15#include <unistd.h> 16#endif 17#include <fcntl.h> 18#include <time.h> 19#if HAVE_SYS_STAT_H 20#include <sys/stat.h> 21#endif 22#if HAVE_SYS_TYPES_H 23#include <sys/types.h> 24#endif 25 26#include "ext2_fs.h" 27#include "ext2fs.h" 28 29/* 30 * For checking structure magic numbers... 31 */ 32 33#define EXT2_CHECK_MAGIC(struct, code) \ 34 if ((struct)->magic != (code)) return (code) 35 36struct test_private_data { 37 int magic; 38 io_channel real; 39 int flags; 40 FILE *outfile; 41 unsigned long block; 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); 46}; 47 48static errcode_t test_open(const char *name, int flags, io_channel *channel); 49static errcode_t test_close(io_channel channel); 50static errcode_t test_set_blksize(io_channel channel, int blksize); 51static errcode_t test_read_blk(io_channel channel, unsigned long block, 52 int count, void *data); 53static errcode_t test_write_blk(io_channel channel, unsigned long block, 54 int count, const void *data); 55static errcode_t test_flush(io_channel channel); 56static errcode_t test_write_byte(io_channel channel, unsigned long offset, 57 int count, const void *buf); 58 59static struct struct_io_manager struct_test_manager = { 60 EXT2_ET_MAGIC_IO_MANAGER, 61 "Test I/O Manager", 62 test_open, 63 test_close, 64 test_set_blksize, 65 test_read_blk, 66 test_write_blk, 67 test_flush, 68 test_write_byte 69 70}; 71 72io_manager test_io_manager = &struct_test_manager; 73 74/* 75 * These global variable can be set by the test program as 76 * necessary *before* calling test_open 77 */ 78io_manager test_io_backing_manager = 0; 79void (*test_io_cb_read_blk) 80 (unsigned long block, int count, errcode_t err) = 0; 81void (*test_io_cb_write_blk) 82 (unsigned long block, int count, errcode_t err) = 0; 83void (*test_io_cb_set_blksize) 84 (int blksize, errcode_t err) = 0; 85void (*test_io_cb_write_byte) 86 (unsigned long block, int count, errcode_t err) = 0; 87 88/* 89 * Test flags 90 */ 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 95 96static void test_dump_block(io_channel channel, 97 struct test_private_data *data, 98 unsigned long block, const void *buf) 99{ 100 const unsigned char *cp; 101 FILE *f = data->outfile; 102 int i; 103 unsigned long cksum = 0; 104 105 for (i=0, cp = buf; i < channel->block_size; i++, cp++) { 106 cksum += *cp; 107 } 108 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum); 109 for (i=0, cp = buf; i < channel->block_size; i++, cp++) { 110 if ((i % 16) == 0) 111 fprintf(f, "%04x: ", i); 112 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' '); 113 } 114} 115 116static errcode_t test_open(const char *name, int flags, io_channel *channel) 117{ 118 io_channel io = NULL; 119 struct test_private_data *data = NULL; 120 errcode_t retval; 121 122 if (name == 0) 123 return EXT2_ET_BAD_DEVICE_NAME; 124 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), 125 (void **) &io); 126 if (retval) 127 return retval; 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), 131 (void **) &data); 132 if (retval) { 133 retval = EXT2_ET_NO_MEMORY; 134 goto cleanup; 135 } 136 io->manager = test_io_manager; 137 retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name); 138 if (retval) 139 goto cleanup; 140 141 strcpy(io->name, name); 142 io->private_data = data; 143 io->block_size = 1024; 144 io->read_error = 0; 145 io->write_error = 0; 146 io->refcount = 1; 147 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, 152 &data->real); 153 if (retval) 154 goto cleanup; 155 } else 156 data->real = 0; 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; 161 162 data->outfile = NULL; 163 if (getenv("TEST_IO_LOGFILE")) 164 data->outfile = fopen(getenv("TEST_IO_LOGFILE"), "w"); 165 if (!data->outfile) 166 data->outfile = stderr; 167 168 data->flags = 0; 169 if (getenv("TEST_IO_FLAGS")) 170 data->flags = strtoul(getenv("TEST_IO_FLAGS"), NULL, 0); 171 172 data->block = 0; 173 if (getenv("TEST_IO_BLOCK")) 174 data->block = strtoul(getenv("TEST_IO_BLOCK"), NULL, 0); 175 176 *channel = io; 177 return 0; 178 179cleanup: 180 if (io) 181 ext2fs_free_mem((void **) &io); 182 if (data) 183 ext2fs_free_mem((void **) &data); 184 return retval; 185} 186 187static errcode_t test_close(io_channel channel) 188{ 189 struct test_private_data *data; 190 errcode_t retval = 0; 191 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); 195 196 if (--channel->refcount > 0) 197 return 0; 198 199 if (data->real) 200 retval = io_channel_close(data->real); 201 202 if (data->outfile && data->outfile != stderr) 203 fclose(data->outfile); 204 205 ext2fs_free_mem((void **) &channel->private_data); 206 if (channel->name) 207 ext2fs_free_mem((void **) &channel->name); 208 ext2fs_free_mem((void **) &channel); 209 return retval; 210} 211 212static errcode_t test_set_blksize(io_channel channel, int blksize) 213{ 214 struct test_private_data *data; 215 errcode_t retval = 0; 216 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); 220 221 if (data->real) 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; 230 return retval; 231} 232 233 234static errcode_t test_read_blk(io_channel channel, unsigned long block, 235 int count, void *buf) 236{ 237 struct test_private_data *data; 238 errcode_t retval = 0; 239 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); 243 244 if (data->real) 245 retval = io_channel_read_blk(data->real, block, count, buf); 246 if (data->read_blk) 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); 254 return retval; 255} 256 257static errcode_t test_write_blk(io_channel channel, unsigned long block, 258 int count, const void *buf) 259{ 260 struct test_private_data *data; 261 errcode_t retval = 0; 262 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); 266 267 if (data->real) 268 retval = io_channel_write_blk(data->real, block, count, buf); 269 if (data->write_blk) 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); 277 return retval; 278} 279 280static errcode_t test_write_byte(io_channel channel, unsigned long offset, 281 int count, const void *buf) 282{ 283 struct test_private_data *data; 284 errcode_t retval = 0; 285 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); 289 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"); 298 return retval; 299} 300 301/* 302 * Flush data buffers to disk. 303 */ 304static errcode_t test_flush(io_channel channel) 305{ 306 struct test_private_data *data; 307 errcode_t retval = 0; 308 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); 312 313 if (data->real) 314 retval = io_channel_flush(data->real); 315 316 if (data->flags & TEST_FLAG_FLUSH) 317 fprintf(data->outfile, "Test_io: flush() returned %s\n", 318 retval ? error_message(retval) : "OK"); 319 320 return retval; 321} 322 323