Whamcloud - gitweb
ChangeLog, util.c:
[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         if (ctx->fs && ctx->fs->io) {
43                 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
44                         io_channel_flush(ctx->fs->io);
45                 else
46                         fprintf(stderr, "e2fsck: io manager magic bad!\n");
47         }
48         ctx->flags |= E2F_FLAG_ABORT;
49         if (ctx->flags & E2F_FLAG_SETJMP_OK)
50                 longjmp(ctx->abort_loc, 1);
51         exit(FSCK_ERROR);
52 }
53
54 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
55                              const char *description)
56 {
57         void *ret;
58         char buf[256];
59
60 #ifdef DEBUG_ALLOCATE_MEMORY
61         printf("Allocating %d bytes for %s...\n", size, description);
62 #endif
63         ret = malloc(size);
64         if (!ret) {
65                 sprintf(buf, "Can't allocate %s\n", description);
66                 fatal_error(ctx, buf);
67         }
68         memset(ret, 0, size);
69         return ret;
70 }
71
72 int ask_yn(const char * string, int def)
73 {
74         int             c;
75         const char      *defstr;
76         const char      *short_yes = _("yY");
77         const char      *short_no = _("nN");
78
79 #ifdef HAVE_TERMIOS_H
80         struct termios  termios, tmp;
81
82         tcgetattr (0, &termios);
83         tmp = termios;
84         tmp.c_lflag &= ~(ICANON | ECHO);
85         tmp.c_cc[VMIN] = 1;
86         tmp.c_cc[VTIME] = 0;
87         tcsetattr (0, TCSANOW, &tmp);
88 #endif
89
90         if (def == 1)
91                 defstr = _("<y>");
92         else if (def == 0)
93                 defstr = _("<n>");
94         else
95                 defstr = _(" (y/n)");
96         printf("%s%s? ", string, defstr);
97         while (1) {
98                 fflush (stdout);
99                 if ((c = read_a_char()) == EOF)
100                         break;
101                 if (strchr(short_yes, (char) c)) {
102                         def = 1;
103                         break;
104                 }
105                 else if (strchr(short_no, (char) c)) {
106                         def = 0;
107                         break;
108                 }
109                 else if ((c == ' ' || c == '\n') && (def != -1))
110                         break;
111         }
112         if (def)
113                 printf ("yes\n\n");
114         else
115                 printf ("no\n\n");
116 #ifdef HAVE_TERMIOS_H
117         tcsetattr (0, TCSANOW, &termios);
118 #endif
119         return def;
120 }
121
122 int ask (e2fsck_t ctx, const char * string, int def)
123 {
124         if (ctx->options & E2F_OPT_NO) {
125                 printf (_("%s? no\n\n"), string);
126                 return 0;
127         }
128         if (ctx->options & E2F_OPT_YES) {
129                 printf (_("%s? yes\n\n"), string);
130                 return 1;
131         }
132         if (ctx->options & E2F_OPT_PREEN) {
133                 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
134                 return def;
135         }
136         return ask_yn(string, def);
137 }
138
139 void e2fsck_read_bitmaps(e2fsck_t ctx)
140 {
141         ext2_filsys fs = ctx->fs;
142         errcode_t       retval;
143
144         if (ctx->invalid_bitmaps) {
145                 com_err(ctx->program_name, 0,
146                     _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
147                         ctx->device_name);
148                 fatal_error(ctx, 0);
149         }
150
151         ehandler_operation(_("reading inode and block bitmaps"));
152         retval = ext2fs_read_bitmaps(fs);
153         ehandler_operation(0);
154         if (retval) {
155                 com_err(ctx->program_name, retval,
156                         _("while retrying to read bitmaps for %s"),
157                         ctx->device_name);
158                 fatal_error(ctx, 0);
159         }
160 }
161
162 void e2fsck_write_bitmaps(e2fsck_t ctx)
163 {
164         ext2_filsys fs = ctx->fs;
165         errcode_t       retval;
166
167         if (ext2fs_test_bb_dirty(fs)) {
168                 ehandler_operation(_("writing block bitmaps"));
169                 retval = ext2fs_write_block_bitmap(fs);
170                 ehandler_operation(0);
171                 if (retval) {
172                         com_err(ctx->program_name, retval,
173                             _("while retrying to write block bitmaps for %s"),
174                                 ctx->device_name);
175                         fatal_error(ctx, 0);
176                 }
177         }
178
179         if (ext2fs_test_ib_dirty(fs)) {
180                 ehandler_operation(_("writing inode bitmaps"));
181                 retval = ext2fs_write_inode_bitmap(fs);
182                 ehandler_operation(0);
183                 if (retval) {
184                         com_err(ctx->program_name, retval,
185                             _("while retrying to write inode bitmaps for %s"),
186                                 ctx->device_name);
187                         fatal_error(ctx, 0);
188                 }
189         }
190 }
191
192 void preenhalt(e2fsck_t ctx)
193 {
194         ext2_filsys fs = ctx->fs;
195
196         if (!(ctx->options & E2F_OPT_PREEN))
197                 return;
198         fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
199                 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
200                ctx->device_name);
201         if (fs != NULL) {
202                 fs->super->s_state |= EXT2_ERROR_FS;
203                 ext2fs_mark_super_dirty(fs);
204                 ext2fs_close(fs);
205         }
206         exit(FSCK_UNCORRECTED);
207 }
208
209 #ifdef RESOURCE_TRACK
210 void init_resource_track(struct resource_track *track)
211 {
212 #ifdef HAVE_GETRUSAGE
213         struct rusage r;
214 #endif
215         
216         track->brk_start = sbrk(0);
217         gettimeofday(&track->time_start, 0);
218 #ifdef HAVE_GETRUSAGE
219 #ifdef sun
220         memset(&r, 0, sizeof(struct rusage));
221 #endif
222         getrusage(RUSAGE_SELF, &r);
223         track->user_start = r.ru_utime;
224         track->system_start = r.ru_stime;
225 #else
226         track->user_start.tv_sec = track->user_start.tv_usec = 0;
227         track->system_start.tv_sec = track->system_start.tv_usec = 0;
228 #endif
229 }
230
231 #ifdef __GNUC__
232 #define _INLINE_ __inline__
233 #else
234 #define _INLINE_
235 #endif
236
237 static _INLINE_ float timeval_subtract(struct timeval *tv1,
238                                        struct timeval *tv2)
239 {
240         return ((tv1->tv_sec - tv2->tv_sec) +
241                 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
242 }
243
244 void print_resource_track(const char *desc, struct resource_track *track)
245 {
246 #ifdef HAVE_GETRUSAGE
247         struct rusage r;
248 #endif
249 #ifdef HAVE_MALLINFO
250         struct mallinfo malloc_info;
251 #endif
252         struct timeval time_end;
253
254         gettimeofday(&time_end, 0);
255
256         if (desc)
257                 printf("%s: ", desc);
258
259 #ifdef HAVE_MALLINFO
260 #define kbytes(x)       (((x) + 1023) / 1024)
261         
262         malloc_info = mallinfo();
263         printf(_("Memory used: %dk/%dk (%dk/%dk), "),
264                kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
265                kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
266 #else
267         printf(_("Memory used: %d, "),
268                (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
269 #endif  
270 #ifdef HAVE_GETRUSAGE
271         getrusage(RUSAGE_SELF, &r);
272
273         printf(_("time: %5.2f/%5.2f/%5.2f\n"),
274                timeval_subtract(&time_end, &track->time_start),
275                timeval_subtract(&r.ru_utime, &track->user_start),
276                timeval_subtract(&r.ru_stime, &track->system_start));
277 #else
278         printf(_("elapsed time: %6.3f\n"),
279                timeval_subtract(&time_end, &track->time_start));
280 #endif
281 }
282 #endif /* RESOURCE_TRACK */
283
284 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
285                               struct ext2_inode * inode, const char *proc)
286 {
287         int retval;
288
289         retval = ext2fs_read_inode(ctx->fs, ino, inode);
290         if (retval) {
291                 com_err("ext2fs_read_inode", retval,
292                         _("while reading inode %ld in %s"), ino, proc);
293                 fatal_error(ctx, 0);
294         }
295 }
296
297 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
298                                struct ext2_inode * inode, const char *proc)
299 {
300         int retval;
301
302         retval = ext2fs_write_inode(ctx->fs, ino, inode);
303         if (retval) {
304                 com_err("ext2fs_write_inode", retval,
305                         _("while writing inode %ld in %s"), ino, proc);
306                 fatal_error(ctx, 0);
307         }
308 }
309
310 #ifdef MTRACE
311 void mtrace_print(char *mesg)
312 {
313         FILE    *malloc_get_mallstream();
314         FILE    *f = malloc_get_mallstream();
315
316         if (f)
317                 fprintf(f, "============= %s\n", mesg);
318 }
319 #endif
320
321 blk_t get_backup_sb(ext2_filsys fs)
322 {
323         if (!fs || !fs->super)
324                 return 8193;
325         return fs->super->s_blocks_per_group + fs->super->s_first_data_block;
326 }
327
328 /*
329  * Given a mode, return the ext2 file type
330  */
331 int ext2_file_type(unsigned int mode)
332 {
333         if (LINUX_S_ISREG(mode))
334                 return EXT2_FT_REG_FILE;
335
336         if (LINUX_S_ISDIR(mode))
337                 return EXT2_FT_DIR;
338         
339         if (LINUX_S_ISCHR(mode))
340                 return EXT2_FT_CHRDEV;
341         
342         if (LINUX_S_ISBLK(mode))
343                 return EXT2_FT_BLKDEV;
344         
345         if (LINUX_S_ISLNK(mode))
346                 return EXT2_FT_SYMLINK;
347
348         if (LINUX_S_ISFIFO(mode))
349                 return EXT2_FT_FIFO;
350         
351         if (LINUX_S_ISSOCK(mode))
352                 return EXT2_FT_SOCK;
353         
354         return 0;
355 }