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