]>
Commit | Line | Data |
---|---|---|
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 "config.h" | |
13 | #include <stdlib.h> | |
14 | #include <stdio.h> | |
15 | #include <unistd.h> | |
16 | #include <string.h> | |
17 | #include <ctype.h> | |
18 | #ifdef __linux__ | |
19 | #include <sys/utsname.h> | |
20 | #endif | |
21 | ||
22 | #ifdef HAVE_CONIO_H | |
23 | #undef HAVE_TERMIOS_H | |
24 | #include <conio.h> | |
25 | #define read_a_char() getch() | |
26 | #else | |
27 | #ifdef HAVE_TERMIOS_H | |
28 | #include <termios.h> | |
29 | #endif | |
30 | #endif | |
31 | ||
32 | #ifdef HAVE_MALLOC_H | |
33 | #include <malloc.h> | |
34 | #endif | |
35 | ||
36 | #ifdef HAVE_ERRNO_H | |
37 | #include <errno.h> | |
38 | #endif | |
39 | ||
40 | #include "e2fsck.h" | |
41 | ||
42 | extern e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */ | |
43 | ||
44 | #include <sys/time.h> | |
45 | #include <sys/resource.h> | |
46 | ||
47 | void fatal_error(e2fsck_t ctx, const char *msg) | |
48 | { | |
49 | if (msg) | |
50 | fprintf (stderr, "e2fsck: %s\n", msg); | |
51 | if (ctx->fs && ctx->fs->io) { | |
52 | if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL) | |
53 | io_channel_flush(ctx->fs->io); | |
54 | else | |
55 | fprintf(stderr, "e2fsck: io manager magic bad!\n"); | |
56 | } | |
57 | ctx->flags |= E2F_FLAG_ABORT; | |
58 | if (ctx->flags & E2F_FLAG_SETJMP_OK) | |
59 | longjmp(ctx->abort_loc, 1); | |
60 | exit(FSCK_ERROR); | |
61 | } | |
62 | ||
63 | void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size, | |
64 | const char *description) | |
65 | { | |
66 | void *ret; | |
67 | char buf[256]; | |
68 | ||
69 | #ifdef DEBUG_ALLOCATE_MEMORY | |
70 | printf("Allocating %u bytes for %s...\n", size, description); | |
71 | #endif | |
72 | ret = malloc(size); | |
73 | if (!ret) { | |
74 | sprintf(buf, "Can't allocate %s\n", description); | |
75 | fatal_error(ctx, buf); | |
76 | } | |
77 | memset(ret, 0, size); | |
78 | return ret; | |
79 | } | |
80 | ||
81 | char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)), | |
82 | const char *str, int len) | |
83 | { | |
84 | char *ret; | |
85 | ||
86 | if (!str) | |
87 | return NULL; | |
88 | if (!len) | |
89 | len = strlen(str); | |
90 | ret = malloc(len+1); | |
91 | if (ret) { | |
92 | strncpy(ret, str, len); | |
93 | ret[len] = 0; | |
94 | } | |
95 | return ret; | |
96 | } | |
97 | ||
98 | #ifndef HAVE_STRNLEN | |
99 | /* | |
100 | * Incredibly, libc5 doesn't appear to have strnlen. So we have to | |
101 | * provide our own. | |
102 | */ | |
103 | int e2fsck_strnlen(const char * s, int count) | |
104 | { | |
105 | const char *cp = s; | |
106 | ||
107 | while (count-- && *cp) | |
108 | cp++; | |
109 | return cp - s; | |
110 | } | |
111 | #endif | |
112 | ||
113 | #ifndef HAVE_CONIO_H | |
114 | static int read_a_char(void) | |
115 | { | |
116 | char c; | |
117 | int r; | |
118 | int fail = 0; | |
119 | ||
120 | while(1) { | |
121 | if (e2fsck_global_ctx && | |
122 | (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) { | |
123 | return 3; | |
124 | } | |
125 | r = read(0, &c, 1); | |
126 | if (r == 1) | |
127 | return c; | |
128 | if (fail++ > 100) | |
129 | break; | |
130 | } | |
131 | return EOF; | |
132 | } | |
133 | #endif | |
134 | ||
135 | int ask_yn(const char * string, int def) | |
136 | { | |
137 | int c; | |
138 | const char *defstr; | |
139 | const char *short_yes = _("yY"); | |
140 | const char *short_no = _("nN"); | |
141 | ||
142 | #ifdef HAVE_TERMIOS_H | |
143 | struct termios termios, tmp; | |
144 | ||
145 | tcgetattr (0, &termios); | |
146 | tmp = termios; | |
147 | tmp.c_lflag &= ~(ICANON | ECHO); | |
148 | tmp.c_cc[VMIN] = 1; | |
149 | tmp.c_cc[VTIME] = 0; | |
150 | tcsetattr (0, TCSANOW, &tmp); | |
151 | #endif | |
152 | ||
153 | if (def == 1) | |
154 | defstr = _(_("<y>")); | |
155 | else if (def == 0) | |
156 | defstr = _(_("<n>")); | |
157 | else | |
158 | defstr = _(" (y/n)"); | |
159 | printf("%s%s? ", string, defstr); | |
160 | while (1) { | |
161 | fflush (stdout); | |
162 | if ((c = read_a_char()) == EOF) | |
163 | break; | |
164 | if (c == 3) { | |
165 | #ifdef HAVE_TERMIOS_H | |
166 | tcsetattr (0, TCSANOW, &termios); | |
167 | #endif | |
168 | if (e2fsck_global_ctx && | |
169 | e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) { | |
170 | puts("\n"); | |
171 | longjmp(e2fsck_global_ctx->abort_loc, 1); | |
172 | } | |
173 | puts(_("cancelled!\n")); | |
174 | return 0; | |
175 | } | |
176 | if (strchr(short_yes, (char) c)) { | |
177 | def = 1; | |
178 | break; | |
179 | } | |
180 | else if (strchr(short_no, (char) c)) { | |
181 | def = 0; | |
182 | break; | |
183 | } | |
184 | else if ((c == 27 || c == ' ' || c == '\n') && (def != -1)) | |
185 | break; | |
186 | } | |
187 | if (def) | |
188 | puts(_("yes\n")); | |
189 | else | |
190 | puts (_("no\n")); | |
191 | #ifdef HAVE_TERMIOS_H | |
192 | tcsetattr (0, TCSANOW, &termios); | |
193 | #endif | |
194 | return def; | |
195 | } | |
196 | ||
197 | int ask (e2fsck_t ctx, const char * string, int def) | |
198 | { | |
199 | if (ctx->options & E2F_OPT_NO) { | |
200 | printf (_("%s? no\n\n"), string); | |
201 | return 0; | |
202 | } | |
203 | if (ctx->options & E2F_OPT_YES) { | |
204 | printf (_("%s? yes\n\n"), string); | |
205 | return 1; | |
206 | } | |
207 | if (ctx->options & E2F_OPT_PREEN) { | |
208 | printf ("%s? %s\n\n", string, def ? _("yes") : _("no")); | |
209 | return def; | |
210 | } | |
211 | return ask_yn(string, def); | |
212 | } | |
213 | ||
214 | void e2fsck_read_bitmaps(e2fsck_t ctx) | |
215 | { | |
216 | ext2_filsys fs = ctx->fs; | |
217 | errcode_t retval; | |
218 | const char *old_op; | |
219 | ||
220 | if (ctx->invalid_bitmaps) { | |
221 | com_err(ctx->program_name, 0, | |
222 | _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"), | |
223 | ctx->device_name); | |
224 | fatal_error(ctx, 0); | |
225 | } | |
226 | ||
227 | old_op = ehandler_operation(_("reading inode and block bitmaps")); | |
228 | retval = ext2fs_read_bitmaps(fs); | |
229 | ehandler_operation(old_op); | |
230 | if (retval) { | |
231 | com_err(ctx->program_name, retval, | |
232 | _("while retrying to read bitmaps for %s"), | |
233 | ctx->device_name); | |
234 | fatal_error(ctx, 0); | |
235 | } | |
236 | } | |
237 | ||
238 | void e2fsck_write_bitmaps(e2fsck_t ctx) | |
239 | { | |
240 | ext2_filsys fs = ctx->fs; | |
241 | errcode_t retval; | |
242 | const char *old_op; | |
243 | ||
244 | old_op = ehandler_operation(_("writing block and inode bitmaps")); | |
245 | retval = ext2fs_write_bitmaps(fs); | |
246 | ehandler_operation(old_op); | |
247 | if (retval) { | |
248 | com_err(ctx->program_name, retval, | |
249 | _("while rewriting block and inode bitmaps for %s"), | |
250 | ctx->device_name); | |
251 | fatal_error(ctx, 0); | |
252 | } | |
253 | } | |
254 | ||
255 | void preenhalt(e2fsck_t ctx) | |
256 | { | |
257 | ext2_filsys fs = ctx->fs; | |
258 | ||
259 | if (!(ctx->options & E2F_OPT_PREEN)) | |
260 | return; | |
261 | fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; " | |
262 | "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"), | |
263 | ctx->device_name); | |
264 | ctx->flags |= E2F_FLAG_EXITING; | |
265 | if (fs != NULL) { | |
266 | fs->super->s_state |= EXT2_ERROR_FS; | |
267 | ext2fs_mark_super_dirty(fs); | |
268 | ext2fs_close(fs); | |
269 | } | |
270 | exit(FSCK_UNCORRECTED); | |
271 | } | |
272 | ||
273 | #ifdef RESOURCE_TRACK | |
274 | void init_resource_track(struct resource_track *track, io_channel channel) | |
275 | { | |
276 | #ifdef HAVE_GETRUSAGE | |
277 | struct rusage r; | |
278 | #endif | |
279 | io_stats io_start = 0; | |
280 | ||
281 | track->brk_start = sbrk(0); | |
282 | gettimeofday(&track->time_start, 0); | |
283 | #ifdef HAVE_GETRUSAGE | |
284 | #ifdef sun | |
285 | memset(&r, 0, sizeof(struct rusage)); | |
286 | #endif | |
287 | getrusage(RUSAGE_SELF, &r); | |
288 | track->user_start = r.ru_utime; | |
289 | track->system_start = r.ru_stime; | |
290 | #else | |
291 | track->user_start.tv_sec = track->user_start.tv_usec = 0; | |
292 | track->system_start.tv_sec = track->system_start.tv_usec = 0; | |
293 | #endif | |
294 | track->bytes_read = 0; | |
295 | track->bytes_written = 0; | |
296 | if (channel && channel->manager && channel->manager->get_stats) | |
297 | channel->manager->get_stats(channel, &io_start); | |
298 | if (io_start) { | |
299 | track->bytes_read = io_start->bytes_read; | |
300 | track->bytes_written = io_start->bytes_written; | |
301 | } | |
302 | } | |
303 | ||
304 | #ifdef __GNUC__ | |
305 | #define _INLINE_ __inline__ | |
306 | #else | |
307 | #define _INLINE_ | |
308 | #endif | |
309 | ||
310 | static _INLINE_ float timeval_subtract(struct timeval *tv1, | |
311 | struct timeval *tv2) | |
312 | { | |
313 | return ((tv1->tv_sec - tv2->tv_sec) + | |
314 | ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000); | |
315 | } | |
316 | ||
317 | void print_resource_track(e2fsck_t ctx, const char *desc, | |
318 | struct resource_track *track, io_channel channel) | |
319 | { | |
320 | #ifdef HAVE_GETRUSAGE | |
321 | struct rusage r; | |
322 | #endif | |
323 | #ifdef HAVE_MALLINFO | |
324 | struct mallinfo malloc_info; | |
325 | #endif | |
326 | struct timeval time_end; | |
327 | ||
328 | if ((desc && !(ctx->options & E2F_OPT_TIME2)) || | |
329 | (!desc && !(ctx->options & E2F_OPT_TIME))) | |
330 | return; | |
331 | ||
332 | e2fsck_clear_progbar(ctx); | |
333 | gettimeofday(&time_end, 0); | |
334 | ||
335 | if (desc) | |
336 | printf("%s: ", desc); | |
337 | ||
338 | #ifdef HAVE_MALLINFO | |
339 | #define kbytes(x) (((unsigned long)(x) + 1023) / 1024) | |
340 | ||
341 | malloc_info = mallinfo(); | |
342 | printf(_("Memory used: %luk/%luk (%luk/%luk), "), | |
343 | kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd), | |
344 | kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks)); | |
345 | #else | |
346 | printf(_("Memory used: %lu, "), | |
347 | (unsigned long) (((char *) sbrk(0)) - | |
348 | ((char *) track->brk_start))); | |
349 | #endif | |
350 | #ifdef HAVE_GETRUSAGE | |
351 | getrusage(RUSAGE_SELF, &r); | |
352 | ||
353 | printf(_("time: %5.2f/%5.2f/%5.2f\n"), | |
354 | timeval_subtract(&time_end, &track->time_start), | |
355 | timeval_subtract(&r.ru_utime, &track->user_start), | |
356 | timeval_subtract(&r.ru_stime, &track->system_start)); | |
357 | #else | |
358 | printf(_("elapsed time: %6.3f\n"), | |
359 | timeval_subtract(&time_end, &track->time_start)); | |
360 | #endif | |
361 | #define mbytes(x) (((x) + 1048575) / 1048576) | |
362 | if (channel && channel->manager && channel->manager->get_stats) { | |
363 | io_stats delta = 0; | |
364 | unsigned long long bytes_read = 0; | |
365 | unsigned long long bytes_written = 0; | |
366 | ||
367 | if (desc) | |
368 | printf("%s: ", desc); | |
369 | ||
370 | channel->manager->get_stats(channel, &delta); | |
371 | if (delta) { | |
372 | bytes_read = delta->bytes_read - track->bytes_read; | |
373 | bytes_written = delta->bytes_written - | |
374 | track->bytes_written; | |
375 | } | |
376 | printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n", | |
377 | mbytes(bytes_read), mbytes(bytes_written), | |
378 | (double)mbytes(bytes_read + bytes_written) / | |
379 | timeval_subtract(&time_end, &track->time_start)); | |
380 | } | |
381 | } | |
382 | #endif /* RESOURCE_TRACK */ | |
383 | ||
384 | void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino, | |
385 | struct ext2_inode * inode, const char *proc) | |
386 | { | |
387 | int retval; | |
388 | ||
389 | retval = ext2fs_read_inode(ctx->fs, ino, inode); | |
390 | if (retval) { | |
391 | com_err("ext2fs_read_inode", retval, | |
392 | _("while reading inode %lu in %s"), ino, proc); | |
393 | fatal_error(ctx, 0); | |
394 | } | |
395 | } | |
396 | ||
397 | void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino, | |
398 | struct ext2_inode *inode, int bufsize, | |
399 | const char *proc) | |
400 | { | |
401 | int retval; | |
402 | ||
403 | retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize); | |
404 | if (retval) { | |
405 | com_err("ext2fs_read_inode_full", retval, | |
406 | _("while reading inode %lu in %s"), ino, proc); | |
407 | fatal_error(ctx, 0); | |
408 | } | |
409 | } | |
410 | ||
411 | extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino, | |
412 | struct ext2_inode * inode, int bufsize, | |
413 | const char *proc) | |
414 | { | |
415 | int retval; | |
416 | ||
417 | retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize); | |
418 | if (retval) { | |
419 | com_err("ext2fs_write_inode", retval, | |
420 | _("while writing inode %lu in %s"), ino, proc); | |
421 | fatal_error(ctx, 0); | |
422 | } | |
423 | } | |
424 | ||
425 | extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino, | |
426 | struct ext2_inode * inode, const char *proc) | |
427 | { | |
428 | int retval; | |
429 | ||
430 | retval = ext2fs_write_inode(ctx->fs, ino, inode); | |
431 | if (retval) { | |
432 | com_err("ext2fs_write_inode", retval, | |
433 | _("while writing inode %lu in %s"), ino, proc); | |
434 | fatal_error(ctx, 0); | |
435 | } | |
436 | } | |
437 | ||
438 | #ifdef MTRACE | |
439 | void mtrace_print(char *mesg) | |
440 | { | |
441 | FILE *malloc_get_mallstream(); | |
442 | FILE *f = malloc_get_mallstream(); | |
443 | ||
444 | if (f) | |
445 | fprintf(f, "============= %s\n", mesg); | |
446 | } | |
447 | #endif | |
448 | ||
449 | blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name, | |
450 | io_manager manager) | |
451 | { | |
452 | struct ext2_super_block *sb; | |
453 | io_channel io = NULL; | |
454 | void *buf = NULL; | |
455 | int blocksize; | |
456 | blk_t superblock, ret_sb = 8193; | |
457 | ||
458 | if (fs && fs->super) { | |
459 | ret_sb = (fs->super->s_blocks_per_group + | |
460 | fs->super->s_first_data_block); | |
461 | if (ctx) { | |
462 | ctx->superblock = ret_sb; | |
463 | ctx->blocksize = fs->blocksize; | |
464 | } | |
465 | return ret_sb; | |
466 | } | |
467 | ||
468 | if (ctx) { | |
469 | if (ctx->blocksize) { | |
470 | ret_sb = ctx->blocksize * 8; | |
471 | if (ctx->blocksize == 1024) | |
472 | ret_sb++; | |
473 | ctx->superblock = ret_sb; | |
474 | return ret_sb; | |
475 | } | |
476 | ctx->superblock = ret_sb; | |
477 | ctx->blocksize = 1024; | |
478 | } | |
479 | ||
480 | if (!name || !manager) | |
481 | goto cleanup; | |
482 | ||
483 | if (manager->open(name, 0, &io) != 0) | |
484 | goto cleanup; | |
485 | ||
486 | if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf)) | |
487 | goto cleanup; | |
488 | sb = (struct ext2_super_block *) buf; | |
489 | ||
490 | for (blocksize = EXT2_MIN_BLOCK_SIZE; | |
491 | blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) { | |
492 | superblock = blocksize*8; | |
493 | if (blocksize == 1024) | |
494 | superblock++; | |
495 | io_channel_set_blksize(io, blocksize); | |
496 | if (io_channel_read_blk64(io, superblock, | |
497 | -SUPERBLOCK_SIZE, buf)) | |
498 | continue; | |
499 | #ifdef WORDS_BIGENDIAN | |
500 | if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) | |
501 | ext2fs_swap_super(sb); | |
502 | #endif | |
503 | if ((sb->s_magic == EXT2_SUPER_MAGIC) && | |
504 | (EXT2_BLOCK_SIZE(sb) == blocksize)) { | |
505 | ret_sb = superblock; | |
506 | if (ctx) { | |
507 | ctx->superblock = superblock; | |
508 | ctx->blocksize = blocksize; | |
509 | } | |
510 | break; | |
511 | } | |
512 | } | |
513 | ||
514 | cleanup: | |
515 | if (io) | |
516 | io_channel_close(io); | |
517 | if (buf) | |
518 | ext2fs_free_mem(&buf); | |
519 | return (ret_sb); | |
520 | } | |
521 | ||
522 | /* | |
523 | * Given a mode, return the ext2 file type | |
524 | */ | |
525 | int ext2_file_type(unsigned int mode) | |
526 | { | |
527 | if (LINUX_S_ISREG(mode)) | |
528 | return EXT2_FT_REG_FILE; | |
529 | ||
530 | if (LINUX_S_ISDIR(mode)) | |
531 | return EXT2_FT_DIR; | |
532 | ||
533 | if (LINUX_S_ISCHR(mode)) | |
534 | return EXT2_FT_CHRDEV; | |
535 | ||
536 | if (LINUX_S_ISBLK(mode)) | |
537 | return EXT2_FT_BLKDEV; | |
538 | ||
539 | if (LINUX_S_ISLNK(mode)) | |
540 | return EXT2_FT_SYMLINK; | |
541 | ||
542 | if (LINUX_S_ISFIFO(mode)) | |
543 | return EXT2_FT_FIFO; | |
544 | ||
545 | if (LINUX_S_ISSOCK(mode)) | |
546 | return EXT2_FT_SOCK; | |
547 | ||
548 | return 0; | |
549 | } | |
550 | ||
551 | #define STRIDE_LENGTH 8 | |
552 | /* | |
553 | * Helper function which zeros out _num_ blocks starting at _blk_. In | |
554 | * case of an error, the details of the error is returned via _ret_blk_ | |
555 | * and _ret_count_ if they are non-NULL pointers. Returns 0 on | |
556 | * success, and an error code on an error. | |
557 | * | |
558 | * As a special case, if the first argument is NULL, then it will | |
559 | * attempt to free the static zeroizing buffer. (This is to keep | |
560 | * programs that check for memory leaks happy.) | |
561 | */ | |
562 | errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num, | |
563 | blk_t *ret_blk, int *ret_count) | |
564 | { | |
565 | int j, count, next_update, next_update_incr; | |
566 | static char *buf; | |
567 | errcode_t retval; | |
568 | ||
569 | /* If fs is null, clean up the static buffer and return */ | |
570 | if (!fs) { | |
571 | if (buf) { | |
572 | free(buf); | |
573 | buf = 0; | |
574 | } | |
575 | return 0; | |
576 | } | |
577 | /* Allocate the zeroizing buffer if necessary */ | |
578 | if (!buf) { | |
579 | buf = malloc(fs->blocksize * STRIDE_LENGTH); | |
580 | if (!buf) { | |
581 | com_err("malloc", ENOMEM, | |
582 | _("while allocating zeroizing buffer")); | |
583 | exit(1); | |
584 | } | |
585 | memset(buf, 0, fs->blocksize * STRIDE_LENGTH); | |
586 | } | |
587 | /* OK, do the write loop */ | |
588 | next_update = 0; | |
589 | next_update_incr = num / 100; | |
590 | if (next_update_incr < 1) | |
591 | next_update_incr = 1; | |
592 | for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) { | |
593 | count = num - j; | |
594 | if (count > STRIDE_LENGTH) | |
595 | count = STRIDE_LENGTH; | |
596 | retval = io_channel_write_blk64(fs->io, blk, count, buf); | |
597 | if (retval) { | |
598 | if (ret_count) | |
599 | *ret_count = count; | |
600 | if (ret_blk) | |
601 | *ret_blk = blk; | |
602 | return retval; | |
603 | } | |
604 | } | |
605 | return 0; | |
606 | } | |
607 | ||
608 | /* | |
609 | * Check to see if a filesystem is in /proc/filesystems. | |
610 | * Returns 1 if found, 0 if not | |
611 | */ | |
612 | int fs_proc_check(const char *fs_name) | |
613 | { | |
614 | FILE *f; | |
615 | char buf[80], *cp, *t; | |
616 | ||
617 | f = fopen("/proc/filesystems", "r"); | |
618 | if (!f) | |
619 | return (0); | |
620 | while (!feof(f)) { | |
621 | if (!fgets(buf, sizeof(buf), f)) | |
622 | break; | |
623 | cp = buf; | |
624 | if (!isspace(*cp)) { | |
625 | while (*cp && !isspace(*cp)) | |
626 | cp++; | |
627 | } | |
628 | while (*cp && isspace(*cp)) | |
629 | cp++; | |
630 | if ((t = strchr(cp, '\n')) != NULL) | |
631 | *t = 0; | |
632 | if ((t = strchr(cp, '\t')) != NULL) | |
633 | *t = 0; | |
634 | if ((t = strchr(cp, ' ')) != NULL) | |
635 | *t = 0; | |
636 | if (!strcmp(fs_name, cp)) { | |
637 | fclose(f); | |
638 | return (1); | |
639 | } | |
640 | } | |
641 | fclose(f); | |
642 | return (0); | |
643 | } | |
644 | ||
645 | /* | |
646 | * Check to see if a filesystem is available as a module | |
647 | * Returns 1 if found, 0 if not | |
648 | */ | |
649 | int check_for_modules(const char *fs_name) | |
650 | { | |
651 | #ifdef __linux__ | |
652 | struct utsname uts; | |
653 | FILE *f; | |
654 | char buf[1024], *cp, *t; | |
655 | int i; | |
656 | ||
657 | if (uname(&uts)) | |
658 | return (0); | |
659 | snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release); | |
660 | ||
661 | f = fopen(buf, "r"); | |
662 | if (!f) | |
663 | return (0); | |
664 | while (!feof(f)) { | |
665 | if (!fgets(buf, sizeof(buf), f)) | |
666 | break; | |
667 | if ((cp = strchr(buf, ':')) != NULL) | |
668 | *cp = 0; | |
669 | else | |
670 | continue; | |
671 | if ((cp = strrchr(buf, '/')) != NULL) | |
672 | cp++; | |
673 | else | |
674 | cp = buf; | |
675 | i = strlen(cp); | |
676 | if (i > 3) { | |
677 | t = cp + i - 3; | |
678 | if (!strcmp(t, ".ko")) | |
679 | *t = 0; | |
680 | } | |
681 | if (!strcmp(cp, fs_name)) { | |
682 | fclose(f); | |
683 | return (1); | |
684 | } | |
685 | } | |
686 | fclose(f); | |
687 | #endif /* __linux__ */ | |
688 | return (0); | |
689 | } | |
690 | ||
691 | /* | |
692 | * Helper function that does the right thing if write returns a | |
693 | * partial write, or an EGAIN/EINTR error. | |
694 | */ | |
695 | int write_all(int fd, char *buf, size_t count) | |
696 | { | |
697 | ssize_t ret; | |
698 | int c = 0; | |
699 | ||
700 | while (count > 0) { | |
701 | ret = write(fd, buf, count); | |
702 | if (ret < 0) { | |
703 | if ((errno == EAGAIN) || (errno == EINTR)) | |
704 | continue; | |
705 | return -1; | |
706 | } | |
707 | count -= ret; | |
708 | buf += ret; | |
709 | c += ret; | |
710 | } | |
711 | return c; | |
712 | } |