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 * %Iu <inode> -> i_uid
36 * %Ig <inode> -> i_gid
37 * %j <ino2> inode number
38 * %m <com_err error message>
40 * %p ext2fs_get_pathname of directory <ino>
41 * %P ext2fs_get_pathname of <dirent>->ino with <ino2> as
42 * the containing directory. (If dirent is NULL
43 * then return the pathname of directory <ino2>)
44 * %q ext2fs_get_pathname of directory <dir>
45 * %Q ext2fs_get_pathname of directory <ino> with <dir> as
46 * the containing directory.
47 * %s <str> miscellaneous string
48 * %S backup superblock
49 * %X <num> hexadecimal format
51 * The following '@' expansions are supported:
57 * @C conflicts with some other fs block
61 * @E Entry '%Dn' in %p (%i)
63 * @F for @i %i (%Q) is
90 #define _INLINE_ __inline__
96 * This structure defines the abbreviations used by the text strings
97 * below. The first character in the string is the index letter. An
98 * abbreviation of the form '@<i>' is expanded by looking up the index
99 * letter <i> in the table below.
101 static const char *abbrevs[] = {
102 N_("Aerror allocating"),
106 N_("Cconflicts with some other fs @b"),
113 N_("E@e '%Dn' in %p (%i)"),
115 N_("Ffor @i %i (%Q) is"),
131 * Give more user friendly names to the "special" inodes.
133 #define num_special_inodes 11
134 static const char *special_inode_name[] =
136 N_("<The NULL inode>"), /* 0 */
137 N_("<The bad blocks inode>"), /* 1 */
139 N_("<The ACL index inode>"), /* 3 */
140 N_("<The ACL data inode>"), /* 4 */
141 N_("<The boot loader inode>"), /* 5 */
142 N_("<The undelete directory inode>"), /* 6 */
143 N_("<The group descriptor inode>"), /* 7 */
144 N_("<The journal inode>"), /* 8 */
145 N_("<Reserved inode 9>"), /* 9 */
146 N_("<Reserved inode 10>"), /* 10 */
150 * This function does "safe" printing. It will convert non-printable
151 * ASCII characters using '^' and M- notation.
153 static void safe_print(const char *cp, int len)
166 if ((ch < 32) || (ch == 0x7f)) {
168 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
176 * This function prints a pathname, using the ext2fs_get_pathname
179 static void print_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino)
184 if (!dir && (ino < num_special_inodes)) {
185 fputs(_(special_inode_name[ino]), stdout);
189 retval = ext2fs_get_pathname(fs, dir, ino, &path);
191 fputs("???", stdout);
193 safe_print(path, -1);
194 ext2fs_free_mem((void **) &path);
199 * This function handles the '@' expansion. We allow recursive
200 * expansion; an @ expression can contain further '@' and '%'
203 static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
204 struct problem_context *pctx,
207 const char **cpp, *str;
209 /* Search for the abbreviation */
210 for (cpp = abbrevs; *cpp; cpp++) {
216 if (*first && islower(*str)) {
218 fputc(toupper(*str++), stdout);
220 print_e2fsck_message(ctx, _(str), pctx, *first);
226 * This function expands '%IX' expressions
228 static _INLINE_ void expand_inode_expression(char ch,
229 struct problem_context *ctx)
231 struct ext2_inode *inode;
235 if (!ctx || !ctx->inode)
242 if (LINUX_S_ISDIR(inode->i_mode))
243 printf("%u", inode->i_size);
245 #ifdef EXT2_NO_64_TYPE
246 if (inode->i_size_high)
247 printf("0x%x%08x", inode->i_size_high,
250 printf("%u", inode->i_size);
252 printf("%llu", (inode->i_size |
253 ((__u64) inode->i_size_high << 32)));
258 printf("%u", inode->i_blocks);
261 printf("%d", inode->i_links_count);
264 printf("0%o", inode->i_mode);
268 time_str = ctime(&t);
269 printf("%.24s", time_str);
272 printf("%u", inode->i_faddr);
275 printf("%u", inode->i_file_acl);
278 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
279 inode->i_dir_acl : 0));
282 printf("%d", (inode->i_uid |
283 (inode->osd2.linux2.l_i_uid_high << 16)));
286 printf("%d", (inode->i_gid |
287 (inode->osd2.linux2.l_i_gid_high << 16)));
297 * This function expands '%dX' expressions
299 static _INLINE_ void expand_dirent_expression(char ch,
300 struct problem_context *ctx)
302 struct ext2_dir_entry *dirent;
305 if (!ctx || !ctx->dirent)
308 dirent = ctx->dirent;
312 printf("%u", dirent->inode);
315 len = dirent->name_len & 0xFF;
316 if (len > EXT2_NAME_LEN)
318 if (len > dirent->rec_len)
319 len = dirent->rec_len;
320 safe_print(dirent->name, len);
323 printf("%u", dirent->rec_len);
326 printf("%u", dirent->name_len & 0xFF);
329 printf("%u", dirent->name_len >> 8);
338 static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
339 struct problem_context *ctx)
349 printf("%u", ctx->blk);
352 #ifdef EXT2_NO_64_TYPE
353 printf("%d", ctx->blkcount);
355 printf("%lld", ctx->blkcount);
359 printf("%u", ctx->blk2);
362 printf("%u", ctx->dir);
365 printf("%d", ctx->group);
368 printf("%u", ctx->ino);
371 printf("%u", ctx->ino2);
374 printf("%s", error_message(ctx->errcode));
377 #ifdef EXT2_NO_64_TYPE
378 printf("%u", ctx->num);
380 printf("%llu", ctx->num);
384 print_pathname(fs, ctx->ino, 0);
387 print_pathname(fs, ctx->ino2,
388 ctx->dirent ? ctx->dirent->inode : 0);
391 print_pathname(fs, ctx->dir, 0);
394 print_pathname(fs, ctx->dir, ctx->ino);
397 printf("%d", get_backup_sb(fs));
400 printf("%s", ctx->str ? ctx->str : "NULL");
403 #ifdef EXT2_NO_64_TYPE
404 printf("0x%x", ctx->num);
406 printf("0x%llx", ctx->num);
416 void print_e2fsck_message(e2fsck_t ctx, const char *msg,
417 struct problem_context *pctx, int first)
419 ext2_filsys fs = ctx->fs;
423 e2fsck_clear_progbar(ctx);
424 for (cp = msg; *cp; cp++) {
427 expand_at_expression(ctx, *cp, pctx, &first);
428 } else if (cp[0] == '%' && cp[1] == 'I') {
430 expand_inode_expression(*cp, pctx);
431 } else if (cp[0] == '%' && cp[1] == 'D') {
433 expand_dirent_expression(*cp, pctx);
434 } else if ((cp[0] == '%')) {
436 expand_percent_expression(fs, *cp, pctx);
438 for (i=0; cp[i]; i++)
439 if ((cp[i] == '@') || cp[i] == '%')
441 printf("%.*s", i, cp);