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 Theosore 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 "resize2fs.h"
16
17 void do_test(FILE *in, FILE *out);
18
do_test(FILE * in,FILE * out)19 void do_test(FILE *in, FILE *out)
20 {
21 char buf[128];
22 char *cp, *cmd, *arg1, *arg2;
23 __u64 num1, num2;
24 __u64 size;
25 errcode_t retval;
26 ext2_extent extent = 0;
27 const char *no_table = "# No extent table\n";
28
29 while (!feof(in)) {
30 if (!fgets(buf, sizeof(buf), in))
31 break;
32 /*
33 * Ignore comments
34 */
35 if (buf[0] =='#')
36 continue;
37
38 /*
39 * Echo command
40 */
41 fputs(buf, out);
42
43 cp = strchr(buf, '\n');
44 if (cp)
45 *cp = '\0';
46
47 /*
48 * Parse command line; simple, at most two arguments
49 */
50 cmd = buf;
51 num1 = num2 = 0;
52 arg1 = arg2 = 0;
53 cp = strchr(buf, ' ');
54 if (cp) {
55 *cp++ = '\0';
56 arg1 = cp;
57 num1 = strtoul(arg1, 0, 0);
58
59 cp = strchr(cp, ' ');
60 }
61 if (cp) {
62 *cp++ = '\0';
63 arg2 = cp;
64 num2 = strtoul(arg2, 0, 0);
65 }
66
67 if (!strcmp(cmd, "create")) {
68 retval = ext2fs_create_extent_table(&extent, num1);
69 if (retval) {
70 handle_error:
71 fprintf(out, "# Error: %s\n",
72 error_message(retval));
73 continue;
74 }
75 continue;
76 }
77 if (!extent) {
78 fputs(no_table, out);
79 continue;
80 }
81 if (!strcmp(cmd, "free")) {
82 ext2fs_free_extent_table(extent);
83 extent = 0;
84 } else if (!strcmp(cmd, "add")) {
85 retval = ext2fs_add_extent_entry(extent, num1, num2);
86 if (retval)
87 goto handle_error;
88 } else if (!strcmp(cmd, "lookup")) {
89 num2 = ext2fs_extent_translate(extent, num1);
90 fprintf(out, "# Answer: %llu%s\n", num2,
91 num2 ? "" : " (not found)");
92 } else if (!strcmp(cmd, "dump")) {
93 ext2fs_extent_dump(extent, out);
94 } else if (!strcmp(cmd, "iter_test")) {
95 retval = ext2fs_iterate_extent(extent, 0, 0, 0);
96 if (retval)
97 goto handle_error;
98 while (1) {
99 retval = ext2fs_iterate_extent(extent,
100 &num1, &num2, &size);
101 if (retval)
102 goto handle_error;
103 if (!size)
104 break;
105 fprintf(out, "# %llu -> %llu (%llu)\n",
106 num1, num2, size);
107 }
108 } else
109 fputs("# Syntax error\n", out);
110 }
111 }
112
113 #ifdef __GNUC__
114 #define ATTR(x) __attribute__(x)
115 #else
116 #define ATTR(x)
117 #endif
118
main(int argc ATTR ((unused)),char ** argv ATTR ((unused)))119 int main(int argc ATTR((unused)), char **argv ATTR((unused)))
120 {
121 do_test(stdin, stdout);
122 exit(0);
123 }
124