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
99 #define _INLINE_ __inline__
105 * This structure defines the abbreviations used by the text strings
106 * below. The first character in the string is the index letter. An
107 * abbreviation of the form '@<i>' is expanded by looking up the index
108 * letter <i> in the table below.
110 static const char *abbrevs[] = {
111 N_("aextended attribute"),
112 N_("Aerror allocating"),
116 N_("Cconflicts with some other fs @b"),
123 N_("E@e '%Dn' in %p (%i)"),
125 N_("Ffor @i %i (%Q) is"),
130 N_("mmultiply-claimed"),
146 * Give more user friendly names to the "special" inodes.
148 #define num_special_inodes 11
149 static const char *special_inode_name[] =
151 N_("<The NULL inode>"), /* 0 */
152 N_("<The bad blocks inode>"), /* 1 */
154 N_("<The user quota inode>"), /* 3 */
155 N_("<The group quota inode>"), /* 4 */
156 N_("<The boot loader inode>"), /* 5 */
157 N_("<The undelete directory inode>"), /* 6 */
158 N_("<The group descriptor inode>"), /* 7 */
159 N_("<The journal inode>"), /* 8 */
160 N_("<Reserved inode 9>"), /* 9 */
161 N_("<Reserved inode 10>"), /* 10 */
165 * This function does "safe" printing. It will convert non-printable
166 * ASCII characters using '^' and M- notation.
168 static void safe_print(const char *cp, int len)
181 if ((ch < 32) || (ch == 0x7f)) {
183 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
191 * This function prints a pathname, using the ext2fs_get_pathname
194 static void print_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino)
199 if (!dir && (ino < num_special_inodes)) {
200 fputs(_(special_inode_name[ino]), stdout);
204 retval = ext2fs_get_pathname(fs, dir, ino, &path);
206 fputs("???", stdout);
208 safe_print(path, -1);
209 ext2fs_free_mem(&path);
213 static void print_time(time_t t)
215 const char * time_str;
216 static int do_gmt = -1;
219 /* The diet libc doesn't respect the TZ environemnt variable */
221 time_str = getenv("TZ");
224 do_gmt = !strcmp(time_str, "GMT0");
227 time_str = asctime((do_gmt > 0) ? gmtime(&t) : localtime(&t));
228 printf("%.24s", time_str);
232 * This function handles the '@' expansion. We allow recursive
233 * expansion; an @ expression can contain further '@' and '%'
236 static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
237 struct problem_context *pctx,
238 int *first, int recurse)
240 const char **cpp, *str;
242 /* Search for the abbreviation */
243 for (cpp = abbrevs; *cpp; cpp++) {
247 if (*cpp && recurse < 10) {
249 if (*first && islower(*str)) {
251 fputc(toupper(*str++), stdout);
253 print_e2fsck_message(ctx, str, pctx, *first, recurse+1);
259 * This function expands '%IX' expressions
261 static _INLINE_ void expand_inode_expression(ext2_filsys fs, char ch,
262 struct problem_context *ctx)
264 struct ext2_inode *inode;
265 struct ext2_inode_large *large_inode;
267 if (!ctx || !ctx->inode)
271 large_inode = (struct ext2_inode_large *) inode;
275 if (LINUX_S_ISDIR(inode->i_mode))
276 printf("%u", inode->i_size);
278 #ifdef EXT2_NO_64_TYPE
279 if (inode->i_size_high)
280 printf("0x%x%08x", inode->i_size_high,
283 printf("%u", inode->i_size);
285 printf("%llu", EXT2_I_SIZE(inode));
290 printf("%u", large_inode->i_extra_isize);
293 if (fs->super->s_feature_ro_compat &
294 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
295 printf("%llu", inode->i_blocks +
296 (((long long) inode->osd2.linux2.l_i_blocks_hi)
299 printf("%u", inode->i_blocks);
302 printf("%d", inode->i_links_count);
305 printf("0%o", inode->i_mode);
308 print_time(inode->i_mtime);
311 printf("%u", inode->i_faddr);
314 printf("%llu", ext2fs_file_acl_block(inode));
317 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
318 inode->i_dir_acl : 0));
321 printf("%d", inode_uid(*inode));
324 printf("%d", inode_gid(*inode));
327 if (LINUX_S_ISREG(inode->i_mode))
328 printf(_("regular file"));
329 else if (LINUX_S_ISDIR(inode->i_mode))
330 printf(_("directory"));
331 else if (LINUX_S_ISCHR(inode->i_mode))
332 printf(_("character device"));
333 else if (LINUX_S_ISBLK(inode->i_mode))
334 printf(_("block device"));
335 else if (LINUX_S_ISFIFO(inode->i_mode))
336 printf(_("named pipe"));
337 else if (LINUX_S_ISLNK(inode->i_mode))
338 printf(_("symbolic link"));
339 else if (LINUX_S_ISSOCK(inode->i_mode))
342 printf(_("unknown file type with mode 0%o"),
353 * This function expands '%dX' expressions
355 static _INLINE_ void expand_dirent_expression(ext2_filsys fs, char ch,
356 struct problem_context *ctx)
358 struct ext2_dir_entry *dirent;
359 unsigned int rec_len;
362 if (!ctx || !ctx->dirent)
365 dirent = ctx->dirent;
369 printf("%u", dirent->inode);
372 len = dirent->name_len & 0xFF;
373 if (len > EXT2_NAME_LEN)
375 if ((ext2fs_get_rec_len(fs, dirent, &rec_len) == 0) &&
378 safe_print(dirent->name, len);
381 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
382 printf("%u", rec_len);
385 printf("%u", dirent->name_len & 0xFF);
388 printf("%u", dirent->name_len >> 8);
397 static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
399 struct problem_context *ctx)
401 e2fsck_t e2fsck_ctx = fs ? (e2fsck_t) fs->priv_data : NULL;
412 #ifdef EXT2_NO_64_TYPE
413 printf("%u", (unsigned long) ctx->blk);
415 printf("%llu", (unsigned long long) ctx->blk);
419 if (ctx->blkcount == BLOCK_COUNT_IND)
420 m = _("indirect block");
421 else if (ctx->blkcount == BLOCK_COUNT_DIND)
422 m = _("double indirect block");
423 else if (ctx->blkcount == BLOCK_COUNT_TIND)
424 m = _("triple indirect block");
425 else if (ctx->blkcount == BLOCK_COUNT_TRANSLATOR)
426 m = _("translator block");
429 if (*first && islower(m[0]))
430 fputc(toupper(*m++), stdout);
432 if (ctx->blkcount >= 0) {
433 #ifdef EXT2_NO_64_TYPE
434 printf("%d", ctx->blkcount);
436 printf("%lld", (long long) ctx->blkcount);
441 #ifdef EXT2_NO_64_TYPE
442 printf("%u", (unsigned long) ctx->blk2);
444 printf("%llu", (unsigned long long) ctx->blk2);
448 printf("%u", ctx->dir);
451 printf("%d", ctx->group);
454 printf("%u", ctx->ino);
457 printf("%u", ctx->ino2);
460 printf("%s", error_message(ctx->errcode));
463 #ifdef EXT2_NO_64_TYPE
464 printf("%u", ctx->num);
466 printf("%llu", (long long)ctx->num);
470 print_pathname(fs, ctx->ino, 0);
473 print_pathname(fs, ctx->ino2,
474 ctx->dirent ? ctx->dirent->inode : 0);
477 print_pathname(fs, ctx->dir, 0);
480 print_pathname(fs, ctx->dir, ctx->ino);
483 #ifdef EXT2_NO_64_TYPE
484 printf("%d", ctx->blkcount);
486 printf("%lld", (long long) ctx->blkcount);
490 printf("%u", get_backup_sb(NULL, fs, NULL, NULL));
493 printf("%s", ctx->str ? ctx->str : "NULL");
496 print_time((time_t) ctx->num);
499 print_time(e2fsck_ctx ? e2fsck_ctx->now : time(0));
502 #ifdef EXT2_NO_64_TYPE
503 printf("0x%x", ctx->num);
505 printf("0x%llx", (long long)ctx->num);
515 void print_e2fsck_message(e2fsck_t ctx, const char *msg,
516 struct problem_context *pctx, int first,
519 ext2_filsys fs = ctx->fs;
523 e2fsck_clear_progbar(ctx);
524 for (cp = msg; *cp; cp++) {
527 expand_at_expression(ctx, *cp, pctx, &first, recurse);
528 } else if (cp[0] == '%' && cp[1] == 'I') {
530 expand_inode_expression(fs, *cp, pctx);
531 } else if (cp[0] == '%' && cp[1] == 'D') {
533 expand_dirent_expression(fs, *cp, pctx);
534 } else if ((cp[0] == '%')) {
536 expand_percent_expression(fs, *cp, &first, pctx);
538 for (i=0; cp[i]; i++)
539 if ((cp[i] == '@') || cp[i] == '%')
541 printf("%.*s", i, cp);