extern int optind;
extern char *optarg;
#endif
-#ifdef HAVE_OPTRESET
-extern int optreset; /* defined by BSD, but not others */
-#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
ext2_ino_t root, cwd;
static void open_filesystem(char *device, int open_flags, blk_t superblock,
- blk_t blocksize, int catastrophic)
+ blk_t blocksize, int catastrophic,
+ char *data_filename)
{
int retval;
+ io_channel data_io = 0;
if (superblock != 0 && blocksize == 0) {
com_err(device, 0, "if you specify the superblock, you must also specify the block size");
return;
}
+ if (data_filename) {
+ if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
+ com_err(device, 0,
+ "The -d option is only valid when reading an e2image file");
+ current_fs = NULL;
+ return;
+ }
+ retval = unix_io_manager->open(data_filename, 0, &data_io);
+ if (retval) {
+ com_err(data_filename, 0, "while opening data source");
+ current_fs = NULL;
+ return;
+ }
+ }
+
if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
com_err(device, 0,
"opening read-only because of catastrophic mode");
goto errout;
}
}
+
+ if (data_io) {
+ retval = ext2fs_set_data_io(current_fs, data_io);
+ if (retval) {
+ com_err(device, retval,
+ "while setting data source");
+ goto errout;
+ }
+ }
+
root = cwd = EXT2_ROOT_INO;
return;
void do_open_filesys(int argc, char **argv)
{
const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
- int c;
+ int c, err;
int catastrophic = 0;
blk_t superblock = 0;
blk_t blocksize = 0;
- char *tmp;
- int open_flags = 0;
+ int open_flags = 0;
+ char *data_filename;
- optind = 0;
-#ifdef HAVE_OPTRESET
- optreset = 1; /* Makes BSD getopt happy */
-#endif
- while ((c = getopt (argc, argv, "wfcb:s:")) != EOF) {
+ reset_getopt();
+ while ((c = getopt (argc, argv, "iwfcb:s:d:")) != EOF) {
switch (c) {
+ case 'i':
+ open_flags |= EXT2_FLAG_IMAGE_FILE;
+ break;
case 'w':
open_flags |= EXT2_FLAG_RW;
break;
case 'c':
catastrophic = 1;
break;
+ case 'd':
+ data_filename = optarg;
+ break;
case 'b':
- blocksize = strtoul(optarg, &tmp, 0);
- if (*tmp) {
- com_err(argv[0], 0,
- "Bad block size - %s", optarg);
+ blocksize = parse_ulong(optarg, argv[0],
+ "block size", &err);
+ if (err)
return;
- }
break;
case 's':
- superblock = strtoul(optarg, &tmp, 0);
- if (*tmp) {
- com_err(argv[0], 0,
- "Bad superblock number - %s", optarg);
+ superblock = parse_ulong(optarg, argv[0],
+ "superblock number", &err);
+ if (err)
return;
- }
break;
default:
com_err(argv[0], 0, usage);
if (check_fs_not_open(argv[0]))
return;
open_filesystem(argv[optind], open_flags,
- superblock, blocksize, catastrophic);
+ superblock, blocksize, catastrophic,
+ data_filename);
}
void do_lcd(int argc, char **argv)
{
- const char *usage = "Usage: lcd <native dir>";
-
- if (argc != 2) {
- com_err(argv[0], 0, usage);
+ if (common_args_process(argc, argv, 2, 2, "lcd",
+ "<native dir>", 0))
return;
- }
if (chdir(argv[1]) == -1) {
com_err(argv[0], errno,
void do_close_filesys(int argc, char **argv)
{
- if (argc > 1) {
- com_err(argv[0], 0, "Usage: close_filesys");
- return;
- }
- if (check_fs_open(argv[0]))
+ if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
return;
close_filesystem();
}
void do_init_filesys(int argc, char **argv)
{
- const char *usage = "Usage: initialize <device> <blocksize>";
struct ext2_super_block param;
errcode_t retval;
- char *tmp;
+ int err;
- if (argc != 3) {
- com_err(argv[0], 0, usage);
- return;
- }
- if (check_fs_not_open(argv[0]))
+ if (common_args_process(argc, argv, 3, 3, "initialize",
+ "<device> <blocksize>", CHECK_FS_NOTOPEN))
return;
memset(¶m, 0, sizeof(struct ext2_super_block));
- param.s_blocks_count = strtoul(argv[2], &tmp, 0);
- if (*tmp) {
- com_err(argv[0], 0, "Bad blocks count - %s", argv[2]);
+ param.s_blocks_count = parse_ulong(argv[2], argv[0],
+ "blocks count", &err);
+ if (err)
return;
- }
retval = ext2fs_initialize(argv[1], 0, ¶m,
unix_io_manager, ¤t_fs);
if (retval) {
static void print_features(struct ext2_super_block * s, FILE *f)
{
int i, j, printed=0;
-__u32 *mask = &s->s_feature_compat, m;
+ __u32 *mask = &s->s_feature_compat, m;
- printf ("Filesystem features:");
+ fputs("Filesystem features:", f);
for (i=0; i <3; i++,mask++) {
for (j=0,m=1; j < 32; j++, m<<=1) {
if (*mask & m) {
}
}
if (printed == 0)
- printf("(none)");
- printf("\n");
+ fputs("(none)", f);
+ fputs("\n", f);
}
void do_show_super_stats(int argc, char *argv[])
{
- int i;
+ dgrp_t i;
FILE *out;
struct ext2_group_desc *gdp;
int c, header_only = 0;
+ int numdirs = 0;
const char *usage = "Usage: show_super [-h]";
- optind = 0;
-#ifdef HAVE_OPTRESET
- optreset = 1; /* Makes BSD getopt happy */
-#endif
+ reset_getopt();
while ((c = getopt (argc, argv, "h")) != EOF) {
switch (c) {
case 'h':
out = open_pager();
list_super2(current_fs->super, out);
-
+ for (i=0; i < current_fs->group_desc_count; i++)
+ numdirs += current_fs->group_desc[i].bg_used_dirs_count;
+ fprintf(out, "Directories: %d\n", numdirs);
+
if (header_only) {
close_pager(out);
return;
close_pager(out);
}
-void do_dirty_filesys(int argc, char **argv)
+void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
+ char **argv EXT2FS_ATTR((unused)))
{
if (check_fs_open(argv[0]))
return;
}
struct list_blocks_struct {
- FILE *f;
- int total;
- blk_t first_block, last_block;
- int first_bcnt, last_bcnt;
- int first;
+ FILE *f;
+ e2_blkcnt_t total;
+ blk_t first_block, last_block;
+ e2_blkcnt_t first_bcnt, last_bcnt;
+ e2_blkcnt_t first;
};
static void finish_range(struct list_blocks_struct *lb)
else
fprintf(lb->f, ", ");
if (lb->first_block == lb->last_block)
- fprintf(lb->f, "(%d):%d", lb->first_bcnt, lb->first_block);
+ fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
else
- fprintf(lb->f, "(%d-%d):%d-%d", lb->first_bcnt,
+ fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
lb->last_bcnt, lb->first_block, lb->last_block);
lb->first_block = 0;
}
-static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
+static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
+ blk_t *blocknr, e2_blkcnt_t blockcnt,
+ blk_t ref_block EXT2FS_ATTR((unused)),
+ int ref_offset EXT2FS_ATTR((unused)),
void *private)
{
struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
}
-static void dump_blocks(FILE *f, char *prefix, ext2_ino_t inode)
+static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
{
struct list_blocks_struct lb;
lb.first_block = 0;
lb.f = f;
lb.first = 1;
- ext2fs_block_iterate(current_fs, inode, 0, NULL,
+ ext2fs_block_iterate2(current_fs, inode, 0, NULL,
list_blocks_proc, (void *)&lb);
finish_range(&lb);
if (lb.total)
- fprintf(f, "\n%sTOTAL: %d\n", prefix, lb.total);
+ fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
fprintf(f,"\n");
}
-void internal_dump_inode(FILE *out, char *prefix,
+void internal_dump_inode(FILE *out, const char *prefix,
ext2_ino_t inode_num, struct ext2_inode *inode,
int do_dump_blocks)
{
inode->i_mode & 0777, inode->i_flags, inode->i_generation);
fprintf(out, "%sUser: %5d Group: %5d Size: ",
prefix, inode->i_uid, inode->i_gid);
- if (LINUX_S_ISDIR(inode->i_mode))
- fprintf(out, "%d\n", inode->i_size);
- else {
+ if (LINUX_S_ISREG(inode->i_mode)) {
__u64 i_size = (inode->i_size |
((unsigned long long)inode->i_size_high << 32));
-
+
fprintf(out, "%lld\n", i_size);
- }
+ } else
+ fprintf(out, "%d\n", inode->i_size);
if (current_fs->super->s_creator_os == EXT2_OS_HURD)
fprintf(out,
"%sFile ACL: %d Directory ACL: %d Translator: %d\n",
if (inode->i_dtime)
fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
time_to_string(inode->i_dtime));
- if (LINUX_S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
+ if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
(int) inode->i_size, (char *)inode->i_block);
+ else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
+ int major, minor;
+ const char *devnote;
+
+ if (inode->i_block[0]) {
+ major = (inode->i_block[0] >> 8) & 255;
+ minor = inode->i_block[0] & 255;
+ devnote = "";
+ } else {
+ major = (inode->i_block[1] & 0xfff00) >> 8;
+ minor = ((inode->i_block[1] & 0xff) |
+ ((inode->i_block[1] >> 12) & 0xfff00));
+ devnote = "(New-style) ";
+ }
+ fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
+ devnote, major, minor, major, minor);
+ }
else if (do_dump_blocks)
dump_blocks(out, prefix, inode_num);
}
close_pager(out);
}
-
void do_stat(int argc, char *argv[])
{
ext2_ino_t inode;
struct ext2_inode inode_buf;
- int retval;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: stat <file>");
- return;
- }
- if (check_fs_open(argv[0]))
- return;
- inode = string_to_inode(argv[1]);
- if (!inode)
+ if (common_inode_args_process(argc, argv, &inode, 0))
return;
- retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
- if (retval)
- {
- com_err(argv[0], retval, "while trying to read inode %d", inode);
- return;
- }
+ if (debugfs_read_inode(inode, &inode_buf, argv[0]))
+ return;
dump_inode(inode,inode_buf);
return;
ext2_ino_t inode;
int retval;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: chroot <file>");
- return;
- }
- if (check_fs_open(argv[0]))
- return;
- inode = string_to_inode(argv[1]);
- if (!inode)
+ if (common_inode_args_process(argc, argv, &inode, 0))
return;
retval = ext2fs_check_directory(current_fs, inode);
void do_clri(int argc, char *argv[])
{
ext2_ino_t inode;
- int retval;
struct ext2_inode inode_buf;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: clri <file>");
- return;
- }
- if (check_fs_open(argv[0]))
- return;
- if (check_fs_read_write(argv[0]))
- return;
- inode = string_to_inode(argv[1]);
- if (!inode)
+ if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
return;
- retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
- if (retval) {
- com_err(argv[0], retval, "while trying to read inode %d",
- inode);
+ if (debugfs_read_inode(inode, &inode_buf, argv[0]))
return;
- }
memset(&inode_buf, 0, sizeof(inode_buf));
- retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
- if (retval) {
- com_err(argv[0], retval, "while trying to write inode %d",
- inode);
+ if (debugfs_write_inode(inode, &inode_buf, argv[0]))
return;
- }
}
void do_freei(int argc, char *argv[])
{
ext2_ino_t inode;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: freei <file>");
- return;
- }
- if (check_fs_open(argv[0]))
- return;
- if (check_fs_read_write(argv[0]))
- return;
- inode = string_to_inode(argv[1]);
- if (!inode)
+ if (common_inode_args_process(argc, argv, &inode,
+ CHECK_FS_RW | CHECK_FS_BITMAPS))
return;
if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
{
ext2_ino_t inode;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: seti <file>");
- return;
- }
- if (check_fs_open(argv[0]))
- return;
- if (check_fs_read_write(argv[0]))
- return;
- inode = string_to_inode(argv[1]);
- if (!inode)
+ if (common_inode_args_process(argc, argv, &inode,
+ CHECK_FS_RW | CHECK_FS_BITMAPS))
return;
if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
{
ext2_ino_t inode;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: testi <file>");
- return;
- }
- if (check_fs_open(argv[0]))
- return;
- if (check_fs_bitmaps(argv[0]))
- return;
- inode = string_to_inode(argv[1]);
- if (!inode)
+ if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
return;
if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
printf("Inode %u is not in use\n", inode);
}
-
void do_freeb(int argc, char *argv[])
{
blk_t block;
- char *tmp;
+ int count = 1;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: freeb <block>");
- return;
- }
- if (check_fs_open(argv[0]))
+ if (common_block_args_process(argc, argv, &block, &count))
return;
if (check_fs_read_write(argv[0]))
return;
- block = strtoul(argv[1], &tmp, 0);
- if (!block || *tmp) {
- com_err(argv[0], 0, "No block 0");
- return;
- }
- if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
- com_err(argv[0], 0, "Warning: block already clear");
- ext2fs_unmark_block_bitmap(current_fs->block_map,block);
+ while (count-- > 0) {
+ if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
+ com_err(argv[0], 0, "Warning: block %d already clear",
+ block);
+ ext2fs_unmark_block_bitmap(current_fs->block_map,block);
+ block++;
+ }
ext2fs_mark_bb_dirty(current_fs);
}
void do_setb(int argc, char *argv[])
{
blk_t block;
- char *tmp;
+ int count = 1;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: setb <block>");
- return;
- }
- if (check_fs_open(argv[0]))
+ if (common_block_args_process(argc, argv, &block, &count))
return;
if (check_fs_read_write(argv[0]))
return;
- block = strtoul(argv[1], &tmp, 0);
- if (!block || *tmp) {
- com_err(argv[0], 0, "No block 0");
- return;
- }
- if (ext2fs_test_block_bitmap(current_fs->block_map,block))
- com_err(argv[0], 0, "Warning: block already set");
- ext2fs_mark_block_bitmap(current_fs->block_map,block);
+ while (count-- > 0) {
+ if (ext2fs_test_block_bitmap(current_fs->block_map,block))
+ com_err(argv[0], 0, "Warning: block %d already set",
+ block);
+ ext2fs_mark_block_bitmap(current_fs->block_map,block);
+ block++;
+ }
ext2fs_mark_bb_dirty(current_fs);
}
void do_testb(int argc, char *argv[])
{
blk_t block;
- char *tmp;
+ int count = 1;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: testb <block>");
+ if (common_block_args_process(argc, argv, &block, &count))
return;
+ while (count-- > 0) {
+ if (ext2fs_test_block_bitmap(current_fs->block_map,block))
+ printf("Block %d marked in use\n", block);
+ else
+ printf("Block %d not in use\n", block);
+ block++;
}
- if (check_fs_open(argv[0]))
- return;
- if (check_fs_bitmaps(argv[0]))
- return;
- block = strtoul(argv[1], &tmp, 0);
- if (!block || *tmp) {
- com_err(argv[0], 0, "No block 0");
- return;
- }
- if (ext2fs_test_block_bitmap(current_fs->block_map,block))
- printf("Block %d marked in use\n", block);
- else printf("Block %d not in use\n", block);
}
static void modify_u8(char *com, const char *prompt,
struct ext2_inode inode;
ext2_ino_t inode_num;
int i;
- errcode_t retval;
unsigned char *frag, *fsize;
char buf[80];
- int os = current_fs->super->s_creator_os;
+ int os;
const char *hex_format = "0x%x";
const char *octal_format = "0%o";
const char *decimal_format = "%d";
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: modify_inode <file>");
- return;
- }
- if (check_fs_open(argv[0]))
- return;
- if (check_fs_read_write(argv[0]))
+ if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
return;
- inode_num = string_to_inode(argv[1]);
- if (!inode_num)
- return;
+ os = current_fs->super->s_creator_os;
- retval = ext2fs_read_inode(current_fs, inode_num, &inode);
- if (retval) {
- com_err(argv[1], retval, "while trying to read inode %d",
- inode_num);
+ if (debugfs_read_inode(inode_num, &inode, argv[1]))
return;
- }
modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
&inode.i_block[EXT2_DIND_BLOCK]);
modify_u32(argv[0], "Triple Indirect Block", decimal_format,
&inode.i_block[EXT2_TIND_BLOCK]);
- retval = ext2fs_write_inode(current_fs, inode_num, &inode);
- if (retval) {
- com_err(argv[1], retval, "while trying to write inode %d",
- inode_num);
+ if (debugfs_write_inode(inode_num, &inode, argv[1]))
return;
- }
}
void do_change_working_dir(int argc, char *argv[])
ext2_ino_t inode;
int retval;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: cd <file>");
- return;
- }
- if (check_fs_open(argv[0]))
- return;
-
- inode = string_to_inode(argv[1]);
- if (!inode)
+ if (common_inode_args_process(argc, argv, &inode, 0))
return;
retval = ext2fs_check_directory(current_fs, inode);
int retval;
char *pathname = NULL;
- if (argc > 1) {
- com_err(argv[0], 0, "Usage: print_working_directory");
- return;
- }
- if (check_fs_open(argv[0]))
+ if (common_args_process(argc, argv, 1, 1,
+ "print_working_directory", "", 0))
return;
retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
return;
}
+/*
+ * Given a mode, return the ext2 file type
+ */
+static int ext2_file_type(unsigned int mode)
+{
+ if (LINUX_S_ISREG(mode))
+ return EXT2_FT_REG_FILE;
+
+ if (LINUX_S_ISDIR(mode))
+ return EXT2_FT_DIR;
+
+ if (LINUX_S_ISCHR(mode))
+ return EXT2_FT_CHRDEV;
+
+ if (LINUX_S_ISBLK(mode))
+ return EXT2_FT_BLKDEV;
+
+ if (LINUX_S_ISLNK(mode))
+ return EXT2_FT_SYMLINK;
+
+ if (LINUX_S_ISFIFO(mode))
+ return EXT2_FT_FIFO;
+
+ if (LINUX_S_ISSOCK(mode))
+ return EXT2_FT_SOCK;
+
+ return 0;
+}
+
static void make_link(char *sourcename, char *destname)
{
- ext2_ino_t inode;
+ ext2_ino_t ino;
+ struct ext2_inode inode;
int retval;
ext2_ino_t dir;
char *dest, *cp, *basename;
/*
* Get the source inode
*/
- inode = string_to_inode(sourcename);
- if (!inode)
+ ino = string_to_inode(sourcename);
+ if (!ino)
return;
basename = strrchr(sourcename, '/');
if (basename)
dest = destname;
}
}
+
+ if (debugfs_read_inode(ino, &inode, sourcename))
+ return;
- retval = ext2fs_link(current_fs, dir, dest, inode, 0);
+ retval = ext2fs_link(current_fs, dir, dest, ino,
+ ext2_file_type(inode.i_mode));
if (retval)
com_err("make_link", retval, "");
return;
void do_link(int argc, char *argv[])
{
- if (argc != 3) {
- com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
+ if (common_args_process(argc, argv, 3, 3, "link",
+ "<source file> <dest_name>", CHECK_FS_RW))
+ return;
+
+ make_link(argv[1], argv[2]);
+}
+
+static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
+ int blockcnt EXT2FS_ATTR((unused)),
+ void *private EXT2FS_ATTR((unused)))
+{
+ blk_t block;
+
+ block = *blocknr;
+ ext2fs_block_alloc_stats(fs, block, +1);
+ return 0;
+}
+
+void do_undel(int argc, char *argv[])
+{
+ ext2_ino_t ino;
+ struct ext2_inode inode;
+
+ if (common_args_process(argc, argv, 3, 3, "undelete",
+ "<inode_num> <dest_name>",
+ CHECK_FS_RW | CHECK_FS_BITMAPS))
+ return;
+
+ ino = string_to_inode(argv[1]);
+ if (!ino)
+ return;
+
+ if (debugfs_read_inode(ino, &inode, argv[1]))
+ return;
+
+ if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
+ com_err(argv[1], 0, "Inode is not marked as deleted");
return;
}
- if (check_fs_open(argv[0]))
+
+ /*
+ * XXX this function doesn't handle changing the links count on the
+ * parent directory when undeleting a directory.
+ */
+ inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
+ inode.i_dtime = 0;
+
+ if (debugfs_write_inode(ino, &inode, argv[0]))
return;
+ ext2fs_block_iterate(current_fs, ino, 0, NULL,
+ mark_blocks_proc, NULL);
+
+ ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
+
make_link(argv[1], argv[2]);
}
void do_unlink(int argc, char *argv[])
{
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: unlink <pathname>");
- return;
- }
- if (check_fs_open(argv[0]))
+ if (common_args_process(argc, argv, 2, 2, "link",
+ "<pathname>", CHECK_FS_RW))
return;
unlink_file_by_name(argv[1]);
void do_find_free_block(int argc, char *argv[])
{
blk_t free_blk, goal;
+ int count;
errcode_t retval;
char *tmp;
- if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
- com_err(argv[0], 0, "Usage: find_free_block [goal]");
+ if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
+ com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
return;
}
if (check_fs_open(argv[0]))
return;
if (argc > 1) {
- goal = strtol(argv[1], &tmp, 0);
+ count = strtol(argv[1],&tmp,0);
+ if (*tmp) {
+ com_err(argv[0], 0, "Bad count - %s", argv[1]);
+ return;
+ }
+ } else
+ count = 1;
+
+ if (argc > 2) {
+ goal = strtol(argv[2], &tmp, 0);
if (*tmp) {
com_err(argv[0], 0, "Bad goal - %s", argv[1]);
return;
else
goal = current_fs->super->s_first_data_block;
- retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
- if (retval)
- com_err("ext2fs_new_block", retval, "");
- else
- printf("Free block found: %d\n", free_blk);
-
+ printf("Free blocks found: ");
+ free_blk = goal - 1;
+ while (count-- > 0) {
+ retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
+ &free_blk);
+ if (retval) {
+ com_err("ext2fs_new_block", retval, "");
+ return;
+ } else
+ printf("%d ", free_blk);
+ }
+ printf("\n");
}
void do_find_free_inode(int argc, char *argv[])
{
ext2_file_t e2_file;
errcode_t retval;
- unsigned int got, written;
+ int got;
+ unsigned int written;
char buf[8192];
char *ptr;
}
}
retval = ext2fs_file_close(e2_file);
-
return retval;
fail:
ext2_ino_t newfile;
errcode_t retval;
struct ext2_inode inode;
- dgrp_t group;
- if (check_fs_open(argv[0]))
- return;
- if (argc != 3) {
- com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
- return;
- }
- if (check_fs_read_write(argv[0]))
+ if (common_args_process(argc, argv, 3, 3, "write",
+ "<native file> <new file>", CHECK_FS_RW))
return;
+
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
- com_err(argv[1], fd, "");
+ com_err(argv[1], errno, "");
return;
}
if (fstat(fd, &statbuf) < 0) {
return;
}
+ retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
+ if (retval == 0) {
+ com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
+ close(fd);
+ return;
+ }
+
retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
if (retval) {
com_err(argv[0], retval, "");
close(fd);
return;
}
- group = ext2fs_group_of_ino(current_fs, newfile);
- current_fs->group_desc[group].bg_free_inodes_count--;
- current_fs->super->s_free_inodes_count--;
- ext2fs_mark_super_dirty(current_fs);
printf("Allocated inode: %u\n", newfile);
- retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
+ retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
+ EXT2_FT_REG_FILE);
+ if (retval == EXT2_ET_DIR_NO_SPACE) {
+ retval = ext2fs_expand_dir(current_fs, cwd);
+ if (retval) {
+ com_err(argv[0], retval, "while expanding directory");
+ return;
+ }
+ retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
+ EXT2_FT_REG_FILE);
+ }
if (retval) {
com_err(argv[2], retval, "");
close(fd);
}
if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
com_err(argv[0], 0, "Warning: inode already set");
- ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
- ext2fs_mark_ib_dirty(current_fs);
+ ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
memset(&inode, 0, sizeof(inode));
- inode.i_mode = statbuf.st_mode;
+ inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
inode.i_links_count = 1;
inode.i_size = statbuf.st_size;
- retval = ext2fs_write_inode(current_fs, newfile, &inode);
- if (retval) {
- com_err(argv[0], retval, "while trying to write inode %d",
- inode);
+ if (debugfs_write_inode(newfile, &inode, argv[0])) {
close(fd);
return;
}
void do_mknod(int argc, char *argv[])
{
- unsigned long mode, major, minor, nr;
+ unsigned long mode, major, minor;
ext2_ino_t newfile;
errcode_t retval;
struct ext2_inode inode;
+ int filetype, nr;
if (check_fs_open(argv[0]))
return;
if (argc < 3 || argv[2][1]) {
+ usage:
com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
return;
}
switch (argv[2][0]) {
case 'p':
mode = LINUX_S_IFIFO;
+ filetype = EXT2_FT_FIFO;
nr = 3;
break;
case 'c':
mode = LINUX_S_IFCHR;
+ filetype = EXT2_FT_CHRDEV;
nr = 5;
break;
case 'b':
mode = LINUX_S_IFBLK;
+ filetype = EXT2_FT_BLKDEV;
nr = 5;
break;
default:
+ filetype = 0;
nr = 0;
}
if (nr == 5) {
major = strtoul(argv[3], argv+3, 0);
minor = strtoul(argv[4], argv+4, 0);
- if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
+ if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
nr = 0;
}
- if (argc != nr) {
- com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
- return;
- }
+ if (argc != nr)
+ goto usage;
if (check_fs_read_write(argv[0]))
return;
retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
return;
}
printf("Allocated inode: %u\n", newfile);
- retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
- if (retval) {
- if (retval == EXT2_ET_DIR_NO_SPACE) {
- retval = ext2fs_expand_dir(current_fs, cwd);
- if (!retval)
- retval = ext2fs_link(current_fs, cwd,
- argv[1], newfile, 0);
- }
+ retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
+ if (retval == EXT2_ET_DIR_NO_SPACE) {
+ retval = ext2fs_expand_dir(current_fs, cwd);
if (retval) {
- com_err(argv[1], retval, "");
+ com_err(argv[0], retval, "while expanding directory");
return;
}
+ retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
+ filetype);
+ }
+ if (retval) {
+ com_err(argv[1], retval, "");
+ return;
}
if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
com_err(argv[0], 0, "Warning: inode already set");
memset(&inode, 0, sizeof(inode));
inode.i_mode = mode;
inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
- inode.i_block[0] = major*256+minor;
+ if ((major < 256) && (minor < 256)) {
+ inode.i_block[0] = major*256+minor;
+ inode.i_block[1] = 0;
+ } else {
+ inode.i_block[0] = 0;
+ inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
+ }
inode.i_links_count = 1;
- retval = ext2fs_write_inode(current_fs, newfile, &inode);
- if (retval) {
- com_err(argv[0], retval, "while trying to write inode %d", inode);
+ if (debugfs_write_inode(newfile, &inode, argv[0]))
return;
- }
}
void do_mkdir(int argc, char *argv[])
char *name;
errcode_t retval;
- if (check_fs_open(argv[0]))
- return;
-
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: mkdir <file>");
- return;
- }
-
- if (check_fs_read_write(argv[0]))
+ if (common_args_process(argc, argv, 2, 2, "mkdir",
+ "<filename>", CHECK_FS_RW))
return;
cp = strrchr(argv[1], '/');
name = argv[1];
}
-
+try_again:
retval = ext2fs_mkdir(current_fs, parent, 0, name);
+ if (retval == EXT2_ET_DIR_NO_SPACE) {
+ retval = ext2fs_expand_dir(current_fs, parent);
+ if (retval) {
+ com_err("argv[0]", retval, "while expanding directory");
+ return;
+ }
+ goto try_again;
+ }
if (retval) {
com_err("ext2fs_mkdir", retval, "");
return;
}
-void do_rmdir(int argc, char *argv[])
-{
- printf("Unimplemented\n");
-}
-
-
static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
- int blockcnt, void *private)
+ int blockcnt EXT2FS_ATTR((unused)),
+ void *private EXT2FS_ATTR((unused)))
{
- printf("%d ", *blocknr);
- ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
+ blk_t block;
+
+ block = *blocknr;
+ ext2fs_block_alloc_stats(fs, block, -1);
return 0;
}
{
struct ext2_inode inode_buf;
- ext2fs_read_inode(current_fs, inode, &inode_buf);
+ if (debugfs_read_inode(inode, &inode_buf, 0))
+ return;
inode_buf.i_dtime = time(NULL);
- ext2fs_write_inode(current_fs, inode, &inode_buf);
+ if (debugfs_write_inode(inode, &inode_buf, 0))
+ return;
+ if (!ext2fs_inode_has_valid_blocks(&inode_buf))
+ return;
- printf("Kill file by inode %u\n", inode);
ext2fs_block_iterate(current_fs, inode, 0, NULL,
release_blocks_proc, NULL);
printf("\n");
- ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
-
- ext2fs_mark_bb_dirty(current_fs);
- ext2fs_mark_ib_dirty(current_fs);
+ ext2fs_inode_alloc_stats2(current_fs, inode, -1,
+ LINUX_S_ISDIR(inode_buf.i_mode));
}
{
ext2_ino_t inode_num;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: kill_file <file>");
- return;
- }
- if (check_fs_open(argv[0]))
+ if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
return;
- if (check_fs_read_write(argv[0]))
- return;
-
- inode_num = string_to_inode(argv[1]);
- if (!inode_num) {
- com_err(argv[0], 0, "Cannot find file");
- return;
- }
kill_file_by_inode(inode_num);
}
ext2_ino_t inode_num;
struct ext2_inode inode;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: rm <filename>");
+ if (common_args_process(argc, argv, 2, 2, "rm",
+ "<filename>", CHECK_FS_RW))
+ return;
+
+ retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
+ if (retval) {
+ com_err(argv[0], retval, "while trying to resolve filename");
return;
}
- if (check_fs_open(argv[0]))
+
+ if (debugfs_read_inode(inode_num, &inode, argv[0]))
return;
- if (check_fs_read_write(argv[0]))
+ if (LINUX_S_ISDIR(inode.i_mode)) {
+ com_err(argv[0], 0, "file is a directory");
+ return;
+ }
+
+ --inode.i_links_count;
+ if (debugfs_write_inode(inode_num, &inode, argv[0]))
+ return;
+
+ unlink_file_by_name(argv[1]);
+ if (inode.i_links_count == 0)
+ kill_file_by_inode(inode_num);
+}
+
+struct rd_struct {
+ ext2_ino_t parent;
+ int empty;
+};
+
+static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
+ int entry EXT2FS_ATTR((unused)),
+ struct ext2_dir_entry *dirent,
+ int offset EXT2FS_ATTR((unused)),
+ int blocksize EXT2FS_ATTR((unused)),
+ char *buf EXT2FS_ATTR((unused)),
+ void *private)
+{
+ struct rd_struct *rds = (struct rd_struct *) private;
+
+ if (dirent->inode == 0)
+ return 0;
+ if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
+ return 0;
+ if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
+ (dirent->name[1] == '.')) {
+ rds->parent = dirent->inode;
+ return 0;
+ }
+ rds->empty = 0;
+ return 0;
+}
+
+void do_rmdir(int argc, char *argv[])
+{
+ int retval;
+ ext2_ino_t inode_num;
+ struct ext2_inode inode;
+ struct rd_struct rds;
+
+ if (common_args_process(argc, argv, 2, 2, "rmdir",
+ "<filename>", CHECK_FS_RW))
return;
retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
return;
}
- retval = ext2fs_read_inode(current_fs,inode_num,&inode);
- if (retval) {
- com_err(argv[0], retval, "while reading file's inode");
+ if (debugfs_read_inode(inode_num, &inode, argv[0]))
return;
- }
- if (LINUX_S_ISDIR(inode.i_mode)) {
- com_err(argv[0], 0, "file is a directory");
+ if (!LINUX_S_ISDIR(inode.i_mode)) {
+ com_err(argv[0], 0, "file is not a directory");
return;
}
- --inode.i_links_count;
- retval = ext2fs_write_inode(current_fs,inode_num,&inode);
+ rds.parent = 0;
+ rds.empty = 1;
+
+ retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
+ 0, rmdir_proc, &rds);
if (retval) {
- com_err(argv[0], retval, "while writing inode");
+ com_err(argv[0], retval, "while iterating over directory");
+ return;
+ }
+ if (rds.empty == 0) {
+ com_err(argv[0], 0, "directory not empty");
return;
}
+ inode.i_links_count = 0;
+ if (debugfs_write_inode(inode_num, &inode, argv[0]))
+ return;
+
unlink_file_by_name(argv[1]);
- if (inode.i_links_count == 0)
- kill_file_by_inode(inode_num);
+ kill_file_by_inode(inode_num);
+
+ if (rds.parent) {
+ if (debugfs_read_inode(rds.parent, &inode, argv[0]))
+ return;
+ if (inode.i_links_count > 1)
+ inode.i_links_count--;
+ if (debugfs_write_inode(rds.parent, &inode, argv[0]))
+ return;
+ }
}
-void do_show_debugfs_params(int argc, char *argv[])
+void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
+ char *argv[] EXT2FS_ATTR((unused)))
{
FILE *out = stdout;
ext2_ino_t inode;
int retval;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: expand_dir <file>");
- return;
- }
- if (check_fs_open(argv[0]))
- return;
- inode = string_to_inode(argv[1]);
- if (!inode)
+ if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
return;
retval = ext2fs_expand_dir(current_fs, inode);
print_features(current_fs->super, stdout);
}
+void do_bmap(int argc, char *argv[])
+{
+ ext2_ino_t ino;
+ blk_t blk, pblk;
+ int err;
+ errcode_t errcode;
+
+ if (common_args_process(argc, argv, 3, 3, argv[0],
+ "<file> logical_blk", 0))
+ return;
+
+ ino = string_to_inode(argv[1]);
+ if (!ino)
+ return;
+ blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
+
+ errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
+ if (errcode) {
+ com_err("argv[0]", errcode,
+ "while mapping logical block %d\n", blk);
+ return;
+ }
+ printf("%d\n", pblk);
+}
+
+void do_imap(int argc, char *argv[])
+{
+ ext2_ino_t ino;
+ unsigned long group, block, block_nr, offset;
+
+ if (common_args_process(argc, argv, 2, 2, argv[0],
+ "<file>", 0))
+ return;
+ ino = string_to_inode(argv[1]);
+ if (!ino)
+ return;
+
+ group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
+ offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
+ EXT2_INODE_SIZE(current_fs->super);
+ block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
+ if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
+ com_err(argv[0], 0, "Inode table for group %lu is missing\n",
+ group);
+ return;
+ }
+ block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
+ block;
+ offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
+
+ printf("Inode %d is part of block group %lu\n"
+ "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
+ block_nr, offset);
+
+}
+
+
+
static int source_file(const char *cmd_file, int sci_idx)
{
FILE *f;
blk_t superblock = 0;
blk_t blocksize = 0;
int catastrophic = 0;
- char *tmp;
+ char *data_filename = 0;
initialize_ext2_error_table();
- fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
- E2FSPROGS_VERSION, E2FSPROGS_DATE,
- EXT2FS_VERSION, EXT2FS_DATE);
+ fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
+ E2FSPROGS_DATE);
- while ((c = getopt (argc, argv, "wcR:f:b:s:V")) != EOF) {
+ while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
switch (c) {
case 'R':
request = optarg;
case 'f':
cmd_file = optarg;
break;
+ case 'd':
+ data_filename = optarg;
+ break;
+ case 'i':
+ open_flags |= EXT2_FLAG_IMAGE_FILE;
+ break;
case 'w':
- open_flags = EXT2_FLAG_RW;
+ open_flags |= EXT2_FLAG_RW;
break;
case 'b':
- blocksize = strtoul(optarg, &tmp, 0);
- if (*tmp) {
- com_err(argv[0], 0,
- "Bad block size - %s", optarg);
- return 1;
- }
+ blocksize = parse_ulong(optarg, argv[0],
+ "block size", 0);
break;
case 's':
- superblock = strtoul(optarg, &tmp, 0);
- if (*tmp) {
- com_err(argv[0], 0,
- "Bad superblock number - %s", optarg);
- return 1;
- }
+ superblock = parse_ulong(optarg, argv[0],
+ "superblock number", 0);
break;
case 'c':
catastrophic = 1;
}
if (optind < argc)
open_filesystem(argv[optind], open_flags,
- superblock, blocksize, catastrophic);
+ superblock, blocksize, catastrophic,
+ data_filename);
sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
&debug_cmds, &retval);
ss_perror(sci_idx, retval, "creating invocation");
exit(1);
}
+ ss_get_readline(sci_idx);
(void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
if (retval) {