X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=debugfs%2Fncheck.c;h=10a38a34bf935f1536bc3820995975d6ddfe7cd2;hb=bad5ce5ca531b65116edfc9c79abc65b1c6ab9ed;hp=7dec34340426462f7b0af2de4c17e03b2b5a2473;hpb=1e3472c5f37ca3686dd69b079d4d02a302f5798d;p=tools%2Fe2fsprogs.git diff --git a/debugfs/ncheck.c b/debugfs/ncheck.c index 7dec343..10a38a3 100644 --- a/debugfs/ncheck.c +++ b/debugfs/ncheck.c @@ -1,10 +1,11 @@ /* * ncheck.c --- given a list of inodes, generate a list of names - * + * * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed * under the terms of the GNU Public License. */ +#include "config.h" #include #include #include @@ -15,113 +16,170 @@ #include #endif #include +#ifdef HAVE_GETOPT_H +#include +#else +extern int optind; +extern char *optarg; +#endif #include "debugfs.h" -struct inode_info { - ino_t ino; - ino_t parent; - char *pathname; -}; - struct inode_walk_struct { - struct inode_info *iarray; + ext2_ino_t dir; + ext2_ino_t *iarray; int inodes_left; int num_inodes; int position; - ino_t parent; + char *parent; + unsigned int get_pathname_failed:1; + unsigned int check_dirent:1; }; static int ncheck_proc(struct ext2_dir_entry *dirent, - int offset, - int blocksize, - char *buf, + int offset EXT2FS_ATTR((unused)), + int blocksize EXT2FS_ATTR((unused)), + char *buf EXT2FS_ATTR((unused)), void *private) { struct inode_walk_struct *iw = (struct inode_walk_struct *) private; - int i; + struct ext2_inode inode; + errcode_t retval; + int filetype = dirent->name_len >> 8; + int i; iw->position++; if (iw->position <= 2) return 0; for (i=0; i < iw->num_inodes; i++) { - if (iw->iarray[i].ino == dirent->inode) { - iw->iarray[i].parent = iw->parent; - iw->inodes_left--; + if (iw->iarray[i] == dirent->inode) { + if (!iw->parent && !iw->get_pathname_failed) { + retval = ext2fs_get_pathname(current_fs, + iw->dir, + 0, &iw->parent); + if (retval) { + com_err("ncheck", retval, + "while calling ext2fs_get_pathname for inode #%u", iw->dir); + iw->get_pathname_failed = 1; + } + } + if (iw->parent) + printf("%u\t%s/%.*s", iw->iarray[i], + iw->parent, + (dirent->name_len & 0xFF), dirent->name); + else + printf("%u\t<%u>/%.*s", iw->iarray[i], + iw->dir, + (dirent->name_len & 0xFF), dirent->name); + if (iw->check_dirent && filetype) { + if (!debugfs_read_inode(dirent->inode, &inode, + "ncheck") && + filetype != ext2_file_type(inode.i_mode)) { + printf(" <--- BAD FILETYPE"); + } + } + putc('\n', stdout); } } if (!iw->inodes_left) return DIRENT_ABORT; - + return 0; } void do_ncheck(int argc, char **argv) { struct inode_walk_struct iw; - struct inode_info *iinfo; - int i; + int c, i; ext2_inode_scan scan = 0; - ino_t ino; - struct ext2_inode inode; + ext2_ino_t ino; + struct ext2_inode *inode = NULL; + int inode_size; errcode_t retval; char *tmp; - - if (argc < 2) { - com_err(argv[0], 0, "Usage: ncheck ..."); + + iw.check_dirent = 0; + + reset_getopt(); + while ((c = getopt (argc, argv, "c")) != EOF) { + switch (c) { + case 'c': + iw.check_dirent = 1; + break; + default: + goto print_usage; + } + } + argc -= optind; + argv += optind; + + if (argc < 1) { + print_usage: + com_err(argv[0], 0, "Usage: ncheck [-c] ..."); return; } if (check_fs_open(argv[0])) return; - iw.iarray = malloc(sizeof(struct inode_info) * argc); + iw.iarray = malloc(sizeof(ext2_ino_t) * argc); if (!iw.iarray) { - com_err("do_ncheck", ENOMEM, - "while allocating inode info array"); + com_err("ncheck", ENOMEM, + "while allocating inode number array"); return; } - memset(iw.iarray, 0, sizeof(struct inode_info) * argc); + memset(iw.iarray, 0, sizeof(ext2_ino_t) * argc); - for (i=1; i < argc; i++) { - iw.iarray[i-1].ino = strtol(argv[i], &tmp, 0); + for (i=0; i < argc; i++) { + iw.iarray[i] = strtol(argv[i], &tmp, 0); if (*tmp) { com_err(argv[0], 0, "Bad inode - %s", argv[i]); - return; + goto error_out; } } - iw.num_inodes = iw.inodes_left = argc-1; + iw.num_inodes = iw.inodes_left = argc; retval = ext2fs_open_inode_scan(current_fs, 0, &scan); if (retval) { com_err("ncheck", retval, "while opening inode scan"); goto error_out; } + inode_size = EXT2_INODE_SIZE(current_fs->super); + retval = ext2fs_get_mem(inode_size, &inode); + if (retval) + goto error_out; - retval = ext2fs_get_next_inode(scan, &ino, &inode); + do { + retval = ext2fs_get_next_inode_full(scan, &ino, + inode, inode_size); + } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE); if (retval) { com_err("ncheck", retval, "while starting inode scan"); goto error_out; } - + + printf("Inode\tPathname\n"); while (ino) { - if (!inode.i_links_count) + if (!inode->i_links_count) goto next; /* * To handle filesystems touched by 0.3c extfs; can be * removed later. */ - if (inode.i_dtime) + if (inode->i_dtime) goto next; /* Ignore anything that isn't a directory */ - if (!LINUX_S_ISDIR(inode.i_mode)) + if (!LINUX_S_ISDIR(inode->i_mode)) goto next; iw.position = 0; - iw.parent = ino; - + iw.parent = 0; + iw.dir = ino; + iw.get_pathname_failed = 0; + retval = ext2fs_dir_iterate(current_fs, ino, 0, 0, ncheck_proc, &iw); + ext2fs_free_mem(&iw.parent); if (retval) { com_err("ncheck", retval, "while calling ext2_dir_iterate"); @@ -132,7 +190,11 @@ void do_ncheck(int argc, char **argv) break; next: - retval = ext2fs_get_next_inode(scan, &ino, &inode); + do { + retval = ext2fs_get_next_inode_full(scan, &ino, + inode, inode_size); + } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE); + if (retval) { com_err("ncheck", retval, "while doing inode scan"); @@ -140,30 +202,8 @@ void do_ncheck(int argc, char **argv) } } - for (i=0, iinfo = iw.iarray; i < iw.num_inodes; i++, iinfo++) { - if (iinfo->parent == 0) - continue; - retval = ext2fs_get_pathname(current_fs, iinfo->parent, - iinfo->ino, &iinfo->pathname); - if (retval) - com_err("ncheck", retval, - "while resolving pathname for inode %d (%d)", - iinfo->parent, iinfo->ino); - } - - printf("Inode\tPathname\n"); - for (i=0, iinfo = iw.iarray; i < iw.num_inodes; i++, iinfo++) { - if (iinfo->parent == 0) { - printf("%ld\t\n", iinfo->ino); - continue; - } - printf("%ld\t%s\n", iinfo->ino, iinfo->pathname ? - iinfo->pathname : ""); - if (iinfo->pathname) - free(iinfo->pathname); - } - error_out: + ext2fs_free_mem(inode); free(iw.iarray); if (scan) ext2fs_close_inode_scan(scan);