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
39 * %j <ino2> inode number
40 * %m <com_err error message>
42 * %p ext2fs_get_pathname of directory <ino>
43 * %P ext2fs_get_pathname of <dirent>->ino with <ino2> as
44 * the containing directory. (If dirent is NULL
45 * then return the pathname of directory <ino2>)
46 * %q ext2fs_get_pathname of directory <dir>
47 * %Q ext2fs_get_pathname of directory <ino> with <dir> as
48 * the containing directory.
49 * %s <str> miscellaneous string
50 * %S backup superblock
51 * %X <num> hexadecimal format
53 * The following '@' expansions are supported:
55 * @a extended attribute
60 * @C conflicts with some other fs block
64 * @E Entry '%Dn' in %p (%i)
66 * @F for @i %i (%Q) is
68 * @h HTREE directory inode
98 #define _INLINE_ __inline__
104 * This structure defines the abbreviations used by the text strings
105 * below. The first character in the string is the index letter. An
106 * abbreviation of the form '@<i>' is expanded by looking up the index
107 * letter <i> in the table below.
109 static const char *abbrevs[] = {
110 N_("aextended attribute"),
111 N_("Aerror allocating"),
115 N_("Cconflicts with some other fs @b"),
122 N_("E@e '%Dn' in %p (%i)"),
124 N_("Ffor @i %i (%Q) is"),
129 N_("mmultiply-claimed"),
145 * Give more user friendly names to the "special" inodes.
147 #define num_special_inodes 11
148 static const char *special_inode_name[] =
150 N_("<The NULL inode>"), /* 0 */
151 N_("<The bad blocks inode>"), /* 1 */
153 N_("<The ACL index inode>"), /* 3 */
154 N_("<The ACL data inode>"), /* 4 */
155 N_("<The boot loader inode>"), /* 5 */
156 N_("<The undelete directory inode>"), /* 6 */
157 N_("<The group descriptor inode>"), /* 7 */
158 N_("<The journal inode>"), /* 8 */
159 N_("<Reserved inode 9>"), /* 9 */
160 N_("<Reserved inode 10>"), /* 10 */
164 * This function does "safe" printing. It will convert non-printable
165 * ASCII characters using '^' and M- notation.
167 static void safe_print(const char *cp, int len)
180 if ((ch < 32) || (ch == 0x7f)) {
182 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
190 * This function prints a pathname, using the ext2fs_get_pathname
193 static void print_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino)
198 if (!dir && (ino < num_special_inodes)) {
199 fputs(_(special_inode_name[ino]), stdout);
203 retval = ext2fs_get_pathname(fs, dir, ino, &path);
205 fputs("???", stdout);
207 safe_print(path, -1);
208 ext2fs_free_mem(&path);
213 * This function handles the '@' expansion. We allow recursive
214 * expansion; an @ expression can contain further '@' and '%'
217 static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
218 struct problem_context *pctx,
219 int *first, int recurse)
221 const char **cpp, *str;
223 /* Search for the abbreviation */
224 for (cpp = abbrevs; *cpp; cpp++) {
228 if (*cpp && recurse < 10) {
230 if (*first && islower(*str)) {
232 fputc(toupper(*str++), stdout);
234 print_e2fsck_message(ctx, str, pctx, *first, recurse+1);
240 * This function expands '%IX' expressions
242 static _INLINE_ void expand_inode_expression(char ch,
243 struct problem_context *ctx)
245 struct ext2_inode *inode;
246 struct ext2_inode_large *large_inode;
247 const char * time_str;
251 if (!ctx || !ctx->inode)
255 large_inode = (struct ext2_inode_large *) inode;
259 if (LINUX_S_ISDIR(inode->i_mode))
260 printf("%u", inode->i_size);
262 #ifdef EXT2_NO_64_TYPE
263 if (inode->i_size_high)
264 printf("0x%x%08x", inode->i_size_high,
267 printf("%u", inode->i_size);
269 printf("%llu", inode->i_size |
270 ((long long)inode->i_size_high << 32));
275 printf("%u", large_inode->i_extra_isize);
278 if (inode->i_flags & EXT4_HUGE_FILE_FL)
279 printf("%llu", inode->i_blocks +
280 (((long long) inode->osd2.linux2.l_i_blocks_hi)
283 printf("%u", inode->i_blocks);
286 printf("%d", inode->i_links_count);
289 printf("0%o", inode->i_mode);
292 /* The diet libc doesn't respect the TZ environemnt variable */
294 time_str = getenv("TZ");
297 do_gmt = !strcmp(time_str, "GMT");
300 time_str = asctime(do_gmt ? gmtime(&t) : localtime(&t));
301 printf("%.24s", time_str);
304 printf("%u", inode->i_faddr);
307 printf("%u", inode->i_file_acl);
310 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
311 inode->i_dir_acl : 0));
314 printf("%d", inode_uid(*inode));
317 printf("%d", inode_gid(*inode));
320 if (LINUX_S_ISREG(inode->i_mode))
321 printf(_("regular file"));
322 else if (LINUX_S_ISDIR(inode->i_mode))
323 printf(_("directory"));
324 else if (LINUX_S_ISCHR(inode->i_mode))
325 printf(_("character device"));
326 else if (LINUX_S_ISBLK(inode->i_mode))
327 printf(_("block device"));
328 else if (LINUX_S_ISFIFO(inode->i_mode))
329 printf(_("named pipe"));
330 else if (LINUX_S_ISLNK(inode->i_mode))
331 printf(_("symbolic link"));
332 else if (LINUX_S_ISSOCK(inode->i_mode))
335 printf(_("unknown file type with mode 0%o"),
346 * This function expands '%dX' expressions
348 static _INLINE_ void expand_dirent_expression(char ch,
349 struct problem_context *ctx)
351 struct ext2_dir_entry *dirent;
354 if (!ctx || !ctx->dirent)
357 dirent = ctx->dirent;
361 printf("%u", dirent->inode);
364 len = dirent->name_len & 0xFF;
365 if (len > EXT2_NAME_LEN)
367 if (len > dirent->rec_len)
368 len = dirent->rec_len;
369 safe_print(dirent->name, len);
372 printf("%u", dirent->rec_len);
375 printf("%u", dirent->name_len & 0xFF);
378 printf("%u", dirent->name_len >> 8);
387 static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
388 struct problem_context *ctx)
398 #ifdef EXT2_NO_64_TYPE
399 printf("%u", (unsigned long) ctx->blk);
401 printf("%llu", (unsigned long long) ctx->blk);
405 #ifdef EXT2_NO_64_TYPE
406 printf("%d", ctx->blkcount);
408 printf("%lld", (long long)ctx->blkcount);
412 #ifdef EXT2_NO_64_TYPE
413 printf("%u", (unsigned long) ctx->blk2);
415 printf("%llu", (unsigned long long) ctx->blk2);
419 printf("%u", ctx->dir);
422 printf("%d", ctx->group);
425 printf("%u", ctx->ino);
428 printf("%u", ctx->ino2);
431 printf("%s", error_message(ctx->errcode));
434 #ifdef EXT2_NO_64_TYPE
435 printf("%u", ctx->num);
437 printf("%llu", (long long)ctx->num);
441 print_pathname(fs, ctx->ino, 0);
444 print_pathname(fs, ctx->ino2,
445 ctx->dirent ? ctx->dirent->inode : 0);
448 print_pathname(fs, ctx->dir, 0);
451 print_pathname(fs, ctx->dir, ctx->ino);
454 printf("%u", get_backup_sb(NULL, fs, NULL, NULL));
457 printf("%s", ctx->str ? ctx->str : "NULL");
460 #ifdef EXT2_NO_64_TYPE
461 printf("0x%x", ctx->num);
463 printf("0x%llx", (long long)ctx->num);
473 void print_e2fsck_message(e2fsck_t ctx, const char *msg,
474 struct problem_context *pctx, int first,
477 ext2_filsys fs = ctx->fs;
481 e2fsck_clear_progbar(ctx);
482 for (cp = msg; *cp; cp++) {
485 expand_at_expression(ctx, *cp, pctx, &first, recurse);
486 } else if (cp[0] == '%' && cp[1] == 'I') {
488 expand_inode_expression(*cp, pctx);
489 } else if (cp[0] == '%' && cp[1] == 'D') {
491 expand_dirent_expression(*cp, pctx);
492 } else if ((cp[0] == '%')) {
494 expand_percent_expression(fs, *cp, pctx);
496 for (i=0; cp[i]; i++)
497 if ((cp[i] == '@') || cp[i] == '%')
499 printf("%.*s", i, cp);