]>
Commit | Line | Data |
---|---|---|
521e3685 TT |
1 | /* |
2 | * test_icount.c | |
efc6f628 | 3 | * |
521e3685 TT |
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 | ||
d1154eb4 | 12 | #include "config.h" |
521e3685 TT |
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 | ||
54c637d4 | 22 | #include <ext2fs/ext2_fs.h> |
521e3685 TT |
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 | */ | |
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 | */ | |
51 | static int parse_inode(const char *request, const char *desc, | |
dfcdc32f | 52 | const char *str, ext2_ino_t *ino) |
521e3685 TT |
53 | { |
54 | char *tmp; | |
efc6f628 | 55 | |
521e3685 TT |
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 | ||
49125d40 | 64 | void do_create_icount(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)), |
2fcbcb1b | 65 | void *infop EXT2FS_ATTR((unused))) |
521e3685 | 66 | { |
dfcdc32f TT |
67 | errcode_t retval; |
68 | char *progname; | |
69 | int flags = 0; | |
70 | ext2_ino_t size = 5; | |
521e3685 TT |
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 | ||
49125d40 | 95 | void do_free_icount(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)), |
2fcbcb1b | 96 | void *infop EXT2FS_ATTR((unused))) |
521e3685 | 97 | { |
ac3256fd TT |
98 | if (argc != 1) { |
99 | printf("Usage: free_icount\n"); | |
100 | return; | |
101 | } | |
521e3685 TT |
102 | if (check_icount(argv[0])) |
103 | return; | |
104 | ||
105 | ext2fs_free_icount(test_icount); | |
106 | test_icount = 0; | |
107 | } | |
108 | ||
49125d40 | 109 | void do_fetch(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)), |
2fcbcb1b | 110 | void *infop EXT2FS_ATTR((unused))) |
521e3685 | 111 | { |
dfcdc32f TT |
112 | errcode_t retval; |
113 | ext2_ino_t ino; | |
114 | __u16 count; | |
efc6f628 | 115 | |
521e3685 | 116 | if (argc < 2) { |
b926292b | 117 | printf("usage: %s inode\n", argv[0]); |
521e3685 TT |
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 | ||
49125d40 | 132 | void do_increment(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)), |
2fcbcb1b | 133 | void *infop EXT2FS_ATTR((unused))) |
521e3685 | 134 | { |
dfcdc32f TT |
135 | errcode_t retval; |
136 | ext2_ino_t ino; | |
137 | __u16 count; | |
efc6f628 | 138 | |
521e3685 | 139 | if (argc < 2) { |
b926292b | 140 | printf("usage: %s inode\n", argv[0]); |
521e3685 TT |
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 | ||
49125d40 | 156 | void do_decrement(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)), |
2fcbcb1b | 157 | void *infop EXT2FS_ATTR((unused))) |
521e3685 | 158 | { |
dfcdc32f TT |
159 | errcode_t retval; |
160 | ext2_ino_t ino; | |
161 | __u16 count; | |
efc6f628 | 162 | |
521e3685 | 163 | if (argc < 2) { |
b926292b | 164 | printf("usage: %s inode\n", argv[0]); |
521e3685 TT |
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, | |
080fbf2b | 174 | "while calling ext2fs_icount_decrement"); |
521e3685 TT |
175 | return; |
176 | } | |
177 | printf("Count is now %u\n", count); | |
178 | } | |
179 | ||
49125d40 | 180 | void do_store(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)), |
2fcbcb1b | 181 | void *infop EXT2FS_ATTR((unused))) |
521e3685 | 182 | { |
dfcdc32f TT |
183 | errcode_t retval; |
184 | ext2_ino_t ino; | |
185 | ext2_ino_t count; | |
efc6f628 | 186 | |
521e3685 | 187 | if (argc < 3) { |
b926292b | 188 | printf("usage: %s inode count\n", argv[0]); |
521e3685 TT |
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 | ||
49125d40 | 209 | void do_dump(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)), |
2fcbcb1b | 210 | void *infop EXT2FS_ATTR((unused))) |
521e3685 TT |
211 | { |
212 | errcode_t retval; | |
dfcdc32f | 213 | ext2_ino_t i; |
521e3685 TT |
214 | __u16 count; |
215 | ||
ac3256fd TT |
216 | if (argc != 1) { |
217 | printf("Usage: dump\n"); | |
218 | return; | |
219 | } | |
521e3685 TT |
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, | |
b969b1b8 | 226 | "while fetching icount for %lu", (unsigned long)i); |
521e3685 TT |
227 | return; |
228 | } | |
229 | if (count) | |
b969b1b8 | 230 | printf("%lu: %u\n", (unsigned long)i, count); |
521e3685 TT |
231 | } |
232 | } | |
233 | ||
49125d40 | 234 | void do_validate(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)), |
2fcbcb1b | 235 | void *infop EXT2FS_ATTR((unused))) |
521e3685 TT |
236 | { |
237 | errcode_t retval; | |
4e27d764 | 238 | |
ac3256fd TT |
239 | if (argc != 1) { |
240 | printf("Usage: validate\n"); | |
241 | return; | |
242 | } | |
521e3685 TT |
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 | ||
49125d40 | 253 | void do_get_size(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)), |
2fcbcb1b | 254 | void *infop EXT2FS_ATTR((unused))) |
521e3685 | 255 | { |
dfcdc32f | 256 | ext2_ino_t size; |
b969b1b8 | 257 | |
ac3256fd TT |
258 | if (argc != 1) { |
259 | printf("Usage: get_size\n"); | |
260 | return; | |
261 | } | |
521e3685 TT |
262 | if (check_icount(argv[0])) |
263 | return; | |
264 | size = ext2fs_get_icount_size(test_icount); | |
b969b1b8 | 265 | printf("Size of icount is: %lu\n", (unsigned long)size); |
521e3685 TT |
266 | } |
267 | ||
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 | } | |
2a7bfe83 TT |
286 | fflush(stdout); |
287 | fflush(stderr); | |
521e3685 TT |
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 | } | |
2bf0739d ES |
314 | if (f != stdin) |
315 | fclose(f); | |
521e3685 TT |
316 | return exit_status; |
317 | } | |
318 | ||
818180cd | 319 | int main(int argc, char **argv) |
521e3685 TT |
320 | { |
321 | int retval; | |
322 | int sci_idx; | |
018100f5 | 323 | int c; |
521e3685 TT |
324 | char *request = 0; |
325 | int exit_status = 0; | |
326 | char *cmd_file = 0; | |
327 | struct ext2_super_block param; | |
efc6f628 | 328 | |
521e3685 TT |
329 | initialize_ext2_error_table(); |
330 | ||
331 | /* | |
332 | * Create a sample filesystem structure | |
333 | */ | |
334 | memset(¶m, 0, sizeof(struct ext2_super_block)); | |
4efbac6f | 335 | ext2fs_blocks_count_set(¶m, 80000); |
521e3685 TT |
336 | param.s_inodes_count = 20000; |
337 | retval = ext2fs_initialize("/dev/null", 0, ¶m, | |
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: | |
5299580c TT |
353 | com_err(argv[0], 0, "Usage: test_icount " |
354 | "[-R request] [-f cmd_file]"); | |
818180cd | 355 | exit(1); |
521e3685 TT |
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 | ||
818180cd | 383 | return(exit_status); |
521e3685 | 384 | } |