2 * message.c --- print e2fsck messages (with compression)
4 * Copyright 1996, 1997 by Theodore Ts'o
7 * This file may be redistributed under the terms of the GNU Public
11 * print_e2fsck_message() prints a message to the user, using
12 * compression techniques and expansions of abbreviations.
14 * The following % expansions are supported:
16 * %b <blk> block number
17 * %B <blkcount> integer
18 * %c <blk2> block number
19 * %di <dirent>->ino inode number
20 * %dn <dirent>->name string
21 * %dr <dirent>->rec_len
22 * %dl <dirent>->name_len
23 * %dt <dirent>->filetype
24 * %D <dir> inode number
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
36 * %m <com_err error message>
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.
45 * %s <str> miscellaneous string
46 * %S backup superblock
48 * The following '@' expansions are supported:
53 * @C conflicts with some other fs block
59 * @E Entry '%Dn' in %p (%i)
61 * @F for @i %i (%Q) is
83 #define _INLINE_ __inline__
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.
94 static const char *abbrevs[] = {
98 "Cconflicts with some other fs @b",
104 "E@e '%Dn' in %p (%i)",
106 "Ffor @i %i (%Q) is",
120 * Give more user friendly names to the "special" inodes.
122 #define num_special_inodes 7
123 static const char *special_inode_name[] =
125 "<The NULL inode>", /* 0 */
126 "<The bad blocks inode>", /* 1 */
128 "<The ACL index inode>", /* 3 */
129 "<The ACL data inode>", /* 4 */
130 "<The boot loader inode>", /* 5 */
131 "<The undelete directory inode>" /* 6 */
135 * This function does "safe" printing. It will convert non-printable
136 * ASCII characters using '^' and M- notation.
138 static void safe_print(const unsigned char *cp, int len)
161 * This function prints a pathname, using the ext2fs_get_pathname
164 static void print_pathname(ext2_filsys fs, ino_t dir, ino_t ino)
169 if (!dir && (ino < num_special_inodes)) {
170 fputs(special_inode_name[ino], stdout);
174 retval = ext2fs_get_pathname(fs, dir, ino, &path);
176 fputs("???", stdout);
178 safe_print(path, -1);
179 ext2fs_free_mem((void **) &path);
184 * This function handles the '@' expansion. We allow recursive
185 * expansion; an @ expression can contain further '@' and '%'
188 static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
189 struct problem_context *pctx,
192 const char **cpp, *str;
194 /* Search for the abbreviation */
195 for (cpp = abbrevs; *cpp; cpp++) {
201 if (*first && islower(*str)) {
203 fputc(toupper(*str++), stdout);
205 print_e2fsck_message(ctx, str, pctx, *first);
211 * This function expands '%kX' expressions
213 static _INLINE_ void expand_inode_expression(char ch,
214 struct problem_context *ctx)
216 struct ext2_inode *inode;
220 if (!ctx || !ctx->inode)
227 if (LINUX_S_ISDIR(inode->i_mode))
228 printf("%u", inode->i_size);
230 #ifdef EXT2_NO_64_TYPE
231 if (inode->i_size_high)
232 printf("0x%x%08x", inode->i_size_high,
235 printf("%u", inode->i_size);
237 printf("%llu", (inode->i_size |
238 ((__u64) inode->i_size_high << 32)));
243 printf("%u", inode->i_blocks);
246 printf("%d", inode->i_links_count);
249 printf("0%o", inode->i_mode);
253 time_str = ctime(&t);
254 printf("%.24s", time_str);
257 printf("%u", inode->i_faddr);
260 printf("%u", inode->i_file_acl);
263 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
264 inode->i_dir_acl : 0));
274 * This function expands '%dX' expressions
276 static _INLINE_ void expand_dirent_expression(char ch,
277 struct problem_context *ctx)
279 struct ext2_dir_entry *dirent;
282 if (!ctx || !ctx->dirent)
285 dirent = ctx->dirent;
289 printf("%u", dirent->inode);
292 len = dirent->name_len & 0xFF;
293 if (len > EXT2_NAME_LEN)
295 if (len > dirent->rec_len)
296 len = dirent->rec_len;
297 safe_print(dirent->name, len);
300 printf("%u", dirent->rec_len);
303 printf("%u", dirent->name_len & 0xFF);
306 printf("%u", dirent->name_len >> 8);
315 static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
316 struct problem_context *ctx)
326 printf("%u", ctx->blk);
329 #ifdef EXT2_NO_64_TYPE
330 printf("%d", ctx->blkcount);
332 printf("%lld", ctx->blkcount);
336 printf("%u", ctx->blk2);
339 printf("%lu", ctx->dir);
342 printf("%d", ctx->group);
345 printf("%lu", ctx->ino);
348 printf("%lu", ctx->ino2);
351 printf("%s", error_message(ctx->errcode));
354 #ifdef EXT2_NO_64_TYPE
355 printf("%u", ctx->num);
357 printf("%llu", ctx->num);
361 print_pathname(fs, ctx->ino, 0);
364 print_pathname(fs, ctx->ino2,
365 ctx->dirent ? ctx->dirent->inode : 0);
368 print_pathname(fs, ctx->dir, 0);
371 print_pathname(fs, ctx->dir, ctx->ino);
374 printf("%d", get_backup_sb(fs));
377 printf("%s", ctx->str);
386 void print_e2fsck_message(e2fsck_t ctx, const char *msg,
387 struct problem_context *pctx, int first)
389 ext2_filsys fs = ctx->fs;
393 e2fsck_clear_progbar(ctx);
394 for (cp = msg; *cp; cp++) {
397 expand_at_expression(ctx, *cp, pctx, &first);
398 } else if (cp[0] == '%' && cp[1] == 'I') {
400 expand_inode_expression(*cp, pctx);
401 } else if (cp[0] == '%' && cp[1] == 'D') {
403 expand_dirent_expression(*cp, pctx);
404 } else if ((cp[0] == '%')) {
406 expand_percent_expression(fs, *cp, pctx);
408 for (i=0; cp[i]; i++)
409 if ((cp[i] == '@') || cp[i] == '%')
411 printf("%.*s", i, cp);