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, int sci_idx EXT2FS_ATTR((unused)),
65 void *infop EXT2FS_ATTR((unused)))
75 if (argc && !strcmp("-i", *argv)) {
76 flags |= EXT2_ICOUNT_OPT_INCREMENT;
80 if (parse_inode(progname, "icount size", argv[0], &size))
85 printf("Creating icount... flags=%d, size=%d\n", flags, (int) size);
87 retval = ext2fs_create_icount(test_fs, flags, (int) size,
90 com_err(progname, retval, "while creating icount");
95 void do_free_icount(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
96 void *infop EXT2FS_ATTR((unused)))
99 printf("Usage: free_icount\n");
102 if (check_icount(argv[0]))
105 ext2fs_free_icount(test_icount);
109 void do_fetch(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
110 void *infop EXT2FS_ATTR((unused)))
112 const char *usage = "usage: %s inode\n";
118 printf(usage, argv[0]);
121 if (check_icount(argv[0]))
123 if (parse_inode(argv[0], "inode", argv[1], &ino))
125 retval = ext2fs_icount_fetch(test_icount, ino, &count);
127 com_err(argv[0], retval, "while calling ext2fs_icount_fetch");
130 printf("Count is %u\n", count);
133 void do_increment(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
134 void *infop EXT2FS_ATTR((unused)))
136 const char *usage = "usage: %s inode\n";
142 printf(usage, argv[0]);
145 if (check_icount(argv[0]))
147 if (parse_inode(argv[0], "inode", argv[1], &ino))
149 retval = ext2fs_icount_increment(test_icount, ino, &count);
151 com_err(argv[0], retval,
152 "while calling ext2fs_icount_increment");
155 printf("Count is now %u\n", count);
158 void do_decrement(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
159 void *infop EXT2FS_ATTR((unused)))
161 const char *usage = "usage: %s inode\n";
167 printf(usage, argv[0]);
170 if (check_icount(argv[0]))
172 if (parse_inode(argv[0], "inode", argv[1], &ino))
174 retval = ext2fs_icount_decrement(test_icount, ino, &count);
176 com_err(argv[0], retval,
177 "while calling ext2fs_icount_decrement");
180 printf("Count is now %u\n", count);
183 void do_store(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
184 void *infop EXT2FS_ATTR((unused)))
186 const char *usage = "usage: %s inode count\n";
192 printf(usage, argv[0]);
195 if (check_icount(argv[0]))
197 if (parse_inode(argv[0], "inode", argv[1], &ino))
199 if (parse_inode(argv[0], "count", argv[2], &count))
202 printf("Count too large.\n");
205 retval = ext2fs_icount_store(test_icount, ino, (__u16) count);
207 com_err(argv[0], retval,
208 "while calling ext2fs_icount_store");
213 void do_dump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
214 void *infop EXT2FS_ATTR((unused)))
221 printf("Usage: dump\n");
224 if (check_icount(argv[0]))
226 for (i=1; i <= test_fs->super->s_inodes_count; i++) {
227 retval = ext2fs_icount_fetch(test_icount, i, &count);
229 com_err(argv[0], retval,
230 "while fetching icount for %lu", (unsigned long)i);
234 printf("%lu: %u\n", (unsigned long)i, count);
238 void do_validate(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
239 void *infop EXT2FS_ATTR((unused)))
244 printf("Usage: validate\n");
247 if (check_icount(argv[0]))
249 retval = ext2fs_icount_validate(test_icount, stdout);
251 com_err(argv[0], retval, "while validating icount structure");
254 printf("Icount structure successfully validated\n");
257 void do_get_size(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
258 void *infop EXT2FS_ATTR((unused)))
263 printf("Usage: get_size\n");
266 if (check_icount(argv[0]))
268 size = ext2fs_get_icount_size(test_icount);
269 printf("Size of icount is: %lu\n", (unsigned long)size);
272 static int source_file(const char *cmd_file, int sci_idx)
281 if (strcmp(cmd_file, "-") == 0)
284 f = fopen(cmd_file, "r");
292 setbuf(stdout, NULL);
293 setbuf(stderr, NULL);
295 if (fgets(buf, sizeof(buf), f) == NULL)
304 cp = strchr(buf, '\n');
307 cp = strchr(buf, '\r');
311 printf("test_icount: %s\n", buf);
312 retval = ss_execute_line(sci_idx, buf);
314 ss_perror(sci_idx, retval, buf);
323 int main(int argc, char **argv)
331 struct ext2_super_block param;
333 initialize_ext2_error_table();
336 * Create a sample filesystem structure
338 memset(¶m, 0, sizeof(struct ext2_super_block));
339 ext2fs_blocks_count_set(¶m, 80000);
340 param.s_inodes_count = 20000;
341 retval = ext2fs_initialize("/dev/null", 0, ¶m,
342 unix_io_manager, &test_fs);
344 com_err("/dev/null", retval, "while setting up test fs");
348 while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
357 com_err(argv[0], 0, "Usage: test_icount "
358 "[-R request] [-f cmd_file]");
362 sci_idx = ss_create_invocation("test_icount", "0.0", (char *) NULL,
363 &test_cmds, &retval);
365 ss_perror(sci_idx, retval, "creating invocation");
369 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
371 ss_perror(sci_idx, retval, "adding standard requests");
376 retval = ss_execute_line(sci_idx, request);
378 ss_perror(sci_idx, retval, request);
381 } else if (cmd_file) {
382 exit_status = source_file(cmd_file, sci_idx);