Whamcloud - gitweb
e2fsck: add a 'yes to all' response in interactive mode
[tools/e2fsprogs.git] / e2fsck / util.c
index f3ba645..e2fb982 100644 (file)
@@ -55,7 +55,7 @@ void fatal_error(e2fsck_t ctx, const char *msg)
                fprintf (stderr, "e2fsck: %s\n", msg);
        if (!fs)
                goto out;
-       if (fs->io) {
+       if (fs->io && fs->super) {
                ext2fs_mmp_stop(ctx->fs);
                if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
                        io_channel_flush(ctx->fs->io);
@@ -121,7 +121,8 @@ void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
 #endif
        ret = malloc(size);
        if (!ret) {
-               sprintf(buf, "Can't allocate %s\n", description);
+               sprintf(buf, "Can't allocate %u bytes for %s\n",
+                       size, description);
                fatal_error(ctx, buf);
        }
        memset(ret, 0, size);
@@ -188,6 +189,10 @@ int ask_yn(e2fsck_t ctx, const char * string, int def)
        const char      *defstr;
        const char      *short_yes = _("yY");
        const char      *short_no = _("nN");
+       const char      *short_yesall = _("aA");
+       const char      *yesall_prompt = _(" ('a' enables 'yes' to all) ");
+       const char      *extra_prompt = "";
+       static int      yes_answers;
 
 #ifdef HAVE_TERMIOS_H
        struct termios  termios, tmp;
@@ -206,7 +211,16 @@ int ask_yn(e2fsck_t ctx, const char * string, int def)
                defstr = _(_("<n>"));
        else
                defstr = _(" (y/n)");
-       log_out(ctx, "%s%s? ", string, defstr);
+       /*
+        * If the user presses 'y' more than 8 (but less than 12) times in
+        * succession without pressing anything else, display a hint about
+        * yes-to-all mode.
+        */
+       if (yes_answers > 12)
+               yes_answers = -1;
+       else if (yes_answers > 8)
+               extra_prompt = yesall_prompt;
+       log_out(ctx, "%s%s%s? ", string, extra_prompt, defstr);
        while (1) {
                fflush (stdout);
                if ((c = read_a_char()) == EOF)
@@ -219,24 +233,35 @@ int ask_yn(e2fsck_t ctx, const char * string, int def)
                                log_out(ctx, "\n");
                                longjmp(e2fsck_global_ctx->abort_loc, 1);
                        }
-                       log_out(ctx, _("cancelled!\n"));
+                       log_out(ctx, "%s", _("cancelled!\n"));
+                       yes_answers = 0;
                        return 0;
                }
                if (strchr(short_yes, (char) c)) {
                        def = 1;
+                       if (yes_answers >= 0)
+                               yes_answers++;
                        break;
-               }
-               else if (strchr(short_no, (char) c)) {
+               } else if (strchr(short_no, (char) c)) {
                        def = 0;
+                       yes_answers = -1;
                        break;
-               }
-               else if ((c == 27 || c == ' ' || c == '\n') && (def != -1))
+               } else if (strchr(short_yesall, (char)c)) {
+                       def = 2;
+                       yes_answers = -1;
+                       ctx->options |= E2F_OPT_YES;
+                       break;
+               } else if ((c == 27 || c == ' ' || c == '\n') && (def != -1)) {
+                       yes_answers = -1;
                        break;
+               }
        }
-       if (def)
-               log_out(ctx, _("yes\n"));
+       if (def == 2)
+               log_out(ctx, "%s", _("yes to all\n"));
+       else if (def)
+               log_out(ctx, "%s", _("yes\n"));
        else
-               log_out(ctx, _("no\n"));
+               log_out(ctx, "%s", _("no\n"));
 #ifdef HAVE_TERMIOS_H
        tcsetattr (0, TCSANOW, &termios);
 #endif
@@ -266,6 +291,7 @@ void e2fsck_read_bitmaps(e2fsck_t ctx)
        errcode_t       retval;
        const char      *old_op;
        unsigned int    save_type;
