]>
Commit | Line | Data |
---|---|---|
3839e657 TT |
1 | /* |
2 | * debugfs.c --- a program which allows you to attach an ext2fs | |
3 | * filesystem and play with it. | |
4 | * | |
5 | * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed | |
6 | * under the terms of the GNU Public License. | |
efc6f628 | 7 | * |
3839e657 TT |
8 | * Modifications by Robert Sanders <gt8134b@prism.gatech.edu> |
9 | */ | |
10 | ||
d1154eb4 | 11 | #include "config.h" |
3839e657 TT |
12 | #include <stdio.h> |
13 | #include <unistd.h> | |
14 | #include <stdlib.h> | |
15 | #include <ctype.h> | |
16 | #include <string.h> | |
17 | #include <time.h> | |
50e1e10f | 18 | #ifdef HAVE_GETOPT_H |
3839e657 | 19 | #include <getopt.h> |
efc6f628 | 20 | #else |
50e1e10f TT |
21 | extern int optind; |
22 | extern char *optarg; | |
23 | #endif | |
50e1e10f TT |
24 | #ifdef HAVE_ERRNO_H |
25 | #include <errno.h> | |
26 | #endif | |
27 | #include <fcntl.h> | |
3839e657 TT |
28 | #include <sys/types.h> |
29 | #include <sys/stat.h> | |
30 | ||
3839e657 | 31 | #include "debugfs.h" |
fc6d9d51 | 32 | #include "uuid/uuid.h" |
f68aa414 | 33 | #include "e2p/e2p.h" |
3839e657 | 34 | |
ea822eeb TT |
35 | #include <ext2fs/ext2_ext_attr.h> |
36 | ||
818180cd | 37 | #include "../version.h" |
03efde8a | 38 | #include "jfs_user.h" |
818180cd | 39 | |
355ffb2f RY |
40 | #ifndef BUFSIZ |
41 | #define BUFSIZ 8192 | |
42 | #endif | |
43 | ||
c756ec42 RY |
44 | /* 64KiB is the minimium blksize to best minimize system call overhead. */ |
45 | #ifndef IO_BUFSIZE | |
46 | #define IO_BUFSIZE 64*1024 | |
47 | #endif | |
48 | ||
49 | /* Block size for `st_blocks' */ | |
50 | #ifndef S_BLKSIZE | |
51 | #define S_BLKSIZE 512 | |
52 | #endif | |
53 | ||
49ce6cb5 | 54 | ss_request_table *extra_cmds; |
2d328bb7 | 55 | const char *debug_prog_name; |
bee7b67c | 56 | int sci_idx; |
3839e657 | 57 | |
b044c2e0 TT |
58 | ext2_filsys current_fs = NULL; |
59 | ext2_ino_t root, cwd; | |
3839e657 | 60 | |
048786d7 VAH |
61 | static void open_filesystem(char *device, int open_flags, blk64_t superblock, |
62 | blk64_t blocksize, int catastrophic, | |
1ad54a94 | 63 | char *data_filename) |
3839e657 TT |
64 | { |
65 | int retval; | |
1ad54a94 | 66 | io_channel data_io = 0; |
2e8d40d5 TT |
67 | |
68 | if (superblock != 0 && blocksize == 0) { | |
69 | com_err(device, 0, "if you specify the superblock, you must also specify the block size"); | |
70 | current_fs = NULL; | |
71 | return; | |
72 | } | |
73 | ||
1ad54a94 TT |
74 | if (data_filename) { |
75 | if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) { | |
efc6f628 | 76 | com_err(device, 0, |
1ad54a94 TT |
77 | "The -d option is only valid when reading an e2image file"); |
78 | current_fs = NULL; | |
79 | return; | |
80 | } | |
81 | retval = unix_io_manager->open(data_filename, 0, &data_io); | |
82 | if (retval) { | |
83 | com_err(data_filename, 0, "while opening data source"); | |
84 | current_fs = NULL; | |
85 | return; | |
86 | } | |
87 | } | |
88 | ||
2e8d40d5 TT |
89 | if (catastrophic && (open_flags & EXT2_FLAG_RW)) { |
90 | com_err(device, 0, | |
91 | "opening read-only because of catastrophic mode"); | |
92 | open_flags &= ~EXT2_FLAG_RW; | |
93 | } | |
0f5eba75 AD |
94 | if (catastrophic) |
95 | open_flags |= EXT2_FLAG_SKIP_MMP; | |
efc6f628 | 96 | |
2e8d40d5 | 97 | retval = ext2fs_open(device, open_flags, superblock, blocksize, |
fc6d9d51 | 98 | unix_io_manager, ¤t_fs); |
3839e657 TT |
99 | if (retval) { |
100 | com_err(device, retval, "while opening filesystem"); | |
fc6d9d51 | 101 | current_fs = NULL; |
3839e657 TT |
102 | return; |
103 | } | |
24dea554 | 104 | current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; |
2e8d40d5 TT |
105 | |
106 | if (catastrophic) | |
107 | com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps"); | |
108 | else { | |
109 | retval = ext2fs_read_inode_bitmap(current_fs); | |
110 | if (retval) { | |
111 | com_err(device, retval, "while reading inode bitmap"); | |
112 | goto errout; | |
113 | } | |
114 | retval = ext2fs_read_block_bitmap(current_fs); | |
115 | if (retval) { | |
116 | com_err(device, retval, "while reading block bitmap"); | |
117 | goto errout; | |
118 | } | |
3839e657 | 119 | } |
1ad54a94 TT |
120 | |
121 | if (data_io) { | |
122 | retval = ext2fs_set_data_io(current_fs, data_io); | |
123 | if (retval) { | |
efc6f628 | 124 | com_err(device, retval, |
1ad54a94 TT |
125 | "while setting data source"); |
126 | goto errout; | |
127 | } | |
128 | } | |
129 | ||
3839e657 TT |
130 | root = cwd = EXT2_ROOT_INO; |
131 | return; | |
132 | ||
133 | errout: | |
fc6d9d51 | 134 | retval = ext2fs_close(current_fs); |
3839e657 TT |
135 | if (retval) |
136 | com_err(device, retval, "while trying to close filesystem"); | |
fc6d9d51 | 137 | current_fs = NULL; |
3839e657 TT |
138 | } |
139 | ||
140 | void do_open_filesys(int argc, char **argv) | |
141 | { | |
e1018eea | 142 | int c, err; |
2e8d40d5 | 143 | int catastrophic = 0; |
048786d7 VAH |
144 | blk64_t superblock = 0; |
145 | blk64_t blocksize = 0; | |
03b9dca6 | 146 | int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; |
b0d17e0d | 147 | char *data_filename = 0; |
efc6f628 | 148 | |
88494bb6 | 149 | reset_getopt(); |
0fd68e02 | 150 | while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) { |
3839e657 | 151 | switch (c) { |
59cf7e0d TT |
152 | case 'i': |
153 | open_flags |= EXT2_FLAG_IMAGE_FILE; | |
154 | break; | |
3839e657 | 155 | case 'w': |
e88c5a33 TT |
156 | #ifdef READ_ONLY |
157 | goto print_usage; | |
158 | #else | |
d3aea7dc | 159 | open_flags |= EXT2_FLAG_RW; |
e88c5a33 | 160 | #endif /* READ_ONLY */ |
d3aea7dc TT |
161 | break; |
162 | case 'f': | |
163 | open_flags |= EXT2_FLAG_FORCE; | |
3839e657 | 164 | break; |
98eb44bd TT |
165 | case 'e': |
166 | open_flags |= EXT2_FLAG_EXCLUSIVE; | |
167 | break; | |
2e8d40d5 TT |
168 | case 'c': |
169 | catastrophic = 1; | |
170 | break; | |
1ad54a94 TT |
171 | case 'd': |
172 | data_filename = optarg; | |
173 | break; | |
0fd68e02 TT |
174 | case 'D': |
175 | open_flags |= EXT2_FLAG_DIRECT_IO; | |
176 | break; | |
2e8d40d5 | 177 | case 'b': |
e1018eea TT |
178 | blocksize = parse_ulong(optarg, argv[0], |
179 | "block size", &err); | |
180 | if (err) | |
2e8d40d5 | 181 | return; |
2e8d40d5 TT |
182 | break; |
183 | case 's': | |
fe56188b | 184 | err = strtoblk(argv[0], optarg, &superblock); |
e1018eea | 185 | if (err) |
2e8d40d5 | 186 | return; |
2e8d40d5 | 187 | break; |
3839e657 | 188 | default: |
9b9a780f | 189 | goto print_usage; |
3839e657 TT |
190 | } |
191 | } | |
192 | if (optind != argc-1) { | |
9b9a780f | 193 | goto print_usage; |
3839e657 TT |
194 | } |
195 | if (check_fs_not_open(argv[0])) | |
196 | return; | |
2e8d40d5 | 197 | open_filesystem(argv[optind], open_flags, |
efc6f628 | 198 | superblock, blocksize, catastrophic, |
1ad54a94 | 199 | data_filename); |
9b9a780f TT |
200 | return; |
201 | ||
202 | print_usage: | |
5093775a ZL |
203 | fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] " |
204 | "[-d image_filename] [-c] [-i] [-f] [-e] [-D] " | |
e88c5a33 TT |
205 | #ifndef READ_ONLY |
206 | "[-w] " | |
207 | #endif | |
208 | "<device>\n", argv[0]); | |
2e8d40d5 TT |
209 | } |
210 | ||
211 | void do_lcd(int argc, char **argv) | |
212 | { | |
57a1cbb6 TT |
213 | if (argc != 2) { |
214 | com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>"); | |
2e8d40d5 | 215 | return; |
57a1cbb6 | 216 | } |
2e8d40d5 TT |
217 | |
218 | if (chdir(argv[1]) == -1) { | |
219 | com_err(argv[0], errno, | |
220 | "while trying to change native directory to %s", | |
221 | argv[1]); | |
222 | return; | |
223 | } | |
3839e657 TT |
224 | } |
225 | ||
50e1e10f | 226 | static void close_filesystem(NOARGS) |
3839e657 TT |
227 | { |
228 | int retval; | |
efc6f628 | 229 | |
fc6d9d51 TT |
230 | if (current_fs->flags & EXT2_FLAG_IB_DIRTY) { |
231 | retval = ext2fs_write_inode_bitmap(current_fs); | |
3839e657 | 232 | if (retval) |
9b9a780f | 233 | com_err("ext2fs_write_inode_bitmap", retval, 0); |
3839e657 | 234 | } |
fc6d9d51 TT |
235 | if (current_fs->flags & EXT2_FLAG_BB_DIRTY) { |
236 | retval = ext2fs_write_block_bitmap(current_fs); | |
3839e657 | 237 | if (retval) |
9b9a780f | 238 | com_err("ext2fs_write_block_bitmap", retval, 0); |
3839e657 | 239 | } |
fc6d9d51 | 240 | retval = ext2fs_close(current_fs); |
3839e657 | 241 | if (retval) |
9b9a780f | 242 | com_err("ext2fs_close", retval, 0); |
fc6d9d51 | 243 | current_fs = NULL; |
3839e657 TT |
244 | return; |
245 | } | |
246 | ||
247 | void do_close_filesys(int argc, char **argv) | |
248 | { | |
6dce5328 TT |
249 | int c; |
250 | ||
251 | if (check_fs_open(argv[0])) | |
3839e657 | 252 | return; |
6dce5328 TT |
253 | |
254 | reset_getopt(); | |
255 | while ((c = getopt (argc, argv, "a")) != EOF) { | |
256 | switch (c) { | |
257 | case 'a': | |
258 | current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; | |
259 | break; | |
260 | default: | |
261 | goto print_usage; | |
262 | } | |
263 | } | |
264 | ||
265 | if (argc > optind) { | |
266 | print_usage: | |
267 | com_err(0, 0, "Usage: close_filesys [-a]"); | |
268 | return; | |
269 | } | |
270 | ||
3839e657 TT |
271 | close_filesystem(); |
272 | } | |
273 | ||
e88c5a33 | 274 | #ifndef READ_ONLY |
3839e657 TT |
275 | void do_init_filesys(int argc, char **argv) |
276 | { | |
3839e657 TT |
277 | struct ext2_super_block param; |
278 | errcode_t retval; | |
e1018eea | 279 | int err; |
fe56188b | 280 | blk64_t blocks; |
efc6f628 | 281 | |
e1018eea | 282 | if (common_args_process(argc, argv, 3, 3, "initialize", |
57d4fb66 | 283 | "<device> <blocks>", CHECK_FS_NOTOPEN)) |
3839e657 TT |
284 | return; |
285 | ||
286 | memset(¶m, 0, sizeof(struct ext2_super_block)); | |
fe56188b DW |
287 | err = strtoblk(argv[0], argv[2], &blocks); |
288 | if (err) | |
289 | return; | |
290 | ext2fs_blocks_count_set(¶m, blocks); | |
e1018eea | 291 | if (err) |
3839e657 | 292 | return; |
fc6d9d51 TT |
293 | retval = ext2fs_initialize(argv[1], 0, ¶m, |
294 | unix_io_manager, ¤t_fs); | |
3839e657 TT |
295 | if (retval) { |
296 | com_err(argv[1], retval, "while initializing filesystem"); | |
fc6d9d51 | 297 | current_fs = NULL; |
3839e657 TT |
298 | return; |
299 | } | |
300 | root = cwd = EXT2_ROOT_INO; | |
301 | return; | |
302 | } | |
303 | ||
5dd8f963 | 304 | static void print_features(struct ext2_super_block * s, FILE *f) |
d3aea7dc | 305 | { |
d3aea7dc | 306 | int i, j, printed=0; |
092c3dec | 307 | __u32 *mask = &s->s_feature_compat, m; |
d3aea7dc | 308 | |
777ebb3e | 309 | fputs("Filesystem features:", f); |
d3aea7dc TT |
310 | for (i=0; i <3; i++,mask++) { |
311 | for (j=0,m=1; j < 32; j++, m<<=1) { | |
312 | if (*mask & m) { | |
313 | fprintf(f, " %s", e2p_feature2string(i, m)); | |
314 | printed++; | |
315 | } | |
316 | } | |
317 | } | |
318 | if (printed == 0) | |
777ebb3e TT |
319 | fputs("(none)", f); |
320 | fputs("\n", f); | |
d3aea7dc | 321 | } |
3cebf9c1 | 322 | #endif /* READ_ONLY */ |
d3aea7dc | 323 | |
732c8cd5 | 324 | static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask, |
f5fa2007 TT |
325 | const char *str, int *first, FILE *f) |
326 | { | |
cd65a24e | 327 | if (ext2fs_bg_flags_test(fs, group, mask)) { |
f5fa2007 TT |
328 | if (*first) { |
329 | fputs(" [", f); | |
330 | *first = 0; | |
331 | } else | |
332 | fputs(", ", f); | |
333 | fputs(str, f); | |
334 | } | |
335 | } | |
336 | ||
3839e657 TT |
337 | void do_show_super_stats(int argc, char *argv[]) |
338 | { | |
2418dfd7 | 339 | const char *units ="block"; |
54434927 | 340 | dgrp_t i; |
3839e657 | 341 | FILE *out; |
d3aea7dc | 342 | int c, header_only = 0; |
8fdf2911 JS |
343 | int numdirs = 0, first, gdt_csum; |
344 | ||
88494bb6 | 345 | reset_getopt(); |
d3aea7dc TT |
346 | while ((c = getopt (argc, argv, "h")) != EOF) { |
347 | switch (c) { | |
348 | case 'h': | |
349 | header_only++; | |
350 | break; | |
351 | default: | |
9b9a780f | 352 | goto print_usage; |
d3aea7dc TT |
353 | } |
354 | } | |
355 | if (optind != argc) { | |
9b9a780f | 356 | goto print_usage; |
3839e657 TT |
357 | } |
358 | if (check_fs_open(argv[0])) | |
359 | return; | |
360 | out = open_pager(); | |
bd09eff9 | 361 | |
2418dfd7 TT |
362 | if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super, |
363 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) | |
364 | units = "cluster"; | |
365 | ||
bd09eff9 | 366 | list_super2(current_fs->super, out); |
34be9609 | 367 | for (i=0; i < current_fs->group_desc_count; i++) |
048786d7 | 368 | numdirs += ext2fs_bg_used_dirs_count(current_fs, i); |
34be9609 | 369 | fprintf(out, "Directories: %d\n", numdirs); |
efc6f628 | 370 | |
d3aea7dc TT |
371 | if (header_only) { |
372 | close_pager(out); | |
373 | return; | |
374 | } | |
efc6f628 | 375 | |
eefa4d95 ES |
376 | gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super, |
377 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM); | |
048786d7 VAH |
378 | for (i = 0; i < current_fs->group_desc_count; i++) { |
379 | fprintf(out, " Group %2d: block bitmap at %llu, " | |
380 | "inode bitmap at %llu, " | |
381 | "inode table at %llu\n" | |
2418dfd7 | 382 | " %u free %s%s, " |
048786d7 VAH |
383 | "%u free %s, " |
384 | "%u used %s%s", | |
385 | i, ext2fs_block_bitmap_loc(current_fs, i), | |
386 | ext2fs_inode_bitmap_loc(current_fs, i), | |
387 | ext2fs_inode_table_loc(current_fs, i), | |
2418dfd7 | 388 | ext2fs_bg_free_blocks_count(current_fs, i), units, |
048786d7 | 389 | ext2fs_bg_free_blocks_count(current_fs, i) != 1 ? |
2418dfd7 | 390 | "s" : "", |
048786d7 VAH |
391 | ext2fs_bg_free_inodes_count(current_fs, i), |
392 | ext2fs_bg_free_inodes_count(current_fs, i) != 1 ? | |
393 | "inodes" : "inode", | |
394 | ext2fs_bg_used_dirs_count(current_fs, i), | |
395 | ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories" | |
396 | : "directory", gdt_csum ? ", " : "\n"); | |
8fdf2911 | 397 | if (gdt_csum) |
048786d7 VAH |
398 | fprintf(out, "%u unused %s\n", |
399 | ext2fs_bg_itable_unused(current_fs, i), | |
400 | ext2fs_bg_itable_unused(current_fs, i) != 1 ? | |
401 | "inodes" : "inode"); | |
f5fa2007 | 402 | first = 1; |
732c8cd5 | 403 | print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init", |
f5fa2007 | 404 | &first, out); |
732c8cd5 | 405 | print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init", |
f5fa2007 | 406 | &first, out); |
8fdf2911 JS |
407 | if (gdt_csum) { |
408 | fprintf(out, "%sChecksum 0x%04x", | |
048786d7 | 409 | first ? " [":", ", ext2fs_bg_checksum(current_fs, i)); |
8fdf2911 JS |
410 | first = 0; |
411 | } | |
f5fa2007 TT |
412 | if (!first) |
413 | fputs("]\n", out); | |
414 | } | |
3839e657 | 415 | close_pager(out); |
9b9a780f TT |
416 | return; |
417 | print_usage: | |
418 | fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]); | |
3839e657 TT |
419 | } |
420 | ||
e88c5a33 | 421 | #ifndef READ_ONLY |
efc6f628 | 422 | void do_dirty_filesys(int argc EXT2FS_ATTR((unused)), |
54434927 | 423 | char **argv EXT2FS_ATTR((unused))) |
21c84b71 TT |
424 | { |
425 | if (check_fs_open(argv[0])) | |
426 | return; | |
601002bd TT |
427 | if (check_fs_read_write(argv[0])) |
428 | return; | |
429 | ||
430 | if (argv[1] && !strcmp(argv[1], "-clean")) | |
431 | current_fs->super->s_state |= EXT2_VALID_FS; | |
432 | else | |
433 | current_fs->super->s_state &= ~EXT2_VALID_FS; | |
21c84b71 TT |
434 | ext2fs_mark_super_dirty(current_fs); |
435 | } | |
e88c5a33 | 436 | #endif /* READ_ONLY */ |
21c84b71 | 437 | |
3839e657 | 438 | struct list_blocks_struct { |
89e25cfd AD |
439 | FILE *f; |
440 | e2_blkcnt_t total; | |
048786d7 | 441 | blk64_t first_block, last_block; |
89e25cfd AD |
442 | e2_blkcnt_t first_bcnt, last_bcnt; |
443 | e2_blkcnt_t first; | |
3839e657 TT |
444 | }; |
445 | ||
0a3db93a TT |
446 | static void finish_range(struct list_blocks_struct *lb) |
447 | { | |
448 | if (lb->first_block == 0) | |
449 | return; | |
450 | if (lb->first) | |
451 | lb->first = 0; | |
452 | else | |
80bfaa3e | 453 | fprintf(lb->f, ", "); |
0a3db93a | 454 | if (lb->first_block == lb->last_block) |
048786d7 | 455 | fprintf(lb->f, "(%lld):%llu", |
de8f3a76 | 456 | (long long)lb->first_bcnt, lb->first_block); |
0a3db93a | 457 | else |
048786d7 | 458 | fprintf(lb->f, "(%lld-%lld):%llu-%llu", |
de8f3a76 AD |
459 | (long long)lb->first_bcnt, (long long)lb->last_bcnt, |
460 | lb->first_block, lb->last_block); | |
0a3db93a TT |
461 | lb->first_block = 0; |
462 | } | |
463 | ||
efc6f628 | 464 | static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), |
048786d7 VAH |
465 | blk64_t *blocknr, e2_blkcnt_t blockcnt, |
466 | blk64_t ref_block EXT2FS_ATTR((unused)), | |
efc6f628 | 467 | int ref_offset EXT2FS_ATTR((unused)), |
54434927 | 468 | void *private) |
3839e657 TT |
469 | { |
470 | struct list_blocks_struct *lb = (struct list_blocks_struct *) private; | |
471 | ||
0a3db93a TT |
472 | lb->total++; |
473 | if (blockcnt >= 0) { | |
474 | /* | |
475 | * See if we can add on to the existing range (if it exists) | |
476 | */ | |
477 | if (lb->first_block && | |
478 | (lb->last_block+1 == *blocknr) && | |
479 | (lb->last_bcnt+1 == blockcnt)) { | |
480 | lb->last_block = *blocknr; | |
481 | lb->last_bcnt = blockcnt; | |
482 | return 0; | |
483 | } | |
484 | /* | |
485 | * Start a new range. | |
486 | */ | |
487 | finish_range(lb); | |
488 | lb->first_block = lb->last_block = *blocknr; | |
489 | lb->first_bcnt = lb->last_bcnt = blockcnt; | |
490 | return 0; | |
491 | } | |
492 | /* | |
493 | * Not a normal block. Always force a new range. | |
494 | */ | |
495 | finish_range(lb); | |
496 | if (lb->first) | |
497 | lb->first = 0; | |
498 | else | |
2c4a5406 | 499 | fprintf(lb->f, ", "); |
a5eef73b | 500 | if (blockcnt == -1) |
048786d7 | 501 | fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr); |
a5eef73b | 502 | else if (blockcnt == -2) |
048786d7 | 503 | fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr); |
a5eef73b | 504 | else if (blockcnt == -3) |
048786d7 | 505 | fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr); |
3839e657 TT |
506 | return 0; |
507 | } | |
508 | ||
9b9a780f | 509 | static void dump_xattr_string(FILE *out, const char *str, int len) |
ea822eeb | 510 | { |
290ac0e3 | 511 | int printable = 0; |
ea822eeb | 512 | int i; |
efc6f628 | 513 | |
290ac0e3 | 514 | /* check: is string "printable enough?" */ |
ea822eeb | 515 | for (i = 0; i < len; i++) |
290ac0e3 ES |
516 | if (isprint(str[i])) |
517 | printable++; | |
518 | ||
519 | if (printable <= len*7/8) | |
520 | printable = 0; | |
ea822eeb TT |
521 | |
522 | for (i = 0; i < len; i++) | |
523 | if (printable) | |
290ac0e3 ES |
524 | fprintf(out, isprint(str[i]) ? "%c" : "\\%03o", |
525 | (unsigned char)str[i]); | |
ea822eeb | 526 | else |
d047e073 | 527 | fprintf(out, "%02x ", (unsigned char)str[i]); |
ea822eeb TT |
528 | } |
529 | ||
efc6f628 | 530 | static void internal_dump_inode_extra(FILE *out, |
de8f3a76 | 531 | const char *prefix EXT2FS_ATTR((unused)), |
efc6f628 | 532 | ext2_ino_t inode_num EXT2FS_ATTR((unused)), |
9b9a780f | 533 | struct ext2_inode_large *inode) |
ea822eeb TT |
534 | { |
535 | struct ext2_ext_attr_entry *entry; | |
536 | __u32 *magic; | |
537 | char *start, *end; | |
9b9a780f | 538 | unsigned int storage_size; |
ea822eeb | 539 | |
8deb80a5 | 540 | fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize); |
ea822eeb TT |
541 | if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) - |
542 | EXT2_GOOD_OLD_INODE_SIZE) { | |
543 | fprintf(stderr, "invalid inode->i_extra_isize (%u)\n", | |
544 | inode->i_extra_isize); | |
545 | return; | |
546 | } | |
547 | storage_size = EXT2_INODE_SIZE(current_fs->super) - | |
548 | EXT2_GOOD_OLD_INODE_SIZE - | |
549 | inode->i_extra_isize; | |
550 | magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE + | |
551 | inode->i_extra_isize); | |
552 | if (*magic == EXT2_EXT_ATTR_MAGIC) { | |
553 | fprintf(out, "Extended attributes stored in inode body: \n"); | |
554 | end = (char *) inode + EXT2_INODE_SIZE(current_fs->super); | |
555 | start = (char *) magic + sizeof(__u32); | |
556 | entry = (struct ext2_ext_attr_entry *) start; | |
557 | while (!EXT2_EXT_IS_LAST_ENTRY(entry)) { | |
558 | struct ext2_ext_attr_entry *next = | |
559 | EXT2_EXT_ATTR_NEXT(entry); | |
560 | if (entry->e_value_size > storage_size || | |
561 | (char *) next >= end) { | |
562 | fprintf(out, "invalid EA entry in inode\n"); | |
563 | return; | |
564 | } | |
565 | fprintf(out, " "); | |
efc6f628 | 566 | dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry), |
ea822eeb TT |
567 | entry->e_name_len); |
568 | fprintf(out, " = \""); | |
569 | dump_xattr_string(out, start + entry->e_value_offs, | |
570 | entry->e_value_size); | |
8deb80a5 | 571 | fprintf(out, "\" (%u)\n", entry->e_value_size); |
ea822eeb TT |
572 | entry = next; |
573 | } | |
574 | } | |
575 | } | |
3839e657 | 576 | |
5e4f0709 | 577 | static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode) |
3839e657 TT |
578 | { |
579 | struct list_blocks_struct lb; | |
580 | ||
da81e3fc | 581 | fprintf(f, "%sBLOCKS:\n%s", prefix, prefix); |
3839e657 | 582 | lb.total = 0; |
0a3db93a | 583 | lb.first_block = 0; |
3839e657 | 584 | lb.f = f; |
0a3db93a | 585 | lb.first = 1; |
048786d7 VAH |
586 | ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL, |
587 | list_blocks_proc, (void *)&lb); | |
0a3db93a | 588 | finish_range(&lb); |
3839e657 | 589 | if (lb.total) |
de8f3a76 | 590 | fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total); |
3839e657 TT |
591 | fprintf(f,"\n"); |
592 | } | |
593 | ||
187cb623 TT |
594 | static int int_log10(unsigned long long arg) |
595 | { | |
596 | int l = 0; | |
597 | ||
598 | arg = arg / 10; | |
599 | while (arg) { | |
600 | l++; | |
601 | arg = arg / 10; | |
602 | } | |
603 | return l; | |
604 | } | |
605 | ||
606 | #define DUMP_LEAF_EXTENTS 0x01 | |
607 | #define DUMP_NODE_EXTENTS 0x02 | |
608 | #define DUMP_EXTENT_TABLE 0x04 | |
609 | ||
610 | static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino, | |
611 | int flags, int logical_width, int physical_width) | |
612 | { | |
613 | ext2_extent_handle_t handle; | |
614 | struct ext2fs_extent extent; | |
615 | struct ext2_extent_info info; | |
616 | int op = EXT2_EXTENT_ROOT; | |
617 | unsigned int printed = 0; | |
618 | errcode_t errcode; | |
619 | ||
620 | errcode = ext2fs_extent_open(current_fs, ino, &handle); | |
621 | if (errcode) | |
622 | return; | |
623 | ||
624 | if (flags & DUMP_EXTENT_TABLE) | |
625 | fprintf(f, "Level Entries %*s %*s Length Flags\n", | |
626 | (logical_width*2)+3, "Logical", | |
627 | (physical_width*2)+3, "Physical"); | |
628 | else | |
629 | fprintf(f, "%sEXTENTS:\n%s", prefix, prefix); | |
630 | ||
631 | while (1) { | |
632 | errcode = ext2fs_extent_get(handle, op, &extent); | |
633 | ||
634 | if (errcode) | |
635 | break; | |
636 | ||
637 | op = EXT2_EXTENT_NEXT; | |
638 | ||
639 | if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT) | |
640 | continue; | |
641 | ||
642 | if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) { | |
643 | if ((flags & DUMP_LEAF_EXTENTS) == 0) | |
644 | continue; | |
645 | } else { | |
646 | if ((flags & DUMP_NODE_EXTENTS) == 0) | |
647 | continue; | |
648 | } | |
649 | ||
650 | errcode = ext2fs_extent_get_info(handle, &info); | |
651 | if (errcode) | |
652 | continue; | |
653 | ||
654 | if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) { | |
655 | if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT) | |
656 | continue; | |
657 | ||
658 | if (flags & DUMP_EXTENT_TABLE) { | |
659 | fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu " | |
660 | "%*llu%*s %6u\n", | |
661 | info.curr_level, info.max_depth, | |
662 | info.curr_entry, info.num_entries, | |
663 | logical_width, | |
664 | extent.e_lblk, | |
665 | logical_width, | |
666 | extent.e_lblk + (extent.e_len - 1), | |
667 | physical_width, | |
668 | extent.e_pblk, | |
669 | physical_width+3, "", extent.e_len); | |
670 | continue; | |
671 | } | |
672 | ||
a9468442 | 673 | fprintf(f, "%s(ETB%d):%lld", |
c4b87b8c | 674 | printed ? ", " : "", info.curr_level, |
187cb623 TT |
675 | extent.e_pblk); |
676 | printed = 1; | |
677 | continue; | |
678 | } | |
679 | ||
680 | if (flags & DUMP_EXTENT_TABLE) { | |
681 | fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu " | |
682 | "%*llu - %*llu %6u %s\n", | |
683 | info.curr_level, info.max_depth, | |
684 | info.curr_entry, info.num_entries, | |
685 | logical_width, | |
686 | extent.e_lblk, | |
687 | logical_width, | |
688 | extent.e_lblk + (extent.e_len - 1), | |
689 | physical_width, | |
690 | extent.e_pblk, | |
691 | physical_width, | |
692 | extent.e_pblk + (extent.e_len - 1), | |
693 | extent.e_len, | |
694 | extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? | |
695 | "Uninit" : ""); | |
696 | continue; | |
697 | } | |
698 | ||
699 | if (extent.e_len == 0) | |
700 | continue; | |
701 | else if (extent.e_len == 1) | |
702 | fprintf(f, | |
a9468442 | 703 | "%s(%lld%s):%lld", |
187cb623 TT |
704 | printed ? ", " : "", |
705 | extent.e_lblk, | |
706 | extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? | |
a9468442 | 707 | "[u]" : "", |
187cb623 TT |
708 | extent.e_pblk); |
709 | else | |
710 | fprintf(f, | |
a9468442 | 711 | "%s(%lld-%lld%s):%lld-%lld", |
187cb623 TT |
712 | printed ? ", " : "", |
713 | extent.e_lblk, | |
714 | extent.e_lblk + (extent.e_len - 1), | |
715 | extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? | |
a9468442 | 716 | "[u]" : "", |
187cb623 TT |
717 | extent.e_pblk, |
718 | extent.e_pblk + (extent.e_len - 1)); | |
719 | printed = 1; | |
720 | } | |
721 | if (printed) | |
722 | fprintf(f, "\n"); | |
723 | } | |
3839e657 | 724 | |
5e4f0709 | 725 | void internal_dump_inode(FILE *out, const char *prefix, |
da81e3fc TT |
726 | ext2_ino_t inode_num, struct ext2_inode *inode, |
727 | int do_dump_blocks) | |
3839e657 | 728 | { |
50e1e10f | 729 | const char *i_type; |
fc6d9d51 TT |
730 | char frag, fsize; |
731 | int os = current_fs->super->s_creator_os; | |
a16031c6 TT |
732 | struct ext2_inode_large *large_inode; |
733 | int is_large_inode = 0; | |
734 | ||
735 | if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE) | |
736 | is_large_inode = 1; | |
737 | large_inode = (struct ext2_inode_large *) inode; | |
738 | ||
da81e3fc TT |
739 | if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory"; |
740 | else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular"; | |
741 | else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink"; | |
742 | else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special"; | |
743 | else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special"; | |
744 | else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO"; | |
745 | else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket"; | |
3839e657 | 746 | else i_type = "bad type"; |
da81e3fc | 747 | fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type); |
a16031c6 TT |
748 | fprintf(out, "%sMode: %04o Flags: 0x%x\n", |
749 | prefix, inode->i_mode & 0777, inode->i_flags); | |
750 | if (is_large_inode && large_inode->i_extra_isize >= 24) { | |
efc6f628 | 751 | fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n", |
a16031c6 TT |
752 | prefix, inode->i_generation, large_inode->i_version_hi, |
753 | inode->osd1.linux1.l_i_version); | |
754 | } else { | |
efc6f628 | 755 | fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix, |
a16031c6 TT |
756 | inode->i_generation, inode->osd1.linux1.l_i_version); |
757 | } | |
da81e3fc | 758 | fprintf(out, "%sUser: %5d Group: %5d Size: ", |
5113a6e3 | 759 | prefix, inode_uid(*inode), inode_gid(*inode)); |
0bd0e593 AD |
760 | if (LINUX_S_ISREG(inode->i_mode)) |
761 | fprintf(out, "%llu\n", EXT2_I_SIZE(inode)); | |
762 | else | |
1a6bb627 | 763 | fprintf(out, "%d\n", inode->i_size); |
5d17119d | 764 | if (os == EXT2_OS_HURD) |
62c06f79 | 765 | fprintf(out, |
da81e3fc TT |
766 | "%sFile ACL: %d Directory ACL: %d Translator: %d\n", |
767 | prefix, | |
768 | inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0, | |
769 | inode->osd1.hurd1.h_i_translator); | |
62c06f79 | 770 | else |
81624c3c | 771 | fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n", |
da81e3fc | 772 | prefix, |
81624c3c TT |
773 | inode->i_file_acl | ((long long) |
774 | (inode->osd2.linux2.l_i_file_acl_high) << 32), | |
775 | LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0); | |
efc6f628 | 776 | if (os == EXT2_OS_LINUX) |
5d17119d | 777 | fprintf(out, "%sLinks: %d Blockcount: %llu\n", |
efc6f628 TT |
778 | prefix, inode->i_links_count, |
779 | (((unsigned long long) | |
780 | inode->osd2.linux2.l_i_blocks_hi << 32)) + | |
5d17119d TT |
781 | inode->i_blocks); |
782 | else | |
783 | fprintf(out, "%sLinks: %d Blockcount: %u\n", | |
784 | prefix, inode->i_links_count, inode->i_blocks); | |
fc6d9d51 | 785 | switch (os) { |
fc6d9d51 | 786 | case EXT2_OS_HURD: |
da81e3fc TT |
787 | frag = inode->osd2.hurd2.h_i_frag; |
788 | fsize = inode->osd2.hurd2.h_i_fsize; | |
fc6d9d51 | 789 | break; |
fc6d9d51 TT |
790 | default: |
791 | frag = fsize = 0; | |
792 | } | |
da81e3fc TT |
793 | fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n", |
794 | prefix, inode->i_faddr, frag, fsize); | |
a16031c6 | 795 | if (is_large_inode && large_inode->i_extra_isize >= 24) { |
efc6f628 | 796 | fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix, |
a16031c6 TT |
797 | inode->i_ctime, large_inode->i_ctime_extra, |
798 | time_to_string(inode->i_ctime)); | |
efc6f628 | 799 | fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix, |
a16031c6 TT |
800 | inode->i_atime, large_inode->i_atime_extra, |
801 | time_to_string(inode->i_atime)); | |
802 | fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix, | |
803 | inode->i_mtime, large_inode->i_mtime_extra, | |
804 | time_to_string(inode->i_mtime)); | |
805 | fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix, | |
806 | large_inode->i_crtime, large_inode->i_crtime_extra, | |
807 | time_to_string(large_inode->i_crtime)); | |
808 | } else { | |
809 | fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime, | |
810 | time_to_string(inode->i_ctime)); | |
811 | fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime, | |
812 | time_to_string(inode->i_atime)); | |
813 | fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime, | |
814 | time_to_string(inode->i_mtime)); | |
815 | } | |
efc6f628 | 816 | if (inode->i_dtime) |
da81e3fc TT |
817 | fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime, |
818 | time_to_string(inode->i_dtime)); | |
ea822eeb TT |
819 | if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE) |
820 | internal_dump_inode_extra(out, prefix, inode_num, | |
821 | (struct ext2_inode_large *) inode); | |
795afc44 | 822 | if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0) |
da81e3fc TT |
823 | fprintf(out, "%sFast_link_dest: %.*s\n", prefix, |
824 | (int) inode->i_size, (char *)inode->i_block); | |
2d10769e TT |
825 | else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) { |
826 | int major, minor; | |
827 | const char *devnote; | |
828 | ||
829 | if (inode->i_block[0]) { | |
830 | major = (inode->i_block[0] >> 8) & 255; | |
831 | minor = inode->i_block[0] & 255; | |
832 | devnote = ""; | |
833 | } else { | |
834 | major = (inode->i_block[1] & 0xfff00) >> 8; | |
efc6f628 | 835 | minor = ((inode->i_block[1] & 0xff) | |
2d10769e TT |
836 | ((inode->i_block[1] >> 12) & 0xfff00)); |
837 | devnote = "(New-style) "; | |
838 | } | |
efc6f628 | 839 | fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n", |
2d10769e | 840 | devnote, major, minor, major, minor); |
187cb623 TT |
841 | } else if (do_dump_blocks) { |
842 | if (inode->i_flags & EXT4_EXTENTS_FL) | |
843 | dump_extents(out, prefix, inode_num, | |
c4b87b8c | 844 | DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0); |
187cb623 TT |
845 | else |
846 | dump_blocks(out, prefix, inode_num); | |
2d10769e | 847 | } |
da81e3fc TT |
848 | } |
849 | ||
ea822eeb | 850 | static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode) |
da81e3fc TT |
851 | { |
852 | FILE *out; | |
efc6f628 | 853 | |
da81e3fc | 854 | out = open_pager(); |
ea822eeb | 855 | internal_dump_inode(out, "", inode_num, inode, 1); |
3839e657 TT |
856 | close_pager(out); |
857 | } | |
858 | ||
3839e657 TT |
859 | void do_stat(int argc, char *argv[]) |
860 | { | |
b044c2e0 | 861 | ext2_ino_t inode; |
ea822eeb | 862 | struct ext2_inode * inode_buf; |
3839e657 | 863 | |
64777391 | 864 | if (check_fs_open(argv[0])) |
8363e357 | 865 | return; |
64777391 | 866 | |
ea822eeb TT |
867 | inode_buf = (struct ext2_inode *) |
868 | malloc(EXT2_INODE_SIZE(current_fs->super)); | |
869 | if (!inode_buf) { | |
870 | fprintf(stderr, "do_stat: can't allocate buffer\n"); | |
3839e657 | 871 | return; |
ea822eeb | 872 | } |
3839e657 | 873 | |
ea822eeb TT |
874 | if (common_inode_args_process(argc, argv, &inode, 0)) { |
875 | free(inode_buf); | |
876 | return; | |
877 | } | |
878 | ||
879 | if (debugfs_read_inode_full(inode, inode_buf, argv[0], | |
880 | EXT2_INODE_SIZE(current_fs->super))) { | |
881 | free(inode_buf); | |
e1018eea | 882 | return; |
ea822eeb | 883 | } |
3839e657 | 884 | |
ea822eeb TT |
885 | dump_inode(inode, inode_buf); |
886 | free(inode_buf); | |
3839e657 TT |
887 | return; |
888 | } | |
889 | ||
cf5301d7 | 890 | void do_dump_extents(int argc, char **argv) |
187cb623 TT |
891 | { |
892 | struct ext2_inode inode; | |
893 | ext2_ino_t ino; | |
894 | FILE *out; | |
895 | int c, flags = 0; | |
896 | int logical_width; | |
897 | int physical_width; | |
898 | ||
899 | reset_getopt(); | |
900 | while ((c = getopt(argc, argv, "nl")) != EOF) { | |
901 | switch (c) { | |
902 | case 'n': | |
903 | flags |= DUMP_NODE_EXTENTS; | |
904 | break; | |
905 | case 'l': | |
906 | flags |= DUMP_LEAF_EXTENTS; | |
907 | break; | |
908 | } | |
909 | } | |
910 | ||
cf5301d7 | 911 | if (argc != optind + 1) { |
187cb623 TT |
912 | com_err(0, 0, "Usage: dump_extents [-n] [-l] file"); |
913 | return; | |
914 | } | |
915 | ||
916 | if (flags == 0) | |
917 | flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS; | |
918 | flags |= DUMP_EXTENT_TABLE; | |
919 | ||
920 | if (check_fs_open(argv[0])) | |
921 | return; | |
922 | ||
923 | ino = string_to_inode(argv[optind]); | |
924 | if (ino == 0) | |
925 | return; | |
926 | ||
927 | if (debugfs_read_inode(ino, &inode, argv[0])) | |
928 | return; | |
929 | ||
930 | if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) { | |
931 | fprintf(stderr, "%s: does not uses extent block maps\n", | |
932 | argv[optind]); | |
933 | return; | |
934 | } | |
935 | ||
0bd0e593 | 936 | logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/ |
187cb623 TT |
937 | current_fs->blocksize) + 1; |
938 | if (logical_width < 5) | |
939 | logical_width = 5; | |
4efbac6f | 940 | physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1; |
187cb623 TT |
941 | if (physical_width < 5) |
942 | physical_width = 5; | |
943 | ||
944 | out = open_pager(); | |
945 | dump_extents(out, "", ino, flags, logical_width, physical_width); | |
946 | close_pager(out); | |
947 | return; | |
948 | } | |
949 | ||
af0df2aa TT |
950 | static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), |
951 | blk64_t *blocknr, | |
3cebf9c1 | 952 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), |
af0df2aa TT |
953 | blk64_t ref_block EXT2FS_ATTR((unused)), |
954 | int ref_offset EXT2FS_ATTR((unused)), | |
955 | void *private EXT2FS_ATTR((unused))) | |
956 | { | |
957 | printf("%llu ", *blocknr); | |
958 | return 0; | |
959 | } | |
960 | ||
961 | void do_blocks(int argc, char *argv[]) | |
962 | { | |
963 | ext2_ino_t inode; | |
964 | ||
965 | if (check_fs_open(argv[0])) | |
966 | return; | |
967 | ||
968 | if (common_inode_args_process(argc, argv, &inode, 0)) { | |
969 | return; | |
970 | } | |
971 | ||
972 | ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL, | |
973 | print_blocks_proc, NULL); | |
974 | fputc('\n', stdout); | |
975 | return; | |
976 | } | |
977 | ||
3839e657 TT |
978 | void do_chroot(int argc, char *argv[]) |
979 | { | |
b044c2e0 | 980 | ext2_ino_t inode; |
3839e657 TT |
981 | int retval; |
982 | ||
e1018eea | 983 | if (common_inode_args_process(argc, argv, &inode, 0)) |
3839e657 TT |
984 | return; |
985 | ||
fc6d9d51 | 986 | retval = ext2fs_check_directory(current_fs, inode); |
3839e657 | 987 | if (retval) { |
9b9a780f | 988 | com_err(argv[1], retval, 0); |
3839e657 TT |
989 | return; |
990 | } | |
991 | root = inode; | |
992 | } | |
993 | ||
e88c5a33 | 994 | #ifndef READ_ONLY |
3839e657 TT |
995 | void do_clri(int argc, char *argv[]) |
996 | { | |
b044c2e0 | 997 | ext2_ino_t inode; |
3839e657 TT |
998 | struct ext2_inode inode_buf; |
999 | ||
e1018eea | 1000 | if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW)) |
3839e657 TT |
1001 | return; |
1002 | ||
e1018eea | 1003 | if (debugfs_read_inode(inode, &inode_buf, argv[0])) |
3839e657 | 1004 | return; |
3839e657 | 1005 | memset(&inode_buf, 0, sizeof(inode_buf)); |
e1018eea | 1006 | if (debugfs_write_inode(inode, &inode_buf, argv[0])) |
3839e657 | 1007 | return; |
3839e657 TT |
1008 | } |
1009 | ||
1010 | void do_freei(int argc, char *argv[]) | |
1011 | { | |
2ae5d1fd TT |
1012 | unsigned int len = 1; |
1013 | int err = 0; | |
1014 | ext2_ino_t inode; | |
1015 | ||
1016 | if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]", | |
1017 | CHECK_FS_RW | CHECK_FS_BITMAPS)) | |
e64e6761 | 1018 | return; |
2ae5d1fd TT |
1019 | if (check_fs_read_write(argv[0])) |
1020 | return; | |
3839e657 | 1021 | |
2ae5d1fd TT |
1022 | inode = string_to_inode(argv[1]); |
1023 | if (!inode) | |
3839e657 TT |
1024 | return; |
1025 | ||
2ae5d1fd TT |
1026 | if (argc == 3) { |
1027 | len = parse_ulong(argv[2], argv[0], "length", &err); | |
1028 | if (err) | |
1029 | return; | |
1030 | } | |
1031 | ||
1032 | if (len == 1 && | |
1033 | !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode)) | |
3839e657 | 1034 | com_err(argv[0], 0, "Warning: inode already clear"); |
2ae5d1fd | 1035 | while (len-- > 0) |
a9b23fc9 | 1036 | ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++); |
fc6d9d51 | 1037 | ext2fs_mark_ib_dirty(current_fs); |
3839e657 TT |
1038 | } |
1039 | ||
1040 | void do_seti(int argc, char *argv[]) | |
1041 | { | |
2ae5d1fd TT |
1042 | unsigned int len = 1; |
1043 | int err = 0; | |
1044 | ext2_ino_t inode; | |
3839e657 | 1045 | |
2ae5d1fd TT |
1046 | if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]", |
1047 | CHECK_FS_RW | CHECK_FS_BITMAPS)) | |
1048 | return; | |
1049 | if (check_fs_read_write(argv[0])) | |
3839e657 TT |
1050 | return; |
1051 | ||
2ae5d1fd TT |
1052 | inode = string_to_inode(argv[1]); |
1053 | if (!inode) | |
1054 | return; | |
1055 | ||
1056 | if (argc == 3) { | |
1057 | len = parse_ulong(argv[2], argv[0], "length", &err); | |
1058 | if (err) | |
1059 | return; | |
1060 | } | |
1061 | ||
1062 | if ((len == 1) && | |
1063 | ext2fs_test_inode_bitmap2(current_fs->inode_map,inode)) | |
3839e657 | 1064 | com_err(argv[0], 0, "Warning: inode already set"); |
2ae5d1fd TT |
1065 | while (len-- > 0) |
1066 | ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++); | |
fc6d9d51 | 1067 | ext2fs_mark_ib_dirty(current_fs); |
3839e657 | 1068 | } |
e88c5a33 | 1069 | #endif /* READ_ONLY */ |
3839e657 TT |
1070 | |
1071 | void do_testi(int argc, char *argv[]) | |
1072 | { | |
b044c2e0 | 1073 | ext2_ino_t inode; |
3839e657 | 1074 | |
e1018eea | 1075 | if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS)) |
3839e657 TT |
1076 | return; |
1077 | ||
6d879a99 | 1078 | if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode)) |
b044c2e0 | 1079 | printf("Inode %u is marked in use\n", inode); |
3839e657 | 1080 | else |
b044c2e0 | 1081 | printf("Inode %u is not in use\n", inode); |
3839e657 TT |
1082 | } |
1083 | ||
e88c5a33 | 1084 | #ifndef READ_ONLY |
3839e657 TT |
1085 | void do_freeb(int argc, char *argv[]) |
1086 | { | |
048786d7 VAH |
1087 | blk64_t block; |
1088 | blk64_t count = 1; | |
3839e657 | 1089 | |
e1018eea | 1090 | if (common_block_args_process(argc, argv, &block, &count)) |
3839e657 | 1091 | return; |
fc6d9d51 | 1092 | if (check_fs_read_write(argv[0])) |
3839e657 | 1093 | return; |
e1018eea | 1094 | while (count-- > 0) { |
6d879a99 | 1095 | if (!ext2fs_test_block_bitmap2(current_fs->block_map,block)) |
048786d7 | 1096 | com_err(argv[0], 0, "Warning: block %llu already clear", |
e1018eea | 1097 | block); |
6d879a99 | 1098 | ext2fs_unmark_block_bitmap2(current_fs->block_map,block); |
e1018eea TT |
1099 | block++; |
1100 | } | |
fc6d9d51 | 1101 | ext2fs_mark_bb_dirty(current_fs); |
3839e657 TT |
1102 | } |
1103 | ||
1104 | void do_setb(int argc, char *argv[]) | |
1105 | { | |
048786d7 VAH |
1106 | blk64_t block; |
1107 | blk64_t count = 1; | |
3839e657 | 1108 | |
e1018eea | 1109 | if (common_block_args_process(argc, argv, &block, &count)) |
3839e657 | 1110 | return; |
fc6d9d51 | 1111 | if (check_fs_read_write(argv[0])) |
3839e657 | 1112 | return; |
e1018eea | 1113 | while (count-- > 0) { |
6d879a99 | 1114 | if (ext2fs_test_block_bitmap2(current_fs->block_map,block)) |
048786d7 | 1115 | com_err(argv[0], 0, "Warning: block %llu already set", |
e1018eea | 1116 | block); |
6d879a99 | 1117 | ext2fs_mark_block_bitmap2(current_fs->block_map,block); |
e1018eea TT |
1118 | block++; |
1119 | } | |
fc6d9d51 | 1120 | ext2fs_mark_bb_dirty(current_fs); |
3839e657 | 1121 | } |
e88c5a33 | 1122 | #endif /* READ_ONLY */ |
3839e657 TT |
1123 | |
1124 | void do_testb(int argc, char *argv[]) | |
1125 | { | |
048786d7 VAH |
1126 | blk64_t block; |
1127 | blk64_t count = 1; | |
3839e657 | 1128 | |
e1018eea | 1129 | if (common_block_args_process(argc, argv, &block, &count)) |
3839e657 | 1130 | return; |
e1018eea | 1131 | while (count-- > 0) { |
6d879a99 | 1132 | if (ext2fs_test_block_bitmap2(current_fs->block_map,block)) |
048786d7 | 1133 | printf("Block %llu marked in use\n", block); |
e1018eea | 1134 | else |
048786d7 | 1135 | printf("Block %llu not in use\n", block); |
e1018eea | 1136 | block++; |
3839e657 | 1137 | } |
3839e657 TT |
1138 | } |
1139 | ||
e88c5a33 | 1140 | #ifndef READ_ONLY |
50e1e10f TT |
1141 | static void modify_u8(char *com, const char *prompt, |
1142 | const char *format, __u8 *val) | |
3839e657 TT |
1143 | { |
1144 | char buf[200]; | |
21c84b71 | 1145 | unsigned long v; |
3839e657 TT |
1146 | char *tmp; |
1147 | ||
1148 | sprintf(buf, format, *val); | |
1149 | printf("%30s [%s] ", prompt, buf); | |
d9039ae0 DL |
1150 | if (!fgets(buf, sizeof(buf), stdin)) |
1151 | return; | |
3839e657 TT |
1152 | if (buf[strlen (buf) - 1] == '\n') |
1153 | buf[strlen (buf) - 1] = '\0'; | |
1154 | if (!buf[0]) | |
1155 | return; | |
21c84b71 | 1156 | v = strtoul(buf, &tmp, 0); |
3839e657 TT |
1157 | if (*tmp) |
1158 | com_err(com, 0, "Bad value - %s", buf); | |
1159 | else | |
1160 | *val = v; | |
1161 | } | |
1162 | ||
50e1e10f TT |
1163 | static void modify_u16(char *com, const char *prompt, |
1164 | const char *format, __u16 *val) | |
3839e657 TT |
1165 | { |
1166 | char buf[200]; | |
21c84b71 | 1167 | unsigned long v; |
3839e657 TT |
1168 | char *tmp; |
1169 | ||
1170 | sprintf(buf, format, *val); | |
1171 | printf("%30s [%s] ", prompt, buf); | |
d9039ae0 DL |
1172 | if (!fgets(buf, sizeof(buf), stdin)) |
1173 | return; | |
3839e657 TT |
1174 | if (buf[strlen (buf) - 1] == '\n') |
1175 | buf[strlen (buf) - 1] = '\0'; | |
1176 | if (!buf[0]) | |
1177 | return; | |
21c84b71 | 1178 | v = strtoul(buf, &tmp, 0); |
3839e657 TT |
1179 | if (*tmp) |
1180 | com_err(com, 0, "Bad value - %s", buf); | |
1181 | else | |
1182 | *val = v; | |
1183 | } | |
1184 | ||
50e1e10f TT |
1185 | static void modify_u32(char *com, const char *prompt, |
1186 | const char *format, __u32 *val) | |
3839e657 TT |
1187 | { |
1188 | char buf[200]; | |
21c84b71 | 1189 | unsigned long v; |
3839e657 TT |
1190 | char *tmp; |
1191 | ||
1192 | sprintf(buf, format, *val); | |
1193 | printf("%30s [%s] ", prompt, buf); | |
d9039ae0 DL |
1194 | if (!fgets(buf, sizeof(buf), stdin)) |
1195 | return; | |
3839e657 TT |
1196 | if (buf[strlen (buf) - 1] == '\n') |
1197 | buf[strlen (buf) - 1] = '\0'; | |
1198 | if (!buf[0]) | |
1199 | return; | |
21c84b71 | 1200 | v = strtoul(buf, &tmp, 0); |
3839e657 TT |
1201 | if (*tmp) |
1202 | com_err(com, 0, "Bad value - %s", buf); | |
1203 | else | |
1204 | *val = v; | |
1205 | } | |
1206 | ||
1207 | ||
1208 | void do_modify_inode(int argc, char *argv[]) | |
1209 | { | |
1210 | struct ext2_inode inode; | |
b044c2e0 TT |
1211 | ext2_ino_t inode_num; |
1212 | int i; | |
b044c2e0 TT |
1213 | unsigned char *frag, *fsize; |
1214 | char buf[80]; | |
7380ac90 | 1215 | int os; |
b044c2e0 TT |
1216 | const char *hex_format = "0x%x"; |
1217 | const char *octal_format = "0%o"; | |
1218 | const char *decimal_format = "%d"; | |
8deb80a5 | 1219 | const char *unsignedlong_format = "%lu"; |
efc6f628 | 1220 | |
e1018eea | 1221 | if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW)) |
3839e657 TT |
1222 | return; |
1223 | ||
7380ac90 TT |
1224 | os = current_fs->super->s_creator_os; |
1225 | ||
e1018eea | 1226 | if (debugfs_read_inode(inode_num, &inode, argv[1])) |
3839e657 | 1227 | return; |
efc6f628 | 1228 | |
50e1e10f TT |
1229 | modify_u16(argv[0], "Mode", octal_format, &inode.i_mode); |
1230 | modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid); | |
1231 | modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid); | |
8deb80a5 | 1232 | modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size); |
50e1e10f TT |
1233 | modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime); |
1234 | modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime); | |
1235 | modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime); | |
1236 | modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime); | |
1237 | modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count); | |
5d17119d | 1238 | if (os == EXT2_OS_LINUX) |
efc6f628 | 1239 | modify_u16(argv[0], "Block count high", unsignedlong_format, |
5d17119d | 1240 | &inode.osd2.linux2.l_i_blocks_hi); |
8deb80a5 | 1241 | modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks); |
50e1e10f | 1242 | modify_u32(argv[0], "File flags", hex_format, &inode.i_flags); |
3db9305a | 1243 | modify_u32(argv[0], "Generation", hex_format, &inode.i_generation); |
50e1e10f TT |
1244 | #if 0 |
1245 | modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1); | |
1246 | #endif | |
1247 | modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl); | |
36a43d67 TT |
1248 | if (LINUX_S_ISDIR(inode.i_mode)) |
1249 | modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl); | |
1250 | else | |
1251 | modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high); | |
62c06f79 | 1252 | |
5d17119d | 1253 | if (os == EXT2_OS_HURD) |
62c06f79 TT |
1254 | modify_u32(argv[0], "Translator Block", |
1255 | decimal_format, &inode.osd1.hurd1.h_i_translator); | |
efc6f628 | 1256 | |
50e1e10f | 1257 | modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr); |
fc6d9d51 | 1258 | switch (os) { |
fc6d9d51 TT |
1259 | case EXT2_OS_HURD: |
1260 | frag = &inode.osd2.hurd2.h_i_frag; | |
1261 | fsize = &inode.osd2.hurd2.h_i_fsize; | |
1262 | break; | |
fc6d9d51 TT |
1263 | default: |
1264 | frag = fsize = 0; | |
1265 | } | |
1266 | if (frag) | |
1267 | modify_u8(argv[0], "Fragment number", decimal_format, frag); | |
1268 | if (fsize) | |
1269 | modify_u8(argv[0], "Fragment size", decimal_format, fsize); | |
1270 | ||
3839e657 TT |
1271 | for (i=0; i < EXT2_NDIR_BLOCKS; i++) { |
1272 | sprintf(buf, "Direct Block #%d", i); | |
50e1e10f | 1273 | modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]); |
3839e657 | 1274 | } |
50e1e10f | 1275 | modify_u32(argv[0], "Indirect Block", decimal_format, |
efc6f628 | 1276 | &inode.i_block[EXT2_IND_BLOCK]); |
50e1e10f | 1277 | modify_u32(argv[0], "Double Indirect Block", decimal_format, |
3839e657 | 1278 | &inode.i_block[EXT2_DIND_BLOCK]); |
50e1e10f | 1279 | modify_u32(argv[0], "Triple Indirect Block", decimal_format, |
3839e657 | 1280 | &inode.i_block[EXT2_TIND_BLOCK]); |
e1018eea | 1281 | if (debugfs_write_inode(inode_num, &inode, argv[1])) |
3839e657 | 1282 | return; |
3839e657 | 1283 | } |
e88c5a33 | 1284 | #endif /* READ_ONLY */ |
3839e657 | 1285 | |
3839e657 TT |
1286 | void do_change_working_dir(int argc, char *argv[]) |
1287 | { | |
b044c2e0 TT |
1288 | ext2_ino_t inode; |
1289 | int retval; | |
efc6f628 | 1290 | |
e1018eea | 1291 | if (common_inode_args_process(argc, argv, &inode, 0)) |
3839e657 TT |
1292 | return; |
1293 | ||
fc6d9d51 | 1294 | retval = ext2fs_check_directory(current_fs, inode); |
3839e657 | 1295 | if (retval) { |
9b9a780f | 1296 | com_err(argv[1], retval, 0); |
3839e657 TT |
1297 | return; |
1298 | } | |
1299 | cwd = inode; | |
1300 | return; | |
1301 | } | |
1302 | ||
3839e657 TT |
1303 | void do_print_working_directory(int argc, char *argv[]) |
1304 | { | |
1305 | int retval; | |
1306 | char *pathname = NULL; | |
efc6f628 | 1307 | |
e1018eea TT |
1308 | if (common_args_process(argc, argv, 1, 1, |
1309 | "print_working_directory", "", 0)) | |
3839e657 TT |
1310 | return; |
1311 | ||
fc6d9d51 | 1312 | retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname); |
3839e657 TT |
1313 | if (retval) { |
1314 | com_err(argv[0], retval, | |
1315 | "while trying to get pathname of cwd"); | |
1316 | } | |
971fe056 BB |
1317 | printf("[pwd] INODE: %6u PATH: %s\n", |
1318 | cwd, pathname ? pathname : "NULL"); | |
1319 | if (pathname) { | |
1320 | free(pathname); | |
1321 | pathname = NULL; | |
1322 | } | |
fc6d9d51 | 1323 | retval = ext2fs_get_pathname(current_fs, root, 0, &pathname); |
3839e657 TT |
1324 | if (retval) { |
1325 | com_err(argv[0], retval, | |
1326 | "while trying to get pathname of root"); | |
1327 | } | |
971fe056 BB |
1328 | printf("[root] INODE: %6u PATH: %s\n", |
1329 | root, pathname ? pathname : "NULL"); | |
1330 | if (pathname) { | |
1331 | free(pathname); | |
1332 | pathname = NULL; | |
1333 | } | |
3839e657 TT |
1334 | return; |
1335 | } | |
1336 | ||
3cebf9c1 | 1337 | #ifndef READ_ONLY |
50e1e10f | 1338 | static void make_link(char *sourcename, char *destname) |
3839e657 | 1339 | { |
abdf84f3 TT |
1340 | ext2_ino_t ino; |
1341 | struct ext2_inode inode; | |
b044c2e0 TT |
1342 | int retval; |
1343 | ext2_ino_t dir; | |
d4e0b1c6 | 1344 | char *dest, *cp, *base_name; |
3839e657 TT |
1345 | |
1346 | /* | |
1347 | * Get the source inode | |
1348 | */ | |
abdf84f3 TT |
1349 | ino = string_to_inode(sourcename); |
1350 | if (!ino) | |
3839e657 | 1351 | return; |
d4e0b1c6 TT |
1352 | base_name = strrchr(sourcename, '/'); |
1353 | if (base_name) | |
1354 | base_name++; | |
3839e657 | 1355 | else |
d4e0b1c6 | 1356 | base_name = sourcename; |
3839e657 TT |
1357 | /* |
1358 | * Figure out the destination. First see if it exists and is | |
efc6f628 | 1359 | * a directory. |
3839e657 | 1360 | */ |
fc6d9d51 | 1361 | if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir))) |
d4e0b1c6 | 1362 | dest = base_name; |
3839e657 TT |
1363 | else { |
1364 | /* | |
1365 | * OK, it doesn't exist. See if it is | |
1366 | * '<dir>/basename' or 'basename' | |
1367 | */ | |
1368 | cp = strrchr(destname, '/'); | |
1369 | if (cp) { | |
1370 | *cp = 0; | |
1371 | dir = string_to_inode(destname); | |
1372 | if (!dir) | |
1373 | return; | |
1374 | dest = cp+1; | |
1375 | } else { | |
1376 | dir = cwd; | |
1377 | dest = destname; | |
1378 | } | |
1379 | } | |
abdf84f3 TT |
1380 | |
1381 | if (debugfs_read_inode(ino, &inode, sourcename)) | |
1382 | return; | |
efc6f628 TT |
1383 | |
1384 | retval = ext2fs_link(current_fs, dir, dest, ino, | |
abdf84f3 | 1385 | ext2_file_type(inode.i_mode)); |
3839e657 | 1386 | if (retval) |
9b9a780f | 1387 | com_err("make_link", retval, 0); |
3839e657 TT |
1388 | return; |
1389 | } | |
1390 | ||
1391 | ||
1392 | void do_link(int argc, char *argv[]) | |
1393 | { | |
e1018eea TT |
1394 | if (common_args_process(argc, argv, 3, 3, "link", |
1395 | "<source file> <dest_name>", CHECK_FS_RW)) | |
1396 | return; | |
1397 | ||
1398 | make_link(argv[1], argv[2]); | |
1399 | } | |
1400 | ||
048786d7 VAH |
1401 | static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr, |
1402 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), | |
1403 | blk64_t ref_block EXT2FS_ATTR((unused)), | |
1404 | int ref_offset EXT2FS_ATTR((unused)), | |
54434927 | 1405 | void *private EXT2FS_ATTR((unused))) |
e1018eea | 1406 | { |
048786d7 | 1407 | blk64_t block; |
e1018eea TT |
1408 | |
1409 | block = *blocknr; | |
048786d7 | 1410 | ext2fs_block_alloc_stats2(fs, block, +1); |
e1018eea TT |
1411 | return 0; |
1412 | } | |
1413 | ||
1414 | void do_undel(int argc, char *argv[]) | |
1415 | { | |
1416 | ext2_ino_t ino; | |
1417 | struct ext2_inode inode; | |
e1018eea | 1418 | |
b026d539 TT |
1419 | if (common_args_process(argc, argv, 2, 3, "undelete", |
1420 | "<inode_num> [dest_name]", | |
e1018eea TT |
1421 | CHECK_FS_RW | CHECK_FS_BITMAPS)) |
1422 | return; | |
1423 | ||
1424 | ino = string_to_inode(argv[1]); | |
1425 | if (!ino) | |
1426 | return; | |
1427 | ||
1428 | if (debugfs_read_inode(ino, &inode, argv[1])) | |
1429 | return; | |
1430 | ||
6d879a99 | 1431 | if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) { |
e1018eea | 1432 | com_err(argv[1], 0, "Inode is not marked as deleted"); |
3839e657 TT |
1433 | return; |
1434 | } | |
e1018eea TT |
1435 | |
1436 | /* | |
1437 | * XXX this function doesn't handle changing the links count on the | |
efc6f628 | 1438 | * parent directory when undeleting a directory. |
e1018eea TT |
1439 | */ |
1440 | inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1; | |
1441 | inode.i_dtime = 0; | |
1442 | ||
1443 | if (debugfs_write_inode(ino, &inode, argv[0])) | |
3839e657 TT |
1444 | return; |
1445 | ||
048786d7 VAH |
1446 | ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL, |
1447 | mark_blocks_proc, NULL); | |
e1018eea | 1448 | |
d7f64ae8 | 1449 | ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0); |
e1018eea | 1450 | |
b026d539 TT |
1451 | if (argc > 2) |
1452 | make_link(argv[1], argv[2]); | |
3839e657 TT |
1453 | } |
1454 | ||
50e1e10f | 1455 | static void unlink_file_by_name(char *filename) |
3839e657 | 1456 | { |
b044c2e0 TT |
1457 | int retval; |
1458 | ext2_ino_t dir; | |
d4e0b1c6 | 1459 | char *base_name; |
efc6f628 | 1460 | |
d4e0b1c6 TT |
1461 | base_name = strrchr(filename, '/'); |
1462 | if (base_name) { | |
1463 | *base_name++ = '\0'; | |
3839e657 TT |
1464 | dir = string_to_inode(filename); |
1465 | if (!dir) | |
1466 | return; | |
1467 | } else { | |
1468 | dir = cwd; | |
d4e0b1c6 | 1469 | base_name = filename; |
3839e657 | 1470 | } |
d4e0b1c6 | 1471 | retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0); |
3839e657 | 1472 | if (retval) |
9b9a780f | 1473 | com_err("unlink_file_by_name", retval, 0); |
3839e657 TT |
1474 | return; |
1475 | } | |
1476 | ||
1477 | void do_unlink(int argc, char *argv[]) | |
1478 | { | |
e1018eea TT |
1479 | if (common_args_process(argc, argv, 2, 2, "link", |
1480 | "<pathname>", CHECK_FS_RW)) | |
3839e657 TT |
1481 | return; |
1482 | ||
1483 | unlink_file_by_name(argv[1]); | |
1484 | } | |
e88c5a33 | 1485 | #endif /* READ_ONLY */ |
3839e657 TT |
1486 | |
1487 | void do_find_free_block(int argc, char *argv[]) | |
1488 | { | |
048786d7 | 1489 | blk64_t free_blk, goal, first_free = 0; |
e1018eea | 1490 | int count; |
3839e657 TT |
1491 | errcode_t retval; |
1492 | char *tmp; | |
efc6f628 | 1493 | |
e1018eea TT |
1494 | if ((argc > 3) || (argc==2 && *argv[1] == '?')) { |
1495 | com_err(argv[0], 0, "Usage: find_free_block [count [goal]]"); | |
3839e657 TT |
1496 | return; |
1497 | } | |
1498 | if (check_fs_open(argv[0])) | |
1499 | return; | |
1500 | ||
1501 | if (argc > 1) { | |
e1018eea TT |
1502 | count = strtol(argv[1],&tmp,0); |
1503 | if (*tmp) { | |
1504 | com_err(argv[0], 0, "Bad count - %s", argv[1]); | |
1505 | return; | |
1506 | } | |
1507 | } else | |
1508 | count = 1; | |
1509 | ||
1510 | if (argc > 2) { | |
1511 | goal = strtol(argv[2], &tmp, 0); | |
3839e657 TT |
1512 | if (*tmp) { |
1513 | com_err(argv[0], 0, "Bad goal - %s", argv[1]); | |
1514 | return; | |
1515 | } | |
1516 | } | |
1517 | else | |
fc6d9d51 | 1518 | goal = current_fs->super->s_first_data_block; |
3839e657 | 1519 | |
e1018eea | 1520 | printf("Free blocks found: "); |
efc6f628 | 1521 | free_blk = goal - 1; |
e1018eea | 1522 | while (count-- > 0) { |
048786d7 VAH |
1523 | retval = ext2fs_new_block2(current_fs, free_blk + 1, 0, |
1524 | &free_blk); | |
5aae7c29 TT |
1525 | if (first_free) { |
1526 | if (first_free == free_blk) | |
1527 | break; | |
1528 | } else | |
1529 | first_free = free_blk; | |
e1018eea | 1530 | if (retval) { |
9b9a780f | 1531 | com_err("ext2fs_new_block", retval, 0); |
e1018eea TT |
1532 | return; |
1533 | } else | |
048786d7 | 1534 | printf("%llu ", free_blk); |
e1018eea TT |
1535 | } |
1536 | printf("\n"); | |
3839e657 TT |
1537 | } |
1538 | ||
1539 | void do_find_free_inode(int argc, char *argv[]) | |
1540 | { | |
b044c2e0 TT |
1541 | ext2_ino_t free_inode, dir; |
1542 | int mode; | |
1543 | int retval; | |
1544 | char *tmp; | |
efc6f628 | 1545 | |
50e1e10f TT |
1546 | if (argc > 3 || (argc>1 && *argv[1] == '?')) { |
1547 | com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]"); | |
3839e657 TT |
1548 | return; |
1549 | } | |
1550 | if (check_fs_open(argv[0])) | |
1551 | return; | |
1552 | ||
1553 | if (argc > 1) { | |
1554 | dir = strtol(argv[1], &tmp, 0); | |
1555 | if (*tmp) { | |
1556 | com_err(argv[0], 0, "Bad dir - %s", argv[1]); | |
1557 | return; | |
1558 | } | |
1559 | } | |
1560 | else | |
1561 | dir = root; | |
1562 | if (argc > 2) { | |
1563 | mode = strtol(argv[2], &tmp, 0); | |
1564 | if (*tmp) { | |
1565 | com_err(argv[0], 0, "Bad mode - %s", argv[2]); | |
1566 | return; | |
1567 | } | |
50e1e10f | 1568 | } else |
3839e657 TT |
1569 | mode = 010755; |
1570 | ||
fc6d9d51 | 1571 | retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode); |
3839e657 | 1572 | if (retval) |
9b9a780f | 1573 | com_err("ext2fs_new_inode", retval, 0); |
3839e657 | 1574 | else |
b044c2e0 | 1575 | printf("Free inode found: %u\n", free_inode); |
3839e657 TT |
1576 | } |
1577 | ||
e88c5a33 | 1578 | #ifndef READ_ONLY |
c756ec42 | 1579 | static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes) |
50e1e10f | 1580 | { |
5a51384e | 1581 | ext2_file_t e2_file; |
50e1e10f | 1582 | errcode_t retval; |
b7846402 TT |
1583 | int got; |
1584 | unsigned int written; | |
c756ec42 | 1585 | char *buf; |
5a51384e | 1586 | char *ptr; |
c756ec42 RY |
1587 | char *zero_buf; |
1588 | int cmp; | |
50e1e10f | 1589 | |
5a51384e TT |
1590 | retval = ext2fs_file_open(current_fs, newfile, |
1591 | EXT2_FILE_WRITE, &e2_file); | |
50e1e10f TT |
1592 | if (retval) |
1593 | return retval; | |
50e1e10f | 1594 | |
50295a3f DW |
1595 | retval = ext2fs_get_mem(bufsize, &buf); |
1596 | if (retval) { | |
1597 | com_err("copy_file", retval, "can't allocate buffer\n"); | |
1598 | return retval; | |
c756ec42 RY |
1599 | } |
1600 | ||
1601 | /* This is used for checking whether the whole block is zero */ | |
1602 | retval = ext2fs_get_memzero(bufsize, &zero_buf); | |
1603 | if (retval) { | |
1604 | com_err("copy_file", retval, "can't allocate buffer\n"); | |
50295a3f | 1605 | ext2fs_free_mem(&buf); |
c756ec42 RY |
1606 | return retval; |
1607 | } | |
1608 | ||
5a51384e | 1609 | while (1) { |
c756ec42 | 1610 | got = read(fd, buf, bufsize); |
5a51384e TT |
1611 | if (got == 0) |
1612 | break; | |
1613 | if (got < 0) { | |
1614 | retval = errno; | |
1615 | goto fail; | |
1616 | } | |
1617 | ptr = buf; | |
c756ec42 RY |
1618 | |
1619 | /* Sparse copy */ | |
1620 | if (make_holes) { | |
1621 | /* Check whether all is zero */ | |
1622 | cmp = memcmp(ptr, zero_buf, got); | |
1623 | if (cmp == 0) { | |
1624 | /* The whole block is zero, make a hole */ | |
1625 | retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL); | |
1626 | if (retval) | |
1627 | goto fail; | |
1628 | got = 0; | |
1629 | } | |
1630 | } | |
1631 | ||
1632 | /* Normal copy */ | |
5a51384e TT |
1633 | while (got > 0) { |
1634 | retval = ext2fs_file_write(e2_file, ptr, | |
1635 | got, &written); | |
1636 | if (retval) | |
1637 | goto fail; | |
1638 | ||
1639 | got -= written; | |
1640 | ptr += written; | |
1641 | } | |
1642 | } | |
50295a3f | 1643 | ext2fs_free_mem(&buf); |
c756ec42 | 1644 | ext2fs_free_mem(&zero_buf); |
5a51384e | 1645 | retval = ext2fs_file_close(e2_file); |
5a51384e TT |
1646 | return retval; |
1647 | ||
1648 | fail: | |
50295a3f | 1649 | ext2fs_free_mem(&buf); |
c756ec42 | 1650 | ext2fs_free_mem(&zero_buf); |
5a51384e TT |
1651 | (void) ext2fs_file_close(e2_file); |
1652 | return retval; | |
50e1e10f TT |
1653 | } |
1654 | ||
5a51384e | 1655 | |
50e1e10f TT |
1656 | void do_write(int argc, char *argv[]) |
1657 | { | |
b044c2e0 TT |
1658 | int fd; |
1659 | struct stat statbuf; | |
1660 | ext2_ino_t newfile; | |
1661 | errcode_t retval; | |
50e1e10f | 1662 | struct ext2_inode inode; |
c756ec42 RY |
1663 | int bufsize = IO_BUFSIZE; |
1664 | int make_holes = 0; | |
50e1e10f | 1665 | |
e1018eea TT |
1666 | if (common_args_process(argc, argv, 3, 3, "write", |
1667 | "<native file> <new file>", CHECK_FS_RW)) | |
50e1e10f | 1668 | return; |
e1018eea | 1669 | |
50e1e10f TT |
1670 | fd = open(argv[1], O_RDONLY); |
1671 | if (fd < 0) { | |
9b9a780f | 1672 | com_err(argv[1], errno, 0); |
50e1e10f TT |
1673 | return; |
1674 | } | |
1675 | if (fstat(fd, &statbuf) < 0) { | |
9b9a780f | 1676 | com_err(argv[1], errno, 0); |
50e1e10f TT |
1677 | close(fd); |
1678 | return; | |
1679 | } | |
1680 | ||
1dd090fa TT |
1681 | retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile); |
1682 | if (retval == 0) { | |
1683 | com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]); | |
1684 | close(fd); | |
1685 | return; | |
1686 | } | |
1687 | ||
fc6d9d51 | 1688 | retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); |
50e1e10f | 1689 | if (retval) { |
9b9a780f | 1690 | com_err(argv[0], retval, 0); |
50e1e10f TT |
1691 | close(fd); |
1692 | return; | |
1693 | } | |
b044c2e0 | 1694 | printf("Allocated inode: %u\n", newfile); |
085cb19a TT |
1695 | retval = ext2fs_link(current_fs, cwd, argv[2], newfile, |
1696 | EXT2_FT_REG_FILE); | |
2d9f0804 TT |
1697 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
1698 | retval = ext2fs_expand_dir(current_fs, cwd); | |
1699 | if (retval) { | |
1700 | com_err(argv[0], retval, "while expanding directory"); | |
4262162a | 1701 | close(fd); |
2d9f0804 TT |
1702 | return; |
1703 | } | |
1704 | retval = ext2fs_link(current_fs, cwd, argv[2], newfile, | |
1705 | EXT2_FT_REG_FILE); | |
1706 | } | |
50e1e10f | 1707 | if (retval) { |
9b9a780f | 1708 | com_err(argv[2], retval, 0); |
50e1e10f TT |
1709 | close(fd); |
1710 | return; | |
1711 | } | |
6d879a99 | 1712 | if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile)) |
50e1e10f | 1713 | com_err(argv[0], 0, "Warning: inode already set"); |
d7f64ae8 | 1714 | ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0); |
50e1e10f | 1715 | memset(&inode, 0, sizeof(inode)); |
04df4919 | 1716 | inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG; |
efc6f628 | 1717 | inode.i_atime = inode.i_ctime = inode.i_mtime = |
4efae606 | 1718 | current_fs->now ? current_fs->now : time(0); |
50e1e10f TT |
1719 | inode.i_links_count = 1; |
1720 | inode.i_size = statbuf.st_size; | |
efc6f628 | 1721 | if (current_fs->super->s_feature_incompat & |
a17e9f30 ES |
1722 | EXT3_FEATURE_INCOMPAT_EXTENTS) { |
1723 | int i; | |
1724 | struct ext3_extent_header *eh; | |
1725 | ||
1726 | eh = (struct ext3_extent_header *) &inode.i_block[0]; | |
1727 | eh->eh_depth = 0; | |
1728 | eh->eh_entries = 0; | |
45f143c2 | 1729 | eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC); |
a17e9f30 ES |
1730 | i = (sizeof(inode.i_block) - sizeof(*eh)) / |
1731 | sizeof(struct ext3_extent); | |
1732 | eh->eh_max = ext2fs_cpu_to_le16(i); | |
eaf8fedc | 1733 | inode.i_flags |= EXT4_EXTENTS_FL; |
a17e9f30 | 1734 | } |
030970ed | 1735 | if (debugfs_write_new_inode(newfile, &inode, argv[0])) { |
50e1e10f TT |
1736 | close(fd); |
1737 | return; | |
1738 | } | |
1739 | if (LINUX_S_ISREG(inode.i_mode)) { | |
c756ec42 RY |
1740 | if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) { |
1741 | make_holes = 1; | |
1742 | /* | |
1743 | * Use I/O blocksize as buffer size when | |
1744 | * copying sparse files. | |
1745 | */ | |
1746 | bufsize = statbuf.st_blksize; | |
1747 | } | |
1748 | retval = copy_file(fd, newfile, bufsize, make_holes); | |
50e1e10f | 1749 | if (retval) |
9b9a780f | 1750 | com_err("copy_file", retval, 0); |
50e1e10f TT |
1751 | } |
1752 | close(fd); | |
1753 | } | |
1754 | ||
1755 | void do_mknod(int argc, char *argv[]) | |
1756 | { | |
54434927 | 1757 | unsigned long mode, major, minor; |
b044c2e0 TT |
1758 | ext2_ino_t newfile; |
1759 | errcode_t retval; | |
50e1e10f | 1760 | struct ext2_inode inode; |
54434927 | 1761 | int filetype, nr; |
50e1e10f TT |
1762 | |
1763 | if (check_fs_open(argv[0])) | |
1764 | return; | |
1765 | if (argc < 3 || argv[2][1]) { | |
e1018eea | 1766 | usage: |
50e1e10f TT |
1767 | com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]"); |
1768 | return; | |
1769 | } | |
1770 | mode = minor = major = 0; | |
1771 | switch (argv[2][0]) { | |
1772 | case 'p': | |
1773 | mode = LINUX_S_IFIFO; | |
085cb19a | 1774 | filetype = EXT2_FT_FIFO; |
50e1e10f TT |
1775 | nr = 3; |
1776 | break; | |
1777 | case 'c': | |
1778 | mode = LINUX_S_IFCHR; | |
085cb19a | 1779 | filetype = EXT2_FT_CHRDEV; |
50e1e10f TT |
1780 | nr = 5; |
1781 | break; | |
1782 | case 'b': | |
1783 | mode = LINUX_S_IFBLK; | |
085cb19a | 1784 | filetype = EXT2_FT_BLKDEV; |
50e1e10f TT |
1785 | nr = 5; |
1786 | break; | |
1787 | default: | |
085cb19a | 1788 | filetype = 0; |
50e1e10f TT |
1789 | nr = 0; |
1790 | } | |
1791 | if (nr == 5) { | |
1792 | major = strtoul(argv[3], argv+3, 0); | |
1793 | minor = strtoul(argv[4], argv+4, 0); | |
2d10769e | 1794 | if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0]) |
50e1e10f TT |
1795 | nr = 0; |
1796 | } | |
e1018eea TT |
1797 | if (argc != nr) |
1798 | goto usage; | |
fc6d9d51 | 1799 | if (check_fs_read_write(argv[0])) |
50e1e10f | 1800 | return; |
fc6d9d51 | 1801 | retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); |
50e1e10f | 1802 | if (retval) { |
9b9a780f | 1803 | com_err(argv[0], retval, 0); |
50e1e10f TT |
1804 | return; |
1805 | } | |
b044c2e0 | 1806 | printf("Allocated inode: %u\n", newfile); |
085cb19a | 1807 | retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype); |
2d9f0804 TT |
1808 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
1809 | retval = ext2fs_expand_dir(current_fs, cwd); | |
50e1e10f | 1810 | if (retval) { |
2d9f0804 | 1811 | com_err(argv[0], retval, "while expanding directory"); |
50e1e10f TT |
1812 | return; |
1813 | } | |
2d9f0804 TT |
1814 | retval = ext2fs_link(current_fs, cwd, argv[1], newfile, |
1815 | filetype); | |
1816 | } | |
1817 | if (retval) { | |
9b9a780f | 1818 | com_err(argv[1], retval, 0); |
2d9f0804 | 1819 | return; |
50e1e10f | 1820 | } |
6d879a99 | 1821 | if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile)) |
50e1e10f | 1822 | com_err(argv[0], 0, "Warning: inode already set"); |
e1e961cb | 1823 | ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0); |
50e1e10f TT |
1824 | memset(&inode, 0, sizeof(inode)); |
1825 | inode.i_mode = mode; | |
efc6f628 | 1826 | inode.i_atime = inode.i_ctime = inode.i_mtime = |
4efae606 | 1827 | current_fs->now ? current_fs->now : time(0); |
2d10769e TT |
1828 | if ((major < 256) && (minor < 256)) { |
1829 | inode.i_block[0] = major*256+minor; | |
1830 | inode.i_block[1] = 0; | |
1831 | } else { | |
1832 | inode.i_block[0] = 0; | |
1833 | inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12); | |
1834 | } | |
50e1e10f | 1835 | inode.i_links_count = 1; |
030970ed | 1836 | if (debugfs_write_new_inode(newfile, &inode, argv[0])) |
50e1e10f | 1837 | return; |
50e1e10f TT |
1838 | } |
1839 | ||
3839e657 TT |
1840 | void do_mkdir(int argc, char *argv[]) |
1841 | { | |
1842 | char *cp; | |
b044c2e0 | 1843 | ext2_ino_t parent; |
3839e657 TT |
1844 | char *name; |
1845 | errcode_t retval; | |
1846 | ||
e1018eea TT |
1847 | if (common_args_process(argc, argv, 2, 2, "mkdir", |
1848 | "<filename>", CHECK_FS_RW)) | |
d3aea7dc TT |
1849 | return; |
1850 | ||
3839e657 TT |
1851 | cp = strrchr(argv[1], '/'); |
1852 | if (cp) { | |
1853 | *cp = 0; | |
1854 | parent = string_to_inode(argv[1]); | |
1855 | if (!parent) { | |
9b9a780f | 1856 | com_err(argv[1], ENOENT, 0); |
3839e657 TT |
1857 | return; |
1858 | } | |
1859 | name = cp+1; | |
1860 | } else { | |
1861 | parent = cwd; | |
1862 | name = argv[1]; | |
1863 | } | |
1864 | ||
2d9f0804 | 1865 | try_again: |
fc6d9d51 | 1866 | retval = ext2fs_mkdir(current_fs, parent, 0, name); |
2d9f0804 TT |
1867 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
1868 | retval = ext2fs_expand_dir(current_fs, parent); | |
1869 | if (retval) { | |
73b05425 | 1870 | com_err(argv[0], retval, "while expanding directory"); |
2d9f0804 TT |
1871 | return; |
1872 | } | |
1873 | goto try_again; | |
1874 | } | |
3839e657 | 1875 | if (retval) { |
9b9a780f | 1876 | com_err("ext2fs_mkdir", retval, 0); |
3839e657 TT |
1877 | return; |
1878 | } | |
1879 | ||
1880 | } | |
1881 | ||
048786d7 VAH |
1882 | static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr, |
1883 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), | |
1884 | blk64_t ref_block EXT2FS_ATTR((unused)), | |
1885 | int ref_offset EXT2FS_ATTR((unused)), | |
54434927 | 1886 | void *private EXT2FS_ATTR((unused))) |
3839e657 | 1887 | { |
048786d7 | 1888 | blk64_t block; |
34436891 TT |
1889 | |
1890 | block = *blocknr; | |
048786d7 | 1891 | ext2fs_block_alloc_stats2(fs, block, -1); |
3839e657 TT |
1892 | return 0; |
1893 | } | |
1894 | ||
b044c2e0 | 1895 | static void kill_file_by_inode(ext2_ino_t inode) |
3839e657 TT |
1896 | { |
1897 | struct ext2_inode inode_buf; | |
1898 | ||
e1018eea TT |
1899 | if (debugfs_read_inode(inode, &inode_buf, 0)) |
1900 | return; | |
4efae606 | 1901 | inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0); |
e1018eea TT |
1902 | if (debugfs_write_inode(inode, &inode_buf, 0)) |
1903 | return; | |
0c80c44b | 1904 | if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) |
9c92d848 | 1905 | return; |
3839e657 | 1906 | |
048786d7 VAH |
1907 | ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL, |
1908 | release_blocks_proc, NULL); | |
521e3685 | 1909 | printf("\n"); |
d7f64ae8 TT |
1910 | ext2fs_inode_alloc_stats2(current_fs, inode, -1, |
1911 | LINUX_S_ISDIR(inode_buf.i_mode)); | |
3839e657 TT |
1912 | } |
1913 | ||
1914 | ||
1915 | void do_kill_file(int argc, char *argv[]) | |
1916 | { | |
b044c2e0 | 1917 | ext2_ino_t inode_num; |
3839e657 | 1918 | |
e1018eea | 1919 | if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW)) |
3839e657 TT |
1920 | return; |
1921 | ||
3839e657 TT |
1922 | kill_file_by_inode(inode_num); |
1923 | } | |
1924 | ||
1925 | void do_rm(int argc, char *argv[]) | |
1926 | { | |
1927 | int retval; | |
b044c2e0 | 1928 | ext2_ino_t inode_num; |
3839e657 TT |
1929 | struct ext2_inode inode; |
1930 | ||
e1018eea TT |
1931 | if (common_args_process(argc, argv, 2, 2, "rm", |
1932 | "<filename>", CHECK_FS_RW)) | |
d3aea7dc TT |
1933 | return; |
1934 | ||
fc6d9d51 | 1935 | retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); |
3839e657 | 1936 | if (retval) { |
91d6d486 | 1937 | com_err(argv[0], retval, "while trying to resolve filename"); |
3839e657 TT |
1938 | return; |
1939 | } | |
1940 | ||
e1018eea | 1941 | if (debugfs_read_inode(inode_num, &inode, argv[0])) |
3839e657 | 1942 | return; |
3839e657 | 1943 | |
50e1e10f | 1944 | if (LINUX_S_ISDIR(inode.i_mode)) { |
3839e657 TT |
1945 | com_err(argv[0], 0, "file is a directory"); |
1946 | return; | |
1947 | } | |
1948 | ||
1949 | --inode.i_links_count; | |
e1018eea | 1950 | if (debugfs_write_inode(inode_num, &inode, argv[0])) |
3839e657 | 1951 | return; |
3839e657 TT |
1952 | |
1953 | unlink_file_by_name(argv[1]); | |
1954 | if (inode.i_links_count == 0) | |
1955 | kill_file_by_inode(inode_num); | |
1956 | } | |
1957 | ||
d7f64ae8 TT |
1958 | struct rd_struct { |
1959 | ext2_ino_t parent; | |
1960 | int empty; | |
1961 | }; | |
1962 | ||
54434927 TT |
1963 | static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), |
1964 | int entry EXT2FS_ATTR((unused)), | |
d7f64ae8 | 1965 | struct ext2_dir_entry *dirent, |
54434927 TT |
1966 | int offset EXT2FS_ATTR((unused)), |
1967 | int blocksize EXT2FS_ATTR((unused)), | |
1968 | char *buf EXT2FS_ATTR((unused)), | |
d7f64ae8 TT |
1969 | void *private) |
1970 | { | |
1971 | struct rd_struct *rds = (struct rd_struct *) private; | |
1972 | ||
1973 | if (dirent->inode == 0) | |
1974 | return 0; | |
1975 | if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.')) | |
1976 | return 0; | |
1977 | if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') && | |
1978 | (dirent->name[1] == '.')) { | |
1979 | rds->parent = dirent->inode; | |
1980 | return 0; | |
1981 | } | |
1982 | rds->empty = 0; | |
1983 | return 0; | |
1984 | } | |
efc6f628 | 1985 | |
d7f64ae8 TT |
1986 | void do_rmdir(int argc, char *argv[]) |
1987 | { | |
1988 | int retval; | |
1989 | ext2_ino_t inode_num; | |
1990 | struct ext2_inode inode; | |
1991 | struct rd_struct rds; | |
1992 | ||
1993 | if (common_args_process(argc, argv, 2, 2, "rmdir", | |
1994 | "<filename>", CHECK_FS_RW)) | |
1995 | return; | |
1996 | ||
1997 | retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); | |
1998 | if (retval) { | |
1999 | com_err(argv[0], retval, "while trying to resolve filename"); | |
2000 | return; | |
2001 | } | |
2002 | ||
2003 | if (debugfs_read_inode(inode_num, &inode, argv[0])) | |
2004 | return; | |
2005 | ||
2006 | if (!LINUX_S_ISDIR(inode.i_mode)) { | |
2007 | com_err(argv[0], 0, "file is not a directory"); | |
2008 | return; | |
2009 | } | |
2010 | ||
2011 | rds.parent = 0; | |
2012 | rds.empty = 1; | |
2013 | ||
2014 | retval = ext2fs_dir_iterate2(current_fs, inode_num, 0, | |
2015 | 0, rmdir_proc, &rds); | |
2016 | if (retval) { | |
2017 | com_err(argv[0], retval, "while iterating over directory"); | |
2018 | return; | |
2019 | } | |
2020 | if (rds.empty == 0) { | |
2021 | com_err(argv[0], 0, "directory not empty"); | |
2022 | return; | |
2023 | } | |
2024 | ||
2025 | inode.i_links_count = 0; | |
2026 | if (debugfs_write_inode(inode_num, &inode, argv[0])) | |
2027 | return; | |
2028 | ||
2029 | unlink_file_by_name(argv[1]); | |
2030 | kill_file_by_inode(inode_num); | |
2031 | ||
2032 | if (rds.parent) { | |
2033 | if (debugfs_read_inode(rds.parent, &inode, argv[0])) | |
2034 | return; | |
2035 | if (inode.i_links_count > 1) | |
2036 | inode.i_links_count--; | |
2037 | if (debugfs_write_inode(rds.parent, &inode, argv[0])) | |
2038 | return; | |
2039 | } | |
2040 | } | |
e88c5a33 | 2041 | #endif /* READ_ONLY */ |
d7f64ae8 | 2042 | |
efc6f628 | 2043 | void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)), |
54434927 | 2044 | char *argv[] EXT2FS_ATTR((unused))) |
3839e657 | 2045 | { |
fc6d9d51 | 2046 | if (current_fs) |
1d520184 ZL |
2047 | printf("Open mode: read-%s\n", |
2048 | current_fs->flags & EXT2_FLAG_RW ? "write" : "only"); | |
2049 | printf("Filesystem in use: %s\n", | |
2050 | current_fs ? current_fs->device_name : "--none--"); | |
3839e657 TT |
2051 | } |
2052 | ||
e88c5a33 | 2053 | #ifndef READ_ONLY |
3839e657 TT |
2054 | void do_expand_dir(int argc, char *argv[]) |
2055 | { | |
b044c2e0 | 2056 | ext2_ino_t inode; |
3839e657 TT |
2057 | int retval; |
2058 | ||
e1018eea | 2059 | if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW)) |
3839e657 TT |
2060 | return; |
2061 | ||
fc6d9d51 | 2062 | retval = ext2fs_expand_dir(current_fs, inode); |
3839e657 | 2063 | if (retval) |
9b9a780f | 2064 | com_err("ext2fs_expand_dir", retval, 0); |
3839e657 TT |
2065 | return; |
2066 | } | |
2067 | ||
d3aea7dc TT |
2068 | void do_features(int argc, char *argv[]) |
2069 | { | |
2070 | int i; | |
efc6f628 | 2071 | |
d3aea7dc TT |
2072 | if (check_fs_open(argv[0])) |
2073 | return; | |
2074 | ||
2075 | if ((argc != 1) && check_fs_read_write(argv[0])) | |
2076 | return; | |
2077 | for (i=1; i < argc; i++) { | |
2078 | if (e2p_edit_feature(argv[i], | |
06968e7e | 2079 | ¤t_fs->super->s_feature_compat, 0)) |
d3aea7dc TT |
2080 | com_err(argv[0], 0, "Unknown feature: %s\n", |
2081 | argv[i]); | |
2082 | else | |
2083 | ext2fs_mark_super_dirty(current_fs); | |
2084 | } | |
5dd8f963 | 2085 | print_features(current_fs->super, stdout); |
d3aea7dc | 2086 | } |
e88c5a33 | 2087 | #endif /* READ_ONLY */ |
d3aea7dc | 2088 | |
b38cd283 TT |
2089 | void do_bmap(int argc, char *argv[]) |
2090 | { | |
2091 | ext2_ino_t ino; | |
048786d7 | 2092 | blk64_t blk, pblk; |
b38cd283 TT |
2093 | int err; |
2094 | errcode_t errcode; | |
efc6f628 | 2095 | |
b38cd283 TT |
2096 | if (common_args_process(argc, argv, 3, 3, argv[0], |
2097 | "<file> logical_blk", 0)) | |
2098 | return; | |
2099 | ||
2100 | ino = string_to_inode(argv[1]); | |
becf36f6 TT |
2101 | if (!ino) |
2102 | return; | |
fe56188b DW |
2103 | err = strtoblk(argv[0], argv[2], &blk); |
2104 | if (err) | |
2105 | return; | |
b38cd283 | 2106 | |
048786d7 | 2107 | errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk); |
b38cd283 | 2108 | if (errcode) { |
50295a3f | 2109 | com_err(argv[0], errcode, |
048786d7 | 2110 | "while mapping logical block %llu\n", blk); |
b38cd283 TT |
2111 | return; |
2112 | } | |
048786d7 | 2113 | printf("%llu\n", pblk); |
b38cd283 TT |
2114 | } |
2115 | ||
becf36f6 TT |
2116 | void do_imap(int argc, char *argv[]) |
2117 | { | |
2118 | ext2_ino_t ino; | |
2119 | unsigned long group, block, block_nr, offset; | |
2120 | ||
2121 | if (common_args_process(argc, argv, 2, 2, argv[0], | |
2122 | "<file>", 0)) | |
2123 | return; | |
2124 | ino = string_to_inode(argv[1]); | |
2125 | if (!ino) | |
88494bb6 | 2126 | return; |
becf36f6 TT |
2127 | |
2128 | group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super); | |
2129 | offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) * | |
2130 | EXT2_INODE_SIZE(current_fs->super); | |
2131 | block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super); | |
048786d7 | 2132 | if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) { |
54434927 | 2133 | com_err(argv[0], 0, "Inode table for group %lu is missing\n", |
becf36f6 TT |
2134 | group); |
2135 | return; | |
2136 | } | |
048786d7 | 2137 | block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) + |
becf36f6 TT |
2138 | block; |
2139 | offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1); | |
2140 | ||
48e6e813 TT |
2141 | printf("Inode %d is part of block group %lu\n" |
2142 | "\tlocated at block %lu, offset 0x%04lx\n", ino, group, | |
becf36f6 TT |
2143 | block_nr, offset); |
2144 | ||
2145 | } | |
2146 | ||
e88c5a33 | 2147 | #ifndef READ_ONLY |
4efae606 TT |
2148 | void do_set_current_time(int argc, char *argv[]) |
2149 | { | |
4efae606 TT |
2150 | time_t now; |
2151 | ||
2152 | if (common_args_process(argc, argv, 2, 2, argv[0], | |
2153 | "<time>", 0)) | |
2154 | return; | |
becf36f6 | 2155 | |
4efae606 TT |
2156 | now = string_to_time(argv[1]); |
2157 | if (now == ((time_t) -1)) { | |
2158 | com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n", | |
2159 | argv[1]); | |
2160 | return; | |
2161 | ||
2162 | } else { | |
2163 | printf("Setting current time to %s\n", time_to_string(now)); | |
2164 | current_fs->now = now; | |
2165 | } | |
2166 | } | |
e88c5a33 | 2167 | #endif /* READ_ONLY */ |
b38cd283 | 2168 | |
03efde8a AD |
2169 | static int find_supp_feature(__u32 *supp, int feature_type, char *name) |
2170 | { | |
2171 | int compat, bit, ret; | |
2172 | unsigned int feature_mask; | |
2173 | ||
2174 | if (name) { | |
2175 | if (feature_type == E2P_FS_FEATURE) | |
2176 | ret = e2p_string2feature(name, &compat, &feature_mask); | |
2177 | else | |
2178 | ret = e2p_jrnl_string2feature(name, &compat, | |
2179 | &feature_mask); | |
2180 | if (ret) | |
2181 | return ret; | |
2182 | ||
2183 | if (!(supp[compat] & feature_mask)) | |
2184 | return 1; | |
2185 | } else { | |
2186 | for (compat = 0; compat < 3; compat++) { | |
2187 | for (bit = 0, feature_mask = 1; bit < 32; | |
2188 | bit++, feature_mask <<= 1) { | |
2189 | if (supp[compat] & feature_mask) { | |
2190 | if (feature_type == E2P_FS_FEATURE) | |
2191 | fprintf(stdout, " %s", | |
2192 | e2p_feature2string(compat, | |
2193 | feature_mask)); | |
2194 | else | |
2195 | fprintf(stdout, " %s", | |
2196 | e2p_jrnl_feature2string(compat, | |
2197 | feature_mask)); | |
2198 | } | |
2199 | } | |
2200 | } | |
2201 | fprintf(stdout, "\n"); | |
2202 | } | |
2203 | ||
2204 | return 0; | |
2205 | } | |
2206 | ||
2207 | void do_supported_features(int argc, char *argv[]) | |
2208 | { | |
42080a86 | 2209 | int ret; |
03efde8a AD |
2210 | __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP, |
2211 | EXT2_LIB_FEATURE_INCOMPAT_SUPP, | |
2212 | EXT2_LIB_FEATURE_RO_COMPAT_SUPP }; | |
2213 | __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES, | |
2214 | JFS_KNOWN_INCOMPAT_FEATURES, | |
2215 | JFS_KNOWN_ROCOMPAT_FEATURES }; | |
2216 | ||
2217 | if (argc > 1) { | |
2218 | ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]); | |
2219 | if (ret) { | |
2220 | ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, | |
2221 | argv[1]); | |
2222 | } | |
2223 | if (ret) | |
2224 | com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]); | |
2225 | else | |
2226 | fprintf(stdout, "Supported feature: %s\n", argv[1]); | |
2227 | } else { | |
2228 | fprintf(stdout, "Supported features:"); | |
2229 | ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL); | |
2230 | ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL); | |
2231 | } | |
2232 | } | |
2233 | ||
e88c5a33 | 2234 | #ifndef READ_ONLY |
86685923 TT |
2235 | void do_punch(int argc, char *argv[]) |
2236 | { | |
2237 | ext2_ino_t ino; | |
2238 | blk64_t start, end; | |
2239 | int err; | |
2240 | errcode_t errcode; | |
2241 | ||
2242 | if (common_args_process(argc, argv, 3, 4, argv[0], | |
2243 | "<file> start_blk [end_blk]", | |
2244 | CHECK_FS_RW | CHECK_FS_BITMAPS)) | |
2245 | return; | |
2246 | ||
2247 | ino = string_to_inode(argv[1]); | |
2248 | if (!ino) | |
2249 | return; | |
fe56188b DW |
2250 | err = strtoblk(argv[0], argv[2], &start); |
2251 | if (err) | |
2252 | return; | |
2253 | if (argc == 4) { | |
2254 | err = strtoblk(argv[0], argv[3], &end); | |
2255 | if (err) | |
2256 | return; | |
2257 | } else | |
86685923 TT |
2258 | end = ~0; |
2259 | ||
2260 | errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end); | |
2261 | ||
2262 | if (errcode) { | |
2263 | com_err(argv[0], errcode, | |
2264 | "while truncating inode %u from %llu to %llu\n", ino, | |
2265 | (unsigned long long) start, (unsigned long long) end); | |
2266 | return; | |
2267 | } | |
2268 | } | |
e88c5a33 | 2269 | #endif /* READ_ONLY */ |
86685923 | 2270 | |
4df6a37b DH |
2271 | void do_symlink(int argc, char *argv[]) |
2272 | { | |
2273 | char *cp; | |
2274 | ext2_ino_t parent; | |
2275 | char *name, *target; | |
2276 | errcode_t retval; | |
2277 | ||
2278 | if (common_args_process(argc, argv, 3, 3, "symlink", | |
2279 | "<filename> <target>", CHECK_FS_RW)) | |
2280 | return; | |
2281 | ||
2282 | cp = strrchr(argv[1], '/'); | |
2283 | if (cp) { | |
2284 | *cp = 0; | |
2285 | parent = string_to_inode(argv[1]); | |
2286 | if (!parent) { | |
2287 | com_err(argv[1], ENOENT, 0); | |
2288 | return; | |
2289 | } | |
2290 | name = cp+1; | |
2291 | } else { | |
2292 | parent = cwd; | |
2293 | name = argv[1]; | |
2294 | } | |
2295 | target = argv[2]; | |
2296 | ||
2297 | try_again: | |
2298 | retval = ext2fs_symlink(current_fs, parent, 0, name, target); | |
2299 | if (retval == EXT2_ET_DIR_NO_SPACE) { | |
2300 | retval = ext2fs_expand_dir(current_fs, parent); | |
2301 | if (retval) { | |
2302 | com_err(argv[0], retval, "while expanding directory"); | |
2303 | return; | |
2304 | } | |
2305 | goto try_again; | |
2306 | } | |
2307 | if (retval) { | |
2308 | com_err("ext2fs_symlink", retval, 0); | |
2309 | return; | |
2310 | } | |
2311 | ||
2312 | } | |
2313 | ||
3cebf9c1 | 2314 | void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[]) |
0f5eba75 | 2315 | { |
7105c183 | 2316 | struct ext2_super_block *sb; |
0f5eba75 AD |
2317 | struct mmp_struct *mmp_s; |
2318 | time_t t; | |
2319 | errcode_t retval = 0; | |
2320 | ||
7105c183 ES |
2321 | if (check_fs_open(argv[0])) |
2322 | return; | |
2323 | ||
2324 | sb = current_fs->super; | |
0f5eba75 AD |
2325 | |
2326 | if (current_fs->mmp_buf == NULL) { | |
2327 | retval = ext2fs_get_mem(current_fs->blocksize, | |
2328 | ¤t_fs->mmp_buf); | |
2329 | if (retval) { | |
2330 | com_err(argv[0], retval, "allocating MMP buffer.\n"); | |
2331 | return; | |
2332 | } | |
2333 | } | |
2334 | ||
2335 | mmp_s = current_fs->mmp_buf; | |
2336 | ||
2337 | retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block, | |
2338 | current_fs->mmp_buf); | |
2339 | if (retval) { | |
2340 | com_err(argv[0], retval, "reading MMP block.\n"); | |
2341 | return; | |
2342 | } | |
2343 | ||
2344 | t = mmp_s->mmp_time; | |
2345 | fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block); | |
2346 | fprintf(stdout, "update_interval: %d\n", | |
2347 | current_fs->super->s_mmp_update_interval); | |
2348 | fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval); | |
2349 | fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq); | |
2350 | fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t)); | |
2351 | fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename); | |
2352 | fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname); | |
7105c183 | 2353 | fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic); |
0f5eba75 AD |
2354 | } |
2355 | ||
9e85208e | 2356 | static int source_file(const char *cmd_file, int ss_idx) |
fc6d9d51 TT |
2357 | { |
2358 | FILE *f; | |
355ffb2f | 2359 | char buf[BUFSIZ]; |
fc6d9d51 TT |
2360 | char *cp; |
2361 | int exit_status = 0; | |
2362 | int retval; | |
2363 | ||
2364 | if (strcmp(cmd_file, "-") == 0) | |
2365 | f = stdin; | |
2366 | else { | |
2367 | f = fopen(cmd_file, "r"); | |
2368 | if (!f) { | |
2369 | perror(cmd_file); | |
2370 | exit(1); | |
2371 | } | |
2372 | } | |
2a7bfe83 TT |
2373 | fflush(stdout); |
2374 | fflush(stderr); | |
fc6d9d51 TT |
2375 | setbuf(stdout, NULL); |
2376 | setbuf(stderr, NULL); | |
2377 | while (!feof(f)) { | |
2378 | if (fgets(buf, sizeof(buf), f) == NULL) | |
2379 | break; | |
2380 | cp = strchr(buf, '\n'); | |
2381 | if (cp) | |
2382 | *cp = 0; | |
2383 | cp = strchr(buf, '\r'); | |
2384 | if (cp) | |
2385 | *cp = 0; | |
2386 | printf("debugfs: %s\n", buf); | |
9e85208e | 2387 | retval = ss_execute_line(ss_idx, buf); |
fc6d9d51 | 2388 | if (retval) { |
9e85208e | 2389 | ss_perror(ss_idx, retval, buf); |
fc6d9d51 TT |
2390 | exit_status++; |
2391 | } | |
2392 | } | |
2d7ef236 PT |
2393 | if (f != stdin) |
2394 | fclose(f); | |
fc6d9d51 TT |
2395 | return exit_status; |
2396 | } | |
2397 | ||
a8859cad | 2398 | int main(int argc, char **argv) |
3839e657 | 2399 | { |
50e1e10f | 2400 | int retval; |
e88c5a33 TT |
2401 | const char *usage = |
2402 | "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] " | |
2403 | "[-R request] [-V] [" | |
2404 | #ifndef READ_ONLY | |
2405 | "[-w] " | |
2406 | #endif | |
2407 | "[-c] device]"; | |
f1304811 | 2408 | int c; |
03b9dca6 | 2409 | int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; |
fc6d9d51 TT |
2410 | char *request = 0; |
2411 | int exit_status = 0; | |
2412 | char *cmd_file = 0; | |
048786d7 VAH |
2413 | blk64_t superblock = 0; |
2414 | blk64_t blocksize = 0; | |
2e8d40d5 | 2415 | int catastrophic = 0; |
1ad54a94 | 2416 | char *data_filename = 0; |
e88c5a33 TT |
2417 | #ifdef READ_ONLY |
2418 | const char *opt_string = "icR:f:b:s:Vd:D"; | |
2419 | #else | |
2420 | const char *opt_string = "iwcR:f:b:s:Vd:D"; | |
2421 | #endif | |
efc6f628 | 2422 | |
49ce6cb5 | 2423 | if (debug_prog_name == 0) |
e88c5a33 TT |
2424 | #ifdef READ_ONLY |
2425 | debug_prog_name = "rdebugfs"; | |
2426 | #else | |
49ce6cb5 | 2427 | debug_prog_name = "debugfs"; |
e88c5a33 | 2428 | #endif |
a6d8302b | 2429 | add_error_table(&et_ext2_error_table); |
49ce6cb5 TT |
2430 | fprintf (stderr, "%s %s (%s)\n", debug_prog_name, |
2431 | E2FSPROGS_VERSION, E2FSPROGS_DATE); | |
3839e657 | 2432 | |
e88c5a33 | 2433 | while ((c = getopt (argc, argv, opt_string)) != EOF) { |
3839e657 | 2434 | switch (c) { |
fc6d9d51 TT |
2435 | case 'R': |
2436 | request = optarg; | |
2437 | break; | |
2438 | case 'f': | |
2439 | cmd_file = optarg; | |
2440 | break; | |
1ad54a94 TT |
2441 | case 'd': |
2442 | data_filename = optarg; | |
2443 | break; | |
59cf7e0d TT |
2444 | case 'i': |
2445 | open_flags |= EXT2_FLAG_IMAGE_FILE; | |
2446 | break; | |
e88c5a33 | 2447 | #ifndef READ_ONLY |
3839e657 | 2448 | case 'w': |
59cf7e0d | 2449 | open_flags |= EXT2_FLAG_RW; |
3839e657 | 2450 | break; |
e88c5a33 | 2451 | #endif |
0fd68e02 TT |
2452 | case 'D': |
2453 | open_flags |= EXT2_FLAG_DIRECT_IO; | |
2454 | break; | |
2e8d40d5 | 2455 | case 'b': |
efc6f628 | 2456 | blocksize = parse_ulong(optarg, argv[0], |
e1018eea | 2457 | "block size", 0); |
2e8d40d5 TT |
2458 | break; |
2459 | case 's': | |
fe56188b | 2460 | retval = strtoblk(argv[0], optarg, &superblock); |
32541fe4 | 2461 | if (retval) |
fe56188b | 2462 | return 1; |
2e8d40d5 TT |
2463 | break; |
2464 | case 'c': | |
2465 | catastrophic = 1; | |
2466 | break; | |
818180cd TT |
2467 | case 'V': |
2468 | /* Print version number and exit */ | |
2469 | fprintf(stderr, "\tUsing %s\n", | |
2470 | error_message(EXT2_ET_BASE)); | |
2471 | exit(0); | |
3839e657 | 2472 | default: |
49ce6cb5 | 2473 | com_err(argv[0], 0, usage, debug_prog_name); |
b4ac9cc3 | 2474 | return 1; |
3839e657 TT |
2475 | } |
2476 | } | |
2477 | if (optind < argc) | |
2e8d40d5 | 2478 | open_filesystem(argv[optind], open_flags, |
1ad54a94 TT |
2479 | superblock, blocksize, catastrophic, |
2480 | data_filename); | |
efc6f628 | 2481 | |
49ce6cb5 | 2482 | sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL, |
3839e657 TT |
2483 | &debug_cmds, &retval); |
2484 | if (retval) { | |
2485 | ss_perror(sci_idx, retval, "creating invocation"); | |
2486 | exit(1); | |
2487 | } | |
3ae497ea | 2488 | ss_get_readline(sci_idx); |
3839e657 TT |
2489 | |
2490 | (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval); | |
2491 | if (retval) { | |
2492 | ss_perror(sci_idx, retval, "adding standard requests"); | |
2493 | exit (1); | |
2494 | } | |
49ce6cb5 TT |
2495 | if (extra_cmds) |
2496 | ss_add_request_table (sci_idx, extra_cmds, 1, &retval); | |
2497 | if (retval) { | |
2498 | ss_perror(sci_idx, retval, "adding extra requests"); | |
2499 | exit (1); | |
2500 | } | |
fc6d9d51 TT |
2501 | if (request) { |
2502 | retval = 0; | |
2503 | retval = ss_execute_line(sci_idx, request); | |
2504 | if (retval) { | |
2505 | ss_perror(sci_idx, retval, request); | |
2506 | exit_status++; | |
2507 | } | |
2508 | } else if (cmd_file) { | |
2509 | exit_status = source_file(cmd_file, sci_idx); | |
2510 | } else { | |
2511 | ss_listen(sci_idx); | |
2512 | } | |
3839e657 | 2513 | |
6e9761c2 TT |
2514 | ss_delete_invocation(sci_idx); |
2515 | ||
fc6d9d51 | 2516 | if (current_fs) |
3839e657 | 2517 | close_filesystem(); |
efc6f628 | 2518 | |
a6d8302b | 2519 | remove_error_table(&et_ext2_error_table); |
e597304a | 2520 | return exit_status; |
3839e657 | 2521 | } |