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