+       int             flags;
 
        if (ctx->invalid_bitmaps) {
                com_err(ctx->program_name, 0,
@@ -277,7 +303,11 @@ void e2fsck_read_bitmaps(e2fsck_t ctx)
        old_op = ehandler_operation(_("reading inode and block bitmaps"));
        e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
                               &save_type);
+       flags = ctx->fs->flags;
+       ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
        retval = ext2fs_read_bitmaps(fs);
+       ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
+                        (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
        fs->default_bitmap_type = save_type;
        ehandler_operation(old_op);
        if (retval) {
@@ -318,7 +348,7 @@ void preenhalt(e2fsck_t ctx)
        if (fs != NULL) {
                fs->super->s_state |= EXT2_ERROR_FS;
                ext2fs_mark_super_dirty(fs);
-               ext2fs_close(fs);
+               ext2fs_close_free(&fs);
        }
        exit(FSCK_UNCORRECTED);
 }
@@ -438,7 +468,7 @@ void print_resource_track(e2fsck_t ctx, const char *desc,
 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
                              struct ext2_inode * inode, const char *proc)
 {
-       int retval;
+       errcode_t retval;
 
        retval = ext2fs_read_inode(ctx->fs, ino, inode);
        if (retval) {
@@ -452,7 +482,7 @@ void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
                            struct ext2_inode *inode, int bufsize,
                            const char *proc)
 {
-       int retval;
+       errcode_t retval;
 
        retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
        if (retval) {
@@ -462,11 +492,11 @@ void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
        }
 }
 
-extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
-                              struct ext2_inode * inode, int bufsize,
-                              const char *proc)
+void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
+                            struct ext2_inode * inode, int bufsize,
+                            const char *proc)
 {
-       int retval;
+       errcode_t retval;
 
        retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
        if (retval) {
@@ -476,10 +506,10 @@ extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
        }
 }
 
-extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
-                              struct ext2_inode * inode, const char *proc)
+void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
+                       struct ext2_inode * inode, const char *proc)
 {
-       int retval;
+       errcode_t retval;
 
        retval = ext2fs_write_inode(ctx->fs, ino, inode);
        if (retval) {
@@ -500,14 +530,14 @@ void mtrace_print(char *mesg)
 }
 #endif
 
-blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
-                  io_manager manager)
+blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
+                     io_manager manager)
 {
        struct ext2_super_block *sb;
        io_channel              io = NULL;
        void                    *buf = NULL;
        int                     blocksize;
-       blk_t                   superblock, ret_sb = 8193;
+       blk64_t                 superblock, ret_sb = 8193;
 
        if (fs && fs->super) {
                ret_sb = (fs->super->s_blocks_per_group +
@@ -602,63 +632,6 @@ int ext2_file_type(unsigned int mode)
        return 0;
 }
 
-#define STRIDE_LENGTH 8
-/*
- * Helper function which zeros out _num_ blocks starting at _blk_.  In
- * case of an error, the details of the error is returned via _ret_blk_
- * and _ret_count_ if they are non-NULL pointers.  Returns 0 on
- * success, and an error code on an error.
- *
- * As a special case, if the first argument is NULL, then it will
- * attempt to free the static zeroizing buffer.  (This is to keep
- * programs that check for memory leaks happy.)
- */
-errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
-                            blk_t *ret_blk, int *ret_count)
-{
-       int             j, count, next_update, next_update_incr;
-       static char     *buf;
-       errcode_t       retval;
-
-       /* If fs is null, clean up the static buffer and return */
-       if (!fs) {
-               if (buf) {
-                       free(buf);
-                       buf = 0;
-               }
-               return 0;
-       }
-       /* Allocate the zeroizing buffer if necessary */
-       if (!buf) {
-               buf = malloc(fs->blocksize * STRIDE_LENGTH);
-               if (!buf) {
-                       com_err("malloc", ENOMEM,
-                               _("while allocating zeroizing buffer"));
-                       exit(1);
-               }
-               memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
-       }
-       /* OK, do the write loop */
-       next_update = 0;
-       next_update_incr = num / 100;
-       if (next_update_incr < 1)
-               next_update_incr = 1;
-       for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
-               count = num - j;
-               if (count > STRIDE_LENGTH)
-                       count = STRIDE_LENGTH;
-               retval = io_channel_write_blk64(fs->io, blk, count, buf);
-               if (retval) {
-                       if (ret_count)
-                               *ret_count = count;
-                       if (ret_blk)
-                               *ret_blk = blk;
-                       return retval;
-               }
-       }
-       return 0;
-}
-
 /*
  * Check to see if a filesystem is in /proc/filesystems.
  * Returns 1 if found, 0 if not
@@ -795,13 +768,13 @@ void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
                            const char *profile_name, unsigned int *old_type)
 {
        unsigned type;
+       e2fsck_t ctx = (e2fsck_t) fs->priv_data;
 
        if (old_type)
                *old_type = fs->default_bitmap_type;
-       profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
-                        profile_name, 0, default_type, &type);
-       profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
-                        "all", 0, type, &type);
+       profile_get_uint(ctx->profile, "bitmaps", profile_name, 0,
+                        default_type, &type);
+       profile_get_uint(ctx->profile, "bitmaps", "all", 0, type, &type);
        fs->default_bitmap_type = type ? type : default_type;
 }