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