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