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