blob: c839bbf27c2f8f6c63298c0fcc423fed3792fc5a [file] [log] [blame]
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001/*
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
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000018 * %c <blk2> block number
Theodore Ts'o21c84b71997-04-29 16:15:03 +000019 * %di <dirent>->ino inode number
20 * %dn <dirent>->name string
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000021 * %dr <dirent>->rec_len
22 * %dl <dirent>->name_len
Theodore Ts'o21c84b71997-04-29 16:15:03 +000023 * %D <dir> inode number
24 * %g <group> integer
25 * %i <ino> inode number
26 * %Is <inode> -> i_size
27 * %Ib <inode> -> i_blocks
28 * %Il <inode> -> i_links_count
29 * %Im <inode> -> i_mode
30 * %IM <inode> -> i_mtime
31 * %IF <inode> -> i_faddr
32 * %If <inode> -> i_file_acl
33 * %Id <inode> -> i_dir_acl
34 * %j <ino2> inode number
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000035 * %m <com_err error message>
Theodore Ts'o21c84b71997-04-29 16:15:03 +000036 * %N <num>
37 * %p ext2fs_get_pathname of directory <ino>
38 * %P ext2fs_get_pathname of <dirent>->ino with <ino2> as
39 * the containing directory. (If dirent is NULL
40 * then return the pathname of directory <ino2>)
41 * %q ext2fs_get_pathname of directory <dir>
42 * %Q ext2fs_get_pathname of directory <ino> with <dir> as
43 * the containing directory.
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000044 * %s <str> miscellaneous string
45 * %S backup superblock
Theodore Ts'o21c84b71997-04-29 16:15:03 +000046 *
47 * The following '@' expansions are supported:
48 *
Theodore Ts'of8188ff1997-11-14 05:23:04 +000049 * @A error allocating
Theodore Ts'o21c84b71997-04-29 16:15:03 +000050 * @b block
51 * @B bitmap
52 * @C conflicts with some other fs block
53 * @i inode
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +000054 * @I illegal
Theodore Ts'o21c84b71997-04-29 16:15:03 +000055 * @D deleted
56 * @d directory
57 * @e entry
58 * @E Entry '%Dn' in %p (%i)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000059 * @f filesystem
Theodore Ts'o21c84b71997-04-29 16:15:03 +000060 * @F for @i %i (%Q) is
61 * @g group
62 * @l lost+found
63 * @L is a link
64 * @u unattached
65 * @r root inode
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000066 * @s should be
67 * @S superblock
Theodore Ts'o21c84b71997-04-29 16:15:03 +000068 * @z zero-length
69 */
70
71#include <stdlib.h>
72#include <unistd.h>
73#include <string.h>
74#include <ctype.h>
75#include <termios.h>
76
77#include "e2fsck.h"
78
79#include "problem.h"
80
81#ifdef __GNUC__
82#define _INLINE_ __inline__
83#else
84#define _INLINE_
85#endif
86
87/*
88 * This structure defines the abbreviations used by the text strings
89 * below. The first character in the string is the index letter. An
90 * abbreviation of the form '@<i>' is expanded by looking up the index
91 * letter <i> in the table below.
92 */
93static const char *abbrevs[] = {
Theodore Ts'of8188ff1997-11-14 05:23:04 +000094 "Aerror allocating",
Theodore Ts'o21c84b71997-04-29 16:15:03 +000095 "bblock",
96 "Bbitmap",
97 "Cconflicts with some other fs @b",
98 "iinode",
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +000099 "Iillegal",
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000100 "Ddeleted",
101 "ddirectory",
102 "eentry",
103 "E@e '%Dn' in %p (%i)",
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000104 "ffilesystem",
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000105 "Ffor @i %i (%Q) is",
106 "ggroup",
107 "llost+found",
108 "Lis a link",
109 "uunattached",
110 "rroot @i",
111 "sshould be",
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000112 "Ssuper@b",
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000113 "zzero-length",
114 "@@",
115 0
116 };
117
118/*
119 * Give more user friendly names to the "special" inodes.
120 */
121#define num_special_inodes 7
122static const char *special_inode_name[] =
123{
124 "<The NULL inode>", /* 0 */
125 "<The bad blocks inode>", /* 1 */
126 "/", /* 2 */
127 "<The ACL index inode>", /* 3 */
128 "<The ACL data inode>", /* 4 */
129 "<The boot loader inode>", /* 5 */
130 "<The undelete directory inode>" /* 6 */
131};
132
133/*
134 * This function prints a pathname, using the ext2fs_get_pathname
135 * function
136 */
137static void print_pathname(ext2_filsys fs, ino_t dir, ino_t ino)
138{
139 errcode_t retval;
140 char *path;
141
142 if (!dir && (ino < num_special_inodes)) {
143 fputs(special_inode_name[ino], stdout);
144 return;
145 }
146
147 retval = ext2fs_get_pathname(fs, dir, ino, &path);
148 if (retval)
149 fputs("???", stdout);
150 else {
151 fputs(path, stdout);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000152 ext2fs_free_mem((void **) &path);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000153 }
154}
155
156/*
157 * This function handles the '@' expansion. We allow recursive
158 * expansion; an @ expression can contain further '@' and '%'
159 * expressions.
160 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000161static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
162 struct problem_context *pctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000163 int *first)
164{
165 const char **cpp, *str;
166
167 /* Search for the abbreviation */
168 for (cpp = abbrevs; *cpp; cpp++) {
169 if (ch == *cpp[0])
170 break;
171 }
172 if (*cpp) {
173 str = (*cpp) + 1;
174 if (*first && islower(*str)) {
175 *first = 0;
176 fputc(toupper(*str++), stdout);
177 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000178 print_e2fsck_message(ctx, str, pctx, *first);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000179 } else
180 printf("@%c", ch);
181}
182
183/*
184 * This function expands '%kX' expressions
185 */
186static _INLINE_ void expand_inode_expression(char ch,
187 struct problem_context *ctx)
188{
189 struct ext2_inode *inode;
190 char * time_str;
191 time_t t;
192
193 if (!ctx || !ctx->inode)
194 goto no_inode;
195
196 inode = ctx->inode;
197
198 switch (ch) {
199 case 's':
Theodore Ts'o246501c1998-03-24 16:22:38 +0000200 if (LINUX_S_ISDIR(inode->i_mode))
201 printf("%u", inode->i_size);
202 else {
203#ifdef EXT2_NO_64_TYPE
204 if (inode->i_size_high)
205 printf("0x%x%08x", inode->i_size_high,
206 inode->i_size);
207 else
208 printf("%u", inode->i_size);
209#else
210 printf("%llu", (inode->i_size |
211 ((__u64) inode->i_size_high << 32)));
212#endif
213 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000214 break;
215 case 'b':
216 printf("%u", inode->i_blocks);
217 break;
218 case 'l':
219 printf("%d", inode->i_links_count);
220 break;
221 case 'm':
222 printf("0%o", inode->i_mode);
223 break;
224 case 'M':
225 t = inode->i_mtime;
226 time_str = ctime(&t);
227 printf("%.24s", time_str);
228 break;
229 case 'F':
230 printf("%u", inode->i_faddr);
231 break;
232 case 'f':
233 printf("%u", inode->i_file_acl);
234 break;
235 case 'd':
Theodore Ts'o246501c1998-03-24 16:22:38 +0000236 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
237 inode->i_dir_acl : 0));
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000238 break;
239 default:
240 no_inode:
241 printf("%%I%c", ch);
242 break;
243 }
244}
245
246/*
247 * This function expands '%dX' expressions
248 */
249static _INLINE_ void expand_dirent_expression(char ch,
250 struct problem_context *ctx)
251{
252 struct ext2_dir_entry *dirent;
253 int len;
254
255 if (!ctx || !ctx->dirent)
256 goto no_dirent;
257
258 dirent = ctx->dirent;
259
260 switch (ch) {
261 case 'i':
262 printf("%u", dirent->inode);
263 break;
264 case 'n':
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000265 len = dirent->name_len & 0xFF;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000266 if (len > EXT2_NAME_LEN)
267 len = EXT2_NAME_LEN;
268 if (len > dirent->rec_len)
269 len = dirent->rec_len;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000270 printf("%.*s", len, dirent->name);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000271 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000272 case 'r':
273 printf("%u", dirent->rec_len);
274 break;
275 case 'l':
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000276 printf("%u", dirent->name_len & 0xFF);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000277 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000278 default:
279 no_dirent:
280 printf("%%D%c", ch);
281 break;
282 }
283}
284
285static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
286 struct problem_context *ctx)
287{
288 if (!ctx)
289 goto no_context;
290
291 switch (ch) {
292 case '%':
293 fputc('%', stdout);
294 break;
295 case 'b':
296 printf("%u", ctx->blk);
297 break;
298 case 'B':
Theodore Ts'o246501c1998-03-24 16:22:38 +0000299#ifdef EXT2_NO_64_TYPE
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000300 printf("%d", ctx->blkcount);
Theodore Ts'o246501c1998-03-24 16:22:38 +0000301#else
302 printf("%lld", ctx->blkcount);
303#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000304 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000305 case 'c':
306 printf("%u", ctx->blk2);
307 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000308 case 'd':
309 printf("%lu", ctx->dir);
310 break;
311 case 'g':
312 printf("%d", ctx->group);
313 break;
314 case 'i':
315 printf("%lu", ctx->ino);
316 break;
317 case 'j':
318 printf("%lu", ctx->ino2);
319 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000320 case 'm':
321 printf("%s", error_message(ctx->errcode));
322 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000323 case 'N':
Theodore Ts'o246501c1998-03-24 16:22:38 +0000324#ifdef EXT2_NO_64_TYPE
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000325 printf("%u", ctx->num);
Theodore Ts'o246501c1998-03-24 16:22:38 +0000326#else
327 printf("%llu", ctx->num);
328#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000329 break;
330 case 'p':
331 print_pathname(fs, ctx->ino, 0);
332 break;
333 case 'P':
334 print_pathname(fs, ctx->ino2,
335 ctx->dirent ? ctx->dirent->inode : 0);
336 break;
337 case 'q':
338 print_pathname(fs, ctx->dir, 0);
339 break;
340 case 'Q':
341 print_pathname(fs, ctx->dir, ctx->ino);
342 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000343 case 'S':
344 printf("%d", get_backup_sb(fs));
345 break;
346 case 's':
347 printf("%s", ctx->str);
348 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000349 default:
350 no_context:
351 printf("%%%c", ch);
352 break;
353 }
354}
355
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000356void print_e2fsck_message(e2fsck_t ctx, const char *msg,
357 struct problem_context *pctx, int first)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000358{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000359 ext2_filsys fs = ctx->fs;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000360 const char * cp;
361 int i;
362
363 for (cp = msg; *cp; cp++) {
364 if (cp[0] == '@') {
365 cp++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000366 expand_at_expression(ctx, *cp, pctx, &first);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000367 } else if (cp[0] == '%' && cp[1] == 'I') {
368 cp += 2;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000369 expand_inode_expression(*cp, pctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000370 } else if (cp[0] == '%' && cp[1] == 'D') {
371 cp += 2;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000372 expand_dirent_expression(*cp, pctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000373 } else if ((cp[0] == '%')) {
374 cp++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000375 expand_percent_expression(fs, *cp, pctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000376 } else {
377 for (i=0; cp[i]; i++)
378 if ((cp[i] == '@') || cp[i] == '%')
379 break;
380 printf("%.*s", i, cp);
381 cp += i-1;
382 }
383 first = 0;
384 }
385}