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