2 * ncheck.c --- given a list of inodes, generate a list of names
4 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
18 #include <sys/types.h>
28 struct inode_walk_struct {
35 unsigned int get_pathname_failed:1;
36 unsigned int check_dirent:1;
39 static int ncheck_proc(struct ext2_dir_entry *dirent,
40 int offset EXT2FS_ATTR((unused)),
41 int blocksize EXT2FS_ATTR((unused)),
42 char *buf EXT2FS_ATTR((unused)),
45 struct inode_walk_struct *iw = (struct inode_walk_struct *) private;
46 struct ext2_inode inode;
48 int filetype = ext2fs_dirent_file_type(dirent);
52 if (iw->position <= 2)
54 if (current_fs->super->s_feature_incompat &
55 EXT4_FEATURE_INCOMPAT_DIRDATA)
56 filetype &= EXT2_FT_MASK;
57 for (i=0; i < iw->num_inodes; i++) {
58 if (iw->iarray[i] == dirent->inode) {
59 if (!iw->parent && !iw->get_pathname_failed) {
60 retval = ext2fs_get_pathname(current_fs,
64 com_err("ncheck", retval,
65 "while calling ext2fs_get_pathname for inode #%u", iw->dir);
66 iw->get_pathname_failed = 1;
70 printf("%u\t%s/%.*s", iw->iarray[i],
72 ext2fs_dirent_name_len(dirent),
75 printf("%u\t<%u>/%.*s", iw->iarray[i],
77 ext2fs_dirent_name_len(dirent),
79 if (iw->check_dirent && filetype) {
80 if (!debugfs_read_inode(dirent->inode, &inode,
82 filetype != ext2_file_type(inode.i_mode)) {
83 printf(" <--- BAD FILETYPE");
96 void do_ncheck(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
97 void *infop EXT2FS_ATTR((unused)))
99 struct inode_walk_struct iw;
101 ext2_inode_scan scan = 0;
103 struct ext2_inode inode;
110 while ((c = getopt (argc, argv, "c")) != EOF) {
122 com_err(argv[0], 0, "Usage: ncheck [-c] <inode number> ...");
125 if (check_fs_open(argv[0]))
130 iw.iarray = malloc(sizeof(ext2_ino_t) * argc);
132 com_err("ncheck", ENOMEM,
133 "while allocating inode number array");
136 memset(iw.iarray, 0, sizeof(ext2_ino_t) * argc);
139 for (i=0; i < argc; i++) {
141 int len = strlen(str);
143 if ((len > 2) && (str[0] == '<') && (str[len - 1] == '>'))
145 iw.iarray[i] = strtol(str, &tmp, 0);
146 if (*tmp && (str == argv[i] || *tmp != '>')) {
147 com_err("ncheck", 0, "Invalid inode number - '%s'",
151 if (debugfs_read_inode(iw.iarray[i], &inode, *argv))
153 if (LINUX_S_ISDIR(inode.i_mode))
156 iw.names_left += inode.i_links_count;
159 iw.num_inodes = argc;
161 retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
163 com_err("ncheck", retval, "while opening inode scan");
168 retval = ext2fs_get_next_inode(scan, &ino, &inode);
169 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
171 com_err("ncheck", retval, "while starting inode scan");
175 printf("Inode\tPathname\n");
177 if (!inode.i_links_count)
180 * To handle filesystems touched by 0.3c extfs; can be
185 /* Ignore anything that isn't a directory */
186 if (!LINUX_S_ISDIR(inode.i_mode))
192 iw.get_pathname_failed = 0;
194 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
196 ext2fs_free_mem(&iw.parent);
198 com_err("ncheck", retval,
199 "while calling ext2_dir_iterate");
203 if (iw.names_left == 0)
208 retval = ext2fs_get_next_inode(scan, &ino, &inode);
209 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
212 com_err("ncheck", retval,
213 "while doing inode scan");
221 ext2fs_close_inode_scan(scan);