lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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; |
| 97 | errcode_t retval; |
| 98 | char *tmp; |
| 99 | |
| 100 | iw.check_dirent = 0; |
| 101 | |
| 102 | reset_getopt(); |
| 103 | while ((c = getopt (argc, argv, "c")) != EOF) { |
| 104 | switch (c) { |
| 105 | case 'c': |
| 106 | iw.check_dirent = 1; |
| 107 | break; |
| 108 | default: |
| 109 | goto print_usage; |
| 110 | } |
| 111 | } |
| 112 | argc -= optind; |
| 113 | argv += optind; |
| 114 | |
| 115 | if (argc < 1) { |
| 116 | print_usage: |
| 117 | com_err(argv[0], 0, "Usage: ncheck [-c] <inode number> ..."); |
| 118 | return; |
| 119 | } |
| 120 | if (check_fs_open(argv[0])) |
| 121 | return; |
| 122 | |
| 123 | iw.iarray = malloc(sizeof(ext2_ino_t) * argc); |
| 124 | if (!iw.iarray) { |
| 125 | com_err("ncheck", ENOMEM, |
| 126 | "while allocating inode number array"); |
| 127 | return; |
| 128 | } |
| 129 | memset(iw.iarray, 0, sizeof(ext2_ino_t) * argc); |
| 130 | |
| 131 | for (i=0; i < argc; i++) { |
| 132 | iw.iarray[i] = strtol(argv[i], &tmp, 0); |
| 133 | if (*tmp) { |
| 134 | com_err(argv[0], 0, "Bad inode - %s", argv[i]); |
| 135 | goto error_out; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | iw.num_inodes = iw.inodes_left = argc; |
| 140 | |
| 141 | retval = ext2fs_open_inode_scan(current_fs, 0, &scan); |
| 142 | if (retval) { |
| 143 | com_err("ncheck", retval, "while opening inode scan"); |
| 144 | goto error_out; |
| 145 | } |
| 146 | |
| 147 | do { |
| 148 | retval = ext2fs_get_next_inode(scan, &ino, &inode); |
| 149 | } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE); |
| 150 | if (retval) { |
| 151 | com_err("ncheck", retval, "while starting inode scan"); |
| 152 | goto error_out; |
| 153 | } |
| 154 | |
| 155 | printf("Inode\tPathname\n"); |
| 156 | while (ino) { |
| 157 | if (!inode.i_links_count) |
| 158 | goto next; |
| 159 | /* |
| 160 | * To handle filesystems touched by 0.3c extfs; can be |
| 161 | * removed later. |
| 162 | */ |
| 163 | if (inode.i_dtime) |
| 164 | goto next; |
| 165 | /* Ignore anything that isn't a directory */ |
| 166 | if (!LINUX_S_ISDIR(inode.i_mode)) |
| 167 | goto next; |
| 168 | |
| 169 | iw.position = 0; |
| 170 | iw.parent = 0; |
| 171 | iw.dir = ino; |
| 172 | iw.get_pathname_failed = 0; |
| 173 | |
| 174 | retval = ext2fs_dir_iterate(current_fs, ino, 0, 0, |
| 175 | ncheck_proc, &iw); |
| 176 | ext2fs_free_mem(&iw.parent); |
| 177 | if (retval) { |
| 178 | com_err("ncheck", retval, |
| 179 | "while calling ext2_dir_iterate"); |
| 180 | goto next; |
| 181 | } |
| 182 | |
| 183 | if (iw.inodes_left == 0) |
| 184 | break; |
| 185 | |
| 186 | next: |
| 187 | do { |
| 188 | retval = ext2fs_get_next_inode(scan, &ino, &inode); |
| 189 | } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE); |
| 190 | |
| 191 | if (retval) { |
| 192 | com_err("ncheck", retval, |
| 193 | "while doing inode scan"); |
| 194 | goto error_out; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | error_out: |
| 199 | free(iw.iarray); |
| 200 | if (scan) |
| 201 | ext2fs_close_inode_scan(scan); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |