4 * Copyright (C) 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
22 #include <ext2fs/ext2_fs.h>
24 #include <et/com_err.h>
26 #include <ext2fs/ext2fs.h>
27 #include <ext2fs/irel.h>
28 #include <ext2fs/brel.h>
30 extern ss_request_table test_cmds;
32 #include "test_icount.h"
35 ext2_icount_t test_icount;
38 * Helper function which assures that the icount structure is valid
40 static int check_icount(char *request)
44 com_err(request, 0, "The icount structure must be allocated.");
49 * Helper function which parses an inode number.
51 static int parse_inode(const char *request, const char *desc,
52 const char *str, ext2_ino_t *ino)
56 *ino = strtoul(str, &tmp, 0);
58 com_err(request, 0, "Bad %s - %s", desc, str);
64 void do_create_icount(int argc, char **argv)
74 if (argc && !strcmp("-i", *argv)) {
75 flags |= EXT2_ICOUNT_OPT_INCREMENT;
79 if (parse_inode(progname, "icount size", argv[0], &size))
84 printf("Creating icount... flags=%d, size=%d\n", flags, (int) size);
86 retval = ext2fs_create_icount(test_fs, flags, (int) size,
89 com_err(progname, retval, "while creating icount");
94 void do_free_icount(int argc, char **argv)
96 if (check_icount(argv[0]))
99 ext2fs_free_icount(test_icount);
103 void do_fetch(int argc, char **argv)
105 const char *usage = "usage: %s inode\n";
111 printf(usage, argv[0]);
114 if (check_icount(argv[0]))
116 if (parse_inode(argv[0], "inode", argv[1], &ino))
118 retval = ext2fs_icount_fetch(test_icount, ino, &count);
120 com_err(argv[0], retval, "while calling ext2fs_icount_fetch");
123 printf("Count is %u\n", count);
126 void do_increment(int argc, char **argv)
128 const char *usage = "usage: %s inode\n";
134 printf(usage, argv[0]);
137 if (check_icount(argv[0]))
139 if (parse_inode(argv[0], "inode", argv[1], &ino))
141 retval = ext2fs_icount_increment(test_icount, ino, &count);
143 com_err(argv[0], retval,
144 "while calling ext2fs_icount_increment");
147 printf("Count is now %u\n", count);
150 void do_decrement(int argc, char **argv)
152 const char *usage = "usage: %s inode\n";
158 printf(usage, argv[0]);
161 if (check_icount(argv[0]))
163 if (parse_inode(argv[0], "inode", argv[1], &ino))
165 retval = ext2fs_icount_decrement(test_icount, ino, &count);
167 com_err(argv[0], retval,
168 "while calling ext2fs_icount_decrement");
171 printf("Count is now %u\n", count);
174 void do_store(int argc, char **argv)
176 const char *usage = "usage: %s inode count\n";
182 printf(usage, argv[0]);
185 if (check_icount(argv[0]))
187 if (parse_inode(argv[0], "inode", argv[1], &ino))
189 if (parse_inode(argv[0], "count", argv[2], &count))
192 printf("Count too large.\n");
195 retval = ext2fs_icount_store(test_icount, ino, (__u16) count);
197 com_err(argv[0], retval,
198 "while calling ext2fs_icount_store");
203 void do_dump(int argc, char **argv)
209 if (check_icount(argv[0]))
211 for (i=1; i <= test_fs->super->s_inodes_count; i++) {
212 retval = ext2fs_icount_fetch(test_icount, i, &count);
214 com_err(argv[0], retval,
215 "while fetching icount for %lu", (unsigned long)i);
219 printf("%lu: %u\n", (unsigned long)i, count);
223 void do_validate(int argc, char **argv)
227 if (check_icount(argv[0]))
229 retval = ext2fs_icount_validate(test_icount, stdout);
231 com_err(argv[0], retval, "while validating icount structure");
234 printf("Icount structure successfully validated\n");
237 void do_get_size(int argc, char **argv)
241 if (check_icount(argv[0]))
243 size = ext2fs_get_icount_size(test_icount);
244 printf("Size of icount is: %lu\n", (unsigned long)size);
247 static int source_file(const char *cmd_file, int sci_idx)
256 if (strcmp(cmd_file, "-") == 0)
259 f = fopen(cmd_file, "r");
267 setbuf(stdout, NULL);
268 setbuf(stderr, NULL);
270 if (fgets(buf, sizeof(buf), f) == NULL)
279 cp = strchr(buf, '\n');
282 cp = strchr(buf, '\r');
286 printf("test_icount: %s\n", buf);
287 retval = ss_execute_line(sci_idx, buf);
289 ss_perror(sci_idx, retval, buf);
298 int main(int argc, char **argv)
306 struct ext2_super_block param;
308 initialize_ext2_error_table();
311 * Create a sample filesystem structure
313 memset(¶m, 0, sizeof(struct ext2_super_block));
314 ext2fs_blocks_count_set(¶m, 80000);
315 param.s_inodes_count = 20000;
316 retval = ext2fs_initialize("/dev/null", 0, ¶m,
317 unix_io_manager, &test_fs);
319 com_err("/dev/null", retval, "while setting up test fs");
323 while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
332 com_err(argv[0], 0, "Usage: test_icount "
333 "[-R request] [-f cmd_file]");
337 sci_idx = ss_create_invocation("test_icount", "0.0", (char *) NULL,
338 &test_cmds, &retval);
340 ss_perror(sci_idx, retval, "creating invocation");
344 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
346 ss_perror(sci_idx, retval, "adding standard requests");
351 retval = ss_execute_line(sci_idx, request);
353 ss_perror(sci_idx, retval, request);
356 } else if (cmd_file) {
357 exit_status = source_file(cmd_file, sci_idx);