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
100 #define _INLINE_ __inline__
106 * This structure defines the abbreviations used by the text strings
107 * below. The first character in the string is the index letter. An
108 * abbreviation of the form '@<i>' is expanded by looking up the index
109 * letter <i> in the table below.
111 static const char *abbrevs
[] = {
112 N_("aextended attribute"),
113 N_("Aerror allocating"),
117 N_("Cconflicts with some other fs @b"),
124 N_("E@e '%Dn' in %p (%i)"),
126 N_("Ffor @i %i (%Q) is"),
131 N_("mmultiply-claimed"),
148 * Give more user friendly names to the "special" inodes.
150 #define num_special_inodes 11
151 static const char *special_inode_name
[] =
153 N_("<The NULL inode>"), /* 0 */
154 N_("<The bad blocks inode>"), /* 1 */
156 N_("<The user quota inode>"), /* 3 */
157 N_("<The group quota inode>"), /* 4 */
158 N_("<The boot loader inode>"), /* 5 */
159 N_("<The undelete directory inode>"), /* 6 */
160 N_("<The group descriptor inode>"), /* 7 */
161 N_("<The journal inode>"), /* 8 */
162 N_("<Reserved inode 9>"), /* 9 */
163 N_("<Reserved inode 10>"), /* 10 */
167 * This function does "safe" printing. It will convert non-printable
168 * ASCII characters using '^' and M- notation.
170 static void safe_print(const char *cp
, int len
)
183 if ((ch
< 32) || (ch
== 0x7f)) {
185 ch
^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
193 * This function prints a pathname, using the ext2fs_get_pathname
196 static void print_pathname(ext2_filsys fs
, ext2_ino_t dir
, ext2_ino_t ino
)
201 if (!dir
&& (ino
< num_special_inodes
)) {
202 fputs(_(special_inode_name
[ino
]), stdout
);
206 retval
= ext2fs_get_pathname(fs
, dir
, ino
, &path
);
208 fputs("???", stdout
);
210 safe_print(path
, -1);
211 ext2fs_free_mem(&path
);
215 static void print_time(time_t t
)
217 const char * time_str
;
218 static int do_gmt
= -1;
221 /* The diet libc doesn't respect the TZ environemnt variable */
223 time_str
= getenv("TZ");
226 do_gmt
= !strcmp(time_str
, "GMT0");
229 time_str
= asctime((do_gmt
> 0) ? gmtime(&t
) : localtime(&t
));
230 printf("%.24s", time_str
);
234 * This function handles the '@' expansion. We allow recursive
235 * expansion; an @ expression can contain further '@' and '%'
238 static _INLINE_
void expand_at_expression(e2fsck_t ctx
, char ch
,
239 struct problem_context
*pctx
,
240 int *first
, int recurse
)
242 const char **cpp
, *str
;
244 /* Search for the abbreviation */
245 for (cpp
= abbrevs
; *cpp
; cpp
++) {
249 if (*cpp
&& recurse
< 10) {
251 if (*first
&& islower(*str
)) {
253 fputc(toupper(*str
++), stdout
);
255 print_e2fsck_message(ctx
, str
, pctx
, *first
, recurse
+1);
261 * This function expands '%IX' expressions
263 static _INLINE_
void expand_inode_expression(ext2_filsys fs
, char ch
,
264 struct problem_context
*ctx
)
266 struct ext2_inode
*inode
;
267 struct ext2_inode_large
*large_inode
;
269 if (!ctx
|| !ctx
->inode
)
273 large_inode
= (struct ext2_inode_large
*) inode
;
277 if (LINUX_S_ISDIR(inode
->i_mode
))
278 printf("%u", inode
->i_size
);
280 #ifdef EXT2_NO_64_TYPE
281 if (inode
->i_size_high
)
282 printf("0x%x%08x", inode
->i_size_high
,
285 printf("%u", inode
->i_size
);
287 printf("%llu", EXT2_I_SIZE(inode
));
292 printf("%u", large_inode
->i_extra_isize
);
295 if (fs
->super
->s_feature_ro_compat
&
296 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
)
297 printf("%llu", inode
->i_blocks
+
298 (((long long) inode
->osd2
.linux2
.l_i_blocks_hi
)
301 printf("%u", inode
->i_blocks
);
304 printf("%d", inode
->i_links_count
);
307 printf("0%o", inode
->i_mode
);
310 print_time(inode
->i_mtime
);
313 printf("%u", inode
->i_faddr
);
316 printf("%llu", ext2fs_file_acl_block(inode
));
319 printf("%u", (LINUX_S_ISDIR(inode
->i_mode
) ?
320 inode
->i_dir_acl
: 0));
323 printf("%d", inode_uid(*inode
));
326 printf("%d", inode_gid(*inode
));
329 if (LINUX_S_ISREG(inode
->i_mode
))
330 printf(_("regular file"));
331 else if (LINUX_S_ISDIR(inode
->i_mode
))
332 printf(_("directory"));
333 else if (LINUX_S_ISCHR(inode
->i_mode
))
334 printf(_("character device"));
335 else if (LINUX_S_ISBLK(inode
->i_mode
))
336 printf(_("block device"));
337 else if (LINUX_S_ISFIFO(inode
->i_mode
))
338 printf(_("named pipe"));
339 else if (LINUX_S_ISLNK(inode
->i_mode
))
340 printf(_("symbolic link"));
341 else if (LINUX_S_ISSOCK(inode
->i_mode
))
344 printf(_("unknown file type with mode 0%o"),
355 * This function expands '%dX' expressions
357 static _INLINE_
void expand_dirent_expression(ext2_filsys fs
, char ch
,
358 struct problem_context
*ctx
)
360 struct ext2_dir_entry
*dirent
;
361 unsigned int rec_len
;
364 if (!ctx
|| !ctx
->dirent
)
367 dirent
= ctx
->dirent
;
371 printf("%u", dirent
->inode
);
374 len
= dirent
->name_len
& 0xFF;
375 if (len
> EXT2_NAME_LEN
)
377 if ((ext2fs_get_rec_len(fs
, dirent
, &rec_len
) == 0) &&
380 safe_print(dirent
->name
, len
);
383 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
384 printf("%u", rec_len
);
387 printf("%u", dirent
->name_len
& 0xFF);
390 printf("%u", dirent
->name_len
>> 8);
399 static _INLINE_
void expand_percent_expression(ext2_filsys fs
, char ch
,
401 struct problem_context
*ctx
)
403 e2fsck_t e2fsck_ctx
= fs
? (e2fsck_t
) fs
->priv_data
: NULL
;
414 #ifdef EXT2_NO_64_TYPE
415 printf("%u", (unsigned long) ctx
->blk
);
417 printf("%llu", (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
++), stdout
);
434 if (ctx
->blkcount
>= 0) {
435 #ifdef EXT2_NO_64_TYPE
436 printf("%d", ctx
->blkcount
);
438 printf("%lld", (long long) ctx
->blkcount
);
443 #ifdef EXT2_NO_64_TYPE
444 printf("%u", (unsigned long) ctx
->blk2
);
446 printf("%llu", (unsigned long long) ctx
->blk2
);
450 printf("%u", ctx
->dir
);
453 printf("%d", ctx
->group
);
456 printf("%u", ctx
->ino
);
459 printf("%u", ctx
->ino2
);
462 printf("%s", error_message(ctx
->errcode
));
465 #ifdef EXT2_NO_64_TYPE
466 printf("%u", ctx
->num
);
468 printf("%llu", (long long)ctx
->num
);
472 print_pathname(fs
, ctx
->ino
, 0);
475 print_pathname(fs
, ctx
->ino2
,
476 ctx
->dirent
? ctx
->dirent
->inode
: 0);
479 print_pathname(fs
, ctx
->dir
, 0);
482 print_pathname(fs
, ctx
->dir
, ctx
->ino
);
485 #ifdef EXT2_NO_64_TYPE
486 printf("%d", ctx
->blkcount
);
488 printf("%lld", (long long) ctx
->blkcount
);
492 printf("%u", get_backup_sb(NULL
, fs
, NULL
, NULL
));
495 printf("%s", ctx
->str
? ctx
->str
: "NULL");
498 print_time((time_t) ctx
->num
);
501 print_time(e2fsck_ctx
? e2fsck_ctx
->now
: time(0));
504 #ifdef EXT2_NO_64_TYPE
505 printf("0x%x", ctx
->num
);
507 printf("0x%llx", (long long)ctx
->num
);
517 void print_e2fsck_message(e2fsck_t ctx
, const char *msg
,
518 struct problem_context
*pctx
, int first
,
521 ext2_filsys fs
= ctx
->fs
;
525 e2fsck_clear_progbar(ctx
);
526 for (cp
= msg
; *cp
; cp
++) {
529 expand_at_expression(ctx
, *cp
, pctx
, &first
, recurse
);
530 } else if (cp
[0] == '%' && cp
[1] == 'I') {
532 expand_inode_expression(fs
, *cp
, pctx
);
533 } else if (cp
[0] == '%' && cp
[1] == 'D') {
535 expand_dirent_expression(fs
, *cp
, pctx
);
536 } else if ((cp
[0] == '%')) {
538 expand_percent_expression(fs
, *cp
, &first
, pctx
);
540 for (i
=0; cp
[i
]; i
++)
541 if ((cp
[i
] == '@') || cp
[i
] == '%')
543 printf("%.*s", i
, cp
);