Whamcloud - gitweb
LU-11545 debugfs: allow <inode> for ncheck
[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 "config.h"
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <time.h>
15 #ifdef HAVE_ERRNO_H
16 #include <errno.h>
17 #endif
18 #include <sys/types.h>
19 #ifdef HAVE_GETOPT_H
20 #include <getopt.h>
21 #else
22 extern int optind;
23 extern char *optarg;
24 #endif
25
26 #include "debugfs.h"
27
28 struct inode_walk_struct {
29         ext2_ino_t              dir;
30         ext2_ino_t              *iarray;
31         int                     names_left;
32         int                     num_inodes;
33         int                     position;
34         char                    *parent;
35         unsigned int            get_pathname_failed:1;
36         unsigned int            check_dirent:1;
37 };
38
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)),
43                        void     *private)
44 {
45         struct inode_walk_struct *iw = (struct inode_walk_struct *) private;
46         struct ext2_inode inode;
47         errcode_t       retval;
48         int             filetype = ext2fs_dirent_file_type(dirent);
49         int             i;
50
51         iw->position++;
52         if (iw->position <= 2)
53                 return 0;
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,
61                                                              iw->dir,
62                                                              0, &iw->parent);
63                                 if (retval) {
64                                         com_err("ncheck", retval,
65                 "while calling ext2fs_get_pathname for inode #%u", iw->dir);
66                                         iw->get_pathname_failed = 1;
67                                 }
68                         }
69                         if (iw->parent)
70                                 printf("%u\t%s/%.*s", iw->iarray[i],
71                                        iw->parent,
72                                        ext2fs_dirent_name_len(dirent),
73                                        dirent->name);
74                         else
75                                 printf("%u\t<%u>/%.*s", iw->iarray[i],
76                                        iw->dir,
77                                        ext2fs_dirent_name_len(dirent),
78                                        dirent->name);
79                         if (iw->check_dirent && filetype) {
80                                 if (!debugfs_read_inode(dirent->inode, &inode,
81                                                         "ncheck") &&
82                                     filetype != ext2_file_type(inode.i_mode)) {
83                                         printf("  <--- BAD FILETYPE");
84                                 }
85                         }
86                         putc('\n', stdout);
87                         iw->names_left--;
88                 }
89         }
90         if (!iw->names_left)
91                 return DIRENT_ABORT;
92
93         return 0;
94 }
95
96 void do_ncheck(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
97                void *infop EXT2FS_ATTR((unused)))
98 {
99         struct inode_walk_struct iw;
100         int                     c, i;
101         ext2_inode_scan         scan = 0;
102         ext2_ino_t              ino;
103         struct ext2_inode       inode;
104         errcode_t               retval;
105         char                    *tmp;
106
107         iw.check_dirent = 0;
108
109         reset_getopt();
110         while ((c = getopt (argc, argv, "c")) != EOF) {
111                 switch (c) {
112                 case 'c':
113                         iw.check_dirent = 1;
114                         break;
115                 default:
116                         goto print_usage;
117                 }
118         }
119
120         if (argc <= 1) {
121         print_usage:
122                 com_err(argv[0], 0, "Usage: ncheck [-c] <inode number> ...");
123                 return;
124         }
125         if (check_fs_open(argv[0]))
126                 return;
127
128         argc -= optind;
129         argv += optind;
130         iw.iarray = malloc(sizeof(ext2_ino_t) * argc);
131         if (!iw.iarray) {
132                 com_err("ncheck", ENOMEM,
133                         "while allocating inode number array");
134                 return;
135         }
136         memset(iw.iarray, 0, sizeof(ext2_ino_t) * argc);
137
138         iw.names_left = 0;
139         for (i=0; i < argc; i++) {
140                 char *str = argv[i];
141                 int len = strlen(str);
142
143                 if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
144                         str[len-1] = '\0';
145                         str++;
146                 }
147                 iw.iarray[i] = strtol(str, &tmp, 0);
148                 if (*tmp) {
149                         if (str != argv[i]) {
150                                 str--;
151                                 str[len-1] = '>';
152                         }
153                         com_err("ncheck", 0, "Invalid inode number - '%s'",
154                                 argv[i]);
155                         goto error_out;
156                 }
157                 if (debugfs_read_inode(iw.iarray[i], &inode, *argv))
158                         goto error_out;
159                 if (LINUX_S_ISDIR(inode.i_mode))
160                         iw.names_left += 1;
161                 else
162                         iw.names_left += inode.i_links_count;
163         }
164
165         iw.num_inodes = argc;
166
167         retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
168         if (retval) {
169                 com_err("ncheck", retval, "while opening inode scan");
170                 goto error_out;
171         }
172
173         do {
174                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
175         } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
176         if (retval) {
177                 com_err("ncheck", retval, "while starting inode scan");
178                 goto error_out;
179         }
180
181         printf("Inode\tPathname\n");
182         while (ino) {
183                 if (!inode.i_links_count)
184                         goto next;
185                 /*
186                  * To handle filesystems touched by 0.3c extfs; can be
187                  * removed later.
188                  */
189                 if (inode.i_dtime)
190                         goto next;
191                 /* Ignore anything that isn't a directory */
192                 if (!LINUX_S_ISDIR(inode.i_mode))
193                         goto next;
194
195                 iw.position = 0;
196                 iw.parent = 0;
197                 iw.dir = ino;
198                 iw.get_pathname_failed = 0;
199
200                 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
201                                             ncheck_proc, &iw);
202                 ext2fs_free_mem(&iw.parent);
203                 if (retval) {
204                         com_err("ncheck", retval,
205                                 "while calling ext2_dir_iterate");
206                         goto next;
207                 }
208
209                 if (iw.names_left == 0)
210                         break;
211
212         next:
213                 do {
214                         retval = ext2fs_get_next_inode(scan, &ino, &inode);
215                 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
216
217                 if (retval) {
218                         com_err("ncheck", retval,
219                                 "while doing inode scan");
220                         goto error_out;
221                 }
222         }
223
224 error_out:
225         free(iw.iarray);
226         if (scan)
227                 ext2fs_close_inode_scan(scan);
228         return;
229 }
230
231
232