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
21 #include <sys/resource.h>
23 void fatal_error (const char *msg)
26 fprintf (stderr, "e2fsck: %s\n", msg);
30 void *allocate_memory(int size, const char *description)
35 #ifdef DEBUG_ALLOCATE_MEMORY
36 printf("Allocating %d bytes for %s...\n", size, description);
40 sprintf(buf, "Can't allocate %s\n", description);
47 int ask_yn(const char * string, int def)
50 struct termios termios, tmp;
53 tcgetattr (0, &termios);
55 tmp.c_lflag &= ~(ICANON | ECHO);
58 tcsetattr (0, TCSANOW, &tmp);
66 printf("%s%s? ", string, defstr);
69 if ((c = getchar()) == EOF)
80 else if ((c == ' ' || c == '\n') && (def != -1))
87 tcsetattr (0, TCSANOW, &termios);
91 int ask (e2fsck_t ctx, const char * string, int def)
93 if (ctx->options & E2F_OPT_NO) {
94 printf ("%s? no\n\n", string);
97 if (ctx->options & E2F_OPT_YES) {
98 printf ("%s? yes\n\n", string);
101 if (ctx->options & E2F_OPT_PREEN) {
102 printf ("%s? %s\n\n", string, def ? "yes" : "no");
105 return ask_yn(string, def);
108 void read_bitmaps(e2fsck_t ctx)
110 ext2_filsys fs = ctx->fs;
113 if (ctx->invalid_bitmaps) {
114 com_err(ctx->program_name, 0,
115 "read_bitmaps: illegal bitmap block(s) for %s",
120 ehandler_operation("reading inode and block bitmaps");
121 retval = ext2fs_read_bitmaps(fs);
122 ehandler_operation(0);
124 com_err(ctx->program_name, retval,
125 "while retrying to read bitmaps for %s",
131 void write_bitmaps(e2fsck_t ctx)
133 ext2_filsys fs = ctx->fs;
136 if (ext2fs_test_bb_dirty(fs)) {
137 ehandler_operation("writing block bitmaps");
138 retval = ext2fs_write_block_bitmap(fs);
139 ehandler_operation(0);
141 com_err(ctx->program_name, retval,
142 "while retrying to write block bitmaps for %s",
148 if (ext2fs_test_ib_dirty(fs)) {
149 ehandler_operation("writing inode bitmaps");
150 retval = ext2fs_write_inode_bitmap(fs);
151 ehandler_operation(0);
153 com_err(ctx->program_name, retval,
154 "while retrying to write inode bitmaps for %s",
161 void preenhalt(e2fsck_t ctx)
163 ext2_filsys fs = ctx->fs;
165 if (!(ctx->options & E2F_OPT_PREEN))
167 fprintf(stderr, "\n\n%s: UNEXPECTED INCONSISTENCY; "
168 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n",
171 fs->super->s_state |= EXT2_ERROR_FS;
172 ext2fs_mark_super_dirty(fs);
175 exit(FSCK_UNCORRECTED);
178 void init_resource_track(struct resource_track *track)
180 #ifdef HAVE_GETRUSAGE
184 track->brk_start = sbrk(0);
185 gettimeofday(&track->time_start, 0);
186 #ifdef HAVE_GETRUSAGE
188 memcpy(&r, 0, sizeof(struct rusage));
190 getrusage(RUSAGE_SELF, &r);
191 track->user_start = r.ru_utime;
192 track->system_start = r.ru_stime;
194 track->user_start.tv_sec = track->user_start.tv_usec = 0;
195 track->system_start.tv_sec = track->system_start.tv_usec = 0;
200 #define _INLINE_ __inline__
205 static _INLINE_ float timeval_subtract(struct timeval *tv1,
208 return ((tv1->tv_sec - tv2->tv_sec) +
209 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
212 void print_resource_track(const char *desc, struct resource_track *track)
214 #ifdef HAVE_GETRUSAGE
217 struct timeval time_end;
219 gettimeofday(&time_end, 0);
222 printf("%s :", desc);
224 #ifdef HAVE_GETRUSAGE
225 getrusage(RUSAGE_SELF, &r);
227 printf("Memory used: %d, elapsed time: %6.3f/%6.3f/%6.3f\n",
228 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
229 timeval_subtract(&time_end, &track->time_start),
230 timeval_subtract(&r.ru_utime, &track->user_start),
231 timeval_subtract(&r.ru_stime, &track->system_start));
233 printf("Memory used: %d, elapsed time: %6.3f\n",
234 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
235 timeval_subtract(&time_end, &track->time_start));
239 void e2fsck_read_inode(ext2_filsys fs, unsigned long ino,
240 struct ext2_inode * inode, const char *proc)
244 retval = ext2fs_read_inode(fs, ino, inode);
246 com_err("ext2fs_read_inode", retval,
247 "while reading inode %ld in %s", ino, proc);
252 extern void e2fsck_write_inode(ext2_filsys fs, unsigned long ino,
253 struct ext2_inode * inode, const char *proc)
257 retval = ext2fs_write_inode(fs, ino, inode);
259 com_err("ext2fs_write_inode", retval,
260 "while writing inode %ld in %s", ino, proc);
266 void mtrace_print(char *mesg)
268 FILE *malloc_get_mallstream();
269 FILE *f = malloc_get_mallstream();
272 fprintf(f, "============= %s\n", mesg);
276 blk_t get_backup_sb(ext2_filsys fs)
278 if (!fs || !fs->super)
280 return fs->super->s_blocks_per_group + 1;