Whamcloud - gitweb
libext2fs: in ext2fs_open[2](), return an error if s_first_meta_bg is too big
[tools/e2fsprogs.git] / resize / test_extent.c
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",
92                                 (unsigned long long) num2,
93                                 num2 ? "" : " (not found)");
94                 } else if (!strcmp(cmd, "dump")) {
95                         ext2fs_extent_dump(extent, out);
96                 } else if (!strcmp(cmd, "iter_test")) {
97                         retval = ext2fs_iterate_extent(extent, 0, 0, 0);
98                         if (retval)
99                                 goto handle_error;
100                         while (1) {
101                                 retval = ext2fs_iterate_extent(extent,
102                                                &num1, &num2, &size);
103                                 if (retval)
104                                         goto handle_error;
105                                 if (!size)
106                                         break;
107                                 fprintf(out, "# %llu -> %llu (%llu)\n",
108                                         (unsigned long long) num1,
109                                         (unsigned long long) num2,
110                                         (unsigned long long) size);
111                         }
112                 } else
113                         fputs("# Syntax error\n", out);
114         }
115         if (extent)
116                 ext2fs_free_extent_table(extent);
117 }
118
119 #ifdef __GNUC__
120 #define ATTR(x) __attribute__(x)
121 #else
122 #define ATTR(x)
123 #endif
124
125 int main(int argc ATTR((unused)), char **argv ATTR((unused)))
126 {
127         do_test(stdin, stdout);
128         exit(0);
129 }