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()
25 #else /* !HAVE_CONIO_H */
27 #define read_a_char getchar()
37 #include <sys/resource.h>
39 void fatal_error(e2fsck_t ctx, const char *msg)
42 fprintf (stderr, "e2fsck: %s\n", msg);
43 ctx->flags |= E2F_FLAG_ABORT;
44 if (ctx->flags & E2F_FLAG_SETJMP_OK)
45 longjmp(ctx->abort_loc, 1);
49 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
50 const char *description)
55 #ifdef DEBUG_ALLOCATE_MEMORY
56 printf("Allocating %d bytes for %s...\n", size, description);
60 sprintf(buf, "Can't allocate %s\n", description);
61 fatal_error(ctx, buf);
67 int ask_yn(const char * string, int def)
73 struct termios termios, tmp;
75 tcgetattr (0, &termios);
77 tmp.c_lflag &= ~(ICANON | ECHO);
80 tcsetattr (0, TCSANOW, &tmp);
89 printf("%s%s? ", string, defstr);
92 if ((c = read_a_char()) == EOF)
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 malloc_info = mallinfo();
255 printf("Memory used: %d/%d, ", malloc_info.arena, malloc_info.hblkhd);
257 printf("Memory used: %d, ",
258 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
260 #ifdef HAVE_GETRUSAGE
261 getrusage(RUSAGE_SELF, &r);
263 printf("elapsed time: %6.3f/%6.3f/%6.3f\n",
264 timeval_subtract(&time_end, &track->time_start),
265 timeval_subtract(&r.ru_utime, &track->user_start),
266 timeval_subtract(&r.ru_stime, &track->system_start));
268 printf("elapsed time: %6.3f\n",
269 timeval_subtract(&time_end, &track->time_start));
272 #endif /* RESOURCE_TRACK */
274 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
275 struct ext2_inode * inode, const char *proc)
279 retval = ext2fs_read_inode(ctx->fs, ino, inode);
281 com_err("ext2fs_read_inode", retval,
282 "while reading inode %ld in %s", ino, proc);
287 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
288 struct ext2_inode * inode, const char *proc)
292 retval = ext2fs_write_inode(ctx->fs, ino, inode);
294 com_err("ext2fs_write_inode", retval,
295 "while writing inode %ld in %s", ino, proc);
301 void mtrace_print(char *mesg)
303 FILE *malloc_get_mallstream();
304 FILE *f = malloc_get_mallstream();
307 fprintf(f, "============= %s\n", mesg);
311 blk_t get_backup_sb(ext2_filsys fs)
313 if (!fs || !fs->super)
315 return fs->super->s_blocks_per_group + 1;