Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * message.c --- print e2fsck messages (with compression) |
| 3 | * |
| 4 | * Copyright 1996, 1997 by Theodore Ts'o |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| 10 | * |
| 11 | * print_e2fsck_message() prints a message to the user, using |
| 12 | * compression techniques and expansions of abbreviations. |
| 13 | * |
| 14 | * The following % expansions are supported: |
| 15 | * |
| 16 | * %b <blk> block number |
| 17 | * %B <blkcount> integer |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 18 | * %c <blk2> block number |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 19 | * %di <dirent>->ino inode number |
| 20 | * %dn <dirent>->name string |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 21 | * %dr <dirent>->rec_len |
| 22 | * %dl <dirent>->name_len |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 23 | * %D <dir> inode number |
| 24 | * %g <group> integer |
| 25 | * %i <ino> inode number |
| 26 | * %Is <inode> -> i_size |
| 27 | * %Ib <inode> -> i_blocks |
| 28 | * %Il <inode> -> i_links_count |
| 29 | * %Im <inode> -> i_mode |
| 30 | * %IM <inode> -> i_mtime |
| 31 | * %IF <inode> -> i_faddr |
| 32 | * %If <inode> -> i_file_acl |
| 33 | * %Id <inode> -> i_dir_acl |
| 34 | * %j <ino2> inode number |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 35 | * %m <com_err error message> |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 36 | * %N <num> |
| 37 | * %p ext2fs_get_pathname of directory <ino> |
| 38 | * %P ext2fs_get_pathname of <dirent>->ino with <ino2> as |
| 39 | * the containing directory. (If dirent is NULL |
| 40 | * then return the pathname of directory <ino2>) |
| 41 | * %q ext2fs_get_pathname of directory <dir> |
| 42 | * %Q ext2fs_get_pathname of directory <ino> with <dir> as |
| 43 | * the containing directory. |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 44 | * %s <str> miscellaneous string |
| 45 | * %S backup superblock |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 46 | * |
| 47 | * The following '@' expansions are supported: |
| 48 | * |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 49 | * @A error allocating |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 50 | * @b block |
| 51 | * @B bitmap |
| 52 | * @C conflicts with some other fs block |
| 53 | * @i inode |
Theodore Ts'o | 7cf73dc | 1997-08-14 17:17:16 +0000 | [diff] [blame] | 54 | * @I illegal |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 55 | * @D deleted |
| 56 | * @d directory |
| 57 | * @e entry |
| 58 | * @E Entry '%Dn' in %p (%i) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 59 | * @f filesystem |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 60 | * @F for @i %i (%Q) is |
| 61 | * @g group |
| 62 | * @l lost+found |
| 63 | * @L is a link |
| 64 | * @u unattached |
| 65 | * @r root inode |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 66 | * @s should be |
| 67 | * @S superblock |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 68 | * @z zero-length |
| 69 | */ |
| 70 | |
| 71 | #include <stdlib.h> |
| 72 | #include <unistd.h> |
| 73 | #include <string.h> |
| 74 | #include <ctype.h> |
| 75 | #include <termios.h> |
| 76 | |
| 77 | #include "e2fsck.h" |
| 78 | |
| 79 | #include "problem.h" |
| 80 | |
| 81 | #ifdef __GNUC__ |
| 82 | #define _INLINE_ __inline__ |
| 83 | #else |
| 84 | #define _INLINE_ |
| 85 | #endif |
| 86 | |
| 87 | /* |
| 88 | * This structure defines the abbreviations used by the text strings |
| 89 | * below. The first character in the string is the index letter. An |
| 90 | * abbreviation of the form '@<i>' is expanded by looking up the index |
| 91 | * letter <i> in the table below. |
| 92 | */ |
| 93 | static const char *abbrevs[] = { |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 94 | "Aerror allocating", |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 95 | "bblock", |
| 96 | "Bbitmap", |
| 97 | "Cconflicts with some other fs @b", |
| 98 | "iinode", |
Theodore Ts'o | 7cf73dc | 1997-08-14 17:17:16 +0000 | [diff] [blame] | 99 | "Iillegal", |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 100 | "Ddeleted", |
| 101 | "ddirectory", |
| 102 | "eentry", |
| 103 | "E@e '%Dn' in %p (%i)", |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 104 | "ffilesystem", |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 105 | "Ffor @i %i (%Q) is", |
| 106 | "ggroup", |
| 107 | "llost+found", |
| 108 | "Lis a link", |
| 109 | "uunattached", |
| 110 | "rroot @i", |
| 111 | "sshould be", |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 112 | "Ssuper@b", |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 113 | "zzero-length", |
| 114 | "@@", |
| 115 | 0 |
| 116 | }; |
| 117 | |
| 118 | /* |
| 119 | * Give more user friendly names to the "special" inodes. |
| 120 | */ |
| 121 | #define num_special_inodes 7 |
| 122 | static const char *special_inode_name[] = |
| 123 | { |
| 124 | "<The NULL inode>", /* 0 */ |
| 125 | "<The bad blocks inode>", /* 1 */ |
| 126 | "/", /* 2 */ |
| 127 | "<The ACL index inode>", /* 3 */ |
| 128 | "<The ACL data inode>", /* 4 */ |
| 129 | "<The boot loader inode>", /* 5 */ |
| 130 | "<The undelete directory inode>" /* 6 */ |
| 131 | }; |
| 132 | |
| 133 | /* |
| 134 | * This function prints a pathname, using the ext2fs_get_pathname |
| 135 | * function |
| 136 | */ |
| 137 | static void print_pathname(ext2_filsys fs, ino_t dir, ino_t ino) |
| 138 | { |
| 139 | errcode_t retval; |
| 140 | char *path; |
| 141 | |
| 142 | if (!dir && (ino < num_special_inodes)) { |
| 143 | fputs(special_inode_name[ino], stdout); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | retval = ext2fs_get_pathname(fs, dir, ino, &path); |
| 148 | if (retval) |
| 149 | fputs("???", stdout); |
| 150 | else { |
| 151 | fputs(path, stdout); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 152 | ext2fs_free_mem((void **) &path); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * This function handles the '@' expansion. We allow recursive |
| 158 | * expansion; an @ expression can contain further '@' and '%' |
| 159 | * expressions. |
| 160 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 161 | static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch, |
| 162 | struct problem_context *pctx, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 163 | int *first) |
| 164 | { |
| 165 | const char **cpp, *str; |
| 166 | |
| 167 | /* Search for the abbreviation */ |
| 168 | for (cpp = abbrevs; *cpp; cpp++) { |
| 169 | if (ch == *cpp[0]) |
| 170 | break; |
| 171 | } |
| 172 | if (*cpp) { |
| 173 | str = (*cpp) + 1; |
| 174 | if (*first && islower(*str)) { |
| 175 | *first = 0; |
| 176 | fputc(toupper(*str++), stdout); |
| 177 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 178 | print_e2fsck_message(ctx, str, pctx, *first); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 179 | } else |
| 180 | printf("@%c", ch); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * This function expands '%kX' expressions |
| 185 | */ |
| 186 | static _INLINE_ void expand_inode_expression(char ch, |
| 187 | struct problem_context *ctx) |
| 188 | { |
| 189 | struct ext2_inode *inode; |
| 190 | char * time_str; |
| 191 | time_t t; |
| 192 | |
| 193 | if (!ctx || !ctx->inode) |
| 194 | goto no_inode; |
| 195 | |
| 196 | inode = ctx->inode; |
| 197 | |
| 198 | switch (ch) { |
| 199 | case 's': |
Theodore Ts'o | 246501c | 1998-03-24 16:22:38 +0000 | [diff] [blame] | 200 | if (LINUX_S_ISDIR(inode->i_mode)) |
| 201 | printf("%u", inode->i_size); |
| 202 | else { |
| 203 | #ifdef EXT2_NO_64_TYPE |
| 204 | if (inode->i_size_high) |
| 205 | printf("0x%x%08x", inode->i_size_high, |
| 206 | inode->i_size); |
| 207 | else |
| 208 | printf("%u", inode->i_size); |
| 209 | #else |
| 210 | printf("%llu", (inode->i_size | |
| 211 | ((__u64) inode->i_size_high << 32))); |
| 212 | #endif |
| 213 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 214 | break; |
| 215 | case 'b': |
| 216 | printf("%u", inode->i_blocks); |
| 217 | break; |
| 218 | case 'l': |
| 219 | printf("%d", inode->i_links_count); |
| 220 | break; |
| 221 | case 'm': |
| 222 | printf("0%o", inode->i_mode); |
| 223 | break; |
| 224 | case 'M': |
| 225 | t = inode->i_mtime; |
| 226 | time_str = ctime(&t); |
| 227 | printf("%.24s", time_str); |
| 228 | break; |
| 229 | case 'F': |
| 230 | printf("%u", inode->i_faddr); |
| 231 | break; |
| 232 | case 'f': |
| 233 | printf("%u", inode->i_file_acl); |
| 234 | break; |
| 235 | case 'd': |
Theodore Ts'o | 246501c | 1998-03-24 16:22:38 +0000 | [diff] [blame] | 236 | printf("%u", (LINUX_S_ISDIR(inode->i_mode) ? |
| 237 | inode->i_dir_acl : 0)); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 238 | break; |
| 239 | default: |
| 240 | no_inode: |
| 241 | printf("%%I%c", ch); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * This function expands '%dX' expressions |
| 248 | */ |
| 249 | static _INLINE_ void expand_dirent_expression(char ch, |
| 250 | struct problem_context *ctx) |
| 251 | { |
| 252 | struct ext2_dir_entry *dirent; |
| 253 | int len; |
| 254 | |
| 255 | if (!ctx || !ctx->dirent) |
| 256 | goto no_dirent; |
| 257 | |
| 258 | dirent = ctx->dirent; |
| 259 | |
| 260 | switch (ch) { |
| 261 | case 'i': |
| 262 | printf("%u", dirent->inode); |
| 263 | break; |
| 264 | case 'n': |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 265 | len = dirent->name_len & 0xFF; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 266 | if (len > EXT2_NAME_LEN) |
| 267 | len = EXT2_NAME_LEN; |
| 268 | if (len > dirent->rec_len) |
| 269 | len = dirent->rec_len; |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 270 | printf("%.*s", len, dirent->name); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 271 | break; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 272 | case 'r': |
| 273 | printf("%u", dirent->rec_len); |
| 274 | break; |
| 275 | case 'l': |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 276 | printf("%u", dirent->name_len & 0xFF); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 277 | break; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 278 | default: |
| 279 | no_dirent: |
| 280 | printf("%%D%c", ch); |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch, |
| 286 | struct problem_context *ctx) |
| 287 | { |
| 288 | if (!ctx) |
| 289 | goto no_context; |
| 290 | |
| 291 | switch (ch) { |
| 292 | case '%': |
| 293 | fputc('%', stdout); |
| 294 | break; |
| 295 | case 'b': |
| 296 | printf("%u", ctx->blk); |
| 297 | break; |
| 298 | case 'B': |
Theodore Ts'o | 246501c | 1998-03-24 16:22:38 +0000 | [diff] [blame] | 299 | #ifdef EXT2_NO_64_TYPE |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 300 | printf("%d", ctx->blkcount); |
Theodore Ts'o | 246501c | 1998-03-24 16:22:38 +0000 | [diff] [blame] | 301 | #else |
| 302 | printf("%lld", ctx->blkcount); |
| 303 | #endif |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 304 | break; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 305 | case 'c': |
| 306 | printf("%u", ctx->blk2); |
| 307 | break; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 308 | case 'd': |
| 309 | printf("%lu", ctx->dir); |
| 310 | break; |
| 311 | case 'g': |
| 312 | printf("%d", ctx->group); |
| 313 | break; |
| 314 | case 'i': |
| 315 | printf("%lu", ctx->ino); |
| 316 | break; |
| 317 | case 'j': |
| 318 | printf("%lu", ctx->ino2); |
| 319 | break; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 320 | case 'm': |
| 321 | printf("%s", error_message(ctx->errcode)); |
| 322 | break; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 323 | case 'N': |
Theodore Ts'o | 246501c | 1998-03-24 16:22:38 +0000 | [diff] [blame] | 324 | #ifdef EXT2_NO_64_TYPE |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 325 | printf("%u", ctx->num); |
Theodore Ts'o | 246501c | 1998-03-24 16:22:38 +0000 | [diff] [blame] | 326 | #else |
| 327 | printf("%llu", ctx->num); |
| 328 | #endif |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 329 | break; |
| 330 | case 'p': |
| 331 | print_pathname(fs, ctx->ino, 0); |
| 332 | break; |
| 333 | case 'P': |
| 334 | print_pathname(fs, ctx->ino2, |
| 335 | ctx->dirent ? ctx->dirent->inode : 0); |
| 336 | break; |
| 337 | case 'q': |
| 338 | print_pathname(fs, ctx->dir, 0); |
| 339 | break; |
| 340 | case 'Q': |
| 341 | print_pathname(fs, ctx->dir, ctx->ino); |
| 342 | break; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 343 | case 'S': |
| 344 | printf("%d", get_backup_sb(fs)); |
| 345 | break; |
| 346 | case 's': |
| 347 | printf("%s", ctx->str); |
| 348 | break; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 349 | default: |
| 350 | no_context: |
| 351 | printf("%%%c", ch); |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 356 | void print_e2fsck_message(e2fsck_t ctx, const char *msg, |
| 357 | struct problem_context *pctx, int first) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 358 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 359 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 360 | const char * cp; |
| 361 | int i; |
| 362 | |
| 363 | for (cp = msg; *cp; cp++) { |
| 364 | if (cp[0] == '@') { |
| 365 | cp++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 366 | expand_at_expression(ctx, *cp, pctx, &first); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 367 | } else if (cp[0] == '%' && cp[1] == 'I') { |
| 368 | cp += 2; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 369 | expand_inode_expression(*cp, pctx); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 370 | } else if (cp[0] == '%' && cp[1] == 'D') { |
| 371 | cp += 2; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 372 | expand_dirent_expression(*cp, pctx); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 373 | } else if ((cp[0] == '%')) { |
| 374 | cp++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 375 | expand_percent_expression(fs, *cp, pctx); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 376 | } else { |
| 377 | for (i=0; cp[i]; i++) |
| 378 | if ((cp[i] == '@') || cp[i] == '%') |
| 379 | break; |
| 380 | printf("%.*s", i, cp); |
| 381 | cp += i-1; |
| 382 | } |
| 383 | first = 0; |
| 384 | } |
| 385 | } |