]>
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. | |
7 | * | |
8 | * Modifications by Robert Sanders <gt8134b@prism.gatech.edu> | |
9 | */ | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <unistd.h> | |
13 | #include <stdlib.h> | |
14 | #include <ctype.h> | |
15 | #include <string.h> | |
16 | #include <time.h> | |
50e1e10f | 17 | #ifdef HAVE_GETOPT_H |
3839e657 | 18 | #include <getopt.h> |
50e1e10f TT |
19 | #else |
20 | extern int optind; | |
21 | extern char *optarg; | |
22 | #endif | |
23 | #ifdef HAVE_OPTRESET | |
24 | extern int optreset; /* defined by BSD, but not others */ | |
25 | #endif | |
26 | #ifdef HAVE_ERRNO_H | |
27 | #include <errno.h> | |
28 | #endif | |
29 | #include <fcntl.h> | |
3839e657 TT |
30 | #include <sys/types.h> |
31 | #include <sys/stat.h> | |
32 | ||
33 | #include "et/com_err.h" | |
34 | #include "ss/ss.h" | |
35 | #include "debugfs.h" | |
fc6d9d51 | 36 | #include "uuid/uuid.h" |
f68aa414 | 37 | #include "e2p/e2p.h" |
3839e657 | 38 | |
818180cd TT |
39 | #include "../version.h" |
40 | ||
3839e657 TT |
41 | extern ss_request_table debug_cmds; |
42 | ||
fc6d9d51 | 43 | ext2_filsys current_fs = NULL; |
3839e657 TT |
44 | ino_t root, cwd; |
45 | ||
2e8d40d5 TT |
46 | static void open_filesystem(char *device, int open_flags, blk_t superblock, |
47 | blk_t blocksize, int catastrophic) | |
3839e657 TT |
48 | { |
49 | int retval; | |
2e8d40d5 TT |
50 | |
51 | if (superblock != 0 && blocksize == 0) { | |
52 | com_err(device, 0, "if you specify the superblock, you must also specify the block size"); | |
53 | current_fs = NULL; | |
54 | return; | |
55 | } | |
56 | ||
57 | if (catastrophic && (open_flags & EXT2_FLAG_RW)) { | |
58 | com_err(device, 0, | |
59 | "opening read-only because of catastrophic mode"); | |
60 | open_flags &= ~EXT2_FLAG_RW; | |
61 | } | |
3839e657 | 62 | |
2e8d40d5 | 63 | retval = ext2fs_open(device, open_flags, superblock, blocksize, |
fc6d9d51 | 64 | unix_io_manager, ¤t_fs); |
3839e657 TT |
65 | if (retval) { |
66 | com_err(device, retval, "while opening filesystem"); | |
fc6d9d51 | 67 | current_fs = NULL; |
3839e657 TT |
68 | return; |
69 | } | |
2e8d40d5 TT |
70 | |
71 | if (catastrophic) | |
72 | com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps"); | |
73 | else { | |
74 | retval = ext2fs_read_inode_bitmap(current_fs); | |
75 | if (retval) { | |
76 | com_err(device, retval, "while reading inode bitmap"); | |
77 | goto errout; | |
78 | } | |
79 | retval = ext2fs_read_block_bitmap(current_fs); | |
80 | if (retval) { | |
81 | com_err(device, retval, "while reading block bitmap"); | |
82 | goto errout; | |
83 | } | |
3839e657 TT |
84 | } |
85 | root = cwd = EXT2_ROOT_INO; | |
86 | return; | |
87 | ||
88 | errout: | |
fc6d9d51 | 89 | retval = ext2fs_close(current_fs); |
3839e657 TT |
90 | if (retval) |
91 | com_err(device, retval, "while trying to close filesystem"); | |
fc6d9d51 | 92 | current_fs = NULL; |
3839e657 TT |
93 | } |
94 | ||
95 | void do_open_filesys(int argc, char **argv) | |
96 | { | |
2e8d40d5 | 97 | const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>"; |
f1304811 | 98 | int c; |
2e8d40d5 TT |
99 | int catastrophic = 0; |
100 | blk_t superblock = 0; | |
101 | blk_t blocksize = 0; | |
102 | char *tmp; | |
3839e657 TT |
103 | int open_flags = 0; |
104 | ||
fc6d9d51 | 105 | optind = 0; |
50e1e10f TT |
106 | #ifdef HAVE_OPTRESET |
107 | optreset = 1; /* Makes BSD getopt happy */ | |
108 | #endif | |
2e8d40d5 | 109 | while ((c = getopt (argc, argv, "wfcb:s:")) != EOF) { |
3839e657 TT |
110 | switch (c) { |
111 | case 'w': | |
d3aea7dc TT |
112 | open_flags |= EXT2_FLAG_RW; |
113 | break; | |
114 | case 'f': | |
115 | open_flags |= EXT2_FLAG_FORCE; | |
3839e657 | 116 | break; |
2e8d40d5 TT |
117 | case 'c': |
118 | catastrophic = 1; | |
119 | break; | |
120 | case 'b': | |
121 | blocksize = strtoul(optarg, &tmp, 0); | |
122 | if (*tmp) { | |
123 | com_err(argv[0], 0, | |
124 | "Bad block size - %s", optarg); | |
125 | return; | |
126 | } | |
127 | break; | |
128 | case 's': | |
129 | superblock = strtoul(optarg, &tmp, 0); | |
130 | if (*tmp) { | |
131 | com_err(argv[0], 0, | |
132 | "Bad superblock number - %s", optarg); | |
133 | return; | |
134 | } | |
135 | break; | |
3839e657 TT |
136 | default: |
137 | com_err(argv[0], 0, usage); | |
138 | return; | |
139 | } | |
140 | } | |
141 | if (optind != argc-1) { | |
142 | com_err(argv[0], 0, usage); | |
143 | return; | |
144 | } | |
145 | if (check_fs_not_open(argv[0])) | |
146 | return; | |
2e8d40d5 TT |
147 | open_filesystem(argv[optind], open_flags, |
148 | superblock, blocksize, catastrophic); | |
149 | } | |
150 | ||
151 | void do_lcd(int argc, char **argv) | |
152 | { | |
153 | const char *usage = "Usage: lcd <native dir>"; | |
154 | ||
155 | if (argc != 2) { | |
156 | com_err(argv[0], 0, usage); | |
157 | return; | |
158 | } | |
159 | ||
160 | if (chdir(argv[1]) == -1) { | |
161 | com_err(argv[0], errno, | |
162 | "while trying to change native directory to %s", | |
163 | argv[1]); | |
164 | return; | |
165 | } | |
3839e657 TT |
166 | } |
167 | ||
50e1e10f | 168 | static void close_filesystem(NOARGS) |
3839e657 TT |
169 | { |
170 | int retval; | |
171 | ||
fc6d9d51 TT |
172 | if (current_fs->flags & EXT2_FLAG_IB_DIRTY) { |
173 | retval = ext2fs_write_inode_bitmap(current_fs); | |
3839e657 TT |
174 | if (retval) |
175 | com_err("ext2fs_write_inode_bitmap", retval, ""); | |
176 | } | |
fc6d9d51 TT |
177 | if (current_fs->flags & EXT2_FLAG_BB_DIRTY) { |
178 | retval = ext2fs_write_block_bitmap(current_fs); | |
3839e657 TT |
179 | if (retval) |
180 | com_err("ext2fs_write_block_bitmap", retval, ""); | |
181 | } | |
fc6d9d51 | 182 | retval = ext2fs_close(current_fs); |
3839e657 TT |
183 | if (retval) |
184 | com_err("ext2fs_close", retval, ""); | |
fc6d9d51 | 185 | current_fs = NULL; |
3839e657 TT |
186 | return; |
187 | } | |
188 | ||
189 | void do_close_filesys(int argc, char **argv) | |
190 | { | |
191 | if (argc > 1) { | |
192 | com_err(argv[0], 0, "Usage: close_filesys"); | |
193 | return; | |
194 | } | |
195 | if (check_fs_open(argv[0])) | |
196 | return; | |
197 | close_filesystem(); | |
198 | } | |
199 | ||
200 | void do_init_filesys(int argc, char **argv) | |
201 | { | |
50e1e10f | 202 | const char *usage = "Usage: initialize <device> <blocksize>"; |
3839e657 TT |
203 | struct ext2_super_block param; |
204 | errcode_t retval; | |
205 | char *tmp; | |
206 | ||
207 | if (argc != 3) { | |
208 | com_err(argv[0], 0, usage); | |
209 | return; | |
210 | } | |
211 | if (check_fs_not_open(argv[0])) | |
212 | return; | |
213 | ||
214 | memset(¶m, 0, sizeof(struct ext2_super_block)); | |
215 | param.s_blocks_count = strtoul(argv[2], &tmp, 0); | |
216 | if (*tmp) { | |
217 | com_err(argv[0], 0, "Bad blocks count - %s", argv[2]); | |
218 | return; | |
219 | } | |
fc6d9d51 TT |
220 | retval = ext2fs_initialize(argv[1], 0, ¶m, |
221 | unix_io_manager, ¤t_fs); | |
3839e657 TT |
222 | if (retval) { |
223 | com_err(argv[1], retval, "while initializing filesystem"); | |
fc6d9d51 | 224 | current_fs = NULL; |
3839e657 TT |
225 | return; |
226 | } | |
227 | root = cwd = EXT2_ROOT_INO; | |
228 | return; | |
229 | } | |
230 | ||
5dd8f963 | 231 | static void print_features(struct ext2_super_block * s, FILE *f) |
d3aea7dc | 232 | { |
d3aea7dc TT |
233 | int i, j, printed=0; |
234 | __u32 *mask = &s->s_feature_compat, m; | |
235 | ||
236 | printf ("Filesystem features:"); | |
237 | for (i=0; i <3; i++,mask++) { | |
238 | for (j=0,m=1; j < 32; j++, m<<=1) { | |
239 | if (*mask & m) { | |
240 | fprintf(f, " %s", e2p_feature2string(i, m)); | |
241 | printed++; | |
242 | } | |
243 | } | |
244 | } | |
245 | if (printed == 0) | |
246 | printf("(none)"); | |
247 | printf("\n"); | |
d3aea7dc TT |
248 | } |
249 | ||
3839e657 TT |
250 | void do_show_super_stats(int argc, char *argv[]) |
251 | { | |
252 | int i; | |
253 | FILE *out; | |
fc6d9d51 | 254 | struct ext2_group_desc *gdp; |
d3aea7dc | 255 | int c, header_only = 0; |
fc6d9d51 TT |
256 | char buf[80]; |
257 | const char *none = "(none)"; | |
d3aea7dc | 258 | const char *usage = "Usage: show_super [-h]"; |
3839e657 | 259 | |
d3aea7dc TT |
260 | optind = 0; |
261 | #ifdef HAVE_OPTRESET | |
262 | optreset = 1; /* Makes BSD getopt happy */ | |
263 | #endif | |
264 | while ((c = getopt (argc, argv, "h")) != EOF) { | |
265 | switch (c) { | |
266 | case 'h': | |
267 | header_only++; | |
268 | break; | |
269 | default: | |
270 | com_err(argv[0], 0, usage); | |
271 | return; | |
272 | } | |
273 | } | |
274 | if (optind != argc) { | |
275 | com_err(argv[0], 0, usage); | |
3839e657 TT |
276 | return; |
277 | } | |
278 | if (check_fs_open(argv[0])) | |
279 | return; | |
280 | out = open_pager(); | |
bd09eff9 TT |
281 | |
282 | list_super2(current_fs->super, out); | |
d3aea7dc TT |
283 | |
284 | if (header_only) { | |
285 | close_pager(out); | |
286 | return; | |
287 | } | |
fc6d9d51 TT |
288 | |
289 | gdp = ¤t_fs->group_desc[0]; | |
290 | for (i = 0; i < current_fs->group_desc_count; i++, gdp++) | |
50e1e10f TT |
291 | fprintf(out, " Group %2d: block bitmap at %d, " |
292 | "inode bitmap at %d, " | |
293 | "inode table at %d\n" | |
3839e657 TT |
294 | " %d free block%s, " |
295 | "%d free inode%s, " | |
296 | "%d used director%s\n", | |
fc6d9d51 TT |
297 | i, gdp->bg_block_bitmap, |
298 | gdp->bg_inode_bitmap, gdp->bg_inode_table, | |
299 | gdp->bg_free_blocks_count, | |
300 | gdp->bg_free_blocks_count != 1 ? "s" : "", | |
301 | gdp->bg_free_inodes_count, | |
302 | gdp->bg_free_inodes_count != 1 ? "s" : "", | |
303 | gdp->bg_used_dirs_count, | |
304 | gdp->bg_used_dirs_count != 1 ? "ies" : "y"); | |
3839e657 TT |
305 | close_pager(out); |
306 | } | |
307 | ||
4a31c48b | 308 | void do_dirty_filesys(int argc, char **argv) |
21c84b71 TT |
309 | { |
310 | if (check_fs_open(argv[0])) | |
311 | return; | |
601002bd TT |
312 | if (check_fs_read_write(argv[0])) |
313 | return; | |
314 | ||
315 | if (argv[1] && !strcmp(argv[1], "-clean")) | |
316 | current_fs->super->s_state |= EXT2_VALID_FS; | |
317 | else | |
318 | current_fs->super->s_state &= ~EXT2_VALID_FS; | |
21c84b71 TT |
319 | ext2fs_mark_super_dirty(current_fs); |
320 | } | |
321 | ||
3839e657 TT |
322 | struct list_blocks_struct { |
323 | FILE *f; | |
324 | int total; | |
0a3db93a TT |
325 | blk_t first_block, last_block; |
326 | int first_bcnt, last_bcnt; | |
327 | int first; | |
3839e657 TT |
328 | }; |
329 | ||
0a3db93a TT |
330 | static void finish_range(struct list_blocks_struct *lb) |
331 | { | |
332 | if (lb->first_block == 0) | |
333 | return; | |
334 | if (lb->first) | |
335 | lb->first = 0; | |
336 | else | |
80bfaa3e | 337 | fprintf(lb->f, ", "); |
0a3db93a TT |
338 | if (lb->first_block == lb->last_block) |
339 | fprintf(lb->f, "(%d):%d", lb->first_bcnt, lb->first_block); | |
340 | else | |
341 | fprintf(lb->f, "(%d-%d):%d-%d", lb->first_bcnt, | |
342 | lb->last_bcnt, lb->first_block, lb->last_block); | |
343 | lb->first_block = 0; | |
344 | } | |
345 | ||
50e1e10f TT |
346 | static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt, |
347 | void *private) | |
3839e657 TT |
348 | { |
349 | struct list_blocks_struct *lb = (struct list_blocks_struct *) private; | |
350 | ||
0a3db93a TT |
351 | lb->total++; |
352 | if (blockcnt >= 0) { | |
353 | /* | |
354 | * See if we can add on to the existing range (if it exists) | |
355 | */ | |
356 | if (lb->first_block && | |
357 | (lb->last_block+1 == *blocknr) && | |
358 | (lb->last_bcnt+1 == blockcnt)) { | |
359 | lb->last_block = *blocknr; | |
360 | lb->last_bcnt = blockcnt; | |
361 | return 0; | |
362 | } | |
363 | /* | |
364 | * Start a new range. | |
365 | */ | |
366 | finish_range(lb); | |
367 | lb->first_block = lb->last_block = *blocknr; | |
368 | lb->first_bcnt = lb->last_bcnt = blockcnt; | |
369 | return 0; | |
370 | } | |
371 | /* | |
372 | * Not a normal block. Always force a new range. | |
373 | */ | |
374 | finish_range(lb); | |
375 | if (lb->first) | |
376 | lb->first = 0; | |
377 | else | |
2c4a5406 | 378 | fprintf(lb->f, ", "); |
a5eef73b | 379 | if (blockcnt == -1) |
0a3db93a | 380 | fprintf(lb->f, "(IND):%d", *blocknr); |
a5eef73b | 381 | else if (blockcnt == -2) |
0a3db93a | 382 | fprintf(lb->f, "(DIND):%d", *blocknr); |
a5eef73b | 383 | else if (blockcnt == -3) |
0a3db93a | 384 | fprintf(lb->f, "(TIND):%d", *blocknr); |
3839e657 TT |
385 | return 0; |
386 | } | |
387 | ||
388 | ||
50e1e10f | 389 | static void dump_blocks(FILE *f, ino_t inode) |
3839e657 TT |
390 | { |
391 | struct list_blocks_struct lb; | |
392 | ||
393 | fprintf(f, "BLOCKS:\n"); | |
394 | lb.total = 0; | |
0a3db93a | 395 | lb.first_block = 0; |
3839e657 | 396 | lb.f = f; |
0a3db93a | 397 | lb.first = 1; |
fc6d9d51 TT |
398 | ext2fs_block_iterate(current_fs, inode, 0, NULL, |
399 | list_blocks_proc, (void *)&lb); | |
0a3db93a | 400 | finish_range(&lb); |
3839e657 TT |
401 | if (lb.total) |
402 | fprintf(f, "\nTOTAL: %d\n", lb.total); | |
403 | fprintf(f,"\n"); | |
404 | } | |
405 | ||
406 | ||
50e1e10f | 407 | static void dump_inode(ino_t inode_num, struct ext2_inode inode) |
3839e657 | 408 | { |
50e1e10f | 409 | const char *i_type; |
3839e657 | 410 | FILE *out; |
fc6d9d51 TT |
411 | char frag, fsize; |
412 | int os = current_fs->super->s_creator_os; | |
3839e657 TT |
413 | |
414 | out = open_pager(); | |
50e1e10f TT |
415 | if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory"; |
416 | else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular"; | |
417 | else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink"; | |
418 | else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special"; | |
419 | else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special"; | |
420 | else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO"; | |
421 | else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket"; | |
3839e657 TT |
422 | else i_type = "bad type"; |
423 | fprintf(out, "Inode: %ld Type: %s ", inode_num, i_type); | |
14197170 | 424 | fprintf(out, "Mode: %04o Flags: 0x%x Generation: %u\n", |
b41d360b | 425 | inode.i_mode & 0777, inode.i_flags, inode.i_generation); |
36a43d67 TT |
426 | fprintf(out, "User: %5d Group: %5d Size: ", |
427 | inode.i_uid, inode.i_gid); | |
428 | if (LINUX_S_ISDIR(inode.i_mode)) | |
429 | fprintf(out, "%d\n", inode.i_size); | |
430 | else { | |
431 | __u64 i_size = (inode.i_size | | |
432 | ((unsigned long long)inode.i_size_high << 32)); | |
433 | ||
434 | fprintf(out, "%lld\n", i_size); | |
435 | } | |
fc6d9d51 | 436 | if (current_fs->super->s_creator_os == EXT2_OS_HURD) |
62c06f79 TT |
437 | fprintf(out, |
438 | "File ACL: %d Directory ACL: %d Translator: %d\n", | |
36a43d67 | 439 | inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0, |
62c06f79 TT |
440 | inode.osd1.hurd1.h_i_translator); |
441 | else | |
442 | fprintf(out, "File ACL: %d Directory ACL: %d\n", | |
36a43d67 | 443 | inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0); |
50e1e10f | 444 | fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count, |
3839e657 | 445 | inode.i_blocks); |
fc6d9d51 TT |
446 | switch (os) { |
447 | case EXT2_OS_LINUX: | |
448 | frag = inode.osd2.linux2.l_i_frag; | |
449 | fsize = inode.osd2.linux2.l_i_fsize; | |
450 | break; | |
451 | case EXT2_OS_HURD: | |
452 | frag = inode.osd2.hurd2.h_i_frag; | |
453 | fsize = inode.osd2.hurd2.h_i_fsize; | |
454 | break; | |
455 | case EXT2_OS_MASIX: | |
456 | frag = inode.osd2.masix2.m_i_frag; | |
457 | fsize = inode.osd2.masix2.m_i_fsize; | |
458 | break; | |
459 | default: | |
460 | frag = fsize = 0; | |
461 | } | |
50e1e10f | 462 | fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n", |
fc6d9d51 | 463 | inode.i_faddr, frag, fsize); |
50e1e10f | 464 | fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime, |
fc6d9d51 | 465 | time_to_string(inode.i_ctime)); |
50e1e10f | 466 | fprintf(out, "atime: 0x%08x -- %s", inode.i_atime, |
fc6d9d51 | 467 | time_to_string(inode.i_atime)); |
50e1e10f | 468 | fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime, |
fc6d9d51 | 469 | time_to_string(inode.i_mtime)); |
3839e657 | 470 | if (inode.i_dtime) |
50e1e10f | 471 | fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime, |
fc6d9d51 | 472 | time_to_string(inode.i_dtime)); |
50e1e10f | 473 | if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0) |
0a3db93a TT |
474 | fprintf(out, "Fast_link_dest: %.*s\n", |
475 | inode.i_size, (char *)inode.i_block); | |
3839e657 TT |
476 | else |
477 | dump_blocks(out, inode_num); | |
478 | close_pager(out); | |
479 | } | |
480 | ||
481 | ||
482 | void do_stat(int argc, char *argv[]) | |
483 | { | |
484 | ino_t inode; | |
485 | struct ext2_inode inode_buf; | |
486 | int retval; | |
487 | ||
488 | if (argc != 2) { | |
489 | com_err(argv[0], 0, "Usage: stat <file>"); | |
490 | return; | |
491 | } | |
492 | if (check_fs_open(argv[0])) | |
493 | return; | |
494 | inode = string_to_inode(argv[1]); | |
495 | if (!inode) | |
496 | return; | |
497 | ||
fc6d9d51 | 498 | retval = ext2fs_read_inode(current_fs, inode, &inode_buf); |
3839e657 TT |
499 | if (retval) |
500 | { | |
91d6d486 | 501 | com_err(argv[0], retval, "while trying to read inode %d", inode); |
3839e657 TT |
502 | return; |
503 | } | |
504 | ||
505 | dump_inode(inode,inode_buf); | |
506 | return; | |
507 | } | |
508 | ||
509 | void do_chroot(int argc, char *argv[]) | |
510 | { | |
511 | ino_t inode; | |
512 | int retval; | |
513 | ||
514 | if (argc != 2) { | |
515 | com_err(argv[0], 0, "Usage: chroot <file>"); | |
516 | return; | |
517 | } | |
518 | if (check_fs_open(argv[0])) | |
519 | return; | |
520 | inode = string_to_inode(argv[1]); | |
521 | if (!inode) | |
522 | return; | |
523 | ||
fc6d9d51 | 524 | retval = ext2fs_check_directory(current_fs, inode); |
3839e657 TT |
525 | if (retval) { |
526 | com_err(argv[1], retval, ""); | |
527 | return; | |
528 | } | |
529 | root = inode; | |
530 | } | |
531 | ||
532 | void do_clri(int argc, char *argv[]) | |
533 | { | |
534 | ino_t inode; | |
535 | int retval; | |
536 | struct ext2_inode inode_buf; | |
537 | ||
538 | if (argc != 2) { | |
539 | com_err(argv[0], 0, "Usage: clri <file>"); | |
540 | return; | |
541 | } | |
542 | if (check_fs_open(argv[0])) | |
543 | return; | |
fc6d9d51 | 544 | if (check_fs_read_write(argv[0])) |
3839e657 | 545 | return; |
3839e657 TT |
546 | inode = string_to_inode(argv[1]); |
547 | if (!inode) | |
548 | return; | |
549 | ||
fc6d9d51 | 550 | retval = ext2fs_read_inode(current_fs, inode, &inode_buf); |
3839e657 | 551 | if (retval) { |
91d6d486 TT |
552 | com_err(argv[0], retval, "while trying to read inode %d", |
553 | inode); | |
3839e657 TT |
554 | return; |
555 | } | |
556 | memset(&inode_buf, 0, sizeof(inode_buf)); | |
fc6d9d51 | 557 | retval = ext2fs_write_inode(current_fs, inode, &inode_buf); |
3839e657 TT |
558 | if (retval) { |
559 | com_err(argv[0], retval, "while trying to write inode %d", | |
560 | inode); | |
561 | return; | |
562 | } | |
563 | } | |
564 | ||
565 | void do_freei(int argc, char *argv[]) | |
566 | { | |
567 | ino_t inode; | |
568 | ||
569 | if (argc != 2) { | |
570 | com_err(argv[0], 0, "Usage: freei <file>"); | |
571 | return; | |
572 | } | |
573 | if (check_fs_open(argv[0])) | |
574 | return; | |
fc6d9d51 | 575 | if (check_fs_read_write(argv[0])) |
3839e657 | 576 | return; |
3839e657 TT |
577 | inode = string_to_inode(argv[1]); |
578 | if (!inode) | |
579 | return; | |
580 | ||
fc6d9d51 | 581 | if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode)) |
3839e657 | 582 | com_err(argv[0], 0, "Warning: inode already clear"); |
fc6d9d51 TT |
583 | ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode); |
584 | ext2fs_mark_ib_dirty(current_fs); | |
3839e657 TT |
585 | } |
586 | ||
587 | void do_seti(int argc, char *argv[]) | |
588 | { | |
589 | ino_t inode; | |
590 | ||
591 | if (argc != 2) { | |
592 | com_err(argv[0], 0, "Usage: seti <file>"); | |
593 | return; | |
594 | } | |
595 | if (check_fs_open(argv[0])) | |
596 | return; | |
fc6d9d51 | 597 | if (check_fs_read_write(argv[0])) |
3839e657 | 598 | return; |
3839e657 TT |
599 | inode = string_to_inode(argv[1]); |
600 | if (!inode) | |
601 | return; | |
602 | ||
fc6d9d51 | 603 | if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode)) |
3839e657 | 604 | com_err(argv[0], 0, "Warning: inode already set"); |
fc6d9d51 TT |
605 | ext2fs_mark_inode_bitmap(current_fs->inode_map,inode); |
606 | ext2fs_mark_ib_dirty(current_fs); | |
3839e657 TT |
607 | } |
608 | ||
609 | void do_testi(int argc, char *argv[]) | |
610 | { | |
611 | ino_t inode; | |
612 | ||
613 | if (argc != 2) { | |
614 | com_err(argv[0], 0, "Usage: testi <file>"); | |
615 | return; | |
616 | } | |
617 | if (check_fs_open(argv[0])) | |
618 | return; | |
d61f6176 TT |
619 | if (check_fs_bitmaps(argv[0])) |
620 | return; | |
3839e657 TT |
621 | inode = string_to_inode(argv[1]); |
622 | if (!inode) | |
623 | return; | |
624 | ||
fc6d9d51 | 625 | if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode)) |
3839e657 TT |
626 | printf("Inode %ld is marked in use\n", inode); |
627 | else | |
628 | printf("Inode %ld is not in use\n", inode); | |
629 | } | |
630 | ||
631 | ||
632 | void do_freeb(int argc, char *argv[]) | |
633 | { | |
634 | blk_t block; | |
635 | char *tmp; | |
636 | ||
637 | if (argc != 2) { | |
638 | com_err(argv[0], 0, "Usage: freeb <block>"); | |
639 | return; | |
640 | } | |
641 | if (check_fs_open(argv[0])) | |
642 | return; | |
fc6d9d51 | 643 | if (check_fs_read_write(argv[0])) |
3839e657 | 644 | return; |
3839e657 TT |
645 | block = strtoul(argv[1], &tmp, 0); |
646 | if (!block || *tmp) { | |
647 | com_err(argv[0], 0, "No block 0"); | |
648 | return; | |
649 | } | |
fc6d9d51 | 650 | if (!ext2fs_test_block_bitmap(current_fs->block_map,block)) |
3839e657 | 651 | com_err(argv[0], 0, "Warning: block already clear"); |
fc6d9d51 TT |
652 | ext2fs_unmark_block_bitmap(current_fs->block_map,block); |
653 | ext2fs_mark_bb_dirty(current_fs); | |
3839e657 TT |
654 | } |
655 | ||
656 | void do_setb(int argc, char *argv[]) | |
657 | { | |
658 | blk_t block; | |
659 | char *tmp; | |
660 | ||
661 | if (argc != 2) { | |
662 | com_err(argv[0], 0, "Usage: setb <block>"); | |
663 | return; | |
664 | } | |
665 | if (check_fs_open(argv[0])) | |
666 | return; | |
fc6d9d51 | 667 | if (check_fs_read_write(argv[0])) |
3839e657 | 668 | return; |
3839e657 TT |
669 | block = strtoul(argv[1], &tmp, 0); |
670 | if (!block || *tmp) { | |
671 | com_err(argv[0], 0, "No block 0"); | |
672 | return; | |
673 | } | |
fc6d9d51 | 674 | if (ext2fs_test_block_bitmap(current_fs->block_map,block)) |
3839e657 | 675 | com_err(argv[0], 0, "Warning: block already set"); |
fc6d9d51 TT |
676 | ext2fs_mark_block_bitmap(current_fs->block_map,block); |
677 | ext2fs_mark_bb_dirty(current_fs); | |
3839e657 TT |
678 | } |
679 | ||
680 | void do_testb(int argc, char *argv[]) | |
681 | { | |
682 | blk_t block; | |
683 | char *tmp; | |
684 | ||
685 | if (argc != 2) { | |
686 | com_err(argv[0], 0, "Usage: testb <block>"); | |
687 | return; | |
688 | } | |
689 | if (check_fs_open(argv[0])) | |
690 | return; | |
d61f6176 TT |
691 | if (check_fs_bitmaps(argv[0])) |
692 | return; | |
3839e657 TT |
693 | block = strtoul(argv[1], &tmp, 0); |
694 | if (!block || *tmp) { | |
695 | com_err(argv[0], 0, "No block 0"); | |
696 | return; | |
697 | } | |
fc6d9d51 | 698 | if (ext2fs_test_block_bitmap(current_fs->block_map,block)) |
50e1e10f TT |
699 | printf("Block %d marked in use\n", block); |
700 | else printf("Block %d not in use\n", block); | |
3839e657 TT |
701 | } |
702 | ||
50e1e10f TT |
703 | static void modify_u8(char *com, const char *prompt, |
704 | const char *format, __u8 *val) | |
3839e657 TT |
705 | { |
706 | char buf[200]; | |
21c84b71 | 707 | unsigned long v; |
3839e657 TT |
708 | char *tmp; |
709 | ||
710 | sprintf(buf, format, *val); | |
711 | printf("%30s [%s] ", prompt, buf); | |
712 | fgets(buf, sizeof(buf), stdin); | |
713 | if (buf[strlen (buf) - 1] == '\n') | |
714 | buf[strlen (buf) - 1] = '\0'; | |
715 | if (!buf[0]) | |
716 | return; | |
21c84b71 | 717 | v = strtoul(buf, &tmp, 0); |
3839e657 TT |
718 | if (*tmp) |
719 | com_err(com, 0, "Bad value - %s", buf); | |
720 | else | |
721 | *val = v; | |
722 | } | |
723 | ||
50e1e10f TT |
724 | static void modify_u16(char *com, const char *prompt, |
725 | const char *format, __u16 *val) | |
3839e657 TT |
726 | { |
727 | char buf[200]; | |
21c84b71 | 728 | unsigned long v; |
3839e657 TT |
729 | char *tmp; |
730 | ||
731 | sprintf(buf, format, *val); | |
732 | printf("%30s [%s] ", prompt, buf); | |
733 | fgets(buf, sizeof(buf), stdin); | |
734 | if (buf[strlen (buf) - 1] == '\n') | |
735 | buf[strlen (buf) - 1] = '\0'; | |
736 | if (!buf[0]) | |
737 | return; | |
21c84b71 | 738 | v = strtoul(buf, &tmp, 0); |
3839e657 TT |
739 | if (*tmp) |
740 | com_err(com, 0, "Bad value - %s", buf); | |
741 | else | |
742 | *val = v; | |
743 | } | |
744 | ||
50e1e10f TT |
745 | static void modify_u32(char *com, const char *prompt, |
746 | const char *format, __u32 *val) | |
3839e657 TT |
747 | { |
748 | char buf[200]; | |
21c84b71 | 749 | unsigned long v; |
3839e657 TT |
750 | char *tmp; |
751 | ||
752 | sprintf(buf, format, *val); | |
753 | printf("%30s [%s] ", prompt, buf); | |
754 | fgets(buf, sizeof(buf), stdin); | |
755 | if (buf[strlen (buf) - 1] == '\n') | |
756 | buf[strlen (buf) - 1] = '\0'; | |
757 | if (!buf[0]) | |
758 | return; | |
21c84b71 | 759 | v = strtoul(buf, &tmp, 0); |
3839e657 TT |
760 | if (*tmp) |
761 | com_err(com, 0, "Bad value - %s", buf); | |
762 | else | |
763 | *val = v; | |
764 | } | |
765 | ||
766 | ||
767 | void do_modify_inode(int argc, char *argv[]) | |
768 | { | |
769 | struct ext2_inode inode; | |
770 | ino_t inode_num; | |
771 | int i; | |
772 | errcode_t retval; | |
fc6d9d51 | 773 | unsigned char *frag, *fsize; |
3839e657 | 774 | char buf[80]; |
fc6d9d51 | 775 | int os = current_fs->super->s_creator_os; |
50e1e10f TT |
776 | const char *hex_format = "0x%x"; |
777 | const char *octal_format = "0%o"; | |
778 | const char *decimal_format = "%d"; | |
3839e657 TT |
779 | |
780 | if (argc != 2) { | |
781 | com_err(argv[0], 0, "Usage: modify_inode <file>"); | |
782 | return; | |
783 | } | |
784 | if (check_fs_open(argv[0])) | |
785 | return; | |
fc6d9d51 | 786 | if (check_fs_read_write(argv[0])) |
3839e657 | 787 | return; |
3839e657 TT |
788 | |
789 | inode_num = string_to_inode(argv[1]); | |
790 | if (!inode_num) | |
791 | return; | |
792 | ||
fc6d9d51 | 793 | retval = ext2fs_read_inode(current_fs, inode_num, &inode); |
3839e657 TT |
794 | if (retval) { |
795 | com_err(argv[1], retval, "while trying to read inode %d", | |
796 | inode_num); | |
797 | return; | |
798 | } | |
799 | ||
50e1e10f TT |
800 | modify_u16(argv[0], "Mode", octal_format, &inode.i_mode); |
801 | modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid); | |
802 | modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid); | |
803 | modify_u32(argv[0], "Size", decimal_format, &inode.i_size); | |
804 | modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime); | |
805 | modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime); | |
806 | modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime); | |
807 | modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime); | |
808 | modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count); | |
809 | modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks); | |
810 | modify_u32(argv[0], "File flags", hex_format, &inode.i_flags); | |
3db9305a | 811 | modify_u32(argv[0], "Generation", hex_format, &inode.i_generation); |
50e1e10f TT |
812 | #if 0 |
813 | modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1); | |
814 | #endif | |
815 | modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl); | |
36a43d67 TT |
816 | if (LINUX_S_ISDIR(inode.i_mode)) |
817 | modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl); | |
818 | else | |
819 | modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high); | |
62c06f79 | 820 | |
fc6d9d51 | 821 | if (current_fs->super->s_creator_os == EXT2_OS_HURD) |
62c06f79 TT |
822 | modify_u32(argv[0], "Translator Block", |
823 | decimal_format, &inode.osd1.hurd1.h_i_translator); | |
824 | ||
50e1e10f | 825 | modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr); |
fc6d9d51 TT |
826 | switch (os) { |
827 | case EXT2_OS_LINUX: | |
828 | frag = &inode.osd2.linux2.l_i_frag; | |
829 | fsize = &inode.osd2.linux2.l_i_fsize; | |
830 | break; | |
831 | case EXT2_OS_HURD: | |
832 | frag = &inode.osd2.hurd2.h_i_frag; | |
833 | fsize = &inode.osd2.hurd2.h_i_fsize; | |
834 | break; | |
835 | case EXT2_OS_MASIX: | |
836 | frag = &inode.osd2.masix2.m_i_frag; | |
837 | fsize = &inode.osd2.masix2.m_i_fsize; | |
838 | break; | |
839 | default: | |
840 | frag = fsize = 0; | |
841 | } | |
842 | if (frag) | |
843 | modify_u8(argv[0], "Fragment number", decimal_format, frag); | |
844 | if (fsize) | |
845 | modify_u8(argv[0], "Fragment size", decimal_format, fsize); | |
846 | ||
3839e657 TT |
847 | for (i=0; i < EXT2_NDIR_BLOCKS; i++) { |
848 | sprintf(buf, "Direct Block #%d", i); | |
50e1e10f | 849 | modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]); |
3839e657 | 850 | } |
50e1e10f | 851 | modify_u32(argv[0], "Indirect Block", decimal_format, |
3839e657 | 852 | &inode.i_block[EXT2_IND_BLOCK]); |
50e1e10f | 853 | modify_u32(argv[0], "Double Indirect Block", decimal_format, |
3839e657 | 854 | &inode.i_block[EXT2_DIND_BLOCK]); |
50e1e10f | 855 | modify_u32(argv[0], "Triple Indirect Block", decimal_format, |
3839e657 | 856 | &inode.i_block[EXT2_TIND_BLOCK]); |
fc6d9d51 | 857 | retval = ext2fs_write_inode(current_fs, inode_num, &inode); |
3839e657 TT |
858 | if (retval) { |
859 | com_err(argv[1], retval, "while trying to write inode %d", | |
860 | inode_num); | |
861 | return; | |
862 | } | |
863 | } | |
864 | ||
3839e657 TT |
865 | void do_change_working_dir(int argc, char *argv[]) |
866 | { | |
867 | ino_t inode; | |
868 | int retval; | |
869 | ||
870 | if (argc != 2) { | |
871 | com_err(argv[0], 0, "Usage: cd <file>"); | |
872 | return; | |
873 | } | |
874 | if (check_fs_open(argv[0])) | |
875 | return; | |
876 | ||
877 | inode = string_to_inode(argv[1]); | |
878 | if (!inode) | |
879 | return; | |
880 | ||
fc6d9d51 | 881 | retval = ext2fs_check_directory(current_fs, inode); |
3839e657 TT |
882 | if (retval) { |
883 | com_err(argv[1], retval, ""); | |
884 | return; | |
885 | } | |
886 | cwd = inode; | |
887 | return; | |
888 | } | |
889 | ||
3839e657 TT |
890 | void do_print_working_directory(int argc, char *argv[]) |
891 | { | |
892 | int retval; | |
893 | char *pathname = NULL; | |
894 | ||
895 | if (argc > 1) { | |
896 | com_err(argv[0], 0, "Usage: print_working_directory"); | |
897 | return; | |
898 | } | |
899 | if (check_fs_open(argv[0])) | |
900 | return; | |
901 | ||
fc6d9d51 | 902 | retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname); |
3839e657 TT |
903 | if (retval) { |
904 | com_err(argv[0], retval, | |
905 | "while trying to get pathname of cwd"); | |
906 | } | |
907 | printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname); | |
908 | free(pathname); | |
fc6d9d51 | 909 | retval = ext2fs_get_pathname(current_fs, root, 0, &pathname); |
3839e657 TT |
910 | if (retval) { |
911 | com_err(argv[0], retval, | |
912 | "while trying to get pathname of root"); | |
913 | } | |
914 | printf("[root] INODE: %6ld PATH: %s\n", root, pathname); | |
915 | free(pathname); | |
916 | return; | |
917 | } | |
918 | ||
50e1e10f | 919 | static void make_link(char *sourcename, char *destname) |
3839e657 TT |
920 | { |
921 | ino_t inode; | |
922 | int retval; | |
923 | ino_t dir; | |
924 | char *dest, *cp, *basename; | |
925 | ||
926 | /* | |
927 | * Get the source inode | |
928 | */ | |
929 | inode = string_to_inode(sourcename); | |
930 | if (!inode) | |
931 | return; | |
932 | basename = strrchr(sourcename, '/'); | |
933 | if (basename) | |
934 | basename++; | |
935 | else | |
936 | basename = sourcename; | |
937 | /* | |
938 | * Figure out the destination. First see if it exists and is | |
939 | * a directory. | |
940 | */ | |
fc6d9d51 | 941 | if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir))) |
3839e657 TT |
942 | dest = basename; |
943 | else { | |
944 | /* | |
945 | * OK, it doesn't exist. See if it is | |
946 | * '<dir>/basename' or 'basename' | |
947 | */ | |
948 | cp = strrchr(destname, '/'); | |
949 | if (cp) { | |
950 | *cp = 0; | |
951 | dir = string_to_inode(destname); | |
952 | if (!dir) | |
953 | return; | |
954 | dest = cp+1; | |
955 | } else { | |
956 | dir = cwd; | |
957 | dest = destname; | |
958 | } | |
959 | } | |
960 | ||
fc6d9d51 | 961 | retval = ext2fs_link(current_fs, dir, dest, inode, 0); |
3839e657 TT |
962 | if (retval) |
963 | com_err("make_link", retval, ""); | |
964 | return; | |
965 | } | |
966 | ||
967 | ||
968 | void do_link(int argc, char *argv[]) | |
969 | { | |
970 | if (argc != 3) { | |
971 | com_err(argv[0], 0, "Usage: link <source_file> <dest_name>"); | |
972 | return; | |
973 | } | |
974 | if (check_fs_open(argv[0])) | |
975 | return; | |
976 | ||
977 | make_link(argv[1], argv[2]); | |
978 | } | |
979 | ||
50e1e10f | 980 | static void unlink_file_by_name(char *filename) |
3839e657 TT |
981 | { |
982 | int retval; | |
983 | ino_t dir; | |
984 | char *basename; | |
985 | ||
986 | basename = strrchr(filename, '/'); | |
987 | if (basename) { | |
fc6d9d51 | 988 | *basename++ = '\0'; |
3839e657 TT |
989 | dir = string_to_inode(filename); |
990 | if (!dir) | |
991 | return; | |
992 | } else { | |
993 | dir = cwd; | |
994 | basename = filename; | |
995 | } | |
fc6d9d51 | 996 | retval = ext2fs_unlink(current_fs, dir, basename, 0, 0); |
3839e657 TT |
997 | if (retval) |
998 | com_err("unlink_file_by_name", retval, ""); | |
999 | return; | |
1000 | } | |
1001 | ||
1002 | void do_unlink(int argc, char *argv[]) | |
1003 | { | |
1004 | if (argc != 2) { | |
1005 | com_err(argv[0], 0, "Usage: unlink <pathname>"); | |
1006 | return; | |
1007 | } | |
1008 | if (check_fs_open(argv[0])) | |
1009 | return; | |
1010 | ||
1011 | unlink_file_by_name(argv[1]); | |
1012 | } | |
1013 | ||
1014 | void do_find_free_block(int argc, char *argv[]) | |
1015 | { | |
1016 | blk_t free_blk, goal; | |
1017 | errcode_t retval; | |
1018 | char *tmp; | |
1019 | ||
50e1e10f TT |
1020 | if ((argc > 2) || (argc==2 && *argv[1] == '?')) { |
1021 | com_err(argv[0], 0, "Usage: find_free_block [goal]"); | |
3839e657 TT |
1022 | return; |
1023 | } | |
1024 | if (check_fs_open(argv[0])) | |
1025 | return; | |
1026 | ||
1027 | if (argc > 1) { | |
1028 | goal = strtol(argv[1], &tmp, 0); | |
1029 | if (*tmp) { | |
1030 | com_err(argv[0], 0, "Bad goal - %s", argv[1]); | |
1031 | return; | |
1032 | } | |
1033 | } | |
1034 | else | |
fc6d9d51 | 1035 | goal = current_fs->super->s_first_data_block; |
3839e657 | 1036 | |
fc6d9d51 | 1037 | retval = ext2fs_new_block(current_fs, goal, 0, &free_blk); |
3839e657 TT |
1038 | if (retval) |
1039 | com_err("ext2fs_new_block", retval, ""); | |
1040 | else | |
50e1e10f | 1041 | printf("Free block found: %d\n", free_blk); |
3839e657 TT |
1042 | |
1043 | } | |
1044 | ||
1045 | void do_find_free_inode(int argc, char *argv[]) | |
1046 | { | |
1047 | ino_t free_inode, dir; | |
1048 | int mode; | |
1049 | int retval; | |
1050 | char *tmp; | |
1051 | ||
50e1e10f TT |
1052 | if (argc > 3 || (argc>1 && *argv[1] == '?')) { |
1053 | com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]"); | |
3839e657 TT |
1054 | return; |
1055 | } | |
1056 | if (check_fs_open(argv[0])) | |
1057 | return; | |
1058 | ||
1059 | if (argc > 1) { | |
1060 | dir = strtol(argv[1], &tmp, 0); | |
1061 | if (*tmp) { | |
1062 | com_err(argv[0], 0, "Bad dir - %s", argv[1]); | |
1063 | return; | |
1064 | } | |
1065 | } | |
1066 | else | |
1067 | dir = root; | |
1068 | if (argc > 2) { | |
1069 | mode = strtol(argv[2], &tmp, 0); | |
1070 | if (*tmp) { | |
1071 | com_err(argv[0], 0, "Bad mode - %s", argv[2]); | |
1072 | return; | |
1073 | } | |
50e1e10f | 1074 | } else |
3839e657 TT |
1075 | mode = 010755; |
1076 | ||
fc6d9d51 | 1077 | retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode); |
3839e657 TT |
1078 | if (retval) |
1079 | com_err("ext2fs_new_inode", retval, ""); | |
1080 | else | |
1081 | printf("Free inode found: %ld\n", free_inode); | |
1082 | } | |
1083 | ||
50e1e10f TT |
1084 | static errcode_t copy_file(int fd, ino_t newfile) |
1085 | { | |
5a51384e | 1086 | ext2_file_t e2_file; |
50e1e10f | 1087 | errcode_t retval; |
4a31c48b | 1088 | unsigned int got, written; |
5a51384e TT |
1089 | char buf[8192]; |
1090 | char *ptr; | |
50e1e10f | 1091 | |
5a51384e TT |
1092 | retval = ext2fs_file_open(current_fs, newfile, |
1093 | EXT2_FILE_WRITE, &e2_file); | |
50e1e10f TT |
1094 | if (retval) |
1095 | return retval; | |
50e1e10f | 1096 | |
5a51384e TT |
1097 | while (1) { |
1098 | got = read(fd, buf, sizeof(buf)); | |
1099 | if (got == 0) | |
1100 | break; | |
1101 | if (got < 0) { | |
1102 | retval = errno; | |
1103 | goto fail; | |
1104 | } | |
1105 | ptr = buf; | |
1106 | while (got > 0) { | |
1107 | retval = ext2fs_file_write(e2_file, ptr, | |
1108 | got, &written); | |
1109 | if (retval) | |
1110 | goto fail; | |
1111 | ||
1112 | got -= written; | |
1113 | ptr += written; | |
1114 | } | |
1115 | } | |
1116 | retval = ext2fs_file_close(e2_file); | |
50e1e10f | 1117 | |
5a51384e TT |
1118 | return retval; |
1119 | ||
1120 | fail: | |
1121 | (void) ext2fs_file_close(e2_file); | |
1122 | return retval; | |
50e1e10f TT |
1123 | } |
1124 | ||
5a51384e | 1125 | |
50e1e10f TT |
1126 | void do_write(int argc, char *argv[]) |
1127 | { | |
1128 | int fd; | |
1129 | struct stat statbuf; | |
1130 | ino_t newfile; | |
1131 | errcode_t retval; | |
1132 | struct ext2_inode inode; | |
5a51384e | 1133 | dgrp_t group; |
50e1e10f TT |
1134 | |
1135 | if (check_fs_open(argv[0])) | |
1136 | return; | |
1137 | if (argc != 3) { | |
1138 | com_err(argv[0], 0, "Usage: write <nativefile> <newfile>"); | |
1139 | return; | |
1140 | } | |
fc6d9d51 | 1141 | if (check_fs_read_write(argv[0])) |
50e1e10f | 1142 | return; |
50e1e10f TT |
1143 | fd = open(argv[1], O_RDONLY); |
1144 | if (fd < 0) { | |
1145 | com_err(argv[1], fd, ""); | |
1146 | return; | |
1147 | } | |
1148 | if (fstat(fd, &statbuf) < 0) { | |
1149 | com_err(argv[1], errno, ""); | |
1150 | close(fd); | |
1151 | return; | |
1152 | } | |
1153 | ||
fc6d9d51 | 1154 | retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); |
50e1e10f TT |
1155 | if (retval) { |
1156 | com_err(argv[0], retval, ""); | |
1157 | close(fd); | |
1158 | return; | |
1159 | } | |
5a51384e TT |
1160 | group = ext2fs_group_of_ino(current_fs, newfile); |
1161 | current_fs->group_desc[group].bg_free_inodes_count--; | |
1162 | current_fs->super->s_free_inodes_count--; | |
1163 | ext2fs_mark_super_dirty(current_fs); | |
50e1e10f | 1164 | printf("Allocated inode: %ld\n", newfile); |
fc6d9d51 | 1165 | retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0); |
50e1e10f TT |
1166 | if (retval) { |
1167 | com_err(argv[2], retval, ""); | |
1168 | close(fd); | |
1169 | return; | |
1170 | } | |
fc6d9d51 | 1171 | if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile)) |
50e1e10f | 1172 | com_err(argv[0], 0, "Warning: inode already set"); |
fc6d9d51 TT |
1173 | ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile); |
1174 | ext2fs_mark_ib_dirty(current_fs); | |
50e1e10f TT |
1175 | memset(&inode, 0, sizeof(inode)); |
1176 | inode.i_mode = statbuf.st_mode; | |
1177 | inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL); | |
1178 | inode.i_links_count = 1; | |
1179 | inode.i_size = statbuf.st_size; | |
fc6d9d51 TT |
1180 | ext2fs_write_inode(current_fs, newfile, &inode); |
1181 | retval = ext2fs_write_inode(current_fs, newfile, &inode); | |
50e1e10f | 1182 | if (retval) { |
fc6d9d51 TT |
1183 | com_err(argv[0], retval, "while trying to write inode %d", |
1184 | inode); | |
50e1e10f TT |
1185 | close(fd); |
1186 | return; | |
1187 | } | |
1188 | if (LINUX_S_ISREG(inode.i_mode)) { | |
1189 | retval = copy_file(fd, newfile); | |
1190 | if (retval) | |
1191 | com_err("copy_file", retval, ""); | |
1192 | } | |
1193 | close(fd); | |
1194 | } | |
1195 | ||
1196 | void do_mknod(int argc, char *argv[]) | |
1197 | { | |
1198 | unsigned long mode, major, minor, nr; | |
1199 | ino_t newfile; | |
1200 | errcode_t retval; | |
1201 | struct ext2_inode inode; | |
1202 | ||
1203 | if (check_fs_open(argv[0])) | |
1204 | return; | |
1205 | if (argc < 3 || argv[2][1]) { | |
1206 | com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]"); | |
1207 | return; | |
1208 | } | |
1209 | mode = minor = major = 0; | |
1210 | switch (argv[2][0]) { | |
1211 | case 'p': | |
1212 | mode = LINUX_S_IFIFO; | |
1213 | nr = 3; | |
1214 | break; | |
1215 | case 'c': | |
1216 | mode = LINUX_S_IFCHR; | |
1217 | nr = 5; | |
1218 | break; | |
1219 | case 'b': | |
1220 | mode = LINUX_S_IFBLK; | |
1221 | nr = 5; | |
1222 | break; | |
1223 | default: | |
1224 | nr = 0; | |
1225 | } | |
1226 | if (nr == 5) { | |
1227 | major = strtoul(argv[3], argv+3, 0); | |
1228 | minor = strtoul(argv[4], argv+4, 0); | |
1229 | if (major > 255 || minor > 255 || argv[3][0] || argv[4][0]) | |
1230 | nr = 0; | |
1231 | } | |
1232 | if (argc != nr) { | |
1233 | com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]"); | |
1234 | return; | |
1235 | } | |
fc6d9d51 | 1236 | if (check_fs_read_write(argv[0])) |
50e1e10f | 1237 | return; |
fc6d9d51 | 1238 | retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); |
50e1e10f TT |
1239 | if (retval) { |
1240 | com_err(argv[0], retval, ""); | |
1241 | return; | |
1242 | } | |
1243 | printf("Allocated inode: %ld\n", newfile); | |
fc6d9d51 | 1244 | retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0); |
50e1e10f TT |
1245 | if (retval) { |
1246 | if (retval == EXT2_ET_DIR_NO_SPACE) { | |
fc6d9d51 | 1247 | retval = ext2fs_expand_dir(current_fs, cwd); |
50e1e10f | 1248 | if (!retval) |
fc6d9d51 TT |
1249 | retval = ext2fs_link(current_fs, cwd, |
1250 | argv[1], newfile, 0); | |
50e1e10f TT |
1251 | } |
1252 | if (retval) { | |
1253 | com_err(argv[1], retval, ""); | |
1254 | return; | |
1255 | } | |
1256 | } | |
fc6d9d51 | 1257 | if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile)) |
50e1e10f | 1258 | com_err(argv[0], 0, "Warning: inode already set"); |
fc6d9d51 TT |
1259 | ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile); |
1260 | ext2fs_mark_ib_dirty(current_fs); | |
50e1e10f TT |
1261 | memset(&inode, 0, sizeof(inode)); |
1262 | inode.i_mode = mode; | |
1263 | inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL); | |
1264 | inode.i_block[0] = major*256+minor; | |
1265 | inode.i_links_count = 1; | |
fc6d9d51 TT |
1266 | ext2fs_write_inode(current_fs, newfile, &inode); |
1267 | retval = ext2fs_write_inode(current_fs, newfile, &inode); | |
50e1e10f TT |
1268 | if (retval) { |
1269 | com_err(argv[0], retval, "while trying to write inode %d", inode); | |
1270 | return; | |
1271 | } | |
1272 | } | |
1273 | ||
3839e657 TT |
1274 | void do_mkdir(int argc, char *argv[]) |
1275 | { | |
1276 | char *cp; | |
1277 | ino_t parent; | |
1278 | char *name; | |
1279 | errcode_t retval; | |
1280 | ||
1281 | if (check_fs_open(argv[0])) | |
1282 | return; | |
1283 | ||
1284 | if (argc != 2) { | |
1285 | com_err(argv[0], 0, "Usage: mkdir <file>"); | |
1286 | return; | |
1287 | } | |
1288 | ||
d3aea7dc TT |
1289 | if (check_fs_read_write(argv[0])) |
1290 | return; | |
1291 | ||
3839e657 TT |
1292 | cp = strrchr(argv[1], '/'); |
1293 | if (cp) { | |
1294 | *cp = 0; | |
1295 | parent = string_to_inode(argv[1]); | |
1296 | if (!parent) { | |
1297 | com_err(argv[1], ENOENT, ""); | |
1298 | return; | |
1299 | } | |
1300 | name = cp+1; | |
1301 | } else { | |
1302 | parent = cwd; | |
1303 | name = argv[1]; | |
1304 | } | |
1305 | ||
1306 | ||
fc6d9d51 | 1307 | retval = ext2fs_mkdir(current_fs, parent, 0, name); |
3839e657 TT |
1308 | if (retval) { |
1309 | com_err("ext2fs_mkdir", retval, ""); | |
1310 | return; | |
1311 | } | |
1312 | ||
1313 | } | |
1314 | ||
1315 | void do_rmdir(int argc, char *argv[]) | |
1316 | { | |
1317 | printf("Unimplemented\n"); | |
1318 | } | |
1319 | ||
1320 | ||
50e1e10f TT |
1321 | static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr, |
1322 | int blockcnt, void *private) | |
3839e657 | 1323 | { |
50e1e10f | 1324 | printf("%d ", *blocknr); |
f3db3566 | 1325 | ext2fs_unmark_block_bitmap(fs->block_map,*blocknr); |
3839e657 TT |
1326 | return 0; |
1327 | } | |
1328 | ||
fc6d9d51 | 1329 | static void kill_file_by_inode(ino_t inode) |
3839e657 TT |
1330 | { |
1331 | struct ext2_inode inode_buf; | |
1332 | ||
fc6d9d51 | 1333 | ext2fs_read_inode(current_fs, inode, &inode_buf); |
3839e657 | 1334 | inode_buf.i_dtime = time(NULL); |
fc6d9d51 | 1335 | ext2fs_write_inode(current_fs, inode, &inode_buf); |
3839e657 TT |
1336 | |
1337 | printf("Kill file by inode %ld\n", inode); | |
fc6d9d51 TT |
1338 | ext2fs_block_iterate(current_fs, inode, 0, NULL, |
1339 | release_blocks_proc, NULL); | |
521e3685 | 1340 | printf("\n"); |
fc6d9d51 | 1341 | ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode); |
3839e657 | 1342 | |
fc6d9d51 TT |
1343 | ext2fs_mark_bb_dirty(current_fs); |
1344 | ext2fs_mark_ib_dirty(current_fs); | |
3839e657 TT |
1345 | } |
1346 | ||
1347 | ||
1348 | void do_kill_file(int argc, char *argv[]) | |
1349 | { | |
1350 | ino_t inode_num; | |
1351 | ||
1352 | if (argc != 2) { | |
1353 | com_err(argv[0], 0, "Usage: kill_file <file>"); | |
1354 | return; | |
1355 | } | |
1356 | if (check_fs_open(argv[0])) | |
1357 | return; | |
1358 | ||
d3aea7dc TT |
1359 | if (check_fs_read_write(argv[0])) |
1360 | return; | |
1361 | ||
3839e657 TT |
1362 | inode_num = string_to_inode(argv[1]); |
1363 | if (!inode_num) { | |
1364 | com_err(argv[0], 0, "Cannot find file"); | |
1365 | return; | |
1366 | } | |
1367 | kill_file_by_inode(inode_num); | |
1368 | } | |
1369 | ||
1370 | void do_rm(int argc, char *argv[]) | |
1371 | { | |
1372 | int retval; | |
1373 | ino_t inode_num; | |
1374 | struct ext2_inode inode; | |
1375 | ||
1376 | if (argc != 2) { | |
1377 | com_err(argv[0], 0, "Usage: rm <filename>"); | |
1378 | return; | |
1379 | } | |
1380 | if (check_fs_open(argv[0])) | |
1381 | return; | |
1382 | ||
d3aea7dc TT |
1383 | if (check_fs_read_write(argv[0])) |
1384 | return; | |
1385 | ||
fc6d9d51 | 1386 | retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); |
3839e657 | 1387 | if (retval) { |
91d6d486 | 1388 | com_err(argv[0], retval, "while trying to resolve filename"); |
3839e657 TT |
1389 | return; |
1390 | } | |
1391 | ||
fc6d9d51 | 1392 | retval = ext2fs_read_inode(current_fs,inode_num,&inode); |
3839e657 TT |
1393 | if (retval) { |
1394 | com_err(argv[0], retval, "while reading file's inode"); | |
1395 | return; | |
1396 | } | |
1397 | ||
50e1e10f | 1398 | if (LINUX_S_ISDIR(inode.i_mode)) { |
3839e657 TT |
1399 | com_err(argv[0], 0, "file is a directory"); |
1400 | return; | |
1401 | } | |
1402 | ||
1403 | --inode.i_links_count; | |
fc6d9d51 | 1404 | retval = ext2fs_write_inode(current_fs,inode_num,&inode); |
3839e657 TT |
1405 | if (retval) { |
1406 | com_err(argv[0], retval, "while writing inode"); | |
1407 | return; | |
1408 | } | |
1409 | ||
1410 | unlink_file_by_name(argv[1]); | |
1411 | if (inode.i_links_count == 0) | |
1412 | kill_file_by_inode(inode_num); | |
1413 | } | |
1414 | ||
1415 | void do_show_debugfs_params(int argc, char *argv[]) | |
1416 | { | |
1417 | FILE *out = stdout; | |
1418 | ||
fc6d9d51 TT |
1419 | if (current_fs) |
1420 | fprintf(out, "Open mode: read-%s\n", | |
1421 | current_fs->flags & EXT2_FLAG_RW ? "write" : "only"); | |
3839e657 | 1422 | fprintf(out, "Filesystem in use: %s\n", |
fc6d9d51 | 1423 | current_fs ? current_fs->device_name : "--none--"); |
3839e657 TT |
1424 | } |
1425 | ||
1426 | void do_expand_dir(int argc, char *argv[]) | |
1427 | { | |
1428 | ino_t inode; | |
1429 | int retval; | |
1430 | ||
1431 | if (argc != 2) { | |
1432 | com_err(argv[0], 0, "Usage: expand_dir <file>"); | |
1433 | return; | |
1434 | } | |
1435 | if (check_fs_open(argv[0])) | |
1436 | return; | |
1437 | inode = string_to_inode(argv[1]); | |
1438 | if (!inode) | |
1439 | return; | |
1440 | ||
fc6d9d51 | 1441 | retval = ext2fs_expand_dir(current_fs, inode); |
3839e657 TT |
1442 | if (retval) |
1443 | com_err("ext2fs_expand_dir", retval, ""); | |
1444 | return; | |
1445 | } | |
1446 | ||
d3aea7dc TT |
1447 | void do_features(int argc, char *argv[]) |
1448 | { | |
1449 | int i; | |
1450 | ||
1451 | if (check_fs_open(argv[0])) | |
1452 | return; | |
1453 | ||
1454 | if ((argc != 1) && check_fs_read_write(argv[0])) | |
1455 | return; | |
1456 | for (i=1; i < argc; i++) { | |
1457 | if (e2p_edit_feature(argv[i], | |
06968e7e | 1458 | ¤t_fs->super->s_feature_compat, 0)) |
d3aea7dc TT |
1459 | com_err(argv[0], 0, "Unknown feature: %s\n", |
1460 | argv[i]); | |
1461 | else | |
1462 | ext2fs_mark_super_dirty(current_fs); | |
1463 | } | |
5dd8f963 | 1464 | print_features(current_fs->super, stdout); |
d3aea7dc TT |
1465 | } |
1466 | ||
fc6d9d51 TT |
1467 | static int source_file(const char *cmd_file, int sci_idx) |
1468 | { | |
1469 | FILE *f; | |
1470 | char buf[256]; | |
1471 | char *cp; | |
1472 | int exit_status = 0; | |
1473 | int retval; | |
1474 | ||
1475 | if (strcmp(cmd_file, "-") == 0) | |
1476 | f = stdin; | |
1477 | else { | |
1478 | f = fopen(cmd_file, "r"); | |
1479 | if (!f) { | |
1480 | perror(cmd_file); | |
1481 | exit(1); | |
1482 | } | |
1483 | } | |
1484 | setbuf(stdout, NULL); | |
1485 | setbuf(stderr, NULL); | |
1486 | while (!feof(f)) { | |
1487 | if (fgets(buf, sizeof(buf), f) == NULL) | |
1488 | break; | |
1489 | cp = strchr(buf, '\n'); | |
1490 | if (cp) | |
1491 | *cp = 0; | |
1492 | cp = strchr(buf, '\r'); | |
1493 | if (cp) | |
1494 | *cp = 0; | |
1495 | printf("debugfs: %s\n", buf); | |
1496 | retval = ss_execute_line(sci_idx, buf); | |
1497 | if (retval) { | |
1498 | ss_perror(sci_idx, retval, buf); | |
1499 | exit_status++; | |
1500 | } | |
1501 | } | |
1502 | return exit_status; | |
1503 | } | |
1504 | ||
a8859cad | 1505 | int main(int argc, char **argv) |
3839e657 | 1506 | { |
50e1e10f TT |
1507 | int retval; |
1508 | int sci_idx; | |
2e8d40d5 | 1509 | const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]"; |
f1304811 | 1510 | int c; |
50e1e10f | 1511 | int open_flags = 0; |
fc6d9d51 TT |
1512 | char *request = 0; |
1513 | int exit_status = 0; | |
1514 | char *cmd_file = 0; | |
2e8d40d5 TT |
1515 | blk_t superblock = 0; |
1516 | blk_t blocksize = 0; | |
1517 | int catastrophic = 0; | |
1518 | char *tmp; | |
3839e657 TT |
1519 | |
1520 | initialize_ext2_error_table(); | |
818180cd TT |
1521 | fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n", |
1522 | E2FSPROGS_VERSION, E2FSPROGS_DATE, | |
1523 | EXT2FS_VERSION, EXT2FS_DATE); | |
3839e657 | 1524 | |
2e8d40d5 | 1525 | while ((c = getopt (argc, argv, "wcR:f:b:s:V")) != EOF) { |
3839e657 | 1526 | switch (c) { |
fc6d9d51 TT |
1527 | case 'R': |
1528 | request = optarg; | |
1529 | break; | |
1530 | case 'f': | |
1531 | cmd_file = optarg; | |
1532 | break; | |
3839e657 TT |
1533 | case 'w': |
1534 | open_flags = EXT2_FLAG_RW; | |
1535 | break; | |
2e8d40d5 TT |
1536 | case 'b': |
1537 | blocksize = strtoul(optarg, &tmp, 0); | |
1538 | if (*tmp) { | |
1539 | com_err(argv[0], 0, | |
1540 | "Bad block size - %s", optarg); | |
1541 | return 1; | |
1542 | } | |
1543 | break; | |
1544 | case 's': | |
1545 | superblock = strtoul(optarg, &tmp, 0); | |
1546 | if (*tmp) { | |
1547 | com_err(argv[0], 0, | |
1548 | "Bad superblock number - %s", optarg); | |
1549 | return 1; | |
1550 | } | |
1551 | break; | |
1552 | case 'c': | |
1553 | catastrophic = 1; | |
1554 | break; | |
818180cd TT |
1555 | case 'V': |
1556 | /* Print version number and exit */ | |
1557 | fprintf(stderr, "\tUsing %s\n", | |
1558 | error_message(EXT2_ET_BASE)); | |
1559 | exit(0); | |
3839e657 TT |
1560 | default: |
1561 | com_err(argv[0], 0, usage); | |
b4ac9cc3 | 1562 | return 1; |
3839e657 TT |
1563 | } |
1564 | } | |
1565 | if (optind < argc) | |
2e8d40d5 TT |
1566 | open_filesystem(argv[optind], open_flags, |
1567 | superblock, blocksize, catastrophic); | |
3839e657 TT |
1568 | |
1569 | sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL, | |
1570 | &debug_cmds, &retval); | |
1571 | if (retval) { | |
1572 | ss_perror(sci_idx, retval, "creating invocation"); | |
1573 | exit(1); | |
1574 | } | |
1575 | ||
1576 | (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval); | |
1577 | if (retval) { | |
1578 | ss_perror(sci_idx, retval, "adding standard requests"); | |
1579 | exit (1); | |
1580 | } | |
fc6d9d51 TT |
1581 | if (request) { |
1582 | retval = 0; | |
1583 | retval = ss_execute_line(sci_idx, request); | |
1584 | if (retval) { | |
1585 | ss_perror(sci_idx, retval, request); | |
1586 | exit_status++; | |
1587 | } | |
1588 | } else if (cmd_file) { | |
1589 | exit_status = source_file(cmd_file, sci_idx); | |
1590 | } else { | |
1591 | ss_listen(sci_idx); | |
1592 | } | |
3839e657 | 1593 | |
fc6d9d51 | 1594 | if (current_fs) |
3839e657 TT |
1595 | close_filesystem(); |
1596 | ||
e597304a | 1597 | return exit_status; |
3839e657 | 1598 | } |