Whamcloud - gitweb
9fd0114537fea8e7dfed5daf506e5ca68184ccea
[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 #include <stdio.h>
26 #define read_a_char()   getchar()
27 #endif
28
29 #ifdef HAVE_MALLOC_H
30 #include <malloc.h>
31 #endif
32
33 #include "e2fsck.h"
34
35 #include <sys/time.h>
36 #include <sys/resource.h>
37
38 void fatal_error(e2fsck_t ctx, const char *msg)
39 {
40         if (msg) 
41                 fprintf (stderr, "e2fsck: %s\n", msg);
42         ctx->flags |= E2F_FLAG_ABORT;
43         if (ctx->flags & E2F_FLAG_SETJMP_OK)
44                 longjmp(ctx->abort_loc, 1);
45         exit(FSCK_ERROR);
46 }
47
48 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
49                              const char *description)
50 {
51         void *ret;
52         char buf[256];
53
54 #ifdef DEBUG_ALLOCATE_MEMORY
55         printf("Allocating %d bytes for %s...\n", size, description);
56 #endif
57         ret = malloc(size);
58         if (!ret) {
59                 sprintf(buf, "Can't allocate %s\n", description);
60                 fatal_error(ctx, buf);
61         }
62         memset(ret, 0, size);
63         return ret;
64 }
65
66 int ask_yn(const char * string, int def)
67 {
68         int             c;
69         const char      *defstr;
70         char            *short_yes = _("yY");
71         char            *short_no = _("nN");
72
73 #ifdef HAVE_TERMIOS_H
74         struct termios  termios, tmp;
75
76         tcgetattr (0, &termios);
77         tmp = termios;
78         tmp.c_lflag &= ~(ICANON | ECHO);
79         tmp.c_cc[VMIN] = 1;
80         tmp.c_cc[VTIME] = 0;
81         tcsetattr (0, TCSANOW, &tmp);
82 #endif
83
84         if (def == 1)
85                 defstr = _("<y>");
86         else if (def == 0)
87                 defstr = _("<n>");
88         else
89                 defstr = _(" (y/n)");
90         printf("%s%s? ", string, defstr);
91         while (1) {
92                 fflush (stdout);
93                 if ((c = read_a_char()) == EOF)
94                         break;
95                 if (strchr(short_yes, (char) c)) {
96                         def = 1;
97                         break;
98                 }
99                 else if (strchr(short_no, (char) c)) {
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 #define kbytes(x)       (((x) + 1023) / 1024)
255         
256         malloc_info = mallinfo();
257         printf(_("Memory used: %dk/%dk (%dk/%dk), "),
258                kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
259                kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
260 #else
261         printf(_("Memory used: %d, "),
262                (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
263 #endif  
264 #ifdef HAVE_GETRUSAGE
265         getrusage(RUSAGE_SELF, &r);
266
267         printf(_("time: %5.2f/%5.2f/%5.2f\n"),
268                timeval_subtract(&time_end, &track->time_start),
269                timeval_subtract(&r.ru_utime, &track->user_start),
270                timeval_subtract(&r.ru_stime, &track->system_start));
271 #else
272         printf(_("elapsed time: %6.3f\n"),
273                timeval_subtract(&time_end, &track->time_start));
274 #endif
275 }
276 #endif /* RESOURCE_TRACK */
277
278 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
279                               struct ext2_inode * inode, const char *proc)
280 {
281         int retval;
282
283         retval = ext2fs_read_inode(ctx->fs, ino, inode);
284         if (retval) {
285                 com_err("ext2fs_read_inode", retval,
286                         _("while reading inode %ld in %s"), ino, proc);
287                 fatal_error(ctx, 0);
288         }
289 }
290
291 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
292                                struct ext2_inode * inode, const char *proc)
293 {
294         int retval;
295
296         retval = ext2fs_write_inode(ctx->fs, ino, inode);
297         if (retval) {
298                 com_err("ext2fs_write_inode", retval,
299                         _("while writing inode %ld in %s"), ino, proc);
300                 fatal_error(ctx, 0);
301         }
302 }
303
304 #ifdef MTRACE
305 void mtrace_print(char *mesg)
306 {
307         FILE    *malloc_get_mallstream();
308         FILE    *f = malloc_get_mallstream();
309
310         if (f)
311                 fprintf(f, "============= %s\n", mesg);
312 }
313 #endif
314
315 blk_t get_backup_sb(ext2_filsys fs)
316 {
317         if (!fs || !fs->super)
318                 return 8193;
319         return fs->super->s_blocks_per_group + 1;
320 }
321
322 /*
323  * Given a mode, return the ext2 file type
324  */
325 int ext2_file_type(unsigned int mode)
326 {
327         if (LINUX_S_ISREG(mode))
328                 return EXT2_FT_REG_FILE;
329
330         if (LINUX_S_ISDIR(mode))
331                 return EXT2_FT_DIR;
332         
333         if (LINUX_S_ISCHR(mode))
334                 return EXT2_FT_CHRDEV;
335         
336         if (LINUX_S_ISBLK(mode))
337                 return EXT2_FT_BLKDEV;
338         
339         if (LINUX_S_ISLNK(mode))
340                 return EXT2_FT_SYMLINK;
341
342         if (LINUX_S_ISFIFO(mode))
343                 return EXT2_FT_FIFO;
344         
345         if (LINUX_S_ISSOCK(mode))
346                 return EXT2_FT_SOCK;
347         
348         return 0;
349 }