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 malloc_info = mallinfo();
255 printf(_("Memory used: %d/%d, "),
256 malloc_info.arena, malloc_info.hblkhd);
258 printf(_("Memory used: %d, "),
259 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
261 #ifdef HAVE_GETRUSAGE
262 getrusage(RUSAGE_SELF, &r);
264 printf(_("elapsed time: %6.3f/%6.3f/%6.3f\n"),
265 timeval_subtract(&time_end, &track->time_start),
266 timeval_subtract(&r.ru_utime, &track->user_start),
267 timeval_subtract(&r.ru_stime, &track->system_start));
269 printf(_("elapsed time: %6.3f\n"),
270 timeval_subtract(&time_end, &track->time_start));
273 #endif /* RESOURCE_TRACK */
275 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
276 struct ext2_inode * inode, const char *proc)
280 retval = ext2fs_read_inode(ctx->fs, ino, inode);
282 com_err("ext2fs_read_inode", retval,
283 _("while reading inode %ld in %s"), ino, proc);
288 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
289 struct ext2_inode * inode, const char *proc)
293 retval = ext2fs_write_inode(ctx->fs, ino, inode);
295 com_err("ext2fs_write_inode", retval,
296 _("while writing inode %ld in %s"), ino, proc);
302 void mtrace_print(char *mesg)
304 FILE *malloc_get_mallstream();
305 FILE *f = malloc_get_mallstream();
308 fprintf(f, "============= %s\n", mesg);
312 blk_t get_backup_sb(ext2_filsys fs)
314 if (!fs || !fs->super)
316 return fs->super->s_blocks_per_group + 1;
320 * Given a mode, return the ext2 file type
322 int ext2_file_type(unsigned int mode)
324 if (LINUX_S_ISREG(mode))
325 return EXT2_FT_REG_FILE;
327 if (LINUX_S_ISDIR(mode))
330 if (LINUX_S_ISCHR(mode))
331 return EXT2_FT_CHRDEV;
333 if (LINUX_S_ISBLK(mode))
334 return EXT2_FT_BLKDEV;
336 if (LINUX_S_ISLNK(mode))
337 return EXT2_FT_SYMLINK;
339 if (LINUX_S_ISFIFO(mode))
342 if (LINUX_S_ISSOCK(mode))