Whamcloud - gitweb
Enhnaced the get backup superblock function so that it does
[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>              integer
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  *      %Ib     <inode> -> i_blocks
29  *      %Il     <inode> -> i_links_count
30  *      %Im     <inode> -> i_mode
31  *      %IM     <inode> -> i_mtime
32  *      %IF     <inode> -> i_faddr
33  *      %If     <inode> -> i_file_acl
34  *      %Id     <inode> -> i_dir_acl
35  *      %Iu     <inode> -> i_uid
36  *      %Ig     <inode> -> i_gid
37  *      %j      <ino2>                  inode number
38  *      %m      <com_err error message>
39  *      %N      <num>
40  *      %p      ext2fs_get_pathname of directory <ino>
41  *      %P      ext2fs_get_pathname of <dirent>->ino with <ino2> as
42  *                      the containing directory.  (If dirent is NULL
43  *                      then return the pathname of directory <ino2>)
44  *      %q      ext2fs_get_pathname of directory <dir>
45  *      %Q      ext2fs_get_pathname of directory <ino> with <dir> as
46  *                      the containing directory.
47  *      %s      <str>                   miscellaneous string
48  *      %S      backup superblock
49  *      %X      <num> hexadecimal format
50  *
51  * The following '@' expansions are supported:
52  *
53  *      @a      extended attribute
54  *      @A      error allocating
55  *      @b      block
56  *      @B      bitmap
57  *      @c      compress
58  *      @C      conflicts with some other fs block
59  *      @D      deleted
60  *      @d      directory
61  *      @e      entry
62  *      @E      Entry '%Dn' in %p (%i)
63  *      @f      filesystem
64  *      @F      for @i %i (%Q) is
65  *      @g      group
66  *      @i      inode
67  *      @I      illegal
68  *      @j      journal
69  *      @l      lost+found
70  *      @L      is a link
71  *      @o      orphaned
72  *      @r      root inode
73  *      @s      should be 
74  *      @S      superblock
75  *      @u      unattached
76  *      @v      device
77  *      @z      zero-length
78  */
79
80 #include <stdlib.h>
81 #include <unistd.h>
82 #include <string.h>
83 #include <ctype.h>
84 #include <termios.h>
85
86 #include "e2fsck.h"
87
88 #include "problem.h"
89
90 #ifdef __GNUC__
91 #define _INLINE_ __inline__
92 #else
93 #define _INLINE_
94 #endif
95
96 /*
97  * This structure defines the abbreviations used by the text strings
98  * below.  The first character in the string is the index letter.  An
99  * abbreviation of the form '@<i>' is expanded by looking up the index
100  * letter <i> in the table below.
101  */
102 static const char *abbrevs[] = {
103         N_("aextended attribute"),
104         N_("Aerror allocating"),
105         N_("bblock"),
106         N_("Bbitmap"),
107         N_("ccompress"),
108         N_("Cconflicts with some other fs @b"),
109         N_("iinode"),
110         N_("Iillegal"),
111         N_("jjournal"),
112         N_("Ddeleted"),
113         N_("ddirectory"),
114         N_("eentry"),
115         N_("E@e '%Dn' in %p (%i)"),
116         N_("ffilesystem"),
117         N_("Ffor @i %i (%Q) is"),
118         N_("ggroup"),
119         N_("llost+found"),
120         N_("Lis a link"),
121         N_("oorphaned"),
122         N_("rroot @i"),
123         N_("sshould be"),
124         N_("Ssuper@b"),
125         N_("uunattached"),
126         N_("vdevice"),
127         N_("zzero-length"),
128         "@@",
129         0
130         };
131
132 /*
133  * Give more user friendly names to the "special" inodes.
134  */
135 #define num_special_inodes      11
136 static const char *special_inode_name[] =
137 {
138         N_("<The NULL inode>"),                 /* 0 */
139         N_("<The bad blocks inode>"),           /* 1 */
140         "/",                                    /* 2 */
141         N_("<The ACL index inode>"),            /* 3 */
142         N_("<The ACL data inode>"),             /* 4 */
143         N_("<The boot loader inode>"),          /* 5 */
144         N_("<The undelete directory inode>"),   /* 6 */
145         N_("<The group descriptor inode>"),     /* 7 */
146         N_("<The journal inode>"),              /* 8 */
147         N_("<Reserved inode 9>"),               /* 9 */
148         N_("<Reserved inode 10>"),              /* 10 */
149 };
150
151 /*
152  * This function does "safe" printing.  It will convert non-printable
153  * ASCII characters using '^' and M- notation.
154  */
155 static void safe_print(const char *cp, int len)
156 {
157         unsigned char   ch;
158
159         if (len < 0)
160                 len = strlen(cp);
161
162         while (len--) {
163                 ch = *cp++;
164                 if (ch > 128) {
165                         fputs("M-", stdout);
166                         ch -= 128;
167                 }
168                 if ((ch < 32) || (ch == 0x7f)) {
169                         fputc('^', stdout);
170                         ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
171                 }
172                 fputc(ch, stdout);
173         }
174 }
175
176
177 /*
178  * This function prints a pathname, using the ext2fs_get_pathname
179  * function
180  */
181 static void print_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino)
182 {
183         errcode_t       retval;
184         char            *path;
185
186         if (!dir && (ino < num_special_inodes)) {
187                 fputs(_(special_inode_name[ino]), stdout);
188                 return;
189         }
190         
191         retval = ext2fs_get_pathname(fs, dir, ino, &path);
192         if (retval)
193                 fputs("???", stdout);
194         else {
195                 safe_print(path, -1);
196                 ext2fs_free_mem((void **) &path);
197         }
198 }
199
200 /*
201  * This function handles the '@' expansion.  We allow recursive
202  * expansion; an @ expression can contain further '@' and '%'
203  * expressions. 
204  */
205 static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
206                                           struct problem_context *pctx,
207                                           int *first)
208 {
209         const char **cpp, *str;
210         
211         /* Search for the abbreviation */
212         for (cpp = abbrevs; *cpp; cpp++) {
213                 if (ch == *cpp[0])
214                         break;
215         }
216         if (*cpp) {
217                 str = (*cpp) + 1;
218                 if (*first && islower(*str)) {
219                         *first = 0;
220                         fputc(toupper(*str++), stdout);
221                 }
222                 print_e2fsck_message(ctx, _(str), pctx, *first);
223         } else
224                 printf("@%c", ch);
225 }
226
227 /*
228  * This function expands '%IX' expressions
229  */
230 static _INLINE_ void expand_inode_expression(char ch,
231                                                struct problem_context *ctx)
232 {
233         struct ext2_inode       *inode;
234         char *                  time_str;
235         time_t                  t;
236
237         if (!ctx || !ctx->inode)
238                 goto no_inode;
239         
240         inode = ctx->inode;
241         
242         switch (ch) {
243         case 's':
244                 if (LINUX_S_ISDIR(inode->i_mode))
245                         printf("%u", inode->i_size);
246                 else {
247 #ifdef EXT2_NO_64_TYPE
248                         if (inode->i_size_high)
249                                 printf("0x%x%08x", inode->i_size_high,
250                                        inode->i_size);
251                         else
252                                 printf("%u", inode->i_size);
253 #else
254                         printf("%llu", (inode->i_size | 
255                                         ((__u64) inode->i_size_high << 32)));
256 #endif
257                 }
258                 break;
259         case 'b':
260                 printf("%u", inode->i_blocks);
261                 break;
262         case 'l':
263                 printf("%d", inode->i_links_count);
264                 break;
265         case 'm':
266                 printf("0%o", inode->i_mode);
267                 break;
268         case 'M':
269                 t = inode->i_mtime;
270                 time_str = ctime(&t);
271                 printf("%.24s", time_str);
272                 break;
273         case 'F':
274                 printf("%u", inode->i_faddr);
275                 break;
276         case 'f':
277                 printf("%u", inode->i_file_acl);
278                 break;
279         case 'd':
280                 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
281                               inode->i_dir_acl : 0));
282                 break;
283         case 'u':
284                 printf("%d", (inode->i_uid |
285                               (inode->osd2.linux2.l_i_uid_high << 16)));
286                 break;
287         case 'g':
288                 printf("%d", (inode->i_gid |
289                               (inode->osd2.linux2.l_i_gid_high << 16)));
290                 break;
291         default:
292         no_inode:
293                 printf("%%I%c", ch);
294                 break;
295         }
296 }
297
298 /*
299  * This function expands '%dX' expressions
300  */
301 static _INLINE_ void expand_dirent_expression(char ch,
302                                               struct problem_context *ctx)
303 {
304         struct ext2_dir_entry   *dirent;
305         int     len;
306         
307         if (!ctx || !ctx->dirent)
308                 goto no_dirent;
309         
310         dirent = ctx->dirent;
311         
312         switch (ch) {
313         case 'i':
314                 printf("%u", dirent->inode);
315                 break;
316         case 'n':
317                 len = dirent->name_len & 0xFF;
318                 if (len > EXT2_NAME_LEN)
319                         len = EXT2_NAME_LEN;
320                 if (len > dirent->rec_len)
321                         len = dirent->rec_len;
322                 safe_print(dirent->name, len);
323                 break;
324         case 'r':
325                 printf("%u", dirent->rec_len);
326                 break;
327         case 'l':
328                 printf("%u", dirent->name_len & 0xFF);
329                 break;
330         case 't':
331                 printf("%u", dirent->name_len >> 8);
332                 break;
333         default:
334         no_dirent:
335                 printf("%%D%c", ch);
336                 break;
337         }
338 }
339
340 static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
341                                                struct problem_context *ctx)
342 {
343         if (!ctx)
344                 goto no_context;
345         
346         switch (ch) {
347         case '%':
348                 fputc('%', stdout);
349                 break;
350         case 'b':
351                 printf("%u", ctx->blk);
352                 break;
353         case 'B':
354 #ifdef EXT2_NO_64_TYPE
355                 printf("%d", ctx->blkcount);
356 #else
357                 printf("%lld", ctx->blkcount);
358 #endif
359                 break;
360         case 'c':
361                 printf("%u", ctx->blk2);
362                 break;
363         case 'd':
364                 printf("%u", ctx->dir);
365                 break;
366         case 'g':
367                 printf("%d", ctx->group);
368                 break;
369         case 'i':
370                 printf("%u", ctx->ino);
371                 break;
372         case 'j':
373                 printf("%u", ctx->ino2);
374                 break;
375         case 'm':
376                 printf("%s", error_message(ctx->errcode));
377                 break;
378         case 'N':
379 #ifdef EXT2_NO_64_TYPE
380                 printf("%u", ctx->num);
381 #else
382                 printf("%llu", ctx->num);
383 #endif
384                 break;
385         case 'p':
386                 print_pathname(fs, ctx->ino, 0);
387                 break;
388         case 'P':
389                 print_pathname(fs, ctx->ino2,
390                                ctx->dirent ? ctx->dirent->inode : 0);
391                 break;
392         case 'q':
393                 print_pathname(fs, ctx->dir, 0);
394                 break;
395         case 'Q':
396                 print_pathname(fs, ctx->dir, ctx->ino);
397                 break;
398         case 'S':
399                 printf("%d", get_backup_sb(NULL, fs, NULL, NULL));
400                 break;
401         case 's':
402                 printf("%s", ctx->str ? ctx->str : "NULL");
403                 break;
404         case 'X':
405 #ifdef EXT2_NO_64_TYPE
406                 printf("0x%x", ctx->num);
407 #else
408                 printf("0x%llx", ctx->num);
409 #endif
410                 break;
411         default:
412         no_context:
413                 printf("%%%c", ch);
414                 break;
415         }
416 }       
417
418 void print_e2fsck_message(e2fsck_t ctx, const char *msg,
419                           struct problem_context *pctx, int first)
420 {
421         ext2_filsys fs = ctx->fs;
422         const char *    cp;
423         int             i;
424
425         e2fsck_clear_progbar(ctx);
426         for (cp = msg; *cp; cp++) {
427                 if (cp[0] == '@') {
428                         cp++;
429                         expand_at_expression(ctx, *cp, pctx, &first);
430                 } else if (cp[0] == '%' && cp[1] == 'I') {
431                         cp += 2;
432                         expand_inode_expression(*cp, pctx);
433                 } else if (cp[0] == '%' && cp[1] == 'D') {
434                         cp += 2;
435                         expand_dirent_expression(*cp, pctx);
436                 } else if ((cp[0] == '%')) {
437                         cp++;
438                         expand_percent_expression(fs, *cp, pctx);
439                 } else {
440                         for (i=0; cp[i]; i++)
441                                 if ((cp[i] == '@') || cp[i] == '%')
442                                         break;
443                         printf("%.*s", i, cp);
444                         cp += i-1;
445                 }
446                 first = 0;
447         }
448 }