Whamcloud - gitweb
LU-10638 build: add support for Scientific
[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                     inodes_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 = dirent->name_len >> 8;
49         int             i;
50
51         iw->position++;
52         if (iw->position <= 2)
53                 return 0;
54         for (i=0; i < iw->num_inodes; i++) {
55                 if (iw->iarray[i] == dirent->inode) {
56                         if (!iw->parent && !iw->get_pathname_failed) {
57                                 retval = ext2fs_get_pathname(current_fs,
58                                                              iw->dir,
59                                                              0, &iw->parent);
60                                 if (retval) {
61                                         com_err("ncheck", retval,
62                 "while calling ext2fs_get_pathname for inode #%u", iw->dir);
63                                         iw->get_pathname_failed = 1;
64                                 }
65                         }
66                         if (iw->parent)
67                                 printf("%u\t%s/%.*s", iw->iarray[i],
68                                        iw->parent,
69                                        (dirent->name_len & 0xFF), dirent->name);
70                         else
71                                 printf("%u\t<%u>/%.*s", iw->iarray[i],
72                                        iw->dir,
73                                        (dirent->name_len & 0xFF), dirent->name);
74                         if (iw->check_dirent && filetype) {
75                                 if (!debugfs_read_inode(dirent->inode, &inode,
76                                                         "ncheck") &&
77                                     filetype != ext2_file_type(inode.i_mode)) {
78                                         printf("  <--- BAD FILETYPE");
79                                 }
80                         }
81                         putc('\n', stdout);
82                 }
83         }
84         if (!iw->inodes_left)
85                 return DIRENT_ABORT;
86
87         return 0;
88 }
89
90 void do_ncheck(int argc, char **argv)
91 {
92         struct inode_walk_struct iw;
93         int                     c, i;
94         ext2_inode_scan         scan = 0;
95         ext2_ino_t              ino;
96         struct ext2_inode       *inode = NULL;
97         int                     inode_size;
98         errcode_t               retval;
99         char                    *tmp;
100
101         iw.check_dirent = 0;
102
103         reset_getopt();
104         while ((c = getopt (argc, argv, "c")) != EOF) {
105                 switch (c) {
106                 case 'c':
107                         iw.check_dirent = 1;
108                         break;
109                 default:
110                         goto print_usage;
111                 }
112         }
113         argc -= optind;
114         argv += optind;
115
116         if (argc < 1) {
117         print_usage:
118                 com_err(argv[0], 0, "Usage: ncheck [-c] <inode number> ...");
119                 return;
120         }
121         if (check_fs_open(argv[0]))
122                 return;
123
124         iw.iarray = malloc(sizeof(ext2_ino_t) * argc);
125         if (!iw.iarray) {
126                 com_err("ncheck", ENOMEM,
127                         "while allocating inode number array");
128                 return;
129         }
130         memset(iw.iarray, 0, sizeof(ext2_ino_t) * argc);
131
132         for (i=0; i < argc; i++) {
133                 iw.iarray[i] = strtol(argv[i], &tmp, 0);
134                 if (*tmp) {
135                         com_err(argv[0], 0, "Bad inode - %s", argv[i]);
136                         goto error_out;
137                 }
138         }
139
140         iw.num_inodes = iw.inodes_left = argc;
141
142         retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
143         if (retval) {
144                 com_err("ncheck", retval, "while opening inode scan");
145                 goto error_out;
146         }
147         inode_size = EXT2_INODE_SIZE(current_fs->super);
148         retval = ext2fs_get_mem(inode_size, &inode);
149         if (retval)
150                 goto error_out;
151
152         do {
153                 retval = ext2fs_get_next_inode_full(scan, &ino,
154                                                 inode, inode_size);
155         } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
156         if (retval) {
157                 com_err("ncheck", retval, "while starting inode scan");
158                 goto error_out;
159         }
160
161         printf("Inode\tPathname\n");
162         while (ino) {
163                 if (!inode->i_links_count)
164                         goto next;
165                 /*
166                  * To handle filesystems touched by 0.3c extfs; can be
167                  * removed later.
168                  */
169                 if (inode->i_dtime)
170                         goto next;
171                 /* Ignore anything that isn't a directory */
172                 if (!LINUX_S_ISDIR(inode->i_mode))
173                         goto next;
174
175                 iw.position = 0;
176                 iw.parent = 0;
177                 iw.dir = ino;
178                 iw.get_pathname_failed = 0;
179
180                 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
181                                             ncheck_proc, &iw);
182                 ext2fs_free_mem(&iw.parent);
183                 if (retval) {
184                         com_err("ncheck", retval,
185                                 "while calling ext2_dir_iterate");
186                         goto next;
187                 }
188
189                 if (iw.inodes_left == 0)
190                         break;
191
192         next:
193                 do {
194                         retval = ext2fs_get_next_inode_full(scan, &ino,
195                                                         inode, inode_size);
196                 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
197
198                 if (retval) {
199                         com_err("ncheck", retval,
200                                 "while doing inode scan");
201                         goto error_out;
202                 }
203         }
204
205 error_out:
206         ext2fs_free_mem(inode);
207         free(iw.iarray);
208         if (scan)
209                 ext2fs_close_inode_scan(scan);
210         return;
211 }
212
213
214