Whamcloud - gitweb
debugfs: fix ncheck so it handles hard links correctly
[tools/e2fsprogs.git] / debugfs / ncheck.c
index 739e001..9304c75 100644 (file)
 #include <errno.h>
 #endif
 #include <sys/types.h>
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#else
+extern int optind;
+extern char *optarg;
+#endif
 
 #include "debugfs.h"
 
 struct inode_walk_struct {
+       ext2_ino_t              dir;
        ext2_ino_t              *iarray;
-       int                     inodes_left;
+       int                     names_left;
        int                     num_inodes;
        int                     position;
        char                    *parent;
+       unsigned int            get_pathname_failed:1;
+       unsigned int            check_dirent:1;
 };
 
 static int ncheck_proc(struct ext2_dir_entry *dirent,
@@ -34,18 +43,51 @@ static int ncheck_proc(struct ext2_dir_entry *dirent,
                       void     *private)
 {
        struct inode_walk_struct *iw = (struct inode_walk_struct *) private;
-       int     i;
+       struct ext2_inode inode;
+       errcode_t       retval;
+       int             filetype = ext2fs_dirent_file_type(dirent);
+       int             i;
 
        iw->position++;
        if (iw->position <= 2)
                return 0;
+       if (current_fs->super->s_feature_incompat &
+                       EXT4_FEATURE_INCOMPAT_DIRDATA)
+               filetype &= EXT2_FT_MASK;
        for (i=0; i < iw->num_inodes; i++) {
                if (iw->iarray[i] == dirent->inode) {
-                       printf("%u\t%s/%.*s\n", iw->iarray[i], iw->parent,
-                              (dirent->name_len & 0xFF), dirent->name);
+                       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,
+                                      ext2fs_dirent_name_len(dirent),
+                                      dirent->name);
+                       else
+                               printf("%u\t<%u>/%.*s", iw->iarray[i],
+                                      iw->dir,
+                                      ext2fs_dirent_name_len(dirent),
+                                      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);
+                       iw->names_left--;
                }
        }
-       if (!iw->inodes_left)
+       if (!iw->names_left)
                return DIRENT_ABORT;
 
        return 0;
@@ -54,37 +96,60 @@ static int ncheck_proc(struct ext2_dir_entry *dirent,
 void do_ncheck(int argc, char **argv)
 {
        struct inode_walk_struct iw;
-       int                     i;
+       int                     c, i;
        ext2_inode_scan         scan = 0;
        ext2_ino_t              ino;
        struct ext2_inode       inode;
        errcode_t               retval;
        char                    *tmp;
 
-       if (argc < 2) {
-               com_err(argv[0], 0, "Usage: ncheck <inode number> ...");
+       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;
+               }
+       }
+
+       if (argc <= 1) {
+       print_usage:
+               com_err(argv[0], 0, "Usage: ncheck [-c] <inode number> ...");
                return;
        }
        if (check_fs_open(argv[0]))
                return;
 
+       argc -= optind;
+       argv += optind;
        iw.iarray = malloc(sizeof(ext2_ino_t) * argc);
        if (!iw.iarray) {
                com_err("ncheck", ENOMEM,
-                       "while allocating inode info array");
+                       "while allocating inode number array");
                return;
        }
        memset(iw.iarray, 0, sizeof(ext2_ino_t) * argc);
 
-       for (i=1; i < argc; i++) {
-               iw.iarray[i-1] = strtol(argv[i], &tmp, 0);
+       iw.names_left = 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]);
+                       com_err("ncheck", 0, "Bad inode - %s", argv[i]);
                        goto error_out;
                }
+               if (debugfs_read_inode(iw.iarray[i], &inode, *argv))
+                       goto error_out;
+               if (LINUX_S_ISDIR(inode.i_mode))
+                       iw.names_left += 1;
+               else
+                       iw.names_left += inode.i_links_count;
        }
 
-       iw.num_inodes = iw.inodes_left = argc-1;
+       iw.num_inodes = argc;
 
        retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
        if (retval) {
@@ -115,13 +180,9 @@ void do_ncheck(int argc, char **argv)
                        goto next;
 
                iw.position = 0;
-
-               retval = ext2fs_get_pathname(current_fs, ino, 0, &iw.parent);
-               if (retval) {
-                       com_err("ncheck", retval, 
-                               "while calling ext2fs_get_pathname");
-                       goto next;
-               }
+               iw.parent = 0;
+               iw.dir = ino;
+               iw.get_pathname_failed = 0;
 
                retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
                                            ncheck_proc, &iw);
@@ -132,7 +193,7 @@ void do_ncheck(int argc, char **argv)
                        goto next;
                }
 
-               if (iw.inodes_left == 0)
+               if (iw.names_left == 0)
                        break;
 
        next: