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 * %D <dir> inode number
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
35 * %m <com_err error message>
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.
44 * %s <str> miscellaneous string
45 * %S backup superblock
47 * The following '@' expansions are supported:
52 * @C conflicts with some other fs block
58 * @E Entry '%Dn' in %p (%i)
60 * @F for @i %i (%Q) is
82 #define _INLINE_ __inline__
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.
93 static const char *abbrevs[] = {
97 "Cconflicts with some other fs @b",
103 "E@e '%Dn' in %p (%i)",
105 "Ffor @i %i (%Q) is",
119 * Give more user friendly names to the "special" inodes.
121 #define num_special_inodes 7
122 static const char *special_inode_name[] =
124 "<The NULL inode>", /* 0 */
125 "<The bad blocks inode>", /* 1 */
127 "<The ACL index inode>", /* 3 */
128 "<The ACL data inode>", /* 4 */
129 "<The boot loader inode>", /* 5 */
130 "<The undelete directory inode>" /* 6 */
134 * This function does "safe" printing. It will convert non-printable
135 * ASCII characters using '^' and M- notation.
137 static void safe_print(const unsigned char *cp, int len)
160 * This function prints a pathname, using the ext2fs_get_pathname
163 static void print_pathname(ext2_filsys fs, ino_t dir, ino_t ino)
168 if (!dir && (ino < num_special_inodes)) {
169 fputs(special_inode_name[ino], stdout);
173 retval = ext2fs_get_pathname(fs, dir, ino, &path);
175 fputs("???", stdout);
177 safe_print(path, -1);
178 ext2fs_free_mem((void **) &path);
183 * This function handles the '@' expansion. We allow recursive
184 * expansion; an @ expression can contain further '@' and '%'
187 static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
188 struct problem_context *pctx,
191 const char **cpp, *str;
193 /* Search for the abbreviation */
194 for (cpp = abbrevs; *cpp; cpp++) {
200 if (*first && islower(*str)) {
202 fputc(toupper(*str++), stdout);
204 print_e2fsck_message(ctx, str, pctx, *first);
210 * This function expands '%kX' expressions
212 static _INLINE_ void expand_inode_expression(char ch,
213 struct problem_context *ctx)
215 struct ext2_inode *inode;
219 if (!ctx || !ctx->inode)
226 if (LINUX_S_ISDIR(inode->i_mode))
227 printf("%u", inode->i_size);
229 #ifdef EXT2_NO_64_TYPE
230 if (inode->i_size_high)
231 printf("0x%x%08x", inode->i_size_high,
234 printf("%u", inode->i_size);
236 printf("%llu", (inode->i_size |
237 ((__u64) inode->i_size_high << 32)));
242 printf("%u", inode->i_blocks);
245 printf("%d", inode->i_links_count);
248 printf("0%o", inode->i_mode);
252 time_str = ctime(&t);
253 printf("%.24s", time_str);
256 printf("%u", inode->i_faddr);
259 printf("%u", inode->i_file_acl);
262 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
263 inode->i_dir_acl : 0));
273 * This function expands '%dX' expressions
275 static _INLINE_ void expand_dirent_expression(char ch,
276 struct problem_context *ctx)
278 struct ext2_dir_entry *dirent;
281 if (!ctx || !ctx->dirent)
284 dirent = ctx->dirent;
288 printf("%u", dirent->inode);
291 len = dirent->name_len & 0xFF;
292 if (len > EXT2_NAME_LEN)
294 if (len > dirent->rec_len)
295 len = dirent->rec_len;
296 safe_print(dirent->name, len);
299 printf("%u", dirent->rec_len);
302 printf("%u", dirent->name_len & 0xFF);
311 static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
312 struct problem_context *ctx)
322 printf("%u", ctx->blk);
325 #ifdef EXT2_NO_64_TYPE
326 printf("%d", ctx->blkcount);
328 printf("%lld", ctx->blkcount);
332 printf("%u", ctx->blk2);
335 printf("%lu", ctx->dir);
338 printf("%d", ctx->group);
341 printf("%lu", ctx->ino);
344 printf("%lu", ctx->ino2);
347 printf("%s", error_message(ctx->errcode));
350 #ifdef EXT2_NO_64_TYPE
351 printf("%u", ctx->num);
353 printf("%llu", ctx->num);
357 print_pathname(fs, ctx->ino, 0);
360 print_pathname(fs, ctx->ino2,
361 ctx->dirent ? ctx->dirent->inode : 0);
364 print_pathname(fs, ctx->dir, 0);
367 print_pathname(fs, ctx->dir, ctx->ino);
370 printf("%d", get_backup_sb(fs));
373 printf("%s", ctx->str);
382 void print_e2fsck_message(e2fsck_t ctx, const char *msg,
383 struct problem_context *pctx, int first)
385 ext2_filsys fs = ctx->fs;
389 for (cp = msg; *cp; cp++) {
392 expand_at_expression(ctx, *cp, pctx, &first);
393 } else if (cp[0] == '%' && cp[1] == 'I') {
395 expand_inode_expression(*cp, pctx);
396 } else if (cp[0] == '%' && cp[1] == 'D') {
398 expand_dirent_expression(*cp, pctx);
399 } else if ((cp[0] == '%')) {
401 expand_percent_expression(fs, *cp, pctx);
403 for (i=0; cp[i]; i++)
404 if ((cp[i] == '@') || cp[i] == '%')
406 printf("%.*s", i, cp);