1 /*
2  * message.c --- print e2fsck messages (with compression)
3  *
4  * Copyright 1996, 1997 by Theodore Ts'o
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  *
11  * print_e2fsck_message() prints a message to the user, using
12  * compression techniques and expansions of abbreviations.
13  *
14  * The following % expansions are supported:
15  *
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
25  * 	%g	<group>			integer
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_size_high
36  * 	%Iu	<inode> -> i_uid
37  * 	%Ig	<inode> -> i_gid
38  *	%It	<inode type>
39  * 	%j	<ino2>			inode number
40  * 	%m	<com_err error message>
41  * 	%N	<num>
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  *	%t	time (in <num>)
52  *	%T	current time
53  *	%U	quota type (in <num>)
54  * 	%S	backup superblock
55  * 	%X	<num> hexadecimal format
56  *
57  * The following '@' expansions are supported:
58  *
59  * 	@a	extended attribute
60  * 	@A	error allocating
61  * 	@b	block
62  * 	@B	bitmap
63  * 	@c	compress
64  * 	@C	conflicts with some other fs block
65  * 	@D	deleted
66  * 	@d	directory
67  * 	@e	entry
68  * 	@E	Entry '%Dn' in %p (%i)
69  * 	@f	filesystem
70  * 	@F	for @i %i (%Q) is
71  * 	@g	group
72  * 	@h	HTREE directory inode
73  * 	@i	inode
74  * 	@I	illegal
75  * 	@j	journal
76  * 	@l	lost+found
77  * 	@L	is a link
78  *	@m	multiply-claimed
79  *	@n	invalid
80  * 	@o	orphaned
81  * 	@p	problem in
82  *	@q	quota
83  * 	@r	root inode
84  * 	@s	should be
85  * 	@S	superblock
86  * 	@u	unattached
87  * 	@v	device
88  *	@x	extent
89  * 	@z	zero-length
90  */
91 
92 #include "config.h"
93 #include <stdlib.h>
94 #include <unistd.h>
95 #include <string.h>
96 #include <ctype.h>
97 #include <termios.h>
98 #include "support/quotaio.h"
99 
100 #include "e2fsck.h"
101 #include "problem.h"
102 
103 #ifdef __GNUC__
104 #define _INLINE_ __inline__
105 #else
106 #define _INLINE_
107 #endif
108 
109 /*
110  * This structure defines the abbreviations used by the text strings
111  * below.  The first character in the string is the index letter.  An
112  * abbreviation of the form '@<i>' is expanded by looking up the index
113  * letter <i> in the table below.
114  */
115 static const char *abbrevs[] = {
116 	N_("aextended attribute"),
117 	N_("Aerror allocating"),
118 	N_("bblock"),
119 	N_("Bbitmap"),
120 	N_("ccompress"),
121 	N_("Cconflicts with some other fs @b"),
122 	N_("ddirectory"),
123 	N_("Ddeleted"),
124 	N_("eentry"),
125 	N_("E@e '%Dn' in %p (%i)"),
126 	N_("ffilesystem"),
127 	N_("Ffor @i %i (%Q) is"),
128 	N_("ggroup"),
129 	N_("hHTREE @d @i"),
130 	N_("iinode"),
131 	N_("Iillegal"),
132 	N_("jjournal"),
133 	N_("llost+found"),
134 	N_("Lis a link"),
135 	N_("mmultiply-claimed"),
136 	N_("ninvalid"),
137 	N_("oorphaned"),
138 	N_("pproblem in"),
139 	N_("qquota"),
140 	N_("rroot @i"),
141 	N_("sshould be"),
142 	N_("Ssuper@b"),
143 	N_("uunattached"),
144 	N_("vdevice"),
145 	N_("xextent"),
146 	N_("zzero-length"),
147 	"@@",
148 	0
149 	};
150 
151 /*
152  * Give more user friendly names to the "special" inodes.
153  */
154 #define num_special_inodes	11
155 static const char *special_inode_name[] =
156 {
157 	N_("<The NULL inode>"),			/* 0 */
158 	N_("<The bad blocks inode>"),		/* 1 */
159 	"/",					/* 2 */
160 	N_("<The user quota inode>"),		/* 3 */
161 	N_("<The group quota inode>"),		/* 4 */
162 	N_("<The boot loader inode>"),		/* 5 */
163 	N_("<The undelete directory inode>"),	/* 6 */
164 	N_("<The group descriptor inode>"),	/* 7 */
165 	N_("<The journal inode>"),		/* 8 */
166 	N_("<Reserved inode 9>"),		/* 9 */
167 	N_("<Reserved inode 10>"),		/* 10 */
168 };
169 
170 /*
171  * This function does "safe" printing.  It will convert non-printable
172  * ASCII characters using '^' and M- notation.
173  */
safe_print(FILE * f,const char * cp,int len)174 static void safe_print(FILE *f, const char *cp, int len)
175 {
176 	unsigned char	ch;
177 
178 	if (len < 0)
179 		len = strlen(cp);
180 
181 	while (len--) {
182 		ch = *cp++;
183 		if (ch > 128) {
184 			fputs("M-", f);
185 			ch -= 128;
186 		}
187 		if ((ch < 32) || (ch == 0x7f)) {
188 			fputc('^', f);
189 			ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
190 		}
191 		fputc(ch, f);
192 	}
193 }
194 
195 
196 /*
197  * This function prints a pathname, using the ext2fs_get_pathname
198  * function
199  */
print_pathname(FILE * f,ext2_filsys fs,ext2_ino_t dir,ext2_ino_t ino)200 static void print_pathname(FILE *f, ext2_filsys fs, ext2_ino_t dir,
201 			   ext2_ino_t ino)
202 {
203 	errcode_t	retval = 0;
204 	char		*path;
205 
206 	if (!dir && (ino < num_special_inodes)) {
207 		fputs(_(special_inode_name[ino]), f);
208 		return;
209 	}
210 
211 	if (fs)
212 		retval = ext2fs_get_pathname(fs, dir, ino, &path);
213 	if (!fs || retval)
214 		fputs("???", f);
215 	else {
216 		safe_print(f, path, -1);
217 		ext2fs_free_mem(&path);
218 	}
219 }
220 
print_time(FILE * f,time_t t)221 static void print_time(FILE *f, time_t t)
222 {
223 	const char *		time_str;
224 	static int		do_gmt = -1;
225 
226 #ifdef __dietlibc__
227 		/* The diet libc doesn't respect the TZ environment variable */
228 		if (do_gmt == -1) {
229 			time_str = getenv("TZ");
230 			if (!time_str)
231 				time_str = "";
232 			do_gmt = !strcmp(time_str, "GMT") ||
233 				!strcmp(time_str, "GMT0");
234 		}
235 #endif
236 		time_str = asctime((do_gmt > 0) ? gmtime(&t) : localtime(&t));
237 		fprintf(f, "%.24s", time_str);
238 }
239 
240 /*
241  * This function handles the '@' expansion.  We allow recursive
242  * expansion; an @ expression can contain further '@' and '%'
243  * expressions.
244  */
expand_at_expression(FILE * f,e2fsck_t ctx,char ch,struct problem_context * pctx,int * first,int recurse)245 static _INLINE_ void expand_at_expression(FILE *f, e2fsck_t ctx, char ch,
246 					  struct problem_context *pctx,
247 					  int *first, int recurse)
248 {
249 	const char **cpp, *str;
250 
251 	/* Search for the abbreviation */
252 	for (cpp = abbrevs; *cpp; cpp++) {
253 		if (ch == *cpp[0])
254 			break;
255 	}
256 	if (*cpp && recurse < 10) {
257 		str = _(*cpp) + 1;
258 		if (*first && islower(*str)) {
259 			*first = 0;
260 			fputc(toupper(*str++), f);
261 		}
262 		print_e2fsck_message(f, ctx, str, pctx, *first, recurse+1);
263 	} else
264 		fprintf(f, "@%c", ch);
265 }
266 
267 /*
268  * This function expands '%IX' expressions
269  */
expand_inode_expression(FILE * f,ext2_filsys fs,char ch,struct problem_context * ctx)270 static _INLINE_ void expand_inode_expression(FILE *f, ext2_filsys fs, char ch,
271 					     struct problem_context *ctx)
272 {
273 	struct ext2_inode	*inode;
274 	struct ext2_inode_large	*large_inode;
275 
276 	if (!ctx || !ctx->inode)
277 		goto no_inode;
278 
279 	inode = ctx->inode;
280 	large_inode = (struct ext2_inode_large *) inode;
281 
282 	switch (ch) {
283 	case 's':
284 		fprintf(f, "%llu", (unsigned long long) EXT2_I_SIZE(inode));
285 		break;
286 	case 'S':
287 		fprintf(f, "%u", large_inode->i_extra_isize);
288 		break;
289 	case 'b':
290 		if (ext2fs_has_feature_huge_file(fs->super))
291 			fprintf(f, "%llu", inode->i_blocks +
292 				(((long long) inode->osd2.linux2.l_i_blocks_hi)
293 				 << 32));
294 		else
295 			fprintf(f, "%u", inode->i_blocks);
296 		break;
297 	case 'l':
298 		fprintf(f, "%d", inode->i_links_count);
299 		break;
300 	case 'm':
301 		fprintf(f, "0%o", inode->i_mode);
302 		break;
303 	case 'M':
304 		print_time(f, inode->i_mtime);
305 		break;
306 	case 'F':
307 		fprintf(f, "%u", inode->i_faddr);
308 		break;
309 	case 'f':
310 		fprintf(f, "%llu",
311 			(unsigned long long) ext2fs_file_acl_block(fs, inode));
312 		break;
313 	case 'd':
314 		fprintf(f, "%u", (LINUX_S_ISDIR(inode->i_mode) ?
315 			inode->i_size_high : 0));
316 		break;
317 	case 'u':
318 		fprintf(f, "%d", inode_uid(*inode));
319 		break;
320 	case 'g':
321 		fprintf(f, "%d", inode_gid(*inode));
322 		break;
323 	case 't':
324 		if (LINUX_S_ISREG(inode->i_mode))
325 			fputs(_("regular file"), f);
326 		else if (LINUX_S_ISDIR(inode->i_mode))
327 			fputs(_("directory"), f);
328 		else if (LINUX_S_ISCHR(inode->i_mode))
329 			fputs(_("character device"), f);
330 		else if (LINUX_S_ISBLK(inode->i_mode))
331 			fputs(_("block device"), f);
332 		else if (LINUX_S_ISFIFO(inode->i_mode))
333 			fputs(_("named pipe"), f);
334 		else if (LINUX_S_ISLNK(inode->i_mode))
335 			fputs(_("symbolic link"), f);
336 		else if (LINUX_S_ISSOCK(inode->i_mode))
337 			fputs(_("socket"), f);
338 		else
339 			fprintf(f, _("unknown file type with mode 0%o"),
340 				inode->i_mode);
341 		break;
342 	default:
343 	no_inode:
344 		fprintf(f, "%%I%c", ch);
345 		break;
346 	}
347 }
348 
349 /*
350  * This function expands '%dX' expressions
351  */
expand_dirent_expression(FILE * f,ext2_filsys fs,char ch,struct problem_context * ctx)352 static _INLINE_ void expand_dirent_expression(FILE *f, ext2_filsys fs, char ch,
353 					      struct problem_context *ctx)
354 {
355 	struct ext2_dir_entry	*dirent;
356 	unsigned int rec_len, len;
357 
358 	if (!ctx || !ctx->dirent)
359 		goto no_dirent;
360 
361 	dirent = ctx->dirent;
362 
363 	switch (ch) {
364 	case 'i':
365 		fprintf(f, "%u", dirent->inode);
366 		break;
367 	case 'n':
368 		len = ext2fs_dirent_name_len(dirent);
369 		if ((ext2fs_get_rec_len(fs, dirent, &rec_len) == 0) &&
370 		    (len > rec_len))
371 			len = rec_len;
372 		safe_print(f, dirent->name, len);
373 		break;
374 	case 'r':
375 		(void) ext2fs_get_rec_len(fs, dirent, &rec_len);
376 		fprintf(f, "%u", rec_len);
377 		break;
378 	case 'l':
379 		fprintf(f, "%u", ext2fs_dirent_name_len(dirent));
380 		break;
381 	case 't':
382 		fprintf(f, "%u", ext2fs_dirent_file_type(dirent));
383 		break;
384 	default:
385 	no_dirent:
386 		fprintf(f, "%%D%c", ch);
387 		break;
388 	}
389 }
390 
expand_percent_expression(FILE * f,ext2_filsys fs,char ch,int width,int * first,struct problem_context * ctx)391 static _INLINE_ void expand_percent_expression(FILE *f, ext2_filsys fs,
392 					       char ch, int width, int *first,
393 					       struct problem_context *ctx)
394 {
395 	e2fsck_t e2fsck_ctx = fs ? (e2fsck_t) fs->priv_data : NULL;
396 	const char *m;
397 
398 	if (!ctx)
399 		goto no_context;
400 
401 	switch (ch) {
402 	case '%':
403 		fputc('%', f);
404 		break;
405 	case 'b':
406 		fprintf(f, "%*llu", width, (unsigned long long) ctx->blk);
407 		break;
408 	case 'B':
409 		if (ctx->blkcount == BLOCK_COUNT_IND)
410 			m = _("indirect block");
411 		else if (ctx->blkcount == BLOCK_COUNT_DIND)
412 			m = _("double indirect block");
413 		else if (ctx->blkcount == BLOCK_COUNT_TIND)
414 			m = _("triple indirect block");
415 		else if (ctx->blkcount == BLOCK_COUNT_TRANSLATOR)
416 			m = _("translator block");
417 		else
418 			m = _("block #");
419 		if (*first && islower(m[0]))
420 			fputc(toupper(*m++), f);
421 		fputs(m, f);
422 		if (ctx->blkcount >= 0)
423 			fprintf(f, "%lld", (long long) ctx->blkcount);
424 		break;
425 	case 'c':
426 		fprintf(f, "%*llu", width, (unsigned long long) ctx->blk2);
427 		break;
428 	case 'd':
429 		fprintf(f, "%*u", width, ctx->dir);
430 		break;
431 	case 'g':
432 		fprintf(f, "%*u", width, ctx->group);
433 		break;
434 	case 'i':
435 		fprintf(f, "%*u", width, ctx->ino);
436 		break;
437 	case 'j':
438 		fprintf(f, "%*u", width, ctx->ino2);
439 		break;
440 	case 'm':
441 		fprintf(f, "%*s", width, error_message(ctx->errcode));
442 		break;
443 	case 'N':
444 		fprintf(f, "%*llu", width, (long long)ctx->num);
445 		break;
446 	case 'n':
447 		fprintf(f, "%*llu", width, (long long)ctx->num2);
448 		break;
449 	case 'p':
450 		print_pathname(f, fs, ctx->ino, 0);
451 		break;
452 	case 'P':
453 		print_pathname(f, fs, ctx->ino2,
454 			       ctx->dirent ? ctx->dirent->inode : 0);
455 		break;
456 	case 'q':
457 		print_pathname(f, fs, ctx->dir, 0);
458 		break;
459 	case 'Q':
460 		print_pathname(f, fs, ctx->dir, ctx->ino);
461 		break;
462 	case 'r':
463 		fprintf(f, "%*lld", width, (long long) ctx->blkcount);
464 		break;
465 	case 'S':
466 		fprintf(f, "%llu",
467 			(unsigned long long) get_backup_sb(NULL, fs,
468 							   NULL, NULL));
469 		break;
470 	case 's':
471 		fprintf(f, "%*s", width, ctx->str ? ctx->str : "NULL");
472 		break;
473 	case 't':
474 		print_time(f, (time_t) ctx->num);
475 		break;
476 	case 'T':
477 		print_time(f, e2fsck_ctx ? e2fsck_ctx->now : time(0));
478 		break;
479 	case 'U':
480 		switch (ctx->num) {
481 		case USRQUOTA:
482 			m = _("user");
483 			break;
484 		case GRPQUOTA:
485 			m = _("group");
486 			break;
487 		case PRJQUOTA:
488 			m = _("project");
489 			break;
490 		default:
491 			m = _("unknown quota type");
492 			break;
493 		}
494 		if (*first && islower(m[0]))
495 			fputc(toupper(*m++), f);
496 		fputs(m, f);
497 		if (ctx->num > PRJQUOTA)
498 			fprintf(f, " %d", (int) ctx->num);
499 		break;
500 	case 'x':
501 		fprintf(f, "0x%0*x", width, ctx->csum1);
502 		break;
503 	case 'X':
504 		fprintf(f, "0x%0*llx", width, (long long)ctx->num);
505 		break;
506 	case 'y':
507 		fprintf(f, "0x%0*x", width, ctx->csum2);
508 		break;
509 	default:
510 	no_context:
511 		fprintf(f, "%%%c", ch);
512 		break;
513 	}
514 }
515 
print_e2fsck_message(FILE * f,e2fsck_t ctx,const char * msg,struct problem_context * pctx,int first,int recurse)516 void print_e2fsck_message(FILE *f, e2fsck_t ctx, const char *msg,
517 			  struct problem_context *pctx, int first,
518 			  int recurse)
519 {
520 	ext2_filsys fs = ctx->fs;
521 	const char *	cp;
522 	int		i, width;
523 
524 	e2fsck_clear_progbar(ctx);
525 	for (cp = msg; *cp; cp++) {
526 		if (cp[0] == '@') {
527 			cp++;
528 			expand_at_expression(f, ctx, *cp, pctx, &first,
529 					     recurse);
530 		} else if (cp[0] == '%') {
531 			cp++;
532 			width = 0;
533 			while (isdigit(cp[0])) {
534 				width = (width * 10) + cp[0] - '0';
535 				cp++;
536 			}
537 			if (cp[0] == 'I') {
538 				cp++;
539 				expand_inode_expression(f, fs, *cp, pctx);
540 			} else if (cp[0] == 'D') {
541 				cp++;
542 				expand_dirent_expression(f, fs, *cp, pctx);
543 			} else {
544 				expand_percent_expression(f, fs, *cp, width,
545 							  &first, pctx);
546 			}
547 		} else {
548 			for (i=0; cp[i]; i++)
549 				if ((cp[i] == '@') || cp[i] == '%')
550 					break;
551 			fprintf(f, "%.*s", i, cp);
552 			cp += i-1;
553 		}
554 		first = 0;
555 	}
556 }
557