blob: 96aedfb82cb18609191f3148770a3ed1d5ca5d53 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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>
14#include <sys/resource.h>
15
16#include "e2fsck.h"
17
18static const char *operation;
19
20static errcode_t e2fsck_handle_read_error(io_channel channel,
21 unsigned long block,
22 int count,
23 void *data,
24 size_t size,
25 int actual,
26 errcode_t error)
27{
28 int i;
29 char *p;
30
31 /*
32 * If more than one block was read, try reading each block
33 * separately. We could use the actual bytes read to figure
34 * out where to start, but we don't bother.
35 */
36 if (count > 1) {
37 p = (char *) data;
38 for (i=0; i < count; i++, p += channel->block_size, block++) {
39 error = io_channel_read_blk(channel, block,
40 1, p);
41 if (error)
42 return error;
43 }
44 return 0;
45 }
46 if (operation)
47 printf("Error reading block %ld (%s) while %s. ", block,
48 error_message(error), operation);
49 else
50 printf("Error reading block %ld (%s). ", block,
51 error_message(error));
52 preenhalt();
53 if (ask("Ignore error", 1))
54 return 0;
55
56 return error;
57}
58
59static errcode_t e2fsck_handle_write_error(io_channel channel,
60 unsigned long block,
61 int count,
62 const void *data,
63 size_t size,
64 int actual,
65 errcode_t error)
66{
67 int i;
68 const char *p;
69
70 /*
71 * If more than one block was written, try writing each block
72 * separately. We could use the actual bytes read to figure
73 * out where to start, but we don't bother.
74 */
75 if (count > 1) {
76 p = (const char *) data;
77 for (i=0; i < count; i++, p += channel->block_size, block++) {
78 error = io_channel_write_blk(channel, block,
79 1, p);
80 if (error)
81 return error;
82 }
83 return 0;
84 }
85
86 if (operation)
87 printf("Error writing block %ld (%s) while %s. ", block,
88 error_message(error), operation);
89 else
90 printf("Error writing block %ld (%s). ", block,
91 error_message(error));
92 preenhalt();
93 if (ask("Ignore error", 1))
94 return 0;
95
96 return error;
97}
98
99const char *ehandler_operation(const char *op)
100{
101 const char *ret = operation;
102
103 operation = op;
104 return ret;
105}
106
107void ehandler_init(io_channel channel)
108{
109 channel->read_error = e2fsck_handle_read_error;
110 channel->write_error = e2fsck_handle_write_error;
111}