Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ehandler.c --- handle bad block errors which come up during the |
| 3 | * course of an e2fsck session. |
| 4 | * |
| 5 | * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed |
| 6 | * under the terms of the GNU Public License. |
| 7 | */ |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
| 11 | #include <string.h> |
| 12 | #include <ctype.h> |
| 13 | #include <termios.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 14 | |
| 15 | #include "e2fsck.h" |
| 16 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 17 | #include <sys/time.h> |
| 18 | #include <sys/resource.h> |
| 19 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 20 | static const char *operation; |
| 21 | |
| 22 | static errcode_t e2fsck_handle_read_error(io_channel channel, |
| 23 | unsigned long block, |
| 24 | int count, |
| 25 | void *data, |
| 26 | size_t size, |
| 27 | int actual, |
| 28 | errcode_t error) |
| 29 | { |
| 30 | int i; |
| 31 | char *p; |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 32 | ext2_filsys fs = (ext2_filsys) channel->app_data; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 33 | e2fsck_t ctx; |
| 34 | |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 35 | ctx = (e2fsck_t) fs->priv_data; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * If more than one block was read, try reading each block |
| 39 | * separately. We could use the actual bytes read to figure |
| 40 | * out where to start, but we don't bother. |
| 41 | */ |
| 42 | if (count > 1) { |
| 43 | p = (char *) data; |
| 44 | for (i=0; i < count; i++, p += channel->block_size, block++) { |
| 45 | error = io_channel_read_blk(channel, block, |
| 46 | 1, p); |
| 47 | if (error) |
| 48 | return error; |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | if (operation) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 53 | printf(_("Error reading block %lu (%s) while %s. "), block, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 54 | error_message(error), operation); |
| 55 | else |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 56 | printf(_("Error reading block %lu (%s). "), block, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 57 | error_message(error)); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 58 | preenhalt(ctx); |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 59 | if (ask(ctx, _("Ignore error"), 1)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 60 | return 0; |
| 61 | |
| 62 | return error; |
| 63 | } |
| 64 | |
| 65 | static errcode_t e2fsck_handle_write_error(io_channel channel, |
| 66 | unsigned long block, |
| 67 | int count, |
| 68 | const void *data, |
| 69 | size_t size, |
| 70 | int actual, |
| 71 | errcode_t error) |
| 72 | { |
| 73 | int i; |
| 74 | const char *p; |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 75 | ext2_filsys fs = (ext2_filsys) channel->app_data; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 76 | e2fsck_t ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 77 | |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 78 | ctx = (e2fsck_t) fs->priv_data; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 79 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 80 | /* |
| 81 | * If more than one block was written, try writing each block |
| 82 | * separately. We could use the actual bytes read to figure |
| 83 | * out where to start, but we don't bother. |
| 84 | */ |
| 85 | if (count > 1) { |
| 86 | p = (const char *) data; |
| 87 | for (i=0; i < count; i++, p += channel->block_size, block++) { |
| 88 | error = io_channel_write_blk(channel, block, |
| 89 | 1, p); |
| 90 | if (error) |
| 91 | return error; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | if (operation) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 97 | printf(_("Error writing block %lu (%s) while %s. "), block, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 98 | error_message(error), operation); |
| 99 | else |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 100 | printf(_("Error writing block %lu (%s). "), block, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 101 | error_message(error)); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 102 | preenhalt(ctx); |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 103 | if (ask(ctx, _("Ignore error"), 1)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 104 | return 0; |
| 105 | |
| 106 | return error; |
| 107 | } |
| 108 | |
| 109 | const char *ehandler_operation(const char *op) |
| 110 | { |
| 111 | const char *ret = operation; |
| 112 | |
| 113 | operation = op; |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | void ehandler_init(io_channel channel) |
| 118 | { |
| 119 | channel->read_error = e2fsck_handle_read_error; |
| 120 | channel->write_error = e2fsck_handle_write_error; |
| 121 | } |