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