]>
Commit | Line | Data |
---|---|---|
c762c8e6 TT |
1 | /* |
2 | * test_extent.c --- tester for the extent abstraction | |
3 | * | |
0cee8a5c TT |
4 | * Copyright (C) 1997, 1998 by Theodore Ts'o and |
5 | * PowerQuest, Inc. | |
6 | * | |
055866d8 | 7 | * Copyright (C) 1999, 2000 by Theodore Ts'o |
efc6f628 | 8 | * |
c762c8e6 | 9 | * %Begin-Header% |
0cee8a5c TT |
10 | * This file may be redistributed under the terms of the GNU Public |
11 | * License. | |
c762c8e6 TT |
12 | * %End-Header% |
13 | */ | |
14 | ||
d1154eb4 | 15 | #include "config.h" |
c762c8e6 TT |
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; | |
8728d506 VAH |
24 | __u64 num1, num2; |
25 | __u64 size; | |
c762c8e6 TT |
26 | errcode_t retval; |
27 | ext2_extent extent = 0; | |
28 | const char *no_table = "# No extent table\n"; | |
efc6f628 | 29 | |
c762c8e6 TT |
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); | |
efc6f628 | 59 | |
c762c8e6 TT |
60 | cp = strchr(cp, ' '); |
61 | } | |
62 | if (cp) { | |
63 | *cp++ = '\0'; | |
64 | arg2 = cp; | |
65 | num2 = strtoul(arg2, 0, 0); | |
66 | } | |
efc6f628 | 67 | |
c762c8e6 TT |
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; | |
efc6f628 | 81 | } |
c762c8e6 TT |
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); | |
8728d506 | 91 | fprintf(out, "# Answer: %llu%s\n", num2, |
c762c8e6 TT |
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; | |
8728d506 | 106 | fprintf(out, "# %llu -> %llu (%llu)\n", |
c762c8e6 TT |
107 | num1, num2, size); |
108 | } | |
efc6f628 | 109 | } else |
c762c8e6 TT |
110 | fputs("# Syntax error\n", out); |
111 | } | |
c2c69597 TT |
112 | if (extent) |
113 | ext2fs_free_extent_table(extent); | |
c762c8e6 TT |
114 | } |
115 | ||
2c25f7e7 TT |
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))) | |
c762c8e6 TT |
123 | { |
124 | do_test(stdin, stdout); | |
125 | exit(0); | |
126 | } |