1 /* 2 * test_extent.c --- tester for the extent abstraction 3 * 4 * Copyright (C) 1997, 1998 by Theodore Ts'o and 5 * PowerQuest, Inc. 6 * 7 * Copyright (C) 1999, 2000 by Theodore Ts'o 8 * 9 * %Begin-Header% 10 * This file may be redistributed under the terms of the GNU Public 11 * License. 12 * %End-Header% 13 */ 14 15 #include "config.h" 16 #include "resize2fs.h" 17 18 void do_test(FILE *in, FILE *out); 19 20 void do_test(FILE *in, FILE *out) 21 { 22 char buf[128]; 23 char *cp, *cmd, *arg1, *arg2; 24 __u64 num1, num2; 25 __u64 size; 26 errcode_t retval; 27 ext2_extent extent = 0; 28 const char *no_table = "# No extent table\n"; 29 30 while (!feof(in)) { 31 if (!fgets(buf, sizeof(buf), in)) 32 break; 33 /* 34 * Ignore comments 35 */ 36 if (buf[0] =='#') 37 continue; 38 39 /* 40 * Echo command 41 */ 42 fputs(buf, out); 43 44 cp = strchr(buf, '\n'); 45 if (cp) 46 *cp = '\0'; 47 48 /* 49 * Parse command line; simple, at most two arguments 50 */ 51 cmd = buf; 52 num1 = num2 = 0; 53 arg1 = arg2 = 0; 54 cp = strchr(buf, ' '); 55 if (cp) { 56 *cp++ = '\0'; 57 arg1 = cp; 58 num1 = strtoul(arg1, 0, 0); 59 60 cp = strchr(cp, ' '); 61 } 62 if (cp) { 63 *cp++ = '\0'; 64 arg2 = cp; 65 num2 = strtoul(arg2, 0, 0); 66 } 67 68 if (!strcmp(cmd, "create")) { 69 retval = ext2fs_create_extent_table(&extent, num1); 70 if (retval) { 71 handle_error: 72 fprintf(out, "# Error: %s\n", 73 error_message(retval)); 74 continue; 75 } 76 continue; 77 } 78 if (!extent) { 79 fputs(no_table, out); 80 continue; 81 } 82 if (!strcmp(cmd, "free")) { 83 ext2fs_free_extent_table(extent); 84 extent = 0; 85 } else if (!strcmp(cmd, "add")) { 86 retval = ext2fs_add_extent_entry(extent, num1, num2); 87 if (retval) 88 goto handle_error; 89 } else if (!strcmp(cmd, "lookup")) { 90 num2 = ext2fs_extent_translate(extent, num1); 91 fprintf(out, "# Answer: %llu%s\n", num2, 92 num2 ? "" : " (not found)"); 93 } else if (!strcmp(cmd, "dump")) { 94 ext2fs_extent_dump(extent, out); 95 } else if (!strcmp(cmd, "iter_test")) { 96 retval = ext2fs_iterate_extent(extent, 0, 0, 0); 97 if (retval) 98 goto handle_error; 99 while (1) { 100 retval = ext2fs_iterate_extent(extent, 101 &num1, &num2, &size); 102 if (retval) 103 goto handle_error; 104 if (!size) 105 break; 106 fprintf(out, "# %llu -> %llu (%llu)\n", 107 num1, num2, size); 108 } 109 } else 110 fputs("# Syntax error\n", out); 111 } 112 if (extent) 113 ext2fs_free_extent_table(extent); 114 } 115 116 #ifdef __GNUC__ 117 #define ATTR(x) __attribute__(x) 118 #else 119 #define ATTR(x) 120 #endif 121 122 int main(int argc ATTR((unused)), char **argv ATTR((unused))) 123 { 124 do_test(stdin, stdout); 125 exit(0); 126 } 127