int main (int argc, char ** argv) { errcode_t retval; ext2_filsys fs; int print_badblocks = 0; blk64_t use_superblock = 0; int use_blocksize = 0; int image_dump = 0; int force = 0; int flags; int header_only = 0; int c; #ifdef ENABLE_NLS setlocale(LC_MESSAGES, ""); setlocale(LC_CTYPE, ""); bindtextdomain(NLS_CAT_NAME, LOCALEDIR); textdomain(NLS_CAT_NAME); set_com_err_gettext(gettext); #endif add_error_table(&et_ext2_error_table); fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); if (argc && *argv) program_name = *argv; while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) { switch (c) { case 'b': print_badblocks++; break; case 'f': force++; break; case 'h': header_only++; break; case 'i': image_dump++; break; case 'o': parse_extended_opts(optarg, &use_superblock, &use_blocksize); break; case 'V': /* Print version number and exit */ fprintf(stderr, _("\tUsing %s\n"), error_message(EXT2_ET_BASE)); exit(0); case 'x': hex_format++; break; default: usage(); } } if (optind > argc - 1) usage(); device_name = argv[optind++]; flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; if (force) flags |= EXT2_FLAG_FORCE; if (image_dump) flags |= EXT2_FLAG_IMAGE_FILE; if (use_superblock && !use_blocksize) { for (use_blocksize = EXT2_MIN_BLOCK_SIZE; use_blocksize <= EXT2_MAX_BLOCK_SIZE; use_blocksize *= 2) { retval = ext2fs_open (device_name, flags, use_superblock, use_blocksize, unix_io_manager, &fs); if (!retval) break; } } else retval = ext2fs_open (device_name, flags, use_superblock, use_blocksize, unix_io_manager, &fs); if (retval) { com_err (program_name, retval, _("while trying to open %s"), device_name); printf("%s", _("Couldn't find valid filesystem superblock.\n")); exit (1); } fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) blocks64 = 1; if (print_badblocks) { list_bad_blocks(fs, 1); } else { list_super (fs->super); if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { print_journal_information(fs); ext2fs_close(fs); exit(0); } if ((fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) && (fs->super->s_journal_inum != 0)) print_inline_journal_information(fs); list_bad_blocks(fs, 0); if (header_only) { ext2fs_close (fs); exit (0); } retval = ext2fs_read_bitmaps (fs); list_desc (fs); if (retval) { printf(_("\n%s: %s: error reading bitmaps: %s\n"), program_name, device_name, error_message(retval)); } } ext2fs_close (fs); remove_error_table(&et_ext2_error_table); exit (0); }
static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx) { int flush = 0; int c, fd; #ifdef MTRACE extern void *mallwatch; #endif e2fsck_t ctx; errcode_t retval; #ifdef HAVE_SIGNAL_H struct sigaction sa; #endif char *extended_opts = 0; char *cp; int res; /* result of sscanf */ #ifdef CONFIG_JBD_DEBUG char *jbd_debug; #endif retval = e2fsck_allocate_context(&ctx); if (retval) return retval; *ret_ctx = ctx; setvbuf(stdout, NULL, _IONBF, BUFSIZ); setvbuf(stderr, NULL, _IONBF, BUFSIZ); if (isatty(0) && isatty(1)) { ctx->interactive = 1; } else { ctx->start_meta[0] = '\001'; ctx->stop_meta[0] = '\002'; } memset(bar, '=', sizeof(bar)-1); memset(spaces, ' ', sizeof(spaces)-1); add_error_table(&et_ext2_error_table); add_error_table(&et_prof_error_table); blkid_get_cache(&ctx->blkid, NULL); if (argc && *argv) ctx->program_name = *argv; else ctx->program_name = "e2fsck"; while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF) switch (c) { case 'C': ctx->progress = e2fsck_update_progress; res = sscanf(optarg, "%d", &ctx->progress_fd); if (res != 1) goto sscanf_err; if (ctx->progress_fd < 0) { ctx->progress = 0; ctx->progress_fd = ctx->progress_fd * -1; } if (!ctx->progress_fd) break; /* Validate the file descriptor to avoid disasters */ fd = dup(ctx->progress_fd); if (fd < 0) { fprintf(stderr, _("Error validating file descriptor %d: %s\n"), ctx->progress_fd, error_message(errno)); fatal_error(ctx, _("Invalid completion information file descriptor")); } else close(fd); break; case 'D': ctx->options |= E2F_OPT_COMPRESS_DIRS; break; case 'E': extended_opts = optarg; break; case 'p': case 'a': if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) { conflict_opt: fatal_error(ctx, _("Only one of the options -p/-a, -n or -y may be specified.")); } ctx->options |= E2F_OPT_PREEN; break; case 'n': if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN)) goto conflict_opt; ctx->options |= E2F_OPT_NO; break; case 'y': if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO)) goto conflict_opt; ctx->options |= E2F_OPT_YES; break; case 't': #ifdef RESOURCE_TRACK if (ctx->options & E2F_OPT_TIME) ctx->options |= E2F_OPT_TIME2; else ctx->options |= E2F_OPT_TIME; #else fprintf(stderr, _("The -t option is not " "supported on this version of e2fsck.\n")); #endif break; case 'c': if (cflag++) ctx->options |= E2F_OPT_WRITECHECK; ctx->options |= E2F_OPT_CHECKBLOCKS; break; case 'r': /* What we do by default, anyway! */ break; case 'b': res = sscanf(optarg, "%u", &ctx->use_superblock); if (res != 1) goto sscanf_err; ctx->flags |= E2F_FLAG_SB_SPECIFIED; break; case 'B': ctx->blocksize = atoi(optarg); break; case 'I': res = sscanf(optarg, "%d", &ctx->inode_buffer_blocks); if (res != 1) goto sscanf_err; break; case 'j': ctx->journal_name = blkid_get_devname(ctx->blkid, optarg, NULL); if (!ctx->journal_name) { com_err(ctx->program_name, 0, _("Unable to resolve '%s'"), optarg); fatal_error(ctx, 0); } break; case 'P': res = sscanf(optarg, "%d", &ctx->process_inode_size); if (res != 1) goto sscanf_err; break; case 'L': replace_bad_blocks++; case 'l': bad_blocks_file = string_copy(ctx, optarg, 0); break; case 'd': ctx->options |= E2F_OPT_DEBUG; break; case 'f': ctx->options |= E2F_OPT_FORCE; break; case 'F': flush = 1; break; case 'v': verbose = 1; break; case 'V': show_version_only = 1; break; #ifdef MTRACE case 'M': mallwatch = (void *) strtol(optarg, NULL, 0); break; #endif case 'N': ctx->device_name = string_copy(ctx, optarg, 0); break; case 'k': keep_bad_blocks++; break; default: usage(ctx); } if (show_version_only) return 0; if (optind != argc - 1) usage(ctx); if ((ctx->options & E2F_OPT_NO) && (ctx->options & E2F_OPT_COMPRESS_DIRS)) { com_err(ctx->program_name, 0, _("The -n and -D options are incompatible.")); fatal_error(ctx, 0); } if ((ctx->options & E2F_OPT_NO) && cflag) { com_err(ctx->program_name, 0, _("The -n and -c options are incompatible.")); fatal_error(ctx, 0); } if ((ctx->options & E2F_OPT_NO) && bad_blocks_file) { com_err(ctx->program_name, 0, _("The -n and -l/-L options are incompatible.")); fatal_error(ctx, 0); } if (ctx->options & E2F_OPT_NO) ctx->options |= E2F_OPT_READONLY; ctx->io_options = strchr(argv[optind], '?'); if (ctx->io_options) *ctx->io_options++ = 0; ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0); if (!ctx->filesystem_name) { com_err(ctx->program_name, 0, _("Unable to resolve '%s'"), argv[optind]); fatal_error(ctx, 0); } if (extended_opts) parse_extended_opts(ctx, extended_opts); if ((cp = getenv("E2FSCK_CONFIG")) != NULL) config_fn[0] = cp; profile_set_syntax_err_cb(syntax_err_report); profile_init(config_fn, &ctx->profile); if (flush) { fd = open(ctx->filesystem_name, O_RDONLY, 0); if (fd < 0) { com_err("open", errno, _("while opening %s for flushing"), ctx->filesystem_name); fatal_error(ctx, 0); } if ((retval = ext2fs_sync_device(fd, 1))) { com_err("ext2fs_sync_device", retval, _("while trying to flush %s"), ctx->filesystem_name); fatal_error(ctx, 0); } close(fd); } if (cflag && bad_blocks_file) { fprintf(stderr, _("The -c and the -l/-L options may " "not be both used at the same time.\n")); exit(FSCK_USAGE); } #ifdef HAVE_SIGNAL_H /* * Set up signal action */ memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = signal_cancel; sigaction(SIGINT, &sa, 0); sigaction(SIGTERM, &sa, 0); #ifdef SA_RESTART sa.sa_flags = SA_RESTART; #endif e2fsck_global_ctx = ctx; sa.sa_handler = signal_progress_on; sigaction(SIGUSR1, &sa, 0); sa.sa_handler = signal_progress_off; sigaction(SIGUSR2, &sa, 0); #endif /* Update our PATH to include /sbin if we need to run badblocks */ if (cflag) { char *oldpath = getenv("PATH"); char *newpath; int len = sizeof(PATH_SET) + 1; if (oldpath) len += strlen(oldpath); newpath = malloc(len); if (!newpath) fatal_error(ctx, "Couldn't malloc() newpath"); strcpy(newpath, PATH_SET); if (oldpath) { strcat(newpath, ":"); strcat(newpath, oldpath); } putenv(newpath); } #ifdef CONFIG_JBD_DEBUG jbd_debug = getenv("E2FSCK_JBD_DEBUG"); if (jbd_debug) { res = sscanf(jbd_debug, "%d", &journal_enable_debug); if (res != 1) { fprintf(stderr, _("E2FSCK_JBD_DEBUG \"%s\" not an integer\n\n"), jbd_debug); exit (1); } } #endif return 0; sscanf_err: fprintf(stderr, _("\nInvalid non-numeric argument to -%c (\"%s\")\n\n"), c, optarg); exit (1); }