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': |
| 200 | printf("%u", inode->i_size); |
| 201 | break; |
| 202 | case 'b': |
| 203 | printf("%u", inode->i_blocks); |
| 204 | break; |
| 205 | case 'l': |
| 206 | printf("%d", inode->i_links_count); |
| 207 | break; |
| 208 | case 'm': |
| 209 | printf("0%o", inode->i_mode); |
| 210 | break; |
| 211 | case 'M': |
| 212 | t = inode->i_mtime; |
| 213 | time_str = ctime(&t); |
| 214 | printf("%.24s", time_str); |
| 215 | break; |
| 216 | case 'F': |
| 217 | printf("%u", inode->i_faddr); |
| 218 | break; |
| 219 | case 'f': |
| 220 | printf("%u", inode->i_file_acl); |
| 221 | break; |
| 222 | case 'd': |
| 223 | printf("%u", inode->i_dir_acl); |
| 224 | break; |
| 225 | default: |
| 226 | no_inode: |
| 227 | printf("%%I%c", ch); |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * This function expands '%dX' expressions |
| 234 | */ |
| 235 | static _INLINE_ void expand_dirent_expression(char ch, |
| 236 | struct problem_context *ctx) |
| 237 | { |
| 238 | struct ext2_dir_entry *dirent; |
| 239 | int len; |
| 240 | |
| 241 | if (!ctx || !ctx->dirent) |
| 242 | goto no_dirent; |
| 243 | |
| 244 | dirent = ctx->dirent; |
| 245 | |
| 246 | switch (ch) { |
| 247 | case 'i': |
| 248 | printf("%u", dirent->inode); |
| 249 | break; |
| 250 | case 'n': |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame^] | 251 | len = dirent->name_len & 0xFF; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 252 | if (len > EXT2_NAME_LEN) |
| 253 | len = EXT2_NAME_LEN; |
| 254 | if (len > dirent->rec_len) |
| 255 | len = dirent->rec_len; |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame^] | 256 | printf("%.*s", len, dirent->name); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 257 | break; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 258 | case 'r': |
| 259 | printf("%u", dirent->rec_len); |
| 260 | break; |
| 261 | case 'l': |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame^] | 262 | printf("%u", dirent->name_len & 0xFF); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 263 | break; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 264 | default: |
| 265 | no_dirent: |
| 266 | printf("%%D%c", ch); |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch, |
| 272 | struct problem_context *ctx) |
| 273 | { |
| 274 | if (!ctx) |
| 275 | goto no_context; |
| 276 | |
| 277 | switch (ch) { |
| 278 | case '%': |
| 279 | fputc('%', stdout); |
| 280 | break; |
| 281 | case 'b': |
| 282 | printf("%u", ctx->blk); |
| 283 | break; |
| 284 | case 'B': |
| 285 | printf("%d", ctx->blkcount); |
| 286 | break; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 287 | case 'c': |
| 288 | printf("%u", ctx->blk2); |
| 289 | break; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 290 | case 'd': |
| 291 | printf("%lu", ctx->dir); |
| 292 | break; |
| 293 | case 'g': |
| 294 | printf("%d", ctx->group); |
| 295 | break; |
| 296 | case 'i': |
| 297 | printf("%lu", ctx->ino); |
| 298 | break; |
| 299 | case 'j': |
| 300 | printf("%lu", ctx->ino2); |
| 301 | break; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 302 | case 'm': |
| 303 | printf("%s", error_message(ctx->errcode)); |
| 304 | break; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 305 | case 'N': |
| 306 | printf("%u", ctx->num); |
| 307 | break; |
| 308 | case 'p': |
| 309 | print_pathname(fs, ctx->ino, 0); |
| 310 | break; |
| 311 | case 'P': |
| 312 | print_pathname(fs, ctx->ino2, |
| 313 | ctx->dirent ? ctx->dirent->inode : 0); |
| 314 | break; |
| 315 | case 'q': |
| 316 | print_pathname(fs, ctx->dir, 0); |
| 317 | break; |
| 318 | case 'Q': |
| 319 | print_pathname(fs, ctx->dir, ctx->ino); |
| 320 | break; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 321 | case 'S': |
| 322 | printf("%d", get_backup_sb(fs)); |
| 323 | break; |
| 324 | case 's': |
| 325 | printf("%s", ctx->str); |
| 326 | break; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 327 | default: |
| 328 | no_context: |
| 329 | printf("%%%c", ch); |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 334 | void print_e2fsck_message(e2fsck_t ctx, const char *msg, |
| 335 | struct problem_context *pctx, int first) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 336 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 337 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 338 | const char * cp; |
| 339 | int i; |
| 340 | |
| 341 | for (cp = msg; *cp; cp++) { |
| 342 | if (cp[0] == '@') { |
| 343 | cp++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 344 | expand_at_expression(ctx, *cp, pctx, &first); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 345 | } else if (cp[0] == '%' && cp[1] == 'I') { |
| 346 | cp += 2; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 347 | expand_inode_expression(*cp, pctx); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 348 | } else if (cp[0] == '%' && cp[1] == 'D') { |
| 349 | cp += 2; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 350 | expand_dirent_expression(*cp, pctx); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 351 | } else if ((cp[0] == '%')) { |
| 352 | cp++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 353 | expand_percent_expression(fs, *cp, pctx); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 354 | } else { |
| 355 | for (i=0; cp[i]; i++) |
| 356 | if ((cp[i] == '@') || cp[i] == '%') |
| 357 | break; |
| 358 | printf("%.*s", i, cp); |
| 359 | cp += i-1; |
| 360 | } |
| 361 | first = 0; |
| 362 | } |
| 363 | } |