summaryrefslogtreecommitdiffstats
path: root/tests/progs/test_icount.c
blob: 6ebb100d203b51251b0d03c3118af1341544968c (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 * test_icount.c
 *
 * Copyright (C) 1997 Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <fcntl.h>

#include <ext2fs/ext2_fs.h>

#include <et/com_err.h>
#include <ss/ss.h>
#include <ext2fs/ext2fs.h>
#include <ext2fs/irel.h>
#include <ext2fs/brel.h>

extern ss_request_table test_cmds;

#include "test_icount.h"

ext2_filsys test_fs;
ext2_icount_t test_icount;

/*
 * Helper function which assures that the icount structure is valid
 */
static int check_icount(char *request)
{
	if (test_icount)
		return 0;
	com_err(request, 0, "The icount structure must be allocated.");
	return 1;
}

/*
 * Helper function which parses an inode number.
 */
static int parse_inode(const char *request, const char *desc,
		       const char *str, ext2_ino_t *ino)
{
	char *tmp;

	*ino = strtoul(str, &tmp, 0);
	if (*tmp) {
		com_err(request, 0, "Bad %s - %s", desc, str);
		return 1;
	}
	return 0;
}

void do_create_icount(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
		      void *infop EXT2FS_ATTR((unused)))
{
	errcode_t	retval;
	char		*progname;
	int		flags = 0;
	ext2_ino_t	size = 5;

	progname = *argv;
	argv++; argc --;

	if (argc && !strcmp("-i", *argv)) {
		flags |= EXT2_ICOUNT_OPT_INCREMENT;
		argv++; argc--;
	}
	if (argc) {
		if (parse_inode(progname, "icount size", argv[0], &size))
			return;
		argv++; argc--;
	}
#if 0
	printf("Creating icount... flags=%d, size=%d\n", flags, (int) size);
#endif
	retval = ext2fs_create_icount(test_fs, flags, (int) size,
				      &test_icount);
	if (retval) {
		com_err(progname, retval, "while creating icount");
		return;
	}
}

void do_free_icount(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
		    void *infop EXT2FS_ATTR((unused)))
{
	if (argc != 1) {
		printf("Usage: free_icount\n");
		return;
	}
	if (check_icount(argv[0]))
		return;

	ext2fs_free_icount(test_icount);
	test_icount = 0;
}

void do_fetch(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
		    void *infop EXT2FS_ATTR((unused)))
{
	errcode_t	retval;
	ext2_ino_t	ino;
	__u16		count;

	if (argc < 2) {
		printf("usage: %s inode\n", argv[0]);
		return;
	}
	if (check_icount(argv[0]))
		return;
	if (parse_inode(argv[0], "inode", argv[1], &ino))
		return;
	retval = ext2fs_icount_fetch(test_icount, ino, &count);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_icount_fetch");
		return;
	}
	printf("Count is %u\n", count);
}

void do_increment(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
		    void *infop EXT2FS_ATTR((unused)))
{
	errcode_t	retval;
	ext2_ino_t	ino;
	__u16		count;

	if (argc < 2) {
		printf("usage: %s inode\n", argv[0]);
		return;
	}
	if (check_icount(argv[0]))
		return;
	if (parse_inode(argv[0], "inode", argv[1], &ino))
		return;
	retval = ext2fs_icount_increment(test_icount, ino, &count);
	if (retval) {
		com_err(argv[0], retval,
			"while calling ext2fs_icount_increment");
		return;
	}
	printf("Count is now %u\n", count);
}

void do_decrement(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
		    void *infop EXT2FS_ATTR((unused)))
{
	errcode_t	retval;
	ext2_ino_t	ino;
	__u16		count;

	if (argc < 2) {
		printf("usage: %s inode\n", argv[0]);
		return;
	}
	if (check_icount(argv[0]))
		return;
	if (parse_inode(argv[0], "inode", argv[1], &ino))
		return;
	retval = ext2fs_icount_decrement(test_icount, ino, &count);
	if (retval) {
		com_err(argv[0], retval,
			"while calling ext2fs_icount_decrement");
		return;
	}
	printf("Count is now %u\n", count);
}

void do_store(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
		    void *infop EXT2FS_ATTR((unused)))
{
	errcode_t	retval;
	ext2_ino_t	ino;
	ext2_ino_t	count;

	if (argc < 3) {
		printf("usage: %s inode count\n", argv[0]);
		return;
	}
	if (check_icount(argv[0]))
		return;
	if (parse_inode(argv[0], "inode", argv[1], &ino))
		return;
	if (parse_inode(argv[0], "count", argv[2], &count))
		return;
	if (count > 65535) {
		printf("Count too large.\n");
		return;
	}
	retval = ext2fs_icount_store(test_icount, ino, (__u16) count);
	if (retval) {
		com_err(argv[0], retval,
			"while calling ext2fs_icount_store");
		return;
	}
}

