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 * %IS <inode> -> i_extra_isize
29 * %Ib <inode> -> i_blocks
30 * %Il <inode> -> i_links_count
31 * %Im <inode> -> i_mode
32 * %IM <inode> -> i_mtime
33 * %IF <inode> -> i_faddr
34 * %If <inode> -> i_file_acl
35 * %Id <inode> -> i_dir_acl
36 * %Iu <inode> -> i_uid
37 * %Ig <inode> -> i_gid
38 * %j <ino2> inode number
39 * %m <com_err error message>
41 * %p ext2fs_get_pathname of directory <ino>
42 * %P ext2fs_get_pathname of <dirent>->ino with <ino2> as
43 * the containing directory. (If dirent is NULL
44 * then return the pathname of directory <ino2>)
45 * %q ext2fs_get_pathname of directory <dir>
46 * %Q ext2fs_get_pathname of directory <ino> with <dir> as
47 * the containing directory.
48 * %s <str> miscellaneous string
49 * %S backup superblock
50 * %X <num> hexadecimal format
52 * The following '@' expansions are supported:
54 * @a extended attribute
59 * @C conflicts with some other fs block
63 * @E Entry '%Dn' in %p (%i)
65 * @F for @i %i (%Q) is
67 * @h HTREE directory inode
96 #define _INLINE_ __inline__
102 * This structure defines the abbreviations used by the text strings
103 * below. The first character in the string is the index letter. An
104 * abbreviation of the form '@<i>' is expanded by looking up the index
105 * letter <i> in the table below.
107 static const char *abbrevs[] = {
108 N_("aextended attribute"),
109 N_("Aerror allocating"),
113 N_("Cconflicts with some other fs @b"),
120 N_("E@e '%Dn' in %p (%i)"),
122 N_("Ffor @i %i (%Q) is"),
127 N_("mmultiply-claimed"),
142 * Give more user friendly names to the "special" inodes.
144 #define num_special_inodes 11
145 static const char *special_inode_name[] =
147 N_("<The NULL inode>"), /* 0 */
148 N_("<The bad blocks inode>"), /* 1 */
150 N_("<The ACL index inode>"), /* 3 */
151 N_("<The ACL data inode>"), /* 4 */
152 N_("<The boot loader inode>"), /* 5 */
153 N_("<The undelete directory inode>"), /* 6 */
154 N_("<The group descriptor inode>"), /* 7 */
155 N_("<The journal inode>"), /* 8 */
156 N_("<Reserved inode 9>"), /* 9 */
157 N_("<Reserved inode 10>"), /* 10 */
161 * This function does "safe" printing. It will convert non-printable
162 * ASCII characters using '^' and M- notation.
164 static void safe_print(const char *cp, int len)
177 if ((ch < 32) || (ch == 0x7f)) {
179 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
187 * This function prints a pathname, using the ext2fs_get_pathname
190 static void print_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino)
195 if (!dir && (ino < num_special_inodes)) {
196 fputs(_(special_inode_name[ino]), stdout);
200 retval = ext2fs_get_pathname(fs, dir, ino, &path);
202 fputs("???", stdout);
204 safe_print(path, -1);
205 ext2fs_free_mem(&path);
210 * This function handles the '@' expansion. We allow recursive
211 * expansion; an @ expression can contain further '@' and '%'
214 static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
215 struct problem_context *pctx,
218 const char **cpp, *str;
220 /* Search for the abbreviation */
221 for (cpp = abbrevs; *cpp; cpp++) {
227 if (*first && islower(*str)) {
229 fputc(toupper(*str++), stdout);
231 print_e2fsck_message(ctx, str, pctx, *first);
237 * This function expands '%IX' expressions
239 static _INLINE_ void expand_inode_expression(char ch,
240 struct problem_context *ctx)
242 struct ext2_inode *inode;
243 struct ext2_inode_large *large_inode;
244 const char * time_str;
248 if (!ctx || !ctx->inode)
252 large_inode = (struct ext2_inode_large *) inode;
256 if (LINUX_S_ISDIR(inode->i_mode))
257 printf("%u", inode->i_size);
259 #ifdef EXT2_NO_64_TYPE
260 if (inode->i_size_high)
261 printf("0x%x%08x", inode->i_size_high,
264 printf("%u", inode->i_size);
266 printf("%llu", (inode->i_size |
267 ((__u64) inode->i_size_high << 32)));
272 printf("%u", large_inode->i_extra_isize);
275 printf("%u", inode->i_blocks);
278 printf("%d", inode->i_links_count);
281 printf("0%o", inode->i_mode);
284 /* The diet libc doesn't respect the TZ environemnt variable */
286 time_str = getenv("TZ");
289 do_gmt = !strcmp(time_str, "GMT");
292 time_str = asctime(do_gmt ? gmtime(&t) : localtime(&t));
293 printf("%.24s", time_str);
296 printf("%u", inode->i_faddr);
299 printf("%u", inode->i_file_acl);
302 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
303 inode->i_dir_acl : 0));
306 printf("%d", (inode->i_uid |
307 (inode->osd2.linux2.l_i_uid_high << 16)));
310 printf("%d", (inode->i_gid |
311 (inode->osd2.linux2.l_i_gid_high << 16)));
321 * This function expands '%dX' expressions
323 static _INLINE_ void expand_dirent_expression(char ch,
324 struct problem_context *ctx)
326 struct ext2_dir_entry *dirent;
329 if (!ctx || !ctx->dirent)
332 dirent = ctx->dirent;
336 printf("%u", dirent->inode);
339 len = dirent->name_len & 0xFF;
340 if (len > EXT2_NAME_LEN)
342 if (len > dirent->rec_len)
343 len = dirent->rec_len;
344 safe_print(dirent->name, len);
347 printf("%u", dirent->rec_len);
350 printf("%u", dirent->name_len & 0xFF);
353 printf("%u", dirent->name_len >> 8);
362 static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
363 struct problem_context *ctx)
373 printf("%u", ctx->blk);
376 #ifdef EXT2_NO_64_TYPE
377 printf("%d", ctx->blkcount);
379 printf("%lld", ctx->blkcount);
383 printf("%u", ctx->blk2);
386 printf("%u", ctx->dir);
389 printf("%d", ctx->group);
392 printf("%u", ctx->ino);
395 printf("%u", ctx->ino2);
398 printf("%s", error_message(ctx->errcode));
401 #ifdef EXT2_NO_64_TYPE
402 printf("%u", ctx->num);
404 printf("%llu", ctx->num);
408 print_pathname(fs, ctx->ino, 0);
411 print_pathname(fs, ctx->ino2,
412 ctx->dirent ? ctx->dirent->inode : 0);
415 print_pathname(fs, ctx->dir, 0);
418 print_pathname(fs, ctx->dir, ctx->ino);
421 printf("%u", get_backup_sb(NULL, fs, NULL, NULL));
424 printf("%s", ctx->str ? ctx->str : "NULL");
427 #ifdef EXT2_NO_64_TYPE
428 printf("0x%x", ctx->num);
430 printf("0x%llx", ctx->num);
440 void print_e2fsck_message(e2fsck_t ctx, const char *msg,
441 struct problem_context *pctx, int first)
443 ext2_filsys fs = ctx->fs;
447 e2fsck_clear_progbar(ctx);
448 for (cp = msg; *cp; cp++) {
451 expand_at_expression(ctx, *cp, pctx, &first);
452 } else if (cp[0] == '%' && cp[1] == 'I') {
454 expand_inode_expression(*cp, pctx);
455 } else if (cp[0] == '%' && cp[1] == 'D') {
457 expand_dirent_expression(*cp, pctx);
458 } else if ((cp[0] == '%')) {
460 expand_percent_expression(fs, *cp, pctx);
462 for (i=0; cp[i]; i++)
463 if ((cp[i] == '@') || cp[i] == '%')
465 printf("%.*s", i, cp);