blob: 5b20a164122779579688826bdd0d932d9c53cf21 [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
Theodore Ts'o695706c2009-10-04 18:02:24 -040017 * %B <blkcount> interpret blkcount as blkcount
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000018 * %c <blk2> block number
Andreas Dilgerfcdebce2001-08-04 01:47:01 -060019 * %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
Theodore Ts'o21c84b71997-04-29 16:15:03 +000025 * %g <group> integer
26 * %i <ino> inode number
27 * %Is <inode> -> i_size
Theodore Ts'ocebe48a2005-03-21 13:15:45 -050028 * %IS <inode> -> i_extra_isize
Theodore Ts'o21c84b71997-04-29 16:15:03 +000029 * %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
Theodore Ts'oecf1b772000-08-20 22:06:31 +000036 * %Iu <inode> -> i_uid
37 * %Ig <inode> -> i_gid
Theodore Ts'occfbb262007-03-31 19:53:53 -040038 * %It <inode type>
Theodore Ts'o21c84b71997-04-29 16:15:03 +000039 * %j <ino2> inode number
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000040 * %m <com_err error message>
Theodore Ts'o21c84b71997-04-29 16:15:03 +000041 * %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.
Theodore Ts'o695706c2009-10-04 18:02:24 -040049 * %r <blkcount> interpret blkcount as refcount
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000050 * %s <str> miscellaneous string
51 * %S backup superblock
Theodore Ts'o3b5386d2000-08-14 14:25:19 +000052 * %X <num> hexadecimal format
Theodore Ts'o21c84b71997-04-29 16:15:03 +000053 *
54 * The following '@' expansions are supported:
55 *
Theodore Ts'o342d8472001-07-02 11:54:09 -040056 * @a extended attribute
Theodore Ts'of8188ff1997-11-14 05:23:04 +000057 * @A error allocating
Theodore Ts'o21c84b71997-04-29 16:15:03 +000058 * @b block
59 * @B bitmap
Theodore Ts'o19178752000-02-11 15:55:07 +000060 * @c compress
Theodore Ts'o21c84b71997-04-29 16:15:03 +000061 * @C conflicts with some other fs block
Theodore Ts'o21c84b71997-04-29 16:15:03 +000062 * @D deleted
63 * @d directory
64 * @e entry
65 * @E Entry '%Dn' in %p (%i)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000066 * @f filesystem
Theodore Ts'o21c84b71997-04-29 16:15:03 +000067 * @F for @i %i (%Q) is
68 * @g group
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040069 * @h HTREE directory inode
Theodore Ts'od74edf42001-01-03 19:38:04 +000070 * @i inode
71 * @I illegal
72 * @j journal
Theodore Ts'o21c84b71997-04-29 16:15:03 +000073 * @l lost+found
74 * @L is a link
Theodore Ts'o7ae19832005-06-19 09:45:36 -040075 * @m multiply-claimed
76 * @n invalid
Theodore Ts'o80bfaa32000-08-18 15:08:37 +000077 * @o orphaned
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040078 * @p problem in
JP Abgralle0ed7402014-03-19 19:08:39 -070079 * @q quota
Theodore Ts'o21c84b71997-04-29 16:15:03 +000080 * @r root inode
Theodore Ts'oefc6f622008-08-27 23:07:54 -040081 * @s should be
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000082 * @S superblock
Theodore Ts'o80bfaa32000-08-18 15:08:37 +000083 * @u unattached
Theodore Ts'od74edf42001-01-03 19:38:04 +000084 * @v device
Theodore Ts'o15d482b2007-08-20 21:31:11 -040085 * @x extent
Theodore Ts'o21c84b71997-04-29 16:15:03 +000086 * @z zero-length
87 */
88
89#include <stdlib.h>
90#include <unistd.h>
91#include <string.h>
92#include <ctype.h>
93#include <termios.h>
94
95#include "e2fsck.h"
96
97#include "problem.h"
98
99#ifdef __GNUC__
100#define _INLINE_ __inline__
101#else
102#define _INLINE_
103#endif
104
105/*
106 * This structure defines the abbreviations used by the text strings
107 * below. The first character in the string is the index letter. An
108 * abbreviation of the form '@<i>' is expanded by looking up the index
109 * letter <i> in the table below.
110 */
111static const char *abbrevs[] = {
Theodore Ts'o342d8472001-07-02 11:54:09 -0400112 N_("aextended attribute"),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000113 N_("Aerror allocating"),
114 N_("bblock"),
115 N_("Bbitmap"),
Theodore Ts'o19178752000-02-11 15:55:07 +0000116 N_("ccompress"),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000117 N_("Cconflicts with some other fs @b"),
118 N_("iinode"),
119 N_("Iillegal"),
Theodore Ts'o1ce64532001-01-03 15:34:20 +0000120 N_("jjournal"),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000121 N_("Ddeleted"),
122 N_("ddirectory"),
123 N_("eentry"),
124 N_("E@e '%Dn' in %p (%i)"),
125 N_("ffilesystem"),
126 N_("Ffor @i %i (%Q) is"),
127 N_("ggroup"),
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400128 N_("hHTREE @d @i"),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000129 N_("llost+found"),
130 N_("Lis a link"),
Theodore Ts'o7ae19832005-06-19 09:45:36 -0400131 N_("mmultiply-claimed"),
132 N_("ninvalid"),
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000133 N_("oorphaned"),
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400134 N_("pproblem in"),
JP Abgralle0ed7402014-03-19 19:08:39 -0700135 N_("qquota"),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000136 N_("rroot @i"),
137 N_("sshould be"),
138 N_("Ssuper@b"),
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000139 N_("uunattached"),
Theodore Ts'od74edf42001-01-03 19:38:04 +0000140 N_("vdevice"),
Theodore Ts'o15d482b2007-08-20 21:31:11 -0400141 N_("xextent"),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000142 N_("zzero-length"),
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000143 "@@",
144 0
145 };
146
147/*
148 * Give more user friendly names to the "special" inodes.
149 */
Theodore Ts'o721edd02001-01-12 21:05:57 +0000150#define num_special_inodes 11
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000151static const char *special_inode_name[] =
152{
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000153 N_("<The NULL inode>"), /* 0 */
154 N_("<The bad blocks inode>"), /* 1 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000155 "/", /* 2 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700156 N_("<The user quota inode>"), /* 3 */
157 N_("<The group quota inode>"), /* 4 */
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000158 N_("<The boot loader inode>"), /* 5 */
Theodore Ts'o721edd02001-01-12 21:05:57 +0000159 N_("<The undelete directory inode>"), /* 6 */
160 N_("<The group descriptor inode>"), /* 7 */
161 N_("<The journal inode>"), /* 8 */
162 N_("<Reserved inode 9>"), /* 9 */
163 N_("<Reserved inode 10>"), /* 10 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000164};
165
166/*
Theodore Ts'o9e51eca1999-01-09 16:32:31 +0000167 * This function does "safe" printing. It will convert non-printable
168 * ASCII characters using '^' and M- notation.
169 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700170static void safe_print(FILE *f, const char *cp, int len)
Theodore Ts'o9e51eca1999-01-09 16:32:31 +0000171{
172 unsigned char ch;
173
174 if (len < 0)
175 len = strlen(cp);
176
177 while (len--) {
178 ch = *cp++;
179 if (ch > 128) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700180 fputs("M-", f);
Theodore Ts'o9e51eca1999-01-09 16:32:31 +0000181 ch -= 128;
182 }
Theodore Ts'oec8d2c31999-11-19 18:52:36 +0000183 if ((ch < 32) || (ch == 0x7f)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700184 fputc('^', f);
Theodore Ts'oec8d2c31999-11-19 18:52:36 +0000185 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
Theodore Ts'o9e51eca1999-01-09 16:32:31 +0000186 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700187 fputc(ch, f);
Theodore Ts'o9e51eca1999-01-09 16:32:31 +0000188 }
189}
190
191
192/*
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000193 * This function prints a pathname, using the ext2fs_get_pathname
194 * function
195 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700196static void print_pathname(FILE *f, ext2_filsys fs, ext2_ino_t dir,
197 ext2_ino_t ino)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000198{
JP Abgralle0ed7402014-03-19 19:08:39 -0700199 errcode_t retval = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000200 char *path;
201
202 if (!dir && (ino < num_special_inodes)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700203 fputs(_(special_inode_name[ino]), f);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000204 return;
205 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400206
JP Abgralle0ed7402014-03-19 19:08:39 -0700207 if (fs)
208 retval = ext2fs_get_pathname(fs, dir, ino, &path);
209 if (!fs || retval)
210 fputs("???", f);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000211 else {
JP Abgralle0ed7402014-03-19 19:08:39 -0700212 safe_print(f, path, -1);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400213 ext2fs_free_mem(&path);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000214 }
215}
216
JP Abgralle0ed7402014-03-19 19:08:39 -0700217static void print_time(FILE *f, time_t t)
Theodore Ts'ofe26a552009-08-08 10:14:48 -0400218{
219 const char * time_str;
220 static int do_gmt = -1;
221
222#ifdef __dietlibc__
223 /* The diet libc doesn't respect the TZ environemnt variable */
224 if (do_gmt == -1) {
225 time_str = getenv("TZ");
226 if (!time_str)
227 time_str = "";
228 do_gmt = !strcmp(time_str, "GMT0");
229 }
230#endif
231 time_str = asctime((do_gmt > 0) ? gmtime(&t) : localtime(&t));
JP Abgralle0ed7402014-03-19 19:08:39 -0700232 fprintf(f, "%.24s", time_str);
Theodore Ts'ofe26a552009-08-08 10:14:48 -0400233}
234
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000235/*
236 * This function handles the '@' expansion. We allow recursive
237 * expansion; an @ expression can contain further '@' and '%'
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400238 * expressions.
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000239 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700240static _INLINE_ void expand_at_expression(FILE *f, e2fsck_t ctx, char ch,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000241 struct problem_context *pctx,
Theodore Ts'o1a191d62007-07-02 19:04:31 -0400242 int *first, int recurse)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000243{
244 const char **cpp, *str;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400245
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000246 /* Search for the abbreviation */
247 for (cpp = abbrevs; *cpp; cpp++) {
248 if (ch == *cpp[0])
249 break;
250 }
Theodore Ts'o1a191d62007-07-02 19:04:31 -0400251 if (*cpp && recurse < 10) {
Theodore Ts'o6177c412003-04-27 17:34:14 -0400252 str = _(*cpp) + 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000253 if (*first && islower(*str)) {
254 *first = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700255 fputc(toupper(*str++), f);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000256 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700257 print_e2fsck_message(f, ctx, str, pctx, *first, recurse+1);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000258 } else
JP Abgralle0ed7402014-03-19 19:08:39 -0700259 fprintf(f, "@%c", ch);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000260}
261
262/*
Theodore Ts'oecf1b772000-08-20 22:06:31 +0000263 * This function expands '%IX' expressions
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000264 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700265static _INLINE_ void expand_inode_expression(FILE *f, ext2_filsys fs, char ch,
Theodore Ts'o8ff1a862004-11-30 19:57:20 -0500266 struct problem_context *ctx)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000267{
268 struct ext2_inode *inode;
Theodore Ts'ocebe48a2005-03-21 13:15:45 -0500269 struct ext2_inode_large *large_inode;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000270
271 if (!ctx || !ctx->inode)
272 goto no_inode;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400273
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000274 inode = ctx->inode;
Theodore Ts'ocebe48a2005-03-21 13:15:45 -0500275 large_inode = (struct ext2_inode_large *) inode;
276
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000277 switch (ch) {
278 case 's':
Theodore Ts'o246501c1998-03-24 16:22:38 +0000279 if (LINUX_S_ISDIR(inode->i_mode))
JP Abgralle0ed7402014-03-19 19:08:39 -0700280 fprintf(f, "%u", inode->i_size);
Theodore Ts'o246501c1998-03-24 16:22:38 +0000281 else {
282#ifdef EXT2_NO_64_TYPE
283 if (inode->i_size_high)
JP Abgralle0ed7402014-03-19 19:08:39 -0700284 fprintf(f, "0x%x%08x", inode->i_size_high,
285 inode->i_size);
Theodore Ts'o246501c1998-03-24 16:22:38 +0000286 else
JP Abgralle0ed7402014-03-19 19:08:39 -0700287 fprintf(f, "%u", inode->i_size);
Theodore Ts'o246501c1998-03-24 16:22:38 +0000288#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700289 fprintf(f, "%llu", EXT2_I_SIZE(inode));
Theodore Ts'o246501c1998-03-24 16:22:38 +0000290#endif
291 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000292 break;
Theodore Ts'ocebe48a2005-03-21 13:15:45 -0500293 case 'S':
JP Abgralle0ed7402014-03-19 19:08:39 -0700294 fprintf(f, "%u", large_inode->i_extra_isize);
Theodore Ts'ocebe48a2005-03-21 13:15:45 -0500295 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000296 case 'b':
Theodore Ts'o8a8f3652009-10-12 21:59:37 -0400297 if (fs->super->s_feature_ro_compat &
298 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
JP Abgralle0ed7402014-03-19 19:08:39 -0700299 fprintf(f, "%llu", inode->i_blocks +
300 (((long long) inode->osd2.linux2.l_i_blocks_hi)
301 << 32));
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400302 else
JP Abgralle0ed7402014-03-19 19:08:39 -0700303 fprintf(f, "%u", inode->i_blocks);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000304 break;
305 case 'l':
JP Abgralle0ed7402014-03-19 19:08:39 -0700306 fprintf(f, "%d", inode->i_links_count);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000307 break;
308 case 'm':
JP Abgralle0ed7402014-03-19 19:08:39 -0700309 fprintf(f, "0%o", inode->i_mode);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000310 break;
311 case 'M':
JP Abgralle0ed7402014-03-19 19:08:39 -0700312 print_time(f, inode->i_mtime);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000313 break;
314 case 'F':
JP Abgralle0ed7402014-03-19 19:08:39 -0700315 fprintf(f, "%u", inode->i_faddr);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000316 break;
317 case 'f':
JP Abgralle0ed7402014-03-19 19:08:39 -0700318 fprintf(f, "%llu", ext2fs_file_acl_block(fs, inode));
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000319 break;
320 case 'd':
JP Abgralle0ed7402014-03-19 19:08:39 -0700321 fprintf(f, "%u", (LINUX_S_ISDIR(inode->i_mode) ?
322 inode->i_dir_acl : 0));
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000323 break;
Theodore Ts'oecf1b772000-08-20 22:06:31 +0000324 case 'u':
JP Abgralle0ed7402014-03-19 19:08:39 -0700325 fprintf(f, "%d", inode_uid(*inode));
Theodore Ts'oecf1b772000-08-20 22:06:31 +0000326 break;
327 case 'g':
JP Abgralle0ed7402014-03-19 19:08:39 -0700328 fprintf(f, "%d", inode_gid(*inode));
Theodore Ts'oecf1b772000-08-20 22:06:31 +0000329 break;
Theodore Ts'occfbb262007-03-31 19:53:53 -0400330 case 't':
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400331 if (LINUX_S_ISREG(inode->i_mode))
JP Abgralle0ed7402014-03-19 19:08:39 -0700332 fputs(_("regular file"), f);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400333 else if (LINUX_S_ISDIR(inode->i_mode))
JP Abgralle0ed7402014-03-19 19:08:39 -0700334 fputs(_("directory"), f);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400335 else if (LINUX_S_ISCHR(inode->i_mode))
JP Abgralle0ed7402014-03-19 19:08:39 -0700336 fputs(_("character device"), f);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400337 else if (LINUX_S_ISBLK(inode->i_mode))
JP Abgralle0ed7402014-03-19 19:08:39 -0700338 fputs(_("block device"), f);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400339 else if (LINUX_S_ISFIFO(inode->i_mode))
JP Abgralle0ed7402014-03-19 19:08:39 -0700340 fputs(_("named pipe"), f);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400341 else if (LINUX_S_ISLNK(inode->i_mode))
JP Abgralle0ed7402014-03-19 19:08:39 -0700342 fputs(_("symbolic link"), f);
Theodore Ts'occfbb262007-03-31 19:53:53 -0400343 else if (LINUX_S_ISSOCK(inode->i_mode))
JP Abgralle0ed7402014-03-19 19:08:39 -0700344 fputs(_("socket"), f);
Theodore Ts'occfbb262007-03-31 19:53:53 -0400345 else
JP Abgralle0ed7402014-03-19 19:08:39 -0700346 fprintf(f, _("unknown file type with mode 0%o"),
347 inode->i_mode);
Theodore Ts'occfbb262007-03-31 19:53:53 -0400348 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000349 default:
350 no_inode:
JP Abgralle0ed7402014-03-19 19:08:39 -0700351 fprintf(f, "%%I%c", ch);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000352 break;
353 }
354}
355
356/*
357 * This function expands '%dX' expressions
358 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700359static _INLINE_ void expand_dirent_expression(FILE *f, ext2_filsys fs, char ch,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000360 struct problem_context *ctx)
361{
362 struct ext2_dir_entry *dirent;
JP Abgralle0ed7402014-03-19 19:08:39 -0700363 unsigned int rec_len, len;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400364
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000365 if (!ctx || !ctx->dirent)
366 goto no_dirent;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400367
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000368 dirent = ctx->dirent;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400369
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000370 switch (ch) {
371 case 'i':
JP Abgralle0ed7402014-03-19 19:08:39 -0700372 fprintf(f, "%u", dirent->inode);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000373 break;
374 case 'n':
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000375 len = dirent->name_len & 0xFF;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400376 if ((ext2fs_get_rec_len(fs, dirent, &rec_len) == 0) &&
377 (len > rec_len))
378 len = rec_len;
JP Abgralle0ed7402014-03-19 19:08:39 -0700379 safe_print(f, dirent->name, len);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000380 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000381 case 'r':
Theodore Ts'o8a480352009-06-21 21:07:38 -0400382 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
JP Abgralle0ed7402014-03-19 19:08:39 -0700383 fprintf(f, "%u", rec_len);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000384 break;
385 case 'l':
JP Abgralle0ed7402014-03-19 19:08:39 -0700386 fprintf(f, "%u", dirent->name_len & 0xFF);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000387 break;
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000388 case 't':
JP Abgralle0ed7402014-03-19 19:08:39 -0700389 fprintf(f, "%u", dirent->name_len >> 8);
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000390 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000391 default:
392 no_dirent:
JP Abgralle0ed7402014-03-19 19:08:39 -0700393 fprintf(f, "%%D%c", ch);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000394 break;
395 }
396}
397
JP Abgralle0ed7402014-03-19 19:08:39 -0700398static _INLINE_ void expand_percent_expression(FILE *f, ext2_filsys fs,
399 char ch, int width, int *first,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000400 struct problem_context *ctx)
401{
Theodore Ts'ofe26a552009-08-08 10:14:48 -0400402 e2fsck_t e2fsck_ctx = fs ? (e2fsck_t) fs->priv_data : NULL;
Theodore Ts'o695706c2009-10-04 18:02:24 -0400403 const char *m;
Theodore Ts'ofe26a552009-08-08 10:14:48 -0400404
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000405 if (!ctx)
406 goto no_context;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400407
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000408 switch (ch) {
409 case '%':
JP Abgralle0ed7402014-03-19 19:08:39 -0700410 fputc('%', f);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000411 break;
412 case 'b':
Theodore Ts'o15d482b2007-08-20 21:31:11 -0400413#ifdef EXT2_NO_64_TYPE
JP Abgralle0ed7402014-03-19 19:08:39 -0700414 fprintf(f, "%*u", width, (unsigned long) ctx->blk);
Theodore Ts'o15d482b2007-08-20 21:31:11 -0400415#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700416 fprintf(f, "%*llu", width, (unsigned long long) ctx->blk);
Theodore Ts'o15d482b2007-08-20 21:31:11 -0400417#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000418 break;
419 case 'B':
Theodore Ts'o695706c2009-10-04 18:02:24 -0400420 if (ctx->blkcount == BLOCK_COUNT_IND)
421 m = _("indirect block");
422 else if (ctx->blkcount == BLOCK_COUNT_DIND)
423 m = _("double indirect block");
424 else if (ctx->blkcount == BLOCK_COUNT_TIND)
425 m = _("triple indirect block");
426 else if (ctx->blkcount == BLOCK_COUNT_TRANSLATOR)
427 m = _("translator block");
428 else
429 m = _("block #");
430 if (*first && islower(m[0]))
JP Abgralle0ed7402014-03-19 19:08:39 -0700431 fputc(toupper(*m++), f);
432 fputs(m, f);
Theodore Ts'o695706c2009-10-04 18:02:24 -0400433 if (ctx->blkcount >= 0) {
Theodore Ts'o246501c1998-03-24 16:22:38 +0000434#ifdef EXT2_NO_64_TYPE
JP Abgralle0ed7402014-03-19 19:08:39 -0700435 fprintf(f, "%d", ctx->blkcount);
Theodore Ts'o246501c1998-03-24 16:22:38 +0000436#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700437 fprintf(f, "%lld", (long long) ctx->blkcount);
Theodore Ts'o246501c1998-03-24 16:22:38 +0000438#endif
Theodore Ts'o695706c2009-10-04 18:02:24 -0400439 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000440 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000441 case 'c':
Theodore Ts'o15d482b2007-08-20 21:31:11 -0400442#ifdef EXT2_NO_64_TYPE
JP Abgralle0ed7402014-03-19 19:08:39 -0700443 fprintf(f, "%*u", width, (unsigned long) ctx->blk2);
Theodore Ts'o15d482b2007-08-20 21:31:11 -0400444#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700445 fprintf(f, "%*llu", width, (unsigned long long) ctx->blk2);
Theodore Ts'o15d482b2007-08-20 21:31:11 -0400446#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000447 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000448 case 'd':
JP Abgralle0ed7402014-03-19 19:08:39 -0700449 fprintf(f, "%*u", width, ctx->dir);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000450 break;
451 case 'g':
JP Abgralle0ed7402014-03-19 19:08:39 -0700452 fprintf(f, "%*u", width, ctx->group);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000453 break;
454 case 'i':
JP Abgralle0ed7402014-03-19 19:08:39 -0700455 fprintf(f, "%*u", width, ctx->ino);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000456 break;
457 case 'j':
JP Abgralle0ed7402014-03-19 19:08:39 -0700458 fprintf(f, "%*u", width, ctx->ino2);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000459 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000460 case 'm':
JP Abgralle0ed7402014-03-19 19:08:39 -0700461 fprintf(f, "%*s", width, error_message(ctx->errcode));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000462 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000463 case 'N':
Theodore Ts'o246501c1998-03-24 16:22:38 +0000464#ifdef EXT2_NO_64_TYPE
JP Abgralle0ed7402014-03-19 19:08:39 -0700465 fprintf(f, "%*u", width, ctx->num);
Theodore Ts'o246501c1998-03-24 16:22:38 +0000466#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700467 fprintf(f, "%*llu", width, (long long)ctx->num);
Theodore Ts'o246501c1998-03-24 16:22:38 +0000468#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000469 break;
470 case 'p':
JP Abgralle0ed7402014-03-19 19:08:39 -0700471 print_pathname(f, fs, ctx->ino, 0);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000472 break;
473 case 'P':
JP Abgralle0ed7402014-03-19 19:08:39 -0700474 print_pathname(f, fs, ctx->ino2,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000475 ctx->dirent ? ctx->dirent->inode : 0);
476 break;
477 case 'q':
JP Abgralle0ed7402014-03-19 19:08:39 -0700478 print_pathname(f, fs, ctx->dir, 0);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000479 break;
480 case 'Q':
JP Abgralle0ed7402014-03-19 19:08:39 -0700481 print_pathname(f, fs, ctx->dir, ctx->ino);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000482 break;
Theodore Ts'o695706c2009-10-04 18:02:24 -0400483 case 'r':
484#ifdef EXT2_NO_64_TYPE
JP Abgralle0ed7402014-03-19 19:08:39 -0700485 fprintf(f, "%*d", width, ctx->blkcount);
Theodore Ts'o695706c2009-10-04 18:02:24 -0400486#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700487 fprintf(f, "%*lld", width, (long long) ctx->blkcount);
Theodore Ts'o695706c2009-10-04 18:02:24 -0400488#endif
489 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000490 case 'S':
JP Abgralle0ed7402014-03-19 19:08:39 -0700491 fprintf(f, "%llu", get_backup_sb(NULL, fs, NULL, NULL));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000492 break;
493 case 's':
JP Abgralle0ed7402014-03-19 19:08:39 -0700494 fprintf(f, "%*s", width, ctx->str ? ctx->str : "NULL");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000495 break;
Theodore Ts'ofe26a552009-08-08 10:14:48 -0400496 case 't':
JP Abgralle0ed7402014-03-19 19:08:39 -0700497 print_time(f, (time_t) ctx->num);
Theodore Ts'ofe26a552009-08-08 10:14:48 -0400498 break;
499 case 'T':
JP Abgralle0ed7402014-03-19 19:08:39 -0700500 print_time(f, e2fsck_ctx ? e2fsck_ctx->now : time(0));
501 break;
502 case 'x':
503 fprintf(f, "0x%0*x", width, ctx->csum1);
Theodore Ts'ob9a64a52012-03-15 19:29:19 -0400504 break;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000505 case 'X':
506#ifdef EXT2_NO_64_TYPE
JP Abgralle0ed7402014-03-19 19:08:39 -0700507 fprintf(f, "0x%0*x", width, ctx->num);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000508#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700509 fprintf(f, "0x%0*llx", width, (long long)ctx->num);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000510#endif
511 break;
JP Abgralle0ed7402014-03-19 19:08:39 -0700512 case 'y':
513 fprintf(f, "0x%0*x", width, ctx->csum2);
514 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000515 default:
516 no_context:
JP Abgralle0ed7402014-03-19 19:08:39 -0700517 fprintf(f, "%%%c", ch);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000518 break;
519 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400520}
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000521
JP Abgralle0ed7402014-03-19 19:08:39 -0700522void print_e2fsck_message(FILE *f, e2fsck_t ctx, const char *msg,
Theodore Ts'o1a191d62007-07-02 19:04:31 -0400523 struct problem_context *pctx, int first,
524 int recurse)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000525{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000526 ext2_filsys fs = ctx->fs;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000527 const char * cp;
JP Abgralle0ed7402014-03-19 19:08:39 -0700528 int i, width;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000529
530 e2fsck_clear_progbar(ctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000531 for (cp = msg; *cp; cp++) {
532 if (cp[0] == '@') {
533 cp++;
JP Abgralle0ed7402014-03-19 19:08:39 -0700534 expand_at_expression(f, ctx, *cp, pctx, &first,
535 recurse);
536 } else if (cp[0] == '%') {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000537 cp++;
JP Abgralle0ed7402014-03-19 19:08:39 -0700538 width = 0;
539 while (isdigit(cp[0])) {
540 width = (width * 10) + cp[0] - '0';
541 cp++;
542 }
543 if (cp[0] == 'I') {
544 cp++;
545 expand_inode_expression(f, fs, *cp, pctx);
546 } else if (cp[0] == 'D') {
547 cp++;
548 expand_dirent_expression(f, fs, *cp, pctx);
549 } else {
550 expand_percent_expression(f, fs, *cp, width,
551 &first, pctx);
552 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000553 } else {
554 for (i=0; cp[i]; i++)
555 if ((cp[i] == '@') || cp[i] == '%')
556 break;
JP Abgralle0ed7402014-03-19 19:08:39 -0700557 fprintf(f, "%.*s", i, cp);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000558 cp += i-1;
559 }
560 first = 0;
561 }
562}