Whamcloud - gitweb
Many files:
[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         malloc_info = mallinfo();
255         printf(_("Memory used: %d/%d, "),
256                 malloc_info.arena, malloc_info.hblkhd);
257 #else
258         printf(_("Memory used: %d, "),
259                (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
260 #endif  
261 #ifdef HAVE_GETRUSAGE
262         getrusage(RUSAGE_SELF, &r);
263
264         printf(_("elapsed time: %6.3f/%6.3f/%6.3f\n"),
265                timeval_subtract(&time_end, &track->time_start),
266                timeval_subtract(&r.ru_utime, &track->user_start),
267                timeval_subtract(&r.ru_stime, &track->system_start));
268 #else
269         printf(_("elapsed time: %6.3f\n"),
270                timeval_subtract(&time_end, &track->time_start));
271 #endif
272 }
273 #endif /* RESOURCE_TRACK */
274
275 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
276                               struct ext2_inode * inode, const char *proc)
277 {
278         int retval;
279
280         retval = ext2fs_read_inode(ctx->fs, ino, inode);
281         if (retval) {
282                 com_err("ext2fs_read_inode", retval,
283                         _("while reading inode %ld in %s"), ino, proc);
284                 fatal_error(ctx, 0);
285         }
286 }
287
288 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
289                                struct ext2_inode * inode, const char *proc)
290 {
291         int retval;
292
293         retval = ext2fs_write_inode(ctx->fs, ino, inode);
294         if (retval) {
295                 com_err("ext2fs_write_inode", retval,
296                         _("while writing inode %ld in %s"), ino, proc);
297                 fatal_error(ctx, 0);
298         }
299 }
300
301 #ifdef MTRACE
302 void mtrace_print(char *mesg)
303 {
304         FILE    *malloc_get_mallstream();
305         FILE    *f = malloc_get_mallstream();
306
307         if (f)
308                 fprintf(f, "============= %s\n", mesg);
309 }
310 #endif
311
312 blk_t get_backup_sb(ext2_filsys fs)
313 {
314         if (!fs || !fs->super)
315                 return 8193;
316         return fs->super->s_blocks_per_group + 1;
317 }
318
319 /*
320  * Given a mode, return the ext2 file type
321  */
322 int ext2_file_type(unsigned int mode)
323 {
324         if (LINUX_S_ISREG(mode))
325                 return EXT2_FT_REG_FILE;
326
327         if (LINUX_S_ISDIR(mode))
328                 return EXT2_FT_DIR;
329         
330         if (LINUX_S_ISCHR(mode))
331                 return EXT2_FT_CHRDEV;
332         
333         if (LINUX_S_ISBLK(mode))
334                 return EXT2_FT_BLKDEV;
335         
336         if (LINUX_S_ISLNK(mode))
337                 return EXT2_FT_SYMLINK;
338
339         if (LINUX_S_ISFIFO(mode))
340                 return EXT2_FT_FIFO;
341         
342         if (LINUX_S_ISSOCK(mode))
343                 return EXT2_FT_SOCK;
344         
345         return 0;
346 }