• 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 	errcode_t	retval;
113 	ext2_ino_t	ino;
114 	__u16		count;
115 
116 	if (argc < 2) {
117 		printf("usage: %s inode\n", argv[0]);
118 		return;
119 	}
120 	if (check_icount(argv[0]))
121 		return;
122 	if (parse_inode(argv[0], "inode", argv[1], &ino))
123 		return;
124 	retval = ext2fs_icount_fetch(test_icount, ino, &count);
125 	if (retval) {
126 		com_err(argv[0], retval, "while calling ext2fs_icount_fetch");
127 		return;
128 	}
129 	printf("Count is %u\n", count);
130 }
131 
do_increment(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))132 void do_increment(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
133 		    void *infop EXT2FS_ATTR((unused)))
134 {
135 	errcode_t	retval;
136 	ext2_ino_t	ino;
137 	__u16		count;
138 
139 	if (argc < 2) {
140 		printf("usage: %s inode\n", argv[0]);
141 		return;
142 	}
143 	if (check_icount(argv[0]))
144 		return;
145 	if (parse_inode(argv[0], "inode", argv[1], &ino))
146 		return;
147 	retval = ext2fs_icount_increment(test_icount, ino, &count);
148 	if (retval) {
149 		com_err(argv[0], retval,
150 			"while calling ext2fs_icount_increment");
151 		return;
152 	}
153 	printf("Count is now %u\n", count);
154 }
155 
do_decrement(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))156 void do_decrement(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
157 		    void *infop EXT2FS_ATTR((unused)))
158 {
159 	errcode_t	retval;
160 	ext2_ino_t	ino;
161 	__u16		count;
162 
163 	if (argc < 2) {
164 		printf("usage: %s inode\n", argv[0]);
165 		return;
166 	}
167 	if (check_icount(argv[0]))
168 		return;
169 	if (parse_inode(argv[0], "inode", argv[1], &ino))
170 		return;
171 	retval = ext2fs_icount_decrement(test_icount, ino, &count);
172 	if (retval) {
173 		com_err(argv[0], retval,
174 			"while calling ext2fs_icount_decrement");
175 		return;
176 	}
177 	printf("Count is now %u\n", count);
178 }
179 
do_store(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))180 void do_store(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
181 		    void *infop EXT2FS_ATTR((unused)))
182 {
183 	errcode_t	retval;
184 	ext2_ino_t	ino;
185 	ext2_ino_t	count;
186 
187 	if (argc < 3) {
188 		printf("usage: %s inode count\n", argv[0]);
189 		return;
190 	}
191 	if (check_icount(argv[0]))
192 		return;
193 	if (parse_inode(argv[0], "inode", argv[1], &ino))
194 		return;
195 	if (parse_inode(argv[0], "count", argv[2], &count))
196 		return;
197 	if (count > 65535) {
198 		printf("Count too large.\n");
199 		return;
200 	}
201 	retval = ext2fs_icount_store(test_icount, ino, (__u16) count);
202 	if (retval) {
203 		com_err(argv[0], retval,
204 			"while calling ext2fs_icount_store");
205 		return;
206 	}
207 }
208 
do_dump(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))209 void do_dump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
210 		    void *infop EXT2FS_ATTR((unused)))
211 {
212 	errcode_t	retval;
213 	ext2_ino_t	i;
214 	__u16		count;
215 
216 	if (argc != 1) {
217 		printf("Usage: dump\n");
218 		return;
219 	}
220 	if (check_icount(argv[0]))
221 		return;
222 	for (i=1; i <= test_fs->super->s_inodes_count; i++) {
223 		retval = ext2fs_icount_fetch(test_icount, i, &count);
224 		if (retval) {
225 			com_err(argv[0], retval,
226 				"while fetching icount for %lu", (unsigned long)i);
227 			return;
228 		}
229 		if (count)
230 			printf("%lu: %u\n", (unsigned long)i, count);
231 	}
232 }
233 
do_validate(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))234 void do_validate(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
235 		    void *infop EXT2FS_ATTR((unused)))
236 {
237 	errcode_t	retval;
238 
239 	if (argc != 1) {
240 		printf("Usage: validate\n");
241 		return;
242 	}
243 	if (check_icount(argv[0]))
244 		return;
245 	retval = ext2fs_icount_validate(test_icount, stdout);
246 	if (retval) {
247 		com_err(argv[0], retval, "while validating icount structure");
248 		return;
249 	}
250 	printf("Icount structure successfully validated\n");
251 }
252 
do_get_size(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))253 void do_get_size(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
254 		    void *infop EXT2FS_ATTR((unused)))
255 {
256 	ext2_ino_t	size;
257 
258 	if (argc != 1) {
259 		printf("Usage: get_size\n");
260 		return;
261 	}
262 	if (check_icount(argv[0]))
263 		return;
264 	size = ext2fs_get_icount_size(test_icount);
265 	printf("Size of icount is: %lu\n", (unsigned long)size);
266 }
267 
source_file(const char * cmd_file,int sci_idx)268 static int source_file(const char *cmd_file, int sci_idx)
269 {
270 	FILE		*f;
271 	char		buf[256];
272 	char		*cp;
273 	int		exit_status = 0;
274 	int		retval;
275 	int 		noecho;
276 
277 	if (strcmp(cmd_file, "-") == 0)
278 		f = stdin;
279 	else {
280 		f = fopen(cmd_file, "r");
281 		if (!f) {
282 			perror(cmd_file);
283 			exit(1);
284 		}
285 	}
286 	fflush(stdout);
287 	fflush(stderr);
288 	setbuf(stdout, NULL);
289 	setbuf(stderr, NULL);
290 	while (!feof(f)) {
291 		if (fgets(buf, sizeof(buf), f) == NULL)
292 			break;
293 		if (buf[0] == '#')
294 			continue;
295 		noecho = 0;
296 		if (buf[0] == '-') {
297 			noecho = 1;
298 			buf[0] = ' ';
299 		}
300 		cp = strchr(buf, '\n');
301 		if (cp)
302 			*cp = 0;
303 		cp = strchr(buf, '\r');
304 		if (cp)
305 			*cp = 0;
306 		if (!noecho)
307 			printf("test_icount: %s\n", buf);
308 		retval = ss_execute_line(sci_idx, buf);
309 		if (retval) {
310 			ss_perror(sci_idx, retval, buf);
311 			exit_status++;
312 		}
313 	}
314 	if (f != stdin)
315 		fclose(f);
316 	return exit_status;
317 }
318 
main(int argc,char ** argv)319 int main(int argc, char **argv)
320 {
321 	int		retval;
322 	int		sci_idx;
323 	int		c;
324 	char		*request = 0;
325 	int		exit_status = 0;
326 	char		*cmd_file = 0;
327 	struct ext2_super_block param;
328 
329 	initialize_ext2_error_table();
330 
331 	/*
332 	 * Create a sample filesystem structure
333 	 */
334 	memset(&param, 0, sizeof(struct ext2_super_block));
335 	ext2fs_blocks_count_set(&param, 80000);
336 	param.s_inodes_count = 20000;
337 	retval = ext2fs_initialize("/dev/null", 0, &param,
338 				   unix_io_manager, &test_fs);
339 	if (retval) {
340 		com_err("/dev/null", retval, "while setting up test fs");
341 		exit(1);
342 	}
343 
344 	while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
345 		switch (c) {
346 		case 'R':
347 			request = optarg;
348 			break;
349 		case 'f':
350 			cmd_file = optarg;
351 			break;
352 		default:
353 			com_err(argv[0], 0, "Usage: test_icount "
354 				"[-R request] [-f cmd_file]");
355 			exit(1);
356 		}
357 	}
358 	sci_idx = ss_create_invocation("test_icount", "0.0", (char *) NULL,
359 				       &test_cmds, &retval);
360 	if (retval) {
361 		ss_perror(sci_idx, retval, "creating invocation");
362 		exit(1);
363 	}
364 
365 	(void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
366 	if (retval) {
367 		ss_perror(sci_idx, retval, "adding standard requests");
368 		exit (1);
369 	}
370 	if (request) {
371 		retval = 0;
372 		retval = ss_execute_line(sci_idx, request);
373 		if (retval) {
374 			ss_perror(sci_idx, retval, request);
375 			exit_status++;
376 		}
377 	} else if (cmd_file) {
378 		exit_status = source_file(cmd_file, sci_idx);
379 	} else {
380 		ss_listen(sci_idx);
381 	}
382 
383 	return(exit_status);
384 }
385