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> interpret blkcount as blkcount
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 * %r <blkcount> interpret blkcount as refcount
50 * %s <str> miscellaneous string
51 * %S backup superblock
52 * %X <num> hexadecimal format
54 * The following '@' expansions are supported:
56 * @a extended attribute
61 * @C conflicts with some other fs block
65 * @E Entry '%Dn' in %p (%i)
67 * @F for @i %i (%Q) is
69 * @h HTREE directory inode
101 #define _INLINE_ __inline__
107 * This structure defines the abbreviations used by the text strings
108 * below. The first character in the string is the index letter. An
109 * abbreviation of the form '@<i>' is expanded by looking up the index
110 * letter <i> in the table below.
112 static const char *abbrevs
[] = {
113 N_("aextended attribute"),
114 N_("Aerror allocating"),
118 N_("Cconflicts with some other fs @b"),
125 N_("E@e '%Dn' in %p (%i)"),
127 N_("Ffor @i %i (%Q) is"),
132 N_("mmultiply-claimed"),
149 * Give more user friendly names to the "special" inodes.
151 #define num_special_inodes 11
152 static const char *special_inode_name
[] =
154 N_("<The NULL inode>"), /* 0 */
155 N_("<The bad blocks inode>"), /* 1 */
157 N_("<The user quota inode>"), /* 3 */
158 N_("<The group quota inode>"), /* 4 */
159 N_("<The boot loader inode>"), /* 5 */
160 N_("<The undelete directory inode>"), /* 6 */
161 N_("<The group descriptor inode>"), /* 7 */
162 N_("<The journal inode>"), /* 8 */
163 N_("<Reserved inode 9>"), /* 9 */
164 N_("<Reserved inode 10>"), /* 10 */
168 * This function does "safe" printing. It will convert non-printable
169 * ASCII characters using '^' and M- notation.
171 static void safe_print(FILE *f
, const char *cp
, int len
)
184 if ((ch
< 32) || (ch
== 0x7f)) {
186 ch
^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
194 * This function prints a pathname, using the ext2fs_get_pathname
197 static void print_pathname(FILE *f
, ext2_filsys fs
, ext2_ino_t dir
,
200 errcode_t retval
= 0;
203 if (!dir
&& (ino
< num_special_inodes
)) {
204 fputs(_(special_inode_name
[ino
]), f
);
209 retval
= ext2fs_get_pathname(fs
, dir
, ino
, &path
);
213 safe_print(f
, path
, -1);
214 ext2fs_free_mem(&path
);
218 static void print_time(FILE *f
, time_t t
)
220 const char * time_str
;
221 static int do_gmt
= -1;
224 /* The diet libc doesn't respect the TZ environemnt variable */
226 time_str
= getenv("TZ");
229 do_gmt
= !strcmp(time_str
, "GMT0");
232 time_str
= asctime((do_gmt
> 0) ? gmtime(&t
) : localtime(&t
));
233 fprintf(f
, "%.24s", time_str
);
237 * This function handles the '@' expansion. We allow recursive
238 * expansion; an @ expression can contain further '@' and '%'
241 static _INLINE_
void expand_at_expression(FILE *f
, e2fsck_t ctx
, char ch
,
242 struct problem_context
*pctx
,
243 int *first
, int recurse
)
245 const char **cpp
, *str
;
247 /* Search for the abbreviation */
248 for (cpp
= abbrevs
; *cpp
; cpp
++) {
252 if (*cpp
&& recurse
< 10) {
254 if (*first
&& islower(*str
)) {
256 fputc(toupper(*str
++), f
);
258 print_e2fsck_message(f
, ctx
, str
, pctx
, *first
, recurse
+1);
260 fprintf(f
, "@%c", ch
);
264 * This function expands '%IX' expressions
266 static _INLINE_
void expand_inode_expression(FILE *f
, ext2_filsys fs
, char ch
,
267 struct problem_context
*ctx
)
269 struct ext2_inode
*inode
;
270 struct ext2_inode_large
*large_inode
;
272 if (!ctx
|| !ctx
->inode
)
276 large_inode
= (struct ext2_inode_large
*) inode
;
280 if (LINUX_S_ISDIR(inode
->i_mode
))
281 fprintf(f
, "%u", inode
->i_size
);
283 #ifdef EXT2_NO_64_TYPE
284 if (inode
->i_size_high
)
285 fprintf(f
, "0x%x%08x", inode
->i_size_high
,
288 fprintf(f
, "%u", inode
->i_size
);
290 fprintf(f
, "%llu", EXT2_I_SIZE(inode
));
295 fprintf(f
, "%u", large_inode
->i_extra_isize
);
298 if (fs
->super
->s_feature_ro_compat
&
299 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
)
300 fprintf(f
, "%llu", inode
->i_blocks
+
301 (((long long) inode
->osd2
.linux2
.l_i_blocks_hi
)
304 fprintf(f
, "%u", inode
->i_blocks
);
307 fprintf(f
, "%d", inode
->i_links_count
);
310 fprintf(f
, "0%o", inode
->i_mode
);
313 print_time(f
, inode
->i_mtime
);
316 fprintf(f
, "%u", inode
->i_faddr
);
319 fprintf(f
, "%llu", ext2fs_file_acl_block(fs
, inode
));
322 fprintf(f
, "%u", (LINUX_S_ISDIR(inode
->i_mode
) ?
323 inode
->i_dir_acl
: 0));
326 fprintf(f
, "%d", inode_uid(*inode
));
329 fprintf(f
, "%d", inode_gid(*inode
));
332 if (LINUX_S_ISREG(inode
->i_mode
))
333 fputs(_("regular file"), f
);
334 else if (LINUX_S_ISDIR(inode
->i_mode
))
335 fputs(_("directory"), f
);
336 else if (LINUX_S_ISCHR(inode
->i_mode
))
337 fputs(_("character device"), f
);
338 else if (LINUX_S_ISBLK(inode
->i_mode
))
339 fputs(_("block device"), f
);
340 else if (LINUX_S_ISFIFO(inode
->i_mode
))
341 fputs(_("named pipe"), f
);
342 else if (LINUX_S_ISLNK(inode
->i_mode
))
343 fputs(_("symbolic link"), f
);
344 else if (LINUX_S_ISSOCK(inode
->i_mode
))
345 fputs(_("socket"), f
);
347 fprintf(f
, _("unknown file type with mode 0%o"),
352 fprintf(f
, "%%I%c", ch
);
358 * This function expands '%dX' expressions
360 static _INLINE_
void expand_dirent_expression(FILE *f
, ext2_filsys fs
, char ch
,
361 struct problem_context
*ctx
)
363 struct ext2_dir_entry
*dirent
;
364 unsigned int rec_len
, len
;
366 if (!ctx
|| !ctx
->dirent
)
369 dirent
= ctx
->dirent
;
373 fprintf(f
, "%u", dirent
->inode
);
376 len
= dirent
->name_len
& 0xFF;
377 if ((ext2fs_get_rec_len(fs
, dirent
, &rec_len
) == 0) &&
380 safe_print(f
, dirent
->name
, len
);
383 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
384 fprintf(f
, "%u", rec_len
);
387 fprintf(f
, "%u", dirent
->name_len
& 0xFF);
390 fprintf(f
, "%u", dirent
->name_len
>> 8);
394 fprintf(f
, "%%D%c", ch
);
399 static _INLINE_
void expand_percent_expression(FILE *f
, ext2_filsys fs
,
400 char ch
, int width
, int *first
,
401 struct problem_context
*ctx
)
403 e2fsck_t e2fsck_ctx
= fs
? (e2fsck_t
) fs
->priv_data
: NULL
;
414 #ifdef EXT2_NO_64_TYPE
415 fprintf(f
, "%*u", width
, (unsigned long) ctx
->blk
);
417 fprintf(f
, "%*llu", width
, (unsigned long long) ctx
->blk
);
421 if (ctx
->blkcount
== BLOCK_COUNT_IND
)
422 m
= _("indirect block");
423 else if (ctx
->blkcount
== BLOCK_COUNT_DIND
)
424 m
= _("double indirect block");
425 else if (ctx
->blkcount
== BLOCK_COUNT_TIND
)
426 m
= _("triple indirect block");
427 else if (ctx
->blkcount
== BLOCK_COUNT_TRANSLATOR
)
428 m
= _("translator block");
431 if (*first
&& islower(m
[0]))
432 fputc(toupper(*m
++), f
);
434 if (ctx
->blkcount
>= 0) {
435 #ifdef EXT2_NO_64_TYPE
436 fprintf(f
, "%d", ctx
->blkcount
);
438 fprintf(f
, "%lld", (long long) ctx
->blkcount
);
443 #ifdef EXT2_NO_64_TYPE
444 fprintf(f
, "%*u", width
, (unsigned long) ctx
->blk2
);
446 fprintf(f
, "%*llu", width
, (unsigned long long) ctx
->blk2
);
450 fprintf(f
, "%*u", width
, ctx
->dir
);
453 fprintf(f
, "%*d", width
, ctx
->group
);
456 fprintf(f
, "%*u", width
, ctx
->ino
);
459 fprintf(f
, "%*u", width
, ctx
->ino2
);
462 fprintf(f
, "%*s", width
, error_message(ctx
->errcode
));
465 #ifdef EXT2_NO_64_TYPE
466 fprintf(f
, "%*u", width
, ctx
->num
);
468 fprintf(f
, "%*llu", width
, (long long)ctx
->num
);
472 print_pathname(f
, fs
, ctx
->ino
, 0);
475 print_pathname(f
, fs
, ctx
->ino2
,
476 ctx
->dirent
? ctx
->dirent
->inode
: 0);
479 print_pathname(f
, fs
, ctx
->dir
, 0);
482 print_pathname(f
, fs
, ctx
->dir
, ctx
->ino
);
485 #ifdef EXT2_NO_64_TYPE
486 fprintf(f
, "%*d", width
, ctx
->blkcount
);
488 fprintf(f
, "%*lld", width
, (long long) ctx
->blkcount
);
492 fprintf(f
, "%u", get_backup_sb(NULL
, fs
, NULL
, NULL
));
495 fprintf(f
, "%*s", width
, ctx
->str
? ctx
->str
: "NULL");
498 print_time(f
, (time_t) ctx
->num
);
501 print_time(f
, e2fsck_ctx
? e2fsck_ctx
->now
: time(0));
504 fprintf(f
, "0x%0*x", width
, ctx
->csum1
);
507 #ifdef EXT2_NO_64_TYPE
508 fprintf(f
, "0x%0*x", width
, ctx
->num
);
510 fprintf(f
, "0x%0*llx", width
, (long long)ctx
->num
);
514 fprintf(f
, "0x%0*x", width
, ctx
->csum2
);
518 fprintf(f
, "%%%c", ch
);
523 void print_e2fsck_message(FILE *f
, e2fsck_t ctx
, const char *msg
,
524 struct problem_context
*pctx
, int first
,
527 ext2_filsys fs
= ctx
->fs
;
531 e2fsck_clear_progbar(ctx
);
532 for (cp
= msg
; *cp
; cp
++) {
535 expand_at_expression(f
, ctx
, *cp
, pctx
, &first
,
537 } else if (cp
[0] == '%') {
540 while (isdigit(cp
[0])) {
541 width
= (width
* 10) + cp
[0] - '0';
546 expand_inode_expression(f
, fs
, *cp
, pctx
);
547 } else if (cp
[0] == 'D') {
549 expand_dirent_expression(f
, fs
, *cp
, pctx
);
551 expand_percent_expression(f
, fs
, *cp
, width
,
555 for (i
=0; cp
[i
]; i
++)
556 if ((cp
[i
] == '@') || cp
[i
] == '%')
558 fprintf(f
, "%.*s", i
, cp
);