2 * util.c --- miscellaneous utilities
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
13 #include <sys/resource.h>
17 const char * fix_msg[2] = { "IGNORED", "FIXED" };
18 const char * clear_msg[2] = { "IGNORED", "CLEARED" };
20 void fatal_error (const char *msg)
23 fprintf (stderr, "%s: %s\n", program_name, msg);
27 void *allocate_memory(int size, const char *description)
32 #ifdef DEBUG_ALLOCATE_MEMORY
33 printf("Allocating %d bytes for %s...\n", size, description);
37 sprintf(buf, "%%s: Can't allocate %s\n", description);
45 int ask_yn(const char * string, int def)
48 struct termios termios, tmp;
51 tcgetattr (0, &termios);
53 tmp.c_lflag &= ~(ICANON | ECHO);
54 tcsetattr (0, TCSANOW, &tmp);
62 printf("%s%s? ", string, defstr);
65 if ((c = getchar()) == EOF)
76 else if ((c == ' ' || c == '\n') && (def != -1))
83 tcsetattr (0, TCSANOW, &termios);
87 int ask (const char * string, int def)
90 printf ("%s? no\n\n", string);
94 printf ("%s? yes\n\n", string);
98 printf ("%s? %s\n\n", string, def ? "yes" : "no");
101 return ask_yn(string, def);
104 void read_bitmaps(ext2_filsys fs)
108 if (invalid_bitmaps) {
109 com_err(program_name, 0,
110 "read_bitmaps: illegal bitmap block(s) for %s",
115 ehandler_operation("reading inode and block bitmaps");
116 retval = ext2fs_read_bitmaps(fs);
117 ehandler_operation(0);
119 com_err(program_name, retval,
120 "while retrying to read bitmaps for %s",
126 void write_bitmaps(ext2_filsys fs)
130 if (ext2fs_test_bb_dirty(fs)) {
131 ehandler_operation("writing block bitmaps");
132 retval = ext2fs_write_block_bitmap(fs);
133 ehandler_operation(0);
135 com_err(program_name, retval,
136 "while retrying to write block bitmaps for %s",
142 if (ext2fs_test_ib_dirty(fs)) {
143 ehandler_operation("writing inode bitmaps");
144 retval = ext2fs_write_inode_bitmap(fs);
145 ehandler_operation(0);
147 com_err(program_name, retval,
148 "while retrying to write inode bitmaps for %s",
155 void preenhalt(NOARGS)
159 fprintf(stderr, "\n\n%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
161 exit(FSCK_UNCORRECTED);
164 void init_resource_track(struct resource_track *track)
168 track->brk_start = sbrk(0);
169 gettimeofday(&track->time_start, 0);
170 getrusage(RUSAGE_SELF, &r);
171 track->user_start = r.ru_utime;
172 track->system_start = r.ru_stime;
175 static __inline__ float timeval_subtract(struct timeval *tv1,
178 return ((tv1->tv_sec - tv2->tv_sec) +
179 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
182 void print_resource_track(struct resource_track *track)
185 struct timeval time_end;
187 gettimeofday(&time_end, 0);
188 getrusage(RUSAGE_SELF, &r);
190 printf("Memory used: %d, elapsed time: %6.3f/%6.3f/%6.3f\n",
191 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
192 timeval_subtract(&time_end, &track->time_start),
193 timeval_subtract(&r.ru_utime, &track->user_start),
194 timeval_subtract(&r.ru_stime, &track->system_start));
197 void e2fsck_read_inode(ext2_filsys fs, unsigned long ino,
198 struct ext2_inode * inode, const char *proc)
202 retval = ext2fs_read_inode(fs, ino, inode);
204 com_err("ext2fs_read_inode", retval,
205 "while reading inode %ld in %s", ino, proc);
210 extern void e2fsck_write_inode(ext2_filsys fs, unsigned long ino,
211 struct ext2_inode * inode, const char *proc)
215 retval = ext2fs_write_inode(fs, ino, inode);
217 com_err("ext2fs_write_inode", retval,
218 "while writing inode %ld in %s", ino, proc);
224 * This function returns 1 if the inode's block entries actually
225 * contain block entries.
227 int inode_has_valid_blocks(struct ext2_inode *inode)
230 * Only directories, regular files, and some symbolic links
231 * have valid block entries.
233 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
234 !S_ISLNK(inode->i_mode))
238 * If the symbolic link is a "fast symlink", then the symlink
239 * target is stored in the block entries.
241 if (S_ISLNK (inode->i_mode) && inode->i_blocks == 0 &&
242 inode->i_size < EXT2_N_BLOCKS * sizeof (unsigned long))
249 void mtrace_print(char *mesg)
251 FILE *malloc_get_mallstream();
252 FILE *f = malloc_get_mallstream();
255 fprintf(f, "============= %s\n", mesg);