Whamcloud - gitweb
fdba6773c64cc76d604b50084159500c6c64ab80
[tools/e2fsprogs.git] / e2fsck / util.c
1 /*
2  * util.c --- miscellaneous utilities
3  * 
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 #ifdef HAVE_CONIO_H
18 #undef HAVE_TERMIOS_H
19 #include <conio.h>
20 #define read_a_char()   getch()
21 #else
22 #ifdef HAVE_TERMIOS_H
23 #include <termios.h>
24 #endif
25 #else /* !HAVE_CONIO_H */
26 #include <stdio.h>
27 #define read_a_char     getchar()
28 #endif
29
30 #ifdef HAVE_MALLOC_H
31 #include <malloc.h>
32 #endif
33
34 #include "e2fsck.h"
35
36 #include <sys/time.h>
37 #include <sys/resource.h>
38
39 void fatal_error(e2fsck_t ctx, const char *msg)
40 {
41         if (msg) 
42                 fprintf (stderr, "e2fsck: %s\n", msg);
43         ctx->flags |= E2F_FLAG_ABORT;
44         if (ctx->flags & E2F_FLAG_SETJMP_OK)
45                 longjmp(ctx->abort_loc, 1);
46         exit(FSCK_ERROR);
47 }
48
49 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
50                              const char *description)
51 {
52         void *ret;
53         char buf[256];
54
55 #ifdef DEBUG_ALLOCATE_MEMORY
56         printf("Allocating %d bytes for %s...\n", size, description);
57 #endif
58         ret = malloc(size);
59         if (!ret) {
60                 sprintf(buf, "Can't allocate %s\n", description);
61                 fatal_error(ctx, buf);
62         }
63         memset(ret, 0, size);
64         return ret;
65 }
66
67 int ask_yn(const char * string, int def)
68 {
69         int             c;
70         const char      *defstr;
71
72 #ifdef HAVE_TERMIOS_H
73         struct termios  termios, tmp;
74
75         tcgetattr (0, &termios);
76         tmp = termios;
77         tmp.c_lflag &= ~(ICANON | ECHO);
78         tmp.c_cc[VMIN] = 1;
79         tmp.c_cc[VTIME] = 0;
80         tcsetattr (0, TCSANOW, &tmp);
81 #endif
82
83         if (def == 1)
84                 defstr = "<y>";
85         else if (def == 0)
86                 defstr = "<n>";
87         else
88                 defstr = " (y/n)";
89         printf("%s%s? ", string, defstr);
90         while (1) {
91                 fflush (stdout);
92                 if ((c = read_a_char()) == EOF)
93                         break;
94                 c = toupper(c);
95                 if (c == 'Y') {
96                         def = 1;
97                         break;
98                 }
99                 else if (c == 'N') {
100                         def = 0;
101                         break;
102                 }
103                 else if ((c == ' ' || c == '\n') && (def != -1))
104                         break;
105         }
106         if (def)
107                 printf ("yes\n\n");
108         else
109                 printf ("no\n\n");
110 #ifdef HAVE_TERMIOS_H
111         tcsetattr (0, TCSANOW, &termios);
112 #endif
113         return def;
114 }
115
116 int ask (e2fsck_t ctx, const char * string, int def)
117 {
118         if (ctx->options & E2F_OPT_NO) {
119                 printf ("%s? no\n\n", string);
120                 return 0;
121         }
122         if (ctx->options & E2F_OPT_YES) {
123                 printf ("%s? yes\n\n", string);
124                 return 1;
125         }
126         if (ctx->options & E2F_OPT_PREEN) {
127                 printf ("%s? %s\n\n", string, def ? "yes" : "no");
128                 return def;
129         }
130         return ask_yn(string, def);
131 }
132
133 void e2fsck_read_bitmaps(e2fsck_t ctx)
134 {
135         ext2_filsys fs = ctx->fs;
136         errcode_t       retval;
137
138         if (ctx->invalid_bitmaps) {
139                 com_err(ctx->program_name, 0,
140                         "e2fsck_read_bitmaps: illegal bitmap block(s) for %s",
141                         ctx->device_name);
142                 fatal_error(ctx, 0);
143         }
144
145         ehandler_operation("reading inode and block bitmaps");
146         retval = ext2fs_read_bitmaps(fs);
147         ehandler_operation(0);
148         if (retval) {
149                 com_err(ctx->program_name, retval,
150                         "while retrying to read bitmaps for %s",
151                         ctx->device_name);
152                 fatal_error(ctx, 0);
153         }
154 }
155
156 void e2fsck_write_bitmaps(e2fsck_t ctx)
157 {
158         ext2_filsys fs = ctx->fs;
159         errcode_t       retval;
160
161         if (ext2fs_test_bb_dirty(fs)) {
162                 ehandler_operation("writing block bitmaps");
163                 retval = ext2fs_write_block_bitmap(fs);
164                 ehandler_operation(0);
165                 if (retval) {
166                         com_err(ctx->program_name, retval,
167                                 "while retrying to write block bitmaps for %s",
168                                 ctx->device_name);
169                         fatal_error(ctx, 0);
170                 }
171         }
172
173         if (ext2fs_test_ib_dirty(fs)) {
174                 ehandler_operation("writing inode bitmaps");
175                 retval = ext2fs_write_inode_bitmap(fs);
176                 ehandler_operation(0);
177                 if (retval) {
178                         com_err(ctx->program_name, retval,
179                                 "while retrying to write inode bitmaps for %s",
180                                 ctx->device_name);
181                         fatal_error(ctx, 0);
182                 }
183         }
184 }
185
186 void preenhalt(e2fsck_t ctx)
187 {
188         ext2_filsys fs = ctx->fs;
189
190         if (!(ctx->options & E2F_OPT_PREEN))
191                 return;
192         fprintf(stderr, "\n\n%s: UNEXPECTED INCONSISTENCY; "
193                 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n",
194                ctx->device_name);
195         if (fs != NULL) {
196                 fs->super->s_state |= EXT2_ERROR_FS;
197                 ext2fs_mark_super_dirty(fs);
198                 ext2fs_close(fs);
199         }
200         exit(FSCK_UNCORRECTED);
201 }
202
203 #ifdef RESOURCE_TRACK
204 void init_resource_track(struct resource_track *track)
205 {
206 #ifdef HAVE_GETRUSAGE
207         struct rusage r;
208 #endif
209         
210         track->brk_start = sbrk(0);
211         gettimeofday(&track->time_start, 0);
212 #ifdef HAVE_GETRUSAGE
213 #ifdef solaris
214         memcpy(&r, 0, sizeof(struct rusage));
215 #endif
216         getrusage(RUSAGE_SELF, &r);
217         track->user_start = r.ru_utime;
218         track->system_start = r.ru_stime;
219 #else
220         track->user_start.tv_sec = track->user_start.tv_usec = 0;
221         track->system_start.tv_sec = track->system_start.tv_usec = 0;
222 #endif
223 }
224
225 #ifdef __GNUC__
226 #define _INLINE_ __inline__
227 #else
228 #define _INLINE_
229 #endif
230
231 static _INLINE_ float timeval_subtract(struct timeval *tv1,
232                                        struct timeval *tv2)
233 {
234         return ((tv1->tv_sec - tv2->tv_sec) +
235                 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
236 }
237
238 void print_resource_track(const char *desc, struct resource_track *track)
239 {
240 #ifdef HAVE_GETRUSAGE
241         struct rusage r;
242 #endif
243 #ifdef HAVE_MALLINFO
244         struct mallinfo malloc_info;
245 #endif
246         struct timeval time_end;
247
248         gettimeofday(&time_end, 0);
249
250         if (desc)
251                 printf("%s: ", desc);
252
253 #ifdef HAVE_MALLINFO
254         malloc_info = mallinfo();
255         printf("Memory used: %d/%d, ", malloc_info.arena, malloc_info.hblkhd);
256 #else
257         printf("Memory used: %d, ",
258                (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
259 #endif  
260 #ifdef HAVE_GETRUSAGE
261         getrusage(RUSAGE_SELF, &r);
262
263         printf("elapsed time: %6.3f/%6.3f/%6.3f\n",
264                timeval_subtract(&time_end, &track->time_start),
265                timeval_subtract(&r.ru_utime, &track->user_start),
266                timeval_subtract(&r.ru_stime, &track->system_start));
267 #else
268         printf("elapsed time: %6.3f\n",
269                timeval_subtract(&time_end, &track->time_start));
270 #endif
271 }
272 #endif /* RESOURCE_TRACK */
273
274 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
275                               struct ext2_inode * inode, const char *proc)
276 {
277         int retval;
278
279         retval = ext2fs_read_inode(ctx->fs, ino, inode);
280         if (retval) {
281                 com_err("ext2fs_read_inode", retval,
282                         "while reading inode %ld in %s", ino, proc);
283                 fatal_error(ctx, 0);
284         }
285 }
286
287 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
288                                struct ext2_inode * inode, const char *proc)
289 {
290         int retval;
291
292         retval = ext2fs_write_inode(ctx->fs, ino, inode);
293         if (retval) {
294                 com_err("ext2fs_write_inode", retval,
295                         "while writing inode %ld in %s", ino, proc);
296                 fatal_error(ctx, 0);
297         }
298 }
299
300 #ifdef MTRACE
301 void mtrace_print(char *mesg)
302 {
303         FILE    *malloc_get_mallstream();
304         FILE    *f = malloc_get_mallstream();
305
306         if (f)
307                 fprintf(f, "============= %s\n", mesg);
308 }
309 #endif
310
311 blk_t get_backup_sb(ext2_filsys fs)
312 {
313         if (!fs || !fs->super)
314                 return 8193;
315         return fs->super->s_blocks_per_group + 1;
316 }
317