/*
* util.c --- miscellaneous utilities
- *
+ *
* Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
*
* %Begin-Header%
*/
#include <stdlib.h>
+#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
+#ifdef __linux__
+#include <sys/utsname.h>
+#endif
#ifdef HAVE_CONIO_H
#undef HAVE_TERMIOS_H
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
-#include <stdio.h>
#endif
#ifdef HAVE_MALLOC_H
void fatal_error(e2fsck_t ctx, const char *msg)
{
- if (msg)
+ if (msg)
fprintf (stderr, "e2fsck: %s\n", msg);
if (ctx->fs && ctx->fs->io) {
if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
char buf[256];
#ifdef DEBUG_ALLOCATE_MEMORY
- printf("Allocating %d bytes for %s...\n", size, description);
+ printf("Allocating %u bytes for %s...\n", size, description);
#endif
ret = malloc(size);
if (!ret) {
return ret;
}
-char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
+char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
const char *str, int len)
{
char *ret;
-
+
if (!str)
return NULL;
if (!len)
def = 0;
break;
}
- else if ((c == ' ' || c == '\n') && (def != -1))
+ else if ((c == 27 || c == ' ' || c == '\n') && (def != -1))
break;
}
if (def)
fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
"RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
ctx->device_name);
+ ctx->flags |= E2F_FLAG_EXITING;
if (fs != NULL) {
fs->super->s_state |= EXT2_ERROR_FS;
ext2fs_mark_super_dirty(fs);
struct rusage r;
#endif
io_stats io_start = 0;
-
+
track->brk_start = sbrk(0);
gettimeofday(&track->time_start, 0);
#ifdef HAVE_GETRUSAGE
((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
}
-void print_resource_track(const char *desc, struct resource_track *track,
- io_channel channel)
+void print_resource_track(e2fsck_t ctx, const char *desc,
+ struct resource_track *track, io_channel channel)
{
#ifdef HAVE_GETRUSAGE
struct rusage r;
#endif
struct timeval time_end;
+ if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
+ (!desc && !(ctx->options & E2F_OPT_TIME)))
+ return;
+
+ e2fsck_clear_progbar(ctx);
gettimeofday(&time_end, 0);
if (desc)
printf("%s: ", desc);
#ifdef HAVE_MALLINFO
-#define kbytes(x) (((x) + 1023) / 1024)
-
+#define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
+
malloc_info = mallinfo();
- printf(_("Memory used: %dk/%dk (%dk/%dk), "),
+ printf(_("Memory used: %luk/%luk (%luk/%luk), "),
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
+ printf(_("Memory used: %lu, "),
+ (unsigned long) (((char *) sbrk(0)) -
+ ((char *) track->brk_start)));
+#endif
#ifdef HAVE_GETRUSAGE
getrusage(RUSAGE_SELF, &r);
channel->manager->get_stats(channel, &delta);
if (delta) {
bytes_read = delta->bytes_read - track->bytes_read;
- bytes_written = delta->bytes_written -
+ bytes_written = delta->bytes_written -
track->bytes_written;
}
printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n",
retval = ext2fs_read_inode(ctx->fs, ino, inode);
if (retval) {
com_err("ext2fs_read_inode", retval,
- _("while reading inode %ld in %s"), ino, proc);
+ _("while reading inode %lu in %s"), ino, proc);
fatal_error(ctx, 0);
}
}
retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
if (retval) {
com_err("ext2fs_read_inode_full", retval,
- _("while reading inode %ld in %s"), ino, proc);
+ _("while reading inode %lu in %s"), ino, proc);
fatal_error(ctx, 0);
}
}
retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
if (retval) {
com_err("ext2fs_write_inode", retval,
- _("while writing inode %ld in %s"), ino, proc);
+ _("while writing inode %lu in %s"), ino, proc);
fatal_error(ctx, 0);
}
}
retval = ext2fs_write_inode(ctx->fs, ino, inode);
if (retval) {
com_err("ext2fs_write_inode", retval,
- _("while writing inode %ld in %s"), ino, proc);
+ _("while writing inode %lu in %s"), ino, proc);
fatal_error(ctx, 0);
}
}
void *buf = NULL;
int blocksize;
blk_t superblock, ret_sb = 8193;
-
+
if (fs && fs->super) {
ret_sb = (fs->super->s_blocks_per_group +
fs->super->s_first_data_block);
}
return ret_sb;
}
-
+
if (ctx) {
if (ctx->blocksize) {
ret_sb = ctx->blocksize * 8;
if (blocksize == 1024)
superblock++;
io_channel_set_blksize(io, blocksize);
- if (io_channel_read_blk(io, superblock,
+ if (io_channel_read_blk64(io, superblock,
-SUPERBLOCK_SIZE, buf))
continue;
#ifdef WORDS_BIGENDIAN
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;
}
count = num - j;
if (count > STRIDE_LENGTH)
count = STRIDE_LENGTH;
- retval = io_channel_write_blk(fs->io, blk, count, buf);
+ retval = io_channel_write_blk64(fs->io, blk, count, buf);
if (retval) {
if (ret_count)
*ret_count = count;
}
return 0;
}
+
+/*
+ * Check to see if a filesystem is in /proc/filesystems.
+ * Returns 1 if found, 0 if not
+ */
+int fs_proc_check(const char *fs_name)
+{
+ FILE *f;
+ char buf[80], *cp, *t;
+
+ f = fopen("/proc/filesystems", "r");
+ if (!f)
+ return (0);
+ while (!feof(f)) {
+ if (!fgets(buf, sizeof(buf), f))
+ break;
+ cp = buf;
+ if (!isspace(*cp)) {
+ while (*cp && !isspace(*cp))
+ cp++;
+ }
+ while (*cp && isspace(*cp))
+ cp++;
+ if ((t = strchr(cp, '\n')) != NULL)
+ *t = 0;
+ if ((t = strchr(cp, '\t')) != NULL)
+ *t = 0;
+ if ((t = strchr(cp, ' ')) != NULL)
+ *t = 0;
+ if (!strcmp(fs_name, cp)) {
+ fclose(f);
+ return (1);
+ }
+ }
+ fclose(f);
+ return (0);
+}
+
+/*
+ * Check to see if a filesystem is available as a module
+ * Returns 1 if found, 0 if not
+ */
+int check_for_modules(const char *fs_name)
+{
+#ifdef __linux__
+ struct utsname uts;
+ FILE *f;
+ char buf[1024], *cp, *t;
+ int i;
+
+ if (uname(&uts))
+ return (0);
+ snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
+
+ f = fopen(buf, "r");
+ if (!f)
+ return (0);
+ while (!feof(f)) {
+ if (!fgets(buf, sizeof(buf), f))
+ break;
+ if ((cp = strchr(buf, ':')) != NULL)
+ *cp = 0;
+ else
+ continue;
+ if ((cp = strrchr(buf, '/')) != NULL)
+ cp++;
+ else
+ cp = buf;
+ i = strlen(cp);
+ if (i > 3) {
+ t = cp + i - 3;
+ if (!strcmp(t, ".ko"))
+ *t = 0;
+ }
+ if (!strcmp(cp, fs_name)) {
+ fclose(f);
+ return (1);
+ }
+ }
+ fclose(f);
+#endif /* __linux__ */
+ return (0);
+}
+
+/*
+ * Helper function that does the right thing if write returns a
+ * partial write, or an EGAIN/EINTR error.
+ */
+int write_all(int fd, char *buf, size_t count)
+{
+ ssize_t ret;
+ int c = 0;
+
+ while (count > 0) {
+ ret = write(fd, buf, count);
+ if (ret < 0) {
+ if ((errno == EAGAIN) || (errno == EINTR))
+ continue;
+ return -1;
+ }
+ count -= ret;
+ buf += ret;
+ c += ret;
+ }
+ return c;
+}