由于我没有得到任何简单的解决方案,我最终通过添加一个do_rstat
基于do_stat
in的新函数来修补 debugfs debugfs/debugfs.c
。有点像这样:
void do_rstat(int argc, char *argv[])
{
ext3_ino_t ifrom, ito, inode;
struct ext3_inode * inode_buf;
errcode_t r;
if (argc<4) {
fprintf(stderr, "do_rstat: needs an inodes range (3 arguments).\n");
return;
}
if (check_fs_open(argv[1]))
return;
if (string_to_inode_range(argv[2], argv[2], &ifrom, &ito) & 0x2) {
fprintf(stderr, "do_rstat: invalid inodes range.\n");
return;
}
inode_buf = (struct ext3_inode *)
malloc(EXT3_INODE_SIZE(current_fs->super));
if (!inode_buf) {
fprintf(stderr, "do_rstat: can't allocate buffer\n");
return;
}
for (inode = ifrom; inode<ito; inode++) {
r = debugfs_read_inode_full(inode, inode_buf, argv[1],
EXT3_INODE_SIZE(current_fs->super));
if (r) {
FILE *out;
out = open_pager();
fprintf(out, "%13ud ERROR %ld\n\n", inode, r);
close_pager(out);
} else
dump_inode(inode, inode_buf);
}
free(inode_buf);
return;
}
(但是,我不会在这里详细说明它,因为它完全符合我的需求,但我还替换了dump_inode(inode, inode_buf);
调用以在具有固定宽度字段的一行上获取输出,以消除调用程序中对模式匹配的需要)。为了做到这一点,我从dump_inode
anddump_inode_internal
函数中启发了自己。
我还需要添加两个函数debugfs/util.c
/*
* This routine is used whenever a command needs to turn an <ino> string
* into an inode.
*/
ext2_ino_t string_to_inode_number(char *str)
{
ext2_ino_t ino;
int len = strlen(str);
char *end;
if ((len<2) || (str[0] != '<') || (str[len-1] != '>')) return 0;
ino=strtoul(str+1, &end, 0);
if (*end!='>') return -1;
return ino;
}
/*
* This routine is used whenever a command needs to turn 2 <ino> strings
* into an inodes range ( [ino1, ino2[ ).
*/
int string_to_inode_range(char *str1, char *str2, ext2_ino_t *ino1, ext2_ino_t *ino2)
{
int invalid = 0;
ext2_ino_t inox;
*ino1 = string_to_inode_number(str1);
*ino2 = string_to_inode_number(str2);
if (*ino2 == -1) {
*ino2 = current_fs->super->s_inodes_count+1;
invalid |= 0x1;
}
if (*ino1 >= *ino2) {
inox = *ino1;
*ino1 = *ino2;
*ino2 = inox;
invalid |= 0x2;
}
if (*ino1 <= 0) {
*ino1 = 1;
invalid |= 0x4;
}
if (*ino2 > current_fs->super->s_inodes_count+1) {
*ino2 = current_fs->super->s_inodes_count+1;
invalid |= 0x8;
}
return invalid;
}
我需要添加他们的原型debugfs/debugfs.h
extern ext2_ino_t string_to_inode_number(char *str);
extern int string_to_inode_range(char *str1, char *str2, ext2_ino_t *ino1, ext2_ino_t *ino2);
我还为新命令添加了一个条目debug_commands.ct
:
request do_rstat, "Show inodes information ",
show_inodes_info, rstat;
当我调用它时,我需要确保输出是通过管道传输的(如在 中 | cat
),以停用烦人的寻呼机。
例如:
> debugfs -cD -R 'rstat <100> <200>' /dev/sdXXX | cat
(别忘了 /dev/sdXXX 一定不能挂载)