OSDN Git Service

debugfs: build read-only variant of debugfs
[android-x86/external-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
20 #include "debugfs.h"
21
22 struct inode_walk_struct {
23         ext2_ino_t              *iarray;
24         int                     inodes_left;
25         int                     num_inodes;
26         int                     position;
27         char                    *parent;
28 };
29
30 static int ncheck_proc(struct ext2_dir_entry *dirent,
31                        int      offset EXT2FS_ATTR((unused)),
32                        int      blocksize EXT2FS_ATTR((unused)),
33                        char     *buf EXT2FS_ATTR((unused)),
34                        void     *private)
35 {
36         struct inode_walk_struct *iw = (struct inode_walk_struct *) private;
37         int     i;
38
39         iw->position++;
40         if (iw->position <= 2)
41                 return 0;
42         for (i=0; i < iw->num_inodes; i++) {
43                 if (iw->iarray[i] == dirent->inode) {
44                         printf("%u\t%s/%.*s\n", iw->iarray[i], iw->parent,
45                                (dirent->name_len & 0xFF), dirent->name);
46                 }
47         }
48         if (!iw->inodes_left)
49                 return DIRENT_ABORT;
50
51         return 0;
52 }
53
54 void do_ncheck(int argc, char **argv)
55 {
56         struct inode_walk_struct iw;
57         int                     i;
58         ext2_inode_scan         scan = 0;
59         ext2_ino_t              ino;
60         struct ext2_inode       inode;
61         errcode_t               retval;
62         char                    *tmp;
63
64         if (argc < 2) {
65                 com_err(argv[0], 0, "Usage: ncheck <inode number> ...");
66                 return;
67         }
68         if (check_fs_open(argv[0]))
69                 return;
70
71         iw.iarray = malloc(sizeof(ext2_ino_t) * argc);
72         if (!iw.iarray) {
73                 com_err("ncheck", ENOMEM,
74                         "while allocating inode info array");
75                 return;
76         }
77         memset(iw.iarray, 0, sizeof(ext2_ino_t) * argc);
78
79         for (i=1; i < argc; i++) {
80                 iw.iarray[i-1] = strtol(argv[i], &tmp, 0);
81                 if (*tmp) {
82                         com_err(argv[0], 0, "Bad inode - %s", argv[i]);
83                         goto error_out;
84                 }
85         }
86
87         iw.num_inodes = iw.inodes_left = argc-1;
88
89         retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
90         if (retval) {
91                 com_err("ncheck", retval, "while opening inode scan");
92                 goto error_out;
93         }
94
95         do {
96                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
97         } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
98         if (retval) {
99                 com_err("ncheck", retval, "while starting inode scan");
100                 goto error_out;
101         }
102
103         printf("Inode\tPathname\n");
104         while (ino) {
105                 if (!inode.i_links_count)
106                         goto next;
107                 /*
108                  * To handle filesystems touched by 0.3c extfs; can be
109                  * removed later.
110                  */
111                 if (inode.i_dtime)
112                         goto next;
113                 /* Ignore anything that isn't a directory */
114                 if (!LINUX_S_ISDIR(inode.i_mode))
115                         goto next;
116
117                 iw.position = 0;
118
119                 retval = ext2fs_get_pathname(current_fs, ino, 0, &iw.parent);
120                 if (retval) {
121                         com_err("ncheck", retval, 
122                                 "while calling ext2fs_get_pathname");
123                         goto next;
124                 }
125
126                 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
127                                             ncheck_proc, &iw);
128                 ext2fs_free_mem(&iw.parent);
129                 if (retval) {
130                         com_err("ncheck", retval,
131                                 "while calling ext2_dir_iterate");
132                         goto next;
133                 }
134
135                 if (iw.inodes_left == 0)
136                         break;
137
138         next:
139                 do {
140                         retval = ext2fs_get_next_inode(scan, &ino, &inode);
141                 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
142
143                 if (retval) {
144                         com_err("ncheck", retval,
145                                 "while doing inode scan");
146                         goto error_out;
147                 }
148         }
149
150 error_out:
151         free(iw.iarray);
152         if (scan)
153                 ext2fs_close_inode_scan(scan);
154         return;
155 }
156
157
158