• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * test_icount.c
3  *
4  * Copyright (C) 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11 
12 #include "config.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #ifdef HAVE_GETOPT_H
18 #include <getopt.h>
19 #endif
20 #include <fcntl.h>
21 
22 #include <ext2fs/ext2_fs.h>
23 
24 #include <et/com_err.h>
25 #include <ss/ss.h>
26 #include <ext2fs/ext2fs.h>
27 #include <ext2fs/irel.h>
28 #include <ext2fs/brel.h>
29 
30 extern ss_request_table test_cmds;
31 
32 #include "test_icount.h"
33 
34 ext2_filsys test_fs;
35 ext2_icount_t test_icount;
36 
37 /*
38  * Helper function which assures that the icount structure is valid
39  */
check_icount(char * request)40 static int check_icount(char *request)
41 {
42 	if (test_icount)
43 		return 0;
44 	com_err(request, 0, "The icount structure must be allocated.");
45 	return 1;
46 }
47 
48 /*
49  * Helper function which parses an inode number.
50  */
parse_inode(const char * request,const char * desc,const char * str,ext2_ino_t * ino)51 static int parse_inode(const char *request, const char *desc,
52 		       const char *str, ext2_ino_t *ino)
53 {
54 	char *tmp;
55 
56 	*ino = strtoul(str, &tmp, 0);
57 	if (*tmp) {
58 		com_err(request, 0, "Bad %s - %s", desc, str);
59 		return 1;
60 	}
61 	return 0;
62 }
63 
do_create_icount(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))64 void do_create_icount(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
65 		      void *infop EXT2FS_ATTR((unused)))
66 {
67 	errcode_t	retval;
68 	char		*progname;
69 	int		flags = 0;
70 	ext2_ino_t	size = 5;
71 
72 	progname = *argv;
73 	argv++; argc --;
74 
75 	if (argc && !strcmp("-i", *argv)) {
76 		flags |= EXT2_ICOUNT_OPT_INCREMENT;
77 		argv++; argc--;
78 	}
79 	if (argc) {
80 		if (parse_inode(progname, "icount size", argv[0], &size))
81 			return;
82 		argv++; argc--;
83 	}
84 #if 0
85 	printf("Creating icount... flags=%d, size=%d\n", flags, (int) size);
86 #endif
87 	retval = ext2fs_create_icount(test_fs, flags, (int) size,
88 				      &test_icount);
89 	if (retval) {
90 		com_err(progname, retval, "while creating icount");
91 		return;
92 	}
93 }
94 
do_free_icount(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))95 void do_free_icount(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
96 		    void *infop EXT2FS_ATTR((unused)))
97 {
98 	if (argc != 1) {
99 		printf("Usage: free_icount\n");
100 		return;
101 	}
102 	if (check_icount(argv[0]))
103 		return;
104 
105 	ext2fs_free_icount(test_icount);
106 	test_icount = 0;
107 }
108 
do_fetch(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))109 void do_fetch(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
110 		    void *infop EXT2FS_ATTR((unused)))
111 {
112 	const char	*usage = "usage: %s inode\n";
113 	errcode_t	retval;
114 	ext2_ino_t	ino;
115 	__u16		count;
116 
117 	if (argc < 2) {
118 		printf(usage, argv[0]);
119 		return;
120 	}
121 	if (check_icount(argv[0]))
122 		return;
123 	if (parse_inode(argv[0], "inode", argv[1], &ino))
124 		return;
125 	retval = ext2fs_icount_fetch(test_icount, ino, &count);
126 	if (retval) {
127 		com_err(argv[0], retval, "while calling ext2fs_icount_fetch");
128 		return;
129 	}
130 	printf("Count is %u\n", count);
131 }
132 
do_increment(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))133 void do_increment(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
134 		    void *infop EXT2FS_ATTR((unused)))
135 {
136 	const char	*usage = "usage: %s inode\n";
137 	errcode_t	retval;
138 	ext2_ino_t	ino;
139 	__u16		count;
140 
141 	if (argc < 2) {
142 		printf(usage, argv[0]);
143 		return;
144 	}
145 	if (check_icount(argv[0]))
146 		return;
147 	if (parse_inode(argv[0], "inode", argv[1], &ino))
148 		return;
149 	retval = ext2fs_icount_increment(test_icount, ino, &count);
150 	if (retval) {
151 		com_err(argv[0], retval,
152 			"while calling ext2fs_icount_increment");
153 		return;
154 	}
155 	printf("Count is now %u\n", count);
156 }
157 
do_decrement(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))158 void do_decrement(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
159 		    void *infop EXT2FS_ATTR((unused)))
160 {
161 	const char	*usage = "usage: %s inode\n";
162 	errcode_t	retval;
163 	ext2_ino_t	ino;
164 	__u16		count;
165 
166 	if (argc < 2) {
167 		printf(usage, argv[0]);
168 		return;
169 	}
170 	if (check_icount(argv[0]))
171 		return;
172 	if (parse_inode(argv[0], "inode", argv[1], &ino))
173 		return;
174 	retval = ext2fs_icount_decrement(test_icount, ino, &count);
175 	if (retval) {
176 		com_err(argv[0], retval,
177 			"while calling ext2fs_icount_decrement");
178 		return;
179 	}
180 	printf("Count is now %u\n", count);
181 }
182 
do_store(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))183 void do_store(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
184 		    void *infop EXT2FS_ATTR((unused)))
185 {
186 	const char	*usage = "usage: %s inode count\n";
187 	errcode_t	retval;
188 	ext2_ino_t	ino;
189 	ext2_ino_t	count;
190 
191 	if (argc < 3) {
192 		printf(usage, argv[0]);
193 		return;
194 	}
195 	if (check_icount(argv[0]))
196 		return;
197 	if (parse_inode(argv[0], "inode", argv[1], &ino))
198 		return;
199 	if (parse_inode(argv[0], "count", argv[2], &count))
200 		return;
201 	if (count > 65535) {
202 		printf("Count too large.\n");
203 		return;
204 	}
205 	retval = ext2fs_icount_store(test_icount, ino, (__u16) count);
206 	if (retval) {
207 		com_err(argv[0], retval,
208 			"while calling ext2fs_icount_store");
209 		return;
210 	}
211 }
212 
do_dump(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))213 void do_dump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
214 		    void *infop EXT2FS_ATTR((unused)))
215 {
216 	errcode_t	retval;
217 	ext2_ino_t	i;
218 	__u16		count;
219 
220 	if (argc != 1) {
221 		printf("Usage: dump\n");
222 		return;
223 	}
224 	if (check_icount(argv[0]))
225 		return;
226 	for (i=1; i <= test_fs->super->s_inodes_count; i++) {
227 		retval = ext2fs_icount_fetch(test_icount, i, &count);
228 		if (retval) {
229 			com_err(argv[0], retval,
230 				"while fetching icount for %lu", (unsigned long)i);
231 			return;
232 		}
233 		if (count)
234 			printf("%lu: %u\n", (unsigned long)i, count);
235 	}
236 }
237 
do_validate(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))238 void do_validate(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
239 		    void *infop EXT2FS_ATTR((unused)))
240 {
241 	errcode_t	retval;
242 
243 	if (argc != 1) {
244 		printf("Usage: validate\n");
245 		return;
246 	}
247 	if (check_icount(argv[0]))
248 		return;
249 	retval = ext2fs_icount_validate(test_icount, stdout);
250 	if (retval) {
251 		com_err(argv[0], retval, "while validating icount structure");
252 		return;
253 	}
254 	printf("Icount structure successfully validated\n");
255 }
256 
do_get_size(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))257 void do_get_size(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
258 		    void *infop EXT2FS_ATTR((unused)))
259 {
260 	ext2_ino_t	size;
261 
262 	if (argc != 1) {
263 		printf("Usage: get_size\n");
264 		return;
265 	}
266 	if (check_icount(argv[0]))
267 		return;
268 	size = ext2fs_get_icount_size(test_icount);
269 	printf("Size of icount is: %lu\n", (unsigned long)size);
270 }
271 
source_file(const char * cmd_file,int sci_idx)272 static int source_file(const char *cmd_file, int sci_idx)
273 {
274 	FILE		*f;
275 	char		buf[256];
276 	char		*cp;
277 	int		exit_status = 0;
278 	int		retval;
279 	int 		noecho;
280 
281 	if (strcmp(cmd_file, "-") == 0)
282 		f = stdin;
283 	else {
284 		f = fopen(cmd_file, "r");
285 		if (!f) {
286 			perror(cmd_file);
287 			exit(1);
288 		}
289 	}
290 	fflush(stdout);
291 	fflush(stderr);
292 	setbuf(stdout, NULL);
293 	setbuf(stderr, NULL);
294 	while (!feof(f)) {
295 		if (fgets(buf, sizeof(buf), f) == NULL)
296 			break;
297 		if (buf[0] == '#')
298 			continue;
299 		noecho = 0;
300 		if (buf[0] == '-') {
301 			noecho = 1;
302 			buf[0] = ' ';
303 		}
304 		cp = strchr(buf, '\n');
305 		if (cp)
306 			*cp = 0;
307 		cp = strchr(buf, '\r');
308 		if (cp)
309 			*cp = 0;
310 		if (!noecho)
311 			printf("test_icount: %s\n", buf);
312 		retval = ss_execute_line(sci_idx, buf);
313 		if (retval) {
314 			ss_perror(sci_idx, retval, buf);
315 			exit_status++;
316 		}
317 	}
318 	if (f != stdin)
319 		fclose(f);
320 	return exit_status;
321 }
322 
main(int argc,char ** argv)323 int main(int argc, char **argv)
324 {
325 	int		retval;
326 	int		sci_idx;
327 	int		c;
328 	char		*request = 0;
329 	int		exit_status = 0;
330 	char		*cmd_file = 0;
331 	struct ext2_super_block param;
332 
333 	initialize_ext2_error_table();
334 
335 	/*
336 	 * Create a sample filesystem structure
337 	 */
338 	memset(&param, 0, sizeof(struct ext2_super_block));
339 	ext2fs_blocks_count_set(&param, 80000);
340 	param.s_inodes_count = 20000;
341 	retval = ext2fs_initialize("/dev/null", 0, &param,
342 				   unix_io_manager, &test_fs);
343 	if (retval) {
344 		com_err("/dev/null", retval, "while setting up test fs");
345 		exit(1);
346 	}
347 
348 	while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
349 		switch (c) {
350 		case 'R':
351 			request = optarg;
352 			break;
353 		case 'f':
354 			cmd_file = optarg;
355 			break;
356 		default:
357 			com_err(argv[0], 0, "Usage: test_icount "
358 				"[-R request] [-f cmd_file]");
359 			exit(1);
360 		}
361 	}
362 	sci_idx = ss_create_invocation("test_icount", "0.0", (char *) NULL,
363 				       &test_cmds, &retval);
364 	if (retval) {
365 		ss_perror(sci_idx, retval, "creating invocation");
366 		exit(1);
367 	}
368 
369 	(void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
370 	if (retval) {
371 		ss_perror(sci_idx, retval, "adding standard requests");
372 		exit (1);
373 	}
374 	if (request) {
375 		retval = 0;
376 		retval = ss_execute_line(sci_idx, request);
377 		if (retval) {
378 			ss_perror(sci_idx, retval, request);
379 			exit_status++;
380 		}
381 	} else if (cmd_file) {
382 		exit_status = source_file(cmd_file, sci_idx);
383 	} else {
384 		ss_listen(sci_idx);
385 	}
386 
387 	return(exit_status);
388 }
389