test_io.c revision c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4e
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), &io); 125 if (retval) 126 return retval; 127 memset(io, 0, sizeof(struct struct_io_channel)); 128 io->magic = EXT2_ET_MAGIC_IO_CHANNEL; 129 retval = ext2fs_get_mem(sizeof(struct test_private_data), &data); 130 if (retval) { 131 retval = EXT2_ET_NO_MEMORY; 132 goto cleanup; 133 } 134 io->manager = test_io_manager; 135 retval = ext2fs_get_mem(strlen(name)+1, &io->name); 136 if (retval) 137 goto cleanup; 138 139 strcpy(io->name, name); 140 io->private_data = data; 141 io->block_size = 1024; 142 io->read_error = 0; 143 io->write_error = 0; 144 io->refcount = 1; 145 146 memset(data, 0, sizeof(struct test_private_data)); 147 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL; 148 if (test_io_backing_manager) { 149 retval = test_io_backing_manager->open(name, flags, 150 &data->real); 151 if (retval) 152 goto cleanup; 153 } else 154 data->real = 0; 155 data->read_blk = test_io_cb_read_blk; 156 data->write_blk = test_io_cb_write_blk; 157 data->set_blksize = test_io_cb_set_blksize; 158 data->write_byte = test_io_cb_write_byte; 159 160 data->outfile = NULL; 161 if (getenv("TEST_IO_LOGFILE")) 162 data->outfile = fopen(getenv("TEST_IO_LOGFILE"), "w"); 163 if (!data->outfile) 164 data->outfile = stderr; 165 166 data->flags = 0; 167 if (getenv("TEST_IO_FLAGS")) 168 data->flags = strtoul(getenv("TEST_IO_FLAGS"), NULL, 0); 169 170 data->block = 0; 171 if (getenv("TEST_IO_BLOCK")) 172 data->block = strtoul(getenv("TEST_IO_BLOCK"), NULL, 0); 173 174 *channel = io; 175 return 0; 176 177cleanup: 178 if (io) 179 ext2fs_free_mem(&io); 180 if (data) 181 ext2fs_free_mem(&data); 182 return retval; 183} 184 185static errcode_t test_close(io_channel channel) 186{ 187 struct test_private_data *data; 188 errcode_t retval = 0; 189 190 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 191 data = (struct test_private_data *) channel->private_data; 192 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 193 194 if (--channel->refcount > 0) 195 return 0; 196 197 if (data->real) 198 retval = io_channel_close(data->real); 199 200 if (data->outfile && data->outfile != stderr) 201 fclose(data->outfile); 202 203 ext2fs_free_mem(&channel->private_data); 204 if (channel->name) 205 ext2fs_free_mem(&channel->name); 206 ext2fs_free_mem(&channel); 207 return retval; 208} 209 210static errcode_t test_set_blksize(io_channel channel, int blksize) 211{ 212 struct test_private_data *data; 213 errcode_t retval = 0; 214 215 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 216 data = (struct test_private_data *) channel->private_data; 217 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 218 219 if (data->real) 220 retval = io_channel_set_blksize(data->real, blksize); 221 if (data->set_blksize) 222 data->set_blksize(blksize, retval); 223 if (data->flags & TEST_FLAG_SET_BLKSIZE) 224 fprintf(data->outfile, 225 "Test_io: set_blksize(%d) returned %s\n", 226 blksize, retval ? error_message(retval) : "OK"); 227 channel->block_size = blksize; 228 return retval; 229} 230 231 232static errcode_t test_read_blk(io_channel channel, unsigned long block, 233 int count, void *buf) 234{ 235 struct test_private_data *data; 236 errcode_t retval = 0; 237 238 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 239 data = (struct test_private_data *) channel->private_data; 240 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 241 242 if (data->real) 243 retval = io_channel_read_blk(data->real, block, count, buf); 244 if (data->read_blk) 245 data->read_blk(block, count, retval); 246 if (data->flags & TEST_FLAG_READ) 247 fprintf(data->outfile, 248 "Test_io: read_blk(%lu, %d) returned %s\n", 249 block, count, retval ? error_message(retval) : "OK"); 250 if (data->block && data->block == block) 251 test_dump_block(channel, data, block, buf); 252 return retval; 253} 254 255static errcode_t test_write_blk(io_channel channel, unsigned long block, 256 int count, const void *buf) 257{ 258 struct test_private_data *data; 259 errcode_t retval = 0; 260 261 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 262 data = (struct test_private_data *) channel->private_data; 263 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 264 265 if (data->real) 266 retval = io_channel_write_blk(data->real, block, count, buf); 267 if (data->write_blk) 268 data->write_blk(block, count, retval); 269 if (data->flags & TEST_FLAG_WRITE) 270 fprintf(data->outfile, 271 "Test_io: write_blk(%lu, %d) returned %s\n", 272 block, count, retval ? error_message(retval) : "OK"); 273 if (data->block && data->block == block) 274 test_dump_block(channel, data, block, buf); 275 return retval; 276} 277 278static errcode_t test_write_byte(io_channel channel, unsigned long offset, 279 int count, const void *buf) 280{ 281 struct test_private_data *data; 282 errcode_t retval = 0; 283 284 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 285 data = (struct test_private_data *) channel->private_data; 286 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 287 288 if (data->real && data->real->manager->write_byte) 289 retval = io_channel_write_byte(data->real, offset, count, buf); 290 if (data->write_byte) 291 data->write_byte(offset, count, retval); 292 if (data->flags & TEST_FLAG_WRITE) 293 fprintf(data->outfile, 294 "Test_io: write_byte(%lu, %d) returned %s\n", 295 offset, count, retval ? error_message(retval) : "OK"); 296 return retval; 297} 298 299/* 300 * Flush data buffers to disk. 301 */ 302static errcode_t test_flush(io_channel channel) 303{ 304 struct test_private_data *data; 305 errcode_t retval = 0; 306 307 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 308 data = (struct test_private_data *) channel->private_data; 309 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 310 311 if (data->real) 312 retval = io_channel_flush(data->real); 313 314 if (data->flags & TEST_FLAG_FLUSH) 315 fprintf(data->outfile, "Test_io: flush() returned %s\n", 316 retval ? error_message(retval) : "OK"); 317 318 return retval; 319} 320 321