aboutsummaryrefslogtreecommitdiffstats
path: root/e2fsck/util.c
blob: 9fc22e92d238bba79ef739d94fd4fd3e1525c032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * util.c --- miscellaneous utilities
 * 
 * Copyright (C) 1993, 1994 Theodore Ts'o.  This file may be
 * redistributed under the terms of the GNU Public License.
 */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <termios.h>

#include "e2fsck.h"

#include <sys/time.h>
#include <sys/resource.h>

const char * fix_msg[2] = { "IGNORED", "FIXED" };
const char * clear_msg[2] = { "IGNORED", "CLEARED" };

void fatal_error (const char *msg)
{
	if (msg) 
		fprintf (stderr, "%s: %s\n", program_name, msg);
	exit(FSCK_ERROR);
}

void *allocate_memory(int size, const char *description)
{
	void *ret;
	char buf[256];

#ifdef DEBUG_ALLOCATE_MEMORY
	printf("Allocating %d bytes for %s...\n", size, description);
#endif
	ret = malloc(size);
	if (!ret) {
		sprintf(buf, "%%s: Can't allocate %s\n", description);
		fatal_error(buf);
	}
	memset(ret, 0, size);
	return ret;
}


int ask_yn(const char * string, int def)
{
	int		c;
	struct termios	termios, tmp;
	const char	*defstr;

	tcgetattr (0, &termios);
	tmp = termios;
	tmp.c_lflag &= ~(ICANON | ECHO);
	tmp.c_cc[VMIN] = 1;
	tmp.c_cc[VTIME] = 0;
	tcsetattr (0, TCSANOW, &tmp);

	if (def == 1)
		defstr = "<y>";
	else if (def == 0)
		defstr = "<n>";
	else
		defstr = " (y/n)";
	printf("%s%s? ", string, defstr);
	while (1) {
		fflush (stdout);
		if ((c = getchar()) == EOF)
			break;
		c = toupper(c);
		if (c == 'Y') {
			def = 1;
			break;
		}
		else if (c == 'N') {
			def = 0;
			break;
		}
		else if ((c == ' ' || c == '\n') && (def != -1))
			break;
	}
	if (def)
		printf ("yes\n\n");
	else
		printf ("no\n\n");
	tcsetattr (0, TCSANOW, &termios);
	return def;
}

int ask (const char * string, int def)
{
	if (nflag) {
		printf ("%s? no\n\n", string);
		return 0;
	}
	if (yflag) {
		printf ("%s? yes\n\n", string);
		return 1;
	}
	if (preen) {
		printf ("%s? %s\n\n", string, def ? "yes" : "no");
		return def;
	}
	return ask_yn(string, def);
}

void read_bitmaps(ext2_filsys fs)
{
	errcode_t	retval;

	if (invalid_bitmaps) {
		com_err(program_name, 0,
			"read_bitmaps: illegal bitmap block(s) for %s",
			device_name);
		fatal_error(0);
	}

	ehandler_operation("reading inode and block bitmaps");
	retval = ext2fs_read_bitmaps(fs);
	ehandler_operation(0);
	if (retval) {
		com_err(program_name, retval,
			"while retrying to read bitmaps for %s",
			device_name);
		fatal_error(0);
	}
}

void write_bitmaps(ext2_filsys fs)
{
	errcode_t	retval;

	if (ext2fs_test_bb_dirty(fs)) {
		ehandler_operation("writing block bitmaps");
		retval = ext2fs_write_block_bitmap(fs);
		ehandler_operation(0);
		if (retval) {
			com_err(program_name, retval,
				"while retrying to write block bitmaps for %s",
				device_name);
			fatal_error(0);
		}
	}

	if (ext2fs_test_ib_dirty(fs)) {
		ehandler_operation("writing inode bitmaps");
		retval = ext2fs_write_inode_bitmap(fs);
		ehandler_operation(0);
		if (retval) {
			com_err(program_name, retval,
				"while retrying to write inode bitmaps for %s",
				device_name);
			fatal_error(0);
		}
	}
}

