4 * Copyright (C) 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
21 #include <ext2fs/ext2_fs.h>
23 #include <et/com_err.h>
25 #include <ext2fs/ext2fs.h>
26 #include <ext2fs/irel.h>
27 #include <ext2fs/brel.h>
29 extern ss_request_table test_cmds;
31 #include "test_icount.h"
34 ext2_icount_t test_icount;
37 * Helper function which assures that the icount structure is valid
39 static int check_icount(char *request)
43 com_err(request, 0, "The icount structure must be allocated.");
48 * Helper function which parses an inode number.
50 static int parse_inode(const char *request, const char *desc,
51 const char *str, ext2_ino_t *ino)
55 *ino = strtoul(str, &tmp, 0);
57 com_err(request, 0, "Bad %s - %s", desc, str);
63 void do_create_icount(int argc, char **argv)
73 if (argc && !strcmp("-i", *argv)) {
74 flags |= EXT2_ICOUNT_OPT_INCREMENT;
78 if (parse_inode(progname, "icount size", argv[0], &size))
83 printf("Creating icount... flags=%d, size=%d\n", flags, (int) size);
85 retval = ext2fs_create_icount(test_fs, flags, (int) size,
88 com_err(progname, retval, "while creating icount");
93 void do_free_icount(int argc, char **argv)
95 if (check_icount(argv[0]))
98 ext2fs_free_icount(test_icount);
102 void do_fetch(int argc, char **argv)
104 const char *usage = "usage: %s inode\n";
110 printf(usage, argv[0]);
113 if (check_icount(argv[0]))
115 if (parse_inode(argv[0], "inode", argv[1], &ino))
117 retval = ext2fs_icount_fetch(test_icount, ino, &count);
119 com_err(argv[0], retval, "while calling ext2fs_icount_fetch");
122 printf("Count is %u\n", count);
125 void do_increment(int argc, char **argv)
127 const char *usage = "usage: %s inode\n";
133 printf(usage, argv[0]);
136 if (check_icount(argv[0]))
138 if (parse_inode(argv[0], "inode", argv[1], &ino))
140 retval = ext2fs_icount_increment(test_icount, ino, &count);
142 com_err(argv[0], retval,
143 "while calling ext2fs_icount_increment");
146 printf("Count is now %u\n", count);
149 void do_decrement(int argc, char **argv)
151 const char *usage = "usage: %s inode\n";
157 printf(usage, argv[0]);
160 if (check_icount(argv[0]))
162 if (parse_inode(argv[0], "inode", argv[1], &ino))
164 retval = ext2fs_icount_decrement(test_icount, ino, &count);
166 com_err(argv[0], retval,
167 "while calling ext2fs_icount_decrement");
170 printf("Count is now %u\n", count);
173 void do_store(int argc, char **argv)
175 const char *usage = "usage: %s inode count\n";
181 printf(usage, argv[0]);
184 if (check_icount(argv[0]))
186 if (parse_inode(argv[0], "inode", argv[1], &ino))
188 if (parse_inode(argv[0], "count", argv[2], &count))
191 printf("Count too large.\n");
194 retval = ext2fs_icount_store(test_icount, ino, (__u16) count);
196 com_err(argv[0], retval,
197 "while calling ext2fs_icount_store");
202 void do_dump(int argc, char **argv)
208 if (check_icount(argv[0]))
210 for (i=1; i <= test_fs->super->s_inodes_count; i++) {
211 retval = ext2fs_icount_fetch(test_icount, i, &count);
213 com_err(argv[0], retval,
214 "while fetching icount for %lu", (unsigned long)i);
218 printf("%lu: %u\n", (unsigned long)i, count);
222 void do_validate(int argc, char **argv)
226 if (check_icount(argv[0]))
228 retval = ext2fs_icount_validate(test_icount, stdout);
230 com_err(argv[0], retval, "while validating icount structure");
233 printf("Icount structure successfully validated\n");
236 void do_get_size(int argc, char **argv)
240 if (check_icount(argv[0]))
242 size = ext2fs_get_icount_size(test_icount);
243 printf("Size of icount is: %lu\n", (unsigned long)size);
246 static int source_file(const char *cmd_file, int sci_idx)
255 if (strcmp(cmd_file, "-") == 0)
258 f = fopen(cmd_file, "r");
264 setbuf(stdout, NULL);
265 setbuf(stderr, NULL);
267 if (fgets(buf, sizeof(buf), f) == NULL)
276 cp = strchr(buf, '\n');
279 cp = strchr(buf, '\r');
283 printf("test_icount: %s\n", buf);
284 retval = ss_execute_line(sci_idx, buf);
286 ss_perror(sci_idx, retval, buf);
293 int main(int argc, char **argv)
297 const char *usage = "Usage: test_icount [-R request] [-f cmd_file]";
302 struct ext2_super_block param;
304 initialize_ext2_error_table();
307 * Create a sample filesystem structure
309 memset(¶m, 0, sizeof(struct ext2_super_block));
310 param.s_blocks_count = 80000;
311 param.s_inodes_count = 20000;
312 retval = ext2fs_initialize("/dev/null", 0, ¶m,
313 unix_io_manager, &test_fs);
315 com_err("/dev/null", retval, "while setting up test fs");
319 while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
328 com_err(argv[0], 0, usage);
332 sci_idx = ss_create_invocation("test_icount", "0.0", (char *) NULL,
333 &test_cmds, &retval);
335 ss_perror(sci_idx, retval, "creating invocation");
339 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
341 ss_perror(sci_idx, retval, "adding standard requests");
346 retval = ss_execute_line(sci_idx, request);
348 ss_perror(sci_idx, retval, request);
351 } else if (cmd_file) {
352 exit_status = source_file(cmd_file, sci_idx);