void do_dump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
		    void *infop EXT2FS_ATTR((unused)))
{
	errcode_t	retval;
	ext2_ino_t	i;
	__u16		count;

	if (argc != 1) {
		printf("Usage: dump\n");
		return;
	}
	if (check_icount(argv[0]))
		return;
	for (i=1; i <= test_fs->super->s_inodes_count; i++) {
		retval = ext2fs_icount_fetch(test_icount, i, &count);
		if (retval) {
			com_err(argv[0], retval,
				"while fetching icount for %lu", (unsigned long)i);
			return;
		}
		if (count)
			printf("%lu: %u\n", (unsigned long)i, count);
	}
}

void do_validate(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
		    void *infop EXT2FS_ATTR((unused)))
{
	errcode_t	retval;

	if (argc != 1) {
		printf("Usage: validate\n");
		return;
	}
	if (check_icount(argv[0]))
		return;
	retval = ext2fs_icount_validate(test_icount, stdout);
	if (retval) {
		com_err(argv[0], retval, "while validating icount structure");
		return;
	}
	printf("Icount structure successfully validated\n");
}

void do_get_size(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
		    void *infop EXT2FS_ATTR((unused)))
{
	ext2_ino_t	size;

	if (argc != 1) {
		printf("Usage: get_size\n");
		return;
	}
	if (check_icount(argv[0]))
		return;
	size = ext2fs_get_icount_size(test_icount);
	printf("Size of icount is: %lu\n", (unsigned long)size);
}

static int source_file(const char *cmd_file, int sci_idx)
{
	FILE		*f;
	char		buf[256];
	char		*cp;
	int		exit_status = 0;
	int		retval;
	int 		noecho;

	if (strcmp(cmd_file, "-") == 0)
		f = stdin;
	else {
		f = fopen(cmd_file, "r");
		if (!f) {
			perror(cmd_file);
			exit(1);
		}
	}
	fflush(stdout);
	fflush(stderr);
	setbuf(stdout, NULL);
	setbuf(stderr, NULL);
	while (!feof(f)) {
		if (fgets(buf, sizeof(buf), f) == NULL)
			break;
		if (buf[0] == '#')
			continue;
		noecho = 0;
		if (buf[0] == '-') {
			noecho = 1;
			buf[0] = ' ';
		}
		cp = strchr(buf, '\n');
		if (cp)
			*cp = 0;
		cp = strchr(buf, '\r');
		if (cp)
			*cp = 0;
		if (!noecho)
			printf("test_icount: %s\n", buf);
		retval = ss_execute_line(sci_idx, buf);
		if (retval) {
			ss_perror(sci_idx, retval, buf);
			exit_status++;
		}
	}
	if (f != stdin)
		fclose(f);
	return exit_status;
}

int main(int argc, char **argv)
{
	int		retval;
	int		sci_idx;
	int		c;
	char		*request = 0;
	int		exit_status = 0;
	char		*cmd_file = 0;
	struct ext2_super_block param;

	initialize_ext2_error_table();

	/*
	 * Create a sample filesystem structure
	 */
	memset(&param, 0, sizeof(struct ext2_super_block));
	ext2fs_blocks_count_set(&param, 80000);
	param.s_inodes_count = 20000;
	retval = ext2fs_initialize("/dev/null", 0, &param,
				   unix_io_manager, &test_fs);
	if (retval) {
		com_err("/dev/null", retval, "while setting up test fs");
		exit(1);
	}

	while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
		switch (c) {
		case 'R':
			request = optarg;
			break;
		case 'f':
			cmd_file = optarg;
			break;
		default:
			com_err(argv[0], 0, "Usage: test_icount "
				"[-R request] [-f cmd_file]");
			exit(1);
		}
	}
	sci_idx = ss_create_invocation("test_icount", "0.0", (char *) NULL,
				       &test_cmds, &retval);
	if (retval) {
		ss_perror(sci_idx, retval, "creating invocation");
		exit(1);
	}

	(void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
	if (retval) {
		ss_perror(sci_idx, retval, "adding standard requests");
		exit (1);
	}
	if (request) {
		retval = 0;
		retval = ss_execute_line(sci_idx, request);
		if (retval) {
			ss_perror(sci_idx, retval, request);
			exit_status++;
		}
	} else if (cmd_file) {
		exit_status = source_file(cmd_file, sci_idx);
	} else {
		ss_listen(sci_idx);
	}

	return(exit_status);
}