void preenhalt(ext2_filsys fs)
{
	if (!preen)
		return;
	fprintf(stderr, "\n\n%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
	       device_name);
	if (fs != NULL) {
		fs->super->s_state |= EXT2_ERROR_FS;
		ext2fs_mark_super_dirty(fs);
		ext2fs_close(fs);
	}
	exit(FSCK_UNCORRECTED);
}

void init_resource_track(struct resource_track *track)
{
#ifdef HAVE_GETRUSAGE
	struct rusage r;
#endif
	
	track->brk_start = sbrk(0);
	gettimeofday(&track->time_start, 0);
#ifdef HAVE_GETRUSAGE
	getrusage(RUSAGE_SELF, &r);
	track->user_start = r.ru_utime;
	track->system_start = r.ru_stime;
#else
	track->user_start.tv_sec = track->user_start.tv_usec = 0;
	track->system_start.tv_sec = track->system_start.tv_usec = 0;
#endif
}

static __inline__ float timeval_subtract(struct timeval *tv1,
					 struct timeval *tv2)
{
	return ((tv1->tv_sec - tv2->tv_sec) +
		((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
}

void print_resource_track(struct resource_track *track)
{
#ifdef HAVE_GETRUSAGE
	struct rusage r;
#endif
	struct timeval time_end;

	gettimeofday(&time_end, 0);
#ifdef HAVE_GETRUSAGE
	getrusage(RUSAGE_SELF, &r);

	printf("Memory used: %d, elapsed time: %6.3f/%6.3f/%6.3f\n",
	       (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
	       timeval_subtract(&time_end, &track->time_start),
	       timeval_subtract(&r.ru_utime, &track->user_start),
	       timeval_subtract(&r.ru_stime, &track->system_start));
#else
	printf("Memory used: %d, elapsed time: %6.3f\n",
	       (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
	       timeval_subtract(&time_end, &track->time_start));
#endif
}

void e2fsck_read_inode(ext2_filsys fs, unsigned long ino,
			      struct ext2_inode * inode, const char *proc)
{
	int retval;

	retval = ext2fs_read_inode(fs, ino, inode);
	if (retval) {
		com_err("ext2fs_read_inode", retval,
			"while reading inode %ld in %s", ino, proc);
		fatal_error(0);
	}
}

extern void e2fsck_write_inode(ext2_filsys fs, unsigned long ino,
			       struct ext2_inode * inode, const char *proc)
{
	int retval;

	retval = ext2fs_write_inode(fs, ino, inode);
	if (retval) {
		com_err("ext2fs_write_inode", retval,
			"while writing inode %ld in %s", ino, proc);
		fatal_error(0);
	}
}

/*
 * This function returns 1 if the inode's block entries actually
 * contain block entries.
 */
int inode_has_valid_blocks(struct ext2_inode *inode)
{
	/*
	 * Only directories, regular files, and some symbolic links
	 * have valid block entries.
	 */
	if (!LINUX_S_ISDIR(inode->i_mode) && !LINUX_S_ISREG(inode->i_mode) &&
	    !LINUX_S_ISLNK(inode->i_mode))
		return 0;
	
	/*
	 * If the symbolic link is a "fast symlink", then the symlink
	 * target is stored in the block entries.
	 */
	if (LINUX_S_ISLNK (inode->i_mode) && inode->i_blocks == 0 &&
	    inode->i_size < EXT2_N_BLOCKS * sizeof (unsigned long))
		return 0;

	return 1;
}

#ifdef MTRACE
void mtrace_print(char *mesg)
{
	FILE	*malloc_get_mallstream();
	FILE	*f = malloc_get_mallstream();

	if (f)
		fprintf(f, "============= %s\n", mesg);
}
#endif