summaryrefslogtreecommitdiff
path: root/resize/test_extent.c
blob: fa3f5052ce1c0ca16d9a1e7742138c76f391ba3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * test_extent.c --- tester for the extent abstraction
 *
 * Copyright (C) 1997, 1998 by Theodore Ts'o and
 * 	PowerQuest, Inc.
 *
 * Copyright (C) 1999, 2000 by Theosore Ts'o
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */

#include "config.h"
#include "resize2fs.h"

void do_test(FILE *in, FILE *out);

void do_test(FILE *in, FILE *out)
{
	char		buf[128];
	char		*cp, *cmd, *arg1, *arg2;
	__u64		num1, num2;
	__u64		size;
	errcode_t	retval;
	ext2_extent	extent = 0;
	const char	*no_table = "# No extent table\n";

	while (!feof(in)) {
		if (!fgets(buf, sizeof(buf), in))
			break;
		/*
		 * Ignore comments
		 */
		if (buf[0] =='#')
			continue;

		/*
		 * Echo command
		 */
		fputs(buf, out);

		cp = strchr(buf, '\n');
		if (cp)
			*cp = '\0';

		/*
		 * Parse command line; simple, at most two arguments
		 */
		cmd = buf;
		num1 = num2 = 0;
		arg1 = arg2 = 0;
		cp = strchr(buf, ' ');
		if (cp) {
			*cp++ = '\0';
			arg1 = cp;
			num1 = strtoul(arg1, 0, 0);

			cp = strchr(cp, ' ');
		}
		if (cp) {
			*cp++ = '\0';
			arg2 = cp;
			num2 = strtoul(arg2, 0, 0);
		}

		if (!strcmp(cmd, "create")) {
			retval = ext2fs_create_extent_table(&extent, num1);
			if (retval) {
			handle_error:
				fprintf(out, "# Error: %s\n",
					error_message(retval));
				continue;
			}
			continue;
		}
		if (!extent) {
			fputs(no_table, out);
			continue;
		}
		if (!strcmp(cmd, "free")) {
			ext2fs_free_extent_table(extent);
			extent = 0;
		} else if (!strcmp(cmd, "add")) {
			retval = ext2fs_add_extent_entry(extent, num1, num2);
			if (retval)
				goto handle_error;
		} else if (!strcmp(cmd, "lookup")) {
			num2 = ext2fs_extent_translate(extent, num1);
			fprintf(out, "# Answer: %llu%s\n", num2,
				num2 ? "" : " (not found)");
		} else if (!strcmp(cmd, "dump")) {
			ext2fs_extent_dump(extent, out);
		} else if (!strcmp(cmd, "iter_test")) {
			retval = ext2fs_iterate_extent(extent, 0, 0, 0);
			if (retval)
				goto handle_error;
			while (1) {
				retval = ext2fs_iterate_extent(extent,
					       &num1, &num2, &size);
				if (retval)
					goto handle_error;
				if (!size)
					break;
				fprintf(out, "# %llu -> %llu (%llu)\n",
					num1, num2, size);
			}
		} else
			fputs("# Syntax error\n", out);
	}
}

#ifdef __GNUC__
#define ATTR(x) __attribute__(x)
#else
#define ATTR(x)
#endif

int main(int argc ATTR((unused)), char **argv ATTR((unused)))
{
	do_test(stdin, stdout);
	exit(0);
}