e2fsprogs/e2fsck/util.c

352 lines
7.3 KiB
C
Raw Normal View History

1997-04-26 21:21:57 +08:00
/*
* util.c --- miscellaneous utilities
*
1997-04-30 00:15:03 +08:00
* Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
1997-04-26 21:21:57 +08:00
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_CONIO_H
#undef HAVE_TERMIOS_H
#include <conio.h>
#define read_a_char() getch()
#else
#ifdef HAVE_TERMIOS_H
1997-04-26 21:21:57 +08:00
#include <termios.h>
#endif
#include <stdio.h>
#define read_a_char() getchar()
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
1997-04-26 21:21:57 +08:00
#include "e2fsck.h"
1997-04-26 21:58:21 +08:00
#include <sys/time.h>
#include <sys/resource.h>
void fatal_error(e2fsck_t ctx, const char *msg)
1997-04-26 21:21:57 +08:00
{
if (msg)
fprintf (stderr, "e2fsck: %s\n", msg);
if (ctx->fs && ctx->fs->io)
io_channel_flush(ctx->fs->io);
ctx->flags |= E2F_FLAG_ABORT;
if (ctx->flags & E2F_FLAG_SETJMP_OK)
longjmp(ctx->abort_loc, 1);
1997-04-26 21:21:57 +08:00
exit(FSCK_ERROR);
}
void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
const char *description)
1997-04-26 21:21:57 +08:00
{
void *ret;
char buf[256];
#ifdef DEBUG_ALLOCATE_MEMORY
printf("Allocating %d bytes for %s...\n", size, description);
#endif
ret = malloc(size);
if (!ret) {
1997-04-26 22:48:50 +08:00
sprintf(buf, "Can't allocate %s\n", description);
fatal_error(ctx, buf);
1997-04-26 21:21:57 +08:00
}
memset(ret, 0, size);
return ret;
}
int ask_yn(const char * string, int def)
{
int c;
const char *defstr;
char *short_yes = _("yY");
char *short_no = _("nN");
1997-04-26 21:21:57 +08:00
#ifdef HAVE_TERMIOS_H
struct termios termios, tmp;
1997-04-26 21:21:57 +08:00
tcgetattr (0, &termios);
tmp = termios;
tmp.c_lflag &= ~(ICANON | ECHO);
1997-04-26 21:58:21 +08:00
tmp.c_cc[VMIN] = 1;
tmp.c_cc[VTIME] = 0;
1997-04-26 21:21:57 +08:00
tcsetattr (0, TCSANOW, &tmp);
#endif
1997-04-26 21:21:57 +08:00
if (def == 1)
defstr = _("<y>");
1997-04-26 21:21:57 +08:00
else if (def == 0)
defstr = _("<n>");
1997-04-26 21:21:57 +08:00
else
defstr = _(" (y/n)");
1997-04-26 21:21:57 +08:00
printf("%s%s? ", string, defstr);
while (1) {
fflush (stdout);
if ((c = read_a_char()) == EOF)
1997-04-26 21:21:57 +08:00
break;
if (strchr(short_yes, (char) c)) {
1997-04-26 21:21:57 +08:00
def = 1;
break;
}
else if (strchr(short_no, (char) c)) {
1997-04-26 21:21:57 +08:00
def = 0;
break;
}
else if ((c == ' ' || c == '\n') && (def != -1))
break;
}
if (def)
printf ("yes\n\n");
else
printf ("no\n\n");
#ifdef HAVE_TERMIOS_H
1997-04-26 21:21:57 +08:00
tcsetattr (0, TCSANOW, &termios);
#endif
1997-04-26 21:21:57 +08:00
return def;
}
int ask (e2fsck_t ctx, const char * string, int def)
1997-04-26 21:21:57 +08:00
{
if (ctx->options & E2F_OPT_NO) {
printf (_("%s? no\n\n"), string);
1997-04-26 21:21:57 +08:00
return 0;
}
if (ctx->options & E2F_OPT_YES) {
printf (_("%s? yes\n\n"), string);
1997-04-26 21:21:57 +08:00
return 1;
}
if (ctx->options & E2F_OPT_PREEN) {
printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
1997-04-26 21:21:57 +08:00
return def;
}
return ask_yn(string, def);
}
void e2fsck_read_bitmaps(e2fsck_t ctx)
1997-04-26 21:21:57 +08:00
{
ext2_filsys fs = ctx->fs;
1997-04-26 21:21:57 +08:00
errcode_t retval;
if (ctx->invalid_bitmaps) {
com_err(ctx->program_name, 0,
_("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
ctx->device_name);
fatal_error(ctx, 0);
1997-04-26 21:21:57 +08:00
}
1997-04-26 21:34:30 +08:00
ehandler_operation(_("reading inode and block bitmaps"));
1997-04-26 21:34:30 +08:00
retval = ext2fs_read_bitmaps(fs);
ehandler_operation(0);
if (retval) {
com_err(ctx->program_name, retval,
_("while retrying to read bitmaps for %s"),
ctx->device_name);
fatal_error(ctx, 0);
1997-04-26 21:21:57 +08:00
}
}
void e2fsck_write_bitmaps(e2fsck_t ctx)
1997-04-26 21:21:57 +08:00
{
ext2_filsys fs = ctx->fs;
1997-04-26 21:21:57 +08:00
errcode_t retval;
if (ext2fs_test_bb_dirty(fs)) {
ehandler_operation(_("writing block bitmaps"));
1997-04-26 21:21:57 +08:00
retval = ext2fs_write_block_bitmap(fs);
ehandler_operation(0);
if (retval) {
com_err(ctx->program_name, retval,
_("while retrying to write block bitmaps for %s"),
ctx->device_name);
fatal_error(ctx, 0);
1997-04-26 21:21:57 +08:00
}
}
if (ext2fs_test_ib_dirty(fs)) {
ehandler_operation(_("writing inode bitmaps"));
1997-04-26 21:21:57 +08:00
retval = ext2fs_write_inode_bitmap(fs);
ehandler_operation(0);
if (retval) {
com_err(ctx->program_name, retval,
_("while retrying to write inode bitmaps for %s"),
ctx->device_name);
fatal_error(ctx, 0);
1997-04-26 21:21:57 +08:00
}
}
}
void preenhalt(e2fsck_t ctx)
1997-04-26 21:21:57 +08:00
{
ext2_filsys fs = ctx->fs;
if (!(ctx->options & E2F_OPT_PREEN))
1997-04-26 21:21:57 +08:00
return;
fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
"RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
ctx->device_name);
1997-04-26 21:58:21 +08:00
if (fs != NULL) {
fs->super->s_state |= EXT2_ERROR_FS;
ext2fs_mark_super_dirty(fs);
ext2fs_close(fs);
}
1997-04-26 21:21:57 +08:00
exit(FSCK_UNCORRECTED);
}
#ifdef RESOURCE_TRACK
1997-04-26 21:21:57 +08:00
void init_resource_track(struct resource_track *track)
{
1997-04-26 21:58:21 +08:00
#ifdef HAVE_GETRUSAGE
1997-04-26 21:21:57 +08:00
struct rusage r;
1997-04-26 21:58:21 +08:00
#endif
1997-04-26 21:21:57 +08:00
track->brk_start = sbrk(0);
gettimeofday(&track->time_start, 0);
1997-04-26 21:58:21 +08:00
#ifdef HAVE_GETRUSAGE
#ifdef solaris
memcpy(&r, 0, sizeof(struct rusage));
#endif
1997-04-26 21:21:57 +08:00
getrusage(RUSAGE_SELF, &r);
track->user_start = r.ru_utime;
track->system_start = r.ru_stime;
1997-04-26 21:58:21 +08:00
#else
track->user_start.tv_sec = track->user_start.tv_usec = 0;
track->system_start.tv_sec = track->system_start.tv_usec = 0;
#endif
1997-04-26 21:21:57 +08:00
}
1997-04-30 00:15:03 +08:00
#ifdef __GNUC__
#define _INLINE_ __inline__
#else
#define _INLINE_
#endif
static _INLINE_ float timeval_subtract(struct timeval *tv1,
struct timeval *tv2)
1997-04-26 21:21:57 +08:00
{
return ((tv1->tv_sec - tv2->tv_sec) +
((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
}
void print_resource_track(const char *desc, struct resource_track *track)
1997-04-26 21:21:57 +08:00
{
1997-04-26 21:58:21 +08:00
#ifdef HAVE_GETRUSAGE
1997-04-26 21:21:57 +08:00
struct rusage r;
#endif
#ifdef HAVE_MALLINFO
struct mallinfo malloc_info;
1997-04-26 21:58:21 +08:00
#endif
1997-04-26 21:21:57 +08:00
struct timeval time_end;
gettimeofday(&time_end, 0);
if (desc)
printf("%s: ", desc);
#ifdef HAVE_MALLINFO
#define kbytes(x) (((x) + 1023) / 1024)
malloc_info = mallinfo();
printf(_("Memory used: %dk/%dk (%dk/%dk), "),
kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
#else
printf(_("Memory used: %d, "),
(int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
#endif
1997-04-26 21:58:21 +08:00
#ifdef HAVE_GETRUSAGE
1997-04-26 21:21:57 +08:00
getrusage(RUSAGE_SELF, &r);
printf(_("time: %5.2f/%5.2f/%5.2f\n"),
1997-04-26 21:21:57 +08:00
timeval_subtract(&time_end, &track->time_start),
timeval_subtract(&r.ru_utime, &track->user_start),
timeval_subtract(&r.ru_stime, &track->system_start));
1997-04-26 21:58:21 +08:00
#else
printf(_("elapsed time: %6.3f\n"),
1997-04-26 21:58:21 +08:00
timeval_subtract(&time_end, &track->time_start));
#endif
1997-04-26 21:21:57 +08:00
}
#endif /* RESOURCE_TRACK */
1997-04-26 21:21:57 +08:00
void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
1997-04-26 21:34:30 +08:00
struct ext2_inode * inode, const char *proc)
{
int retval;
retval = ext2fs_read_inode(ctx->fs, ino, inode);
1997-04-26 21:34:30 +08:00
if (retval) {
com_err("ext2fs_read_inode", retval,
_("while reading inode %ld in %s"), ino, proc);
fatal_error(ctx, 0);
1997-04-26 21:34:30 +08:00
}
}
extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
1997-04-26 21:34:30 +08:00
struct ext2_inode * inode, const char *proc)
{
int retval;
retval = ext2fs_write_inode(ctx->fs, ino, inode);
1997-04-26 21:34:30 +08:00
if (retval) {
com_err("ext2fs_write_inode", retval,
_("while writing inode %ld in %s"), ino, proc);
fatal_error(ctx, 0);
1997-04-26 21:34:30 +08:00
}
}
1997-04-26 21:21:57 +08:00
#ifdef MTRACE
void mtrace_print(char *mesg)
{
FILE *malloc_get_mallstream();
FILE *f = malloc_get_mallstream();
if (f)
fprintf(f, "============= %s\n", mesg);
}
#endif
blk_t get_backup_sb(ext2_filsys fs)
{
if (!fs || !fs->super)
return 8193;
return fs->super->s_blocks_per_group + fs->super->s_first_data_block;
}
1997-04-26 21:21:57 +08:00
/*
* Given a mode, return the ext2 file type
*/
int ext2_file_type(unsigned int mode)
{
if (LINUX_S_ISREG(mode))
return EXT2_FT_REG_FILE;
if (LINUX_S_ISDIR(mode))
return EXT2_FT_DIR;
if (LINUX_S_ISCHR(mode))
return EXT2_FT_CHRDEV;
if (LINUX_S_ISBLK(mode))
return EXT2_FT_BLKDEV;
if (LINUX_S_ISLNK(mode))
return EXT2_FT_SYMLINK;
if (LINUX_S_ISFIFO(mode))
return EXT2_FT_FIFO;
if (LINUX_S_ISSOCK(mode))
return EXT2_FT_SOCK;
return 0;
}