Whamcloud - gitweb
1ab0271757763d5164e0f823dde34dbfa642941c
[tools/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_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 #include "ext2fs/lfsck.h"
103
104 #ifdef __GNUC__
105 #define _INLINE_ __inline__
106 #else
107 #define _INLINE_
108 #endif
109
110 /*
111  * This structure defines the abbreviations used by the text strings
112  * below.  The first character in the string is the index letter.  An
113  * abbreviation of the form '@<i>' is expanded by looking up the index
114  * letter <i> in the table below.
115  */
116 static const char *abbrevs[] = {
117         N_("aextended attribute"),
118         N_("Aerror allocating"),
119         N_("bblock"),
120         N_("Bbitmap"),
121         N_("ccompress"),
122         N_("Cconflicts with some other fs @b"),
123         N_("ddirectory"),
124         N_("Ddeleted"),
125         N_("eentry"),
126         N_("E@e '%Dn' in %p (%i)"),
127         N_("ffilesystem"),
128         N_("Ffor @i %i (%Q) is"),
129         N_("ggroup"),
130         N_("hHTREE @d @i"),
131         N_("iinode"),
132         N_("Iillegal"),
133         N_("jjournal"),
134         N_("llost+found"),
135         N_("Lis a link"),
136         N_("mmultiply-claimed"),
137         N_("ninvalid"),
138         N_("oorphaned"),
139         N_("pproblem in"),
140         N_("qquota"),
141         N_("rroot @i"),
142         N_("sshould be"),
143         N_("Ssuper@b"),
144         N_("uunattached"),
145         N_("vdevice"),
146         N_("xextent"),
147         N_("zzero-length"),
148         "@@",
149         0
150         };
151
152 /*
153  * Give more user friendly names to the "special" inodes.
154  */
155 #define num_special_inodes      11
156 static const char *special_inode_name[] =
157 {
158         N_("<The NULL inode>"),                 /* 0 */
159         N_("<The bad blocks inode>"),           /* 1 */
160         "/",                                    /* 2 */
161         N_("<The user quota inode>"),           /* 3 */
162         N_("<The group quota inode>"),          /* 4 */
163         N_("<The boot loader inode>"),          /* 5 */
164         N_("<The undelete directory inode>"),   /* 6 */
165         N_("<The group descriptor inode>"),     /* 7 */
166         N_("<The journal inode>"),              /* 8 */
167         N_("<Reserved inode 9>"),               /* 9 */
168         N_("<Reserved inode 10>"),              /* 10 */
169 };
170
171 /*
172  * This function does "safe" printing.  It will convert non-printable
173  * ASCII characters using '^' and M- notation.
174  */
175 static void safe_print(FILE *f, const char *cp, int len)
176 {
177         unsigned char   ch;
178
179         if (len < 0)
180                 len = strlen(cp);
181
182         while (len--) {
183                 ch = *cp++;
184                 if (ch > 128) {
185                         fputs("M-", f);
186                         ch -= 128;
187                 }
188                 if ((ch < 32) || (ch == 0x7f)) {
189                         fputc('^', f);
190                         ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
191                 }
192                 fputc(ch, f);
193         }
194 }
195
196
197 /*
198  * This function prints a pathname, using the ext2fs_get_pathname
199  * function
200  */
201 static void print_pathname(FILE *f, ext2_filsys fs, ext2_ino_t dir,
202                            ext2_ino_t ino)
203 {
204         errcode_t       retval = 0;
205         char            *path;
206
207         if (!dir && (ino < num_special_inodes)) {
208                 fputs(_(special_inode_name[ino]), f);
209                 return;
210         }
211
212         if (fs)
213                 retval = ext2fs_get_pathname(fs, dir, ino, &path);
214         if (!fs || retval)
215                 fputs("???", f);
216         else {
217                 safe_print(f, path, -1);
218                 ext2fs_free_mem(&path);
219         }
220 }
221
222 static void print_time(FILE *f, time_t t)
223 {
224         const char *            time_str;
225         static int              do_gmt = -1;
226
227 #ifdef __dietlibc__
228                 /* The diet libc doesn't respect the TZ environment variable */
229                 if (do_gmt == -1) {
230                         time_str = getenv("TZ");
231                         if (!time_str)
232                                 time_str = "";
233                         do_gmt = !strcmp(time_str, "GMT") ||
234                                 !strcmp(time_str, "GMT0");
235                 }
236 #endif
237                 time_str = asctime((do_gmt > 0) ? gmtime(&t) : localtime(&t));
238                 fprintf(f, "%.24s", time_str);
239 }
240
241 /*
242  * This function handles the '@' expansion.  We allow recursive
243  * expansion; an @ expression can contain further '@' and '%'
244  * expressions.
245  */
246 static _INLINE_ void expand_at_expression(FILE *f, e2fsck_t ctx, char ch,
247                                           struct problem_context *pctx,
248                                           int *first, int recurse)
249 {
250         const char **cpp, *str;
251
252         /* Search for the abbreviation */
253         for (cpp = abbrevs; *cpp; cpp++) {
254                 if (ch == *cpp[0])
255                         break;
256         }
257         if (*cpp && recurse < 10) {
258                 str = _(*cpp) + 1;
259                 if (*first && islower(*str)) {
260                         *first = 0;
261                         fputc(toupper(*str++), f);
262                 }
263                 print_e2fsck_message(f, ctx, str, pctx, *first, recurse+1);
264         } else
265                 fprintf(f, "@%c", ch);
266 }
267
268 /*
269  * This function expands '%IX' expressions
270  */
271 static _INLINE_ void expand_inode_expression(FILE *f, ext2_filsys fs, char ch,
272                                              struct problem_context *ctx)
273 {
274         struct ext2_inode       *inode;
275         struct ext2_inode_large *large_inode;
276
277         if (!ctx || !ctx->inode)
278                 goto no_inode;
279
280         inode = ctx->inode;
281         large_inode = (struct ext2_inode_large *) inode;
282
283         switch (ch) {
284         case 's':
285                 fprintf(f, "%llu", EXT2_I_SIZE(inode));
286                 break;
287         case 'S':
288                 fprintf(f, "%u", large_inode->i_extra_isize);
289                 break;
290         case 'b':
291                 if (ext2fs_has_feature_huge_file(fs->super))
292                         fprintf(f, "%llu", inode->i_blocks +
293                                 (((long long) inode->osd2.linux2.l_i_blocks_hi)
294                                  << 32));
295                 else
296                         fprintf(f, "%u", inode->i_blocks);
297                 break;
298         case 'l':
299                 fprintf(f, "%d", inode->i_links_count);
300                 break;
301         case 'm':
302                 fprintf(f, "0%o", inode->i_mode);
303                 break;
304         case 'M':
305                 print_time(f, inode->i_mtime);
306                 break;
307         case 'F':
308                 fprintf(f, "%u", inode->i_faddr);
309                 break;
310         case 'f':
311                 fprintf(f, "%llu", 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 const struct lu_fid *get_dirent_fid(struct ext2_dir_entry *dirent)
350 {
351         unsigned char *data = (unsigned char *)dirent->name +
352                               (dirent->name_len & EXT2_NAME_LEN) + 1;
353         __u8 file_type = dirent->name_len >> 8;
354         struct lu_fid *fid = NULL;
355         __u8 dirdata_mask;
356
357         for (dirdata_mask = EXT2_FT_MASK + 1;
358              dirdata_mask != 0; dirdata_mask <<= 1) {
359                 int dlen;
360
361                 if ((dirdata_mask & file_type) == 0)
362                         continue;
363
364                 dlen = data[0];
365                 if (dirdata_mask == EXT2_DIRENT_LUFID) {
366                         fid = (struct lu_fid *)(data + 1);
367                         fid_be_to_cpu(fid, fid);
368                         break;
369                 }
370                 data += dlen;
371         }
372
373         return fid;
374 }
375
376 /*
377  * This function expands '%dX' expressions
378  */
379 static _INLINE_ void expand_dirent_expression(FILE *f, ext2_filsys fs, char ch,
380                                               struct problem_context *ctx)
381 {
382         struct ext2_dir_entry   *dirent;
383         unsigned int rec_len, len;
384
385         if (!ctx || !ctx->dirent)
386                 goto no_dirent;
387
388         dirent = ctx->dirent;
389
390         switch (ch) {
391         case 'i': {
392                 const struct lu_fid *fid;
393
394                 fprintf(f, "%u", dirent->inode);
395
396                 fid = get_dirent_fid(dirent);
397                 if (fid != NULL)
398                         fprintf(f, " fid="DFID, PFID(fid));
399
400                 break;
401         }
402         case 'n':
403                 len = ext2fs_dirent_name_len(dirent);
404                 if ((ext2fs_get_rec_len(fs, dirent, &rec_len) == 0) &&
405                     (len > rec_len))
406                         len = rec_len;
407                 safe_print(f, dirent->name, len);
408                 break;
409         case 'r':
410                 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
411                 fprintf(f, "%u", rec_len);
412                 break;
413         case 'l':
414                 fprintf(f, "%u", ext2fs_dirent_name_len(dirent));
415                 break;
416         case 't':
417                 fprintf(f, "%u", ext2fs_dirent_file_type(dirent));
418                 break;
419         default:
420         no_dirent:
421                 fprintf(f, "%%D%c", ch);
422                 break;
423         }
424 }
425
426 static _INLINE_ void expand_percent_expression(FILE *f, ext2_filsys fs,
427                                                char ch, int width, int *first,
428                                                struct problem_context *ctx)
429 {
430         e2fsck_t e2fsck_ctx = fs ? (e2fsck_t) fs->priv_data : NULL;
431         const char *m;
432
433         if (!ctx)
434                 goto no_context;
435
436         switch (ch) {
437         case '%':
438                 fputc('%', f);
439                 break;
440         case 'b':
441                 fprintf(f, "%*llu", width, (unsigned long long) ctx->blk);
442                 break;
443         case 'B':
444                 if (ctx->blkcount == BLOCK_COUNT_IND)
445                         m = _("indirect block");
446                 else if (ctx->blkcount == BLOCK_COUNT_DIND)
447                         m = _("double indirect block");
448                 else if (ctx->blkcount == BLOCK_COUNT_TIND)
449                         m = _("triple indirect block");
450                 else if (ctx->blkcount == BLOCK_COUNT_TRANSLATOR)
451                         m = _("translator block");
452                 else
453                         m = _("block #");
454                 if (*first && islower(m[0]))
455                         fputc(toupper(*m++), f);
456                 fputs(m, f);
457                 if (ctx->blkcount >= 0)
458                         fprintf(f, "%lld", (long long) ctx->blkcount);
459                 break;
460         case 'c':
461                 fprintf(f, "%*llu", width, (unsigned long long) ctx->blk2);
462                 break;
463         case 'd':
464                 fprintf(f, "%*u", width, ctx->dir);
465                 break;
466         case 'g':
467                 fprintf(f, "%*u", width, ctx->group);
468                 break;
469         case 'i':
470                 fprintf(f, "%*u", width, ctx->ino);
471                 break;
472         case 'j':
473                 fprintf(f, "%*u", width, ctx->ino2);
474                 break;
475         case 'm':
476                 fprintf(f, "%*s", width, error_message(ctx->errcode));
477                 break;
478         case 'N':
479                 fprintf(f, "%*llu", width, (long long)ctx->num);
480                 break;
481         case 'n':
482                 fprintf(f, "%*llu", width, (long long)ctx->num2);
483                 break;
484         case 'p':
485                 print_pathname(f, fs, ctx->ino, 0);
486                 break;
487         case 'P':
488                 print_pathname(f, fs, ctx->ino2,
489                                ctx->dirent ? ctx->dirent->inode : 0);
490                 break;
491         case 'q':
492                 print_pathname(f, fs, ctx->dir, 0);
493                 break;
494         case 'Q':
495                 print_pathname(f, fs, ctx->dir, ctx->ino);
496                 break;
497         case 'r':
498                 fprintf(f, "%*lld", width, (long long) ctx->blkcount);
499                 break;
500         case 'S':
501                 fprintf(f, "%llu", get_backup_sb(NULL, fs, NULL, NULL));
502                 break;
503         case 's':
504                 fprintf(f, "%*s", width, ctx->str ? ctx->str : "NULL");
505                 break;
506         case 't':
507                 print_time(f, (time_t) ctx->num);
508                 break;
509         case 'T':
510                 print_time(f, e2fsck_ctx ? e2fsck_ctx->now : time(0));
511                 break;
512         case 'U':
513                 switch (ctx->num) {
514                 case USRQUOTA:
515                         m = _("user");
516                         break;
517                 case GRPQUOTA:
518                         m = _("group");
519                         break;
520                 case PRJQUOTA:
521                         m = _("project");
522                         break;
523                 default:
524                         m = _("unknown quota type");
525                         break;
526                 }
527                 if (*first && islower(m[0]))
528                         fputc(toupper(*m++), f);
529                 fputs(m, f);
530                 if (ctx->num > PRJQUOTA)
531                         fprintf(f, " %d", (int) ctx->num);
532                 break;
533         case 'x':
534                 fprintf(f, "0x%0*x", width, ctx->csum1);
535                 break;
536         case 'X':
537                 fprintf(f, "0x%0*llx", width, (long long)ctx->num);
538                 break;
539         case 'y':
540                 fprintf(f, "0x%0*x", width, ctx->csum2);
541                 break;
542         default:
543         no_context:
544                 fprintf(f, "%%%c", ch);
545                 break;
546         }
547 }
548
549 void print_e2fsck_message(FILE *f, e2fsck_t ctx, const char *msg,
550                           struct problem_context *pctx, int first,
551                           int recurse)
552 {
553         ext2_filsys fs = ctx->fs;
554         const char *    cp;
555         int             i, width;
556
557         e2fsck_clear_progbar(ctx);
558         for (cp = msg; *cp; cp++) {
559                 if (cp[0] == '@') {
560                         cp++;
561                         expand_at_expression(f, ctx, *cp, pctx, &first,
562                                              recurse);
563                 } else if (cp[0] == '%') {
564                         cp++;
565                         width = 0;
566                         while (isdigit(cp[0])) {
567                                 width = (width * 10) + cp[0] - '0';
568                                 cp++;
569                         }
570                         if (cp[0] == 'I') {
571                                 cp++;
572                                 expand_inode_expression(f, fs, *cp, pctx);
573                         } else if (cp[0] == 'D') {
574                                 cp++;
575                                 expand_dirent_expression(f, fs, *cp, pctx);
576                         } else {
577                                 expand_percent_expression(f, fs, *cp, width,
578                                                           &first, pctx);
579                         }
580                 } else {
581                         for (i=0; cp[i]; i++)
582                                 if ((cp[i] == '@') || cp[i] == '%')
583                                         break;
584                         fprintf(f, "%.*s", i, cp);
585                         cp += i-1;
586                 }
587                 first = 0;
588         }
589 }