Whamcloud - gitweb
debugfs: Fix ncheck when printing pathnames for multiple hardlinks in a directory
[tools/e2fsprogs.git] / debugfs / ncheck.c
1 /*
2  * ncheck.c --- given a list of inodes, generate a list of names
3  *
4  * Copyright (C) 1994 Theodore Ts'o.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <time.h>
14 #ifdef HAVE_ERRNO_H
15 #include <errno.h>
16 #endif
17 #include <sys/types.h>
18
19 #include "debugfs.h"
20
21 struct inode_walk_struct {
22         ext2_ino_t              *iarray;
23         int                     inodes_left;
24         int                     num_inodes;
25         int                     position;
26         char                    *parent;
27 };
28
29 static int ncheck_proc(struct ext2_dir_entry *dirent,
30                        int      offset EXT2FS_ATTR((unused)),
31                        int      blocksize EXT2FS_ATTR((unused)),
32                        char     *buf EXT2FS_ATTR((unused)),
33                        void     *private)
34 {
35         struct inode_walk_struct *iw = (struct inode_walk_struct *) private;
36         int     i;
37         char    *pathname;
38         errcode_t       retval;
39
40         iw->position++;
41         if (iw->position <= 2)
42                 return 0;
43         for (i=0; i < iw->num_inodes; i++) {
44                 if (iw->iarray[i] == dirent->inode) {
45                         printf("%u\t%s/%*s\n", iw->iarray[i], iw->parent,
46                                (dirent->name_len & 0xFF), dirent->name);
47                 }
48         }
49         if (!iw->inodes_left)
50                 return DIRENT_ABORT;
51
52         return 0;
53 }
54
55 void do_ncheck(int argc, char **argv)
56 {
57         struct inode_walk_struct iw;
58         int                     i;
59         ext2_inode_scan         scan = 0;
60         ext2_ino_t              ino;
61         struct ext2_inode       inode;
62         errcode_t               retval;
63         char                    *tmp;
64
65         if (argc < 2) {
66                 com_err(argv[0], 0, "Usage: ncheck <inode number> ...");
67                 return;
68         }
69         if (check_fs_open(argv[0]))
70                 return;
71
72         iw.iarray = malloc(sizeof(ext2_ino_t) * argc);
73         if (!iw.iarray) {
74                 com_err("ncheck", ENOMEM,
75                         "while allocating inode info array");
76                 return;
77         }
78         memset(iw.iarray, 0, sizeof(ext2_ino_t) * argc);
79
80         for (i=1; i < argc; i++) {
81                 iw.iarray[i-1] = strtol(argv[i], &tmp, 0);
82                 if (*tmp) {
83                         com_err(argv[0], 0, "Bad inode - %s", argv[i]);
84                         goto error_out;
85                 }
86         }
87
88         iw.num_inodes = iw.inodes_left = argc-1;
89
90         retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
91         if (retval) {
92                 com_err("ncheck", retval, "while opening inode scan");
93                 goto error_out;
94         }
95
96         do {
97                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
98         } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
99         if (retval) {
100                 com_err("ncheck", retval, "while starting inode scan");
101                 goto error_out;
102         }
103
104         printf("Inode\tPathname\n");
105         while (ino) {
106                 if (!inode.i_links_count)
107                         goto next;
108                 /*
109                  * To handle filesystems touched by 0.3c extfs; can be
110                  * removed later.
111                  */
112                 if (inode.i_dtime)
113                         goto next;
114                 /* Ignore anything that isn't a directory */
115                 if (!LINUX_S_ISDIR(inode.i_mode))
116                         goto next;
117
118                 iw.position = 0;
119
120                 retval = ext2fs_get_pathname(current_fs, ino, 0, &iw.parent);
121                 if (retval) {
122                         com_err("ncheck", retval, 
123                                 "while calling ext2fs_get_pathname");
124                         goto next;
125                 }
126
127                 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
128                                             ncheck_proc, &iw);
129                 ext2fs_free_mem(&iw.parent);
130                 if (retval) {
131                         com_err("ncheck", retval,
132                                 "while calling ext2_dir_iterate");
133                         goto next;
134                 }
135
136                 if (iw.inodes_left == 0)
137                         break;
138
139         next:
140                 do {
141                         retval = ext2fs_get_next_inode(scan, &ino, &inode);
142                 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
143
144                 if (retval) {
145                         com_err("ncheck", retval,
146                                 "while doing inode scan");
147                         goto error_out;
148                 }
149         }
150
151 error_out:
152         free(iw.iarray);
153         if (scan)
154                 ext2fs_close_inode_scan(scan);
155         return;
156 }
157
158
159