2 * util.c --- miscellaneous utilities
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
20 #define read_a_char() getch()
26 #define read_a_char() getchar()
36 #include <sys/resource.h>
38 void fatal_error(e2fsck_t ctx, const char *msg)
41 fprintf (stderr, "e2fsck: %s\n", msg);
42 ctx->flags |= E2F_FLAG_ABORT;
43 if (ctx->flags & E2F_FLAG_SETJMP_OK)
44 longjmp(ctx->abort_loc, 1);
48 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
49 const char *description)
54 #ifdef DEBUG_ALLOCATE_MEMORY
55 printf("Allocating %d bytes for %s...\n", size, description);
59 sprintf(buf, "Can't allocate %s\n", description);
60 fatal_error(ctx, buf);
66 int ask_yn(const char * string, int def)
70 char *short_yes = _("yY");
71 char *short_no = _("nN");
74 struct termios termios, tmp;
76 tcgetattr (0, &termios);
78 tmp.c_lflag &= ~(ICANON | ECHO);
81 tcsetattr (0, TCSANOW, &tmp);
90 printf("%s%s? ", string, defstr);
93 if ((c = read_a_char()) == EOF)
95 if (strchr(short_yes, (char) c)) {
99 else if (strchr(short_no, (char) c)) {
103 else if ((c == ' ' || c == '\n') && (def != -1))
110 #ifdef HAVE_TERMIOS_H
111 tcsetattr (0, TCSANOW, &termios);
116 int ask (e2fsck_t ctx, const char * string, int def)
118 if (ctx->options & E2F_OPT_NO) {
119 printf (_("%s? no\n\n"), string);
122 if (ctx->options & E2F_OPT_YES) {
123 printf (_("%s? yes\n\n"), string);
126 if (ctx->options & E2F_OPT_PREEN) {
127 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
130 return ask_yn(string, def);
133 void e2fsck_read_bitmaps(e2fsck_t ctx)
135 ext2_filsys fs = ctx->fs;
138 if (ctx->invalid_bitmaps) {
139 com_err(ctx->program_name, 0,
140 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
145 ehandler_operation(_("reading inode and block bitmaps"));
146 retval = ext2fs_read_bitmaps(fs);
147 ehandler_operation(0);
149 com_err(ctx->program_name, retval,
150 _("while retrying to read bitmaps for %s"),
156 void e2fsck_write_bitmaps(e2fsck_t ctx)
158 ext2_filsys fs = ctx->fs;
161 if (ext2fs_test_bb_dirty(fs)) {
162 ehandler_operation(_("writing block bitmaps"));
163 retval = ext2fs_write_block_bitmap(fs);
164 ehandler_operation(0);
166 com_err(ctx->program_name, retval,
167 _("while retrying to write block bitmaps for %s"),
173 if (ext2fs_test_ib_dirty(fs)) {
174 ehandler_operation(_("writing inode bitmaps"));
175 retval = ext2fs_write_inode_bitmap(fs);
176 ehandler_operation(0);
178 com_err(ctx->program_name, retval,
179 _("while retrying to write inode bitmaps for %s"),
186 void preenhalt(e2fsck_t ctx)
188 ext2_filsys fs = ctx->fs;
190 if (!(ctx->options & E2F_OPT_PREEN))
192 fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
193 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
196 fs->super->s_state |= EXT2_ERROR_FS;
197 ext2fs_mark_super_dirty(fs);
200 exit(FSCK_UNCORRECTED);
203 #ifdef RESOURCE_TRACK
204 void init_resource_track(struct resource_track *track)
206 #ifdef HAVE_GETRUSAGE
210 track->brk_start = sbrk(0);
211 gettimeofday(&track->time_start, 0);
212 #ifdef HAVE_GETRUSAGE
214 memcpy(&r, 0, sizeof(struct rusage));
216 getrusage(RUSAGE_SELF, &r);
217 track->user_start = r.ru_utime;
218 track->system_start = r.ru_stime;
220 track->user_start.tv_sec = track->user_start.tv_usec = 0;
221 track->system_start.tv_sec = track->system_start.tv_usec = 0;
226 #define _INLINE_ __inline__
231 static _INLINE_ float timeval_subtract(struct timeval *tv1,
234 return ((tv1->tv_sec - tv2->tv_sec) +
235 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
238 void print_resource_track(const char *desc, struct resource_track *track)
240 #ifdef HAVE_GETRUSAGE
244 struct mallinfo malloc_info;
246 struct timeval time_end;
248 gettimeofday(&time_end, 0);
251 printf("%s: ", desc);
254 #define kbytes(x) (((x) + 1023) / 1024)
256 malloc_info = mallinfo();
257 printf(_("Memory used: %dk/%dk (%dk/%dk), "),
258 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
259 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
261 printf(_("Memory used: %d, "),
262 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
264 #ifdef HAVE_GETRUSAGE
265 getrusage(RUSAGE_SELF, &r);
267 printf(_("time: %5.2f/%5.2f/%5.2f\n"),
268 timeval_subtract(&time_end, &track->time_start),
269 timeval_subtract(&r.ru_utime, &track->user_start),
270 timeval_subtract(&r.ru_stime, &track->system_start));
272 printf(_("elapsed time: %6.3f\n"),
273 timeval_subtract(&time_end, &track->time_start));
276 #endif /* RESOURCE_TRACK */
278 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
279 struct ext2_inode * inode, const char *proc)
283 retval = ext2fs_read_inode(ctx->fs, ino, inode);
285 com_err("ext2fs_read_inode", retval,
286 _("while reading inode %ld in %s"), ino, proc);
291 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
292 struct ext2_inode * inode, const char *proc)
296 retval = ext2fs_write_inode(ctx->fs, ino, inode);
298 com_err("ext2fs_write_inode", retval,
299 _("while writing inode %ld in %s"), ino, proc);
305 void mtrace_print(char *mesg)
307 FILE *malloc_get_mallstream();
308 FILE *f = malloc_get_mallstream();
311 fprintf(f, "============= %s\n", mesg);
315 blk_t get_backup_sb(ext2_filsys fs)
317 if (!fs || !fs->super)
319 return fs->super->s_blocks_per_group + 1;
323 * Given a mode, return the ext2 file type
325 int ext2_file_type(unsigned int mode)
327 if (LINUX_S_ISREG(mode))
328 return EXT2_FT_REG_FILE;
330 if (LINUX_S_ISDIR(mode))
333 if (LINUX_S_ISCHR(mode))
334 return EXT2_FT_CHRDEV;
336 if (LINUX_S_ISBLK(mode))
337 return EXT2_FT_BLKDEV;
339 if (LINUX_S_ISLNK(mode))
340 return EXT2_FT_SYMLINK;
342 if (LINUX_S_ISFIFO(mode))
345 if (LINUX_S_ISSOCK(mode))