#include <unistd.h>
#include <setjmp.h>
#include <time.h>
+#include <limits.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include "nls-enable.h"
const char * program_name = "badblocks";
-const char * done_string = N_("done \n");
+const char * done_string = N_("done \n");
static int v_flag = 0; /* verbose */
static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
static int t_max = 0; /* allocated test patterns */
static unsigned long *t_patts = NULL; /* test patterns */
static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */
+static int exclusive_ok = 0;
#define T_INC 32
exit (1);
}
+static void exclusive_usage(void)
+{
+ fprintf(stderr,
+ _("%s: The -n and -w options are mutually exclusive.\n\n"),
+ program_name);
+ exit(1);
+}
+
static unsigned long currently_testing = 0;
static unsigned long num_blocks = 0;
static ext2_badblocks_list bb_list = NULL;
static void print_status(void)
{
- fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
- fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
+ fprintf(stderr, "%15ld/%15ld", currently_testing, num_blocks);
+ fputs("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", stderr);
fflush (stderr);
}
alarm(1);
if (!num_blocks)
return;
- fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
- fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
- fflush (stderr);
+ print_status();
}
static void *terminate_addr = NULL;
}
if (v_flag) {
fprintf (stderr, _("Checking blocks %lu to %lu\n"), from_count,
- last_block);
+ last_block - 1);
}
if (t_flag) {
fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
flush_bufs();
try = blocks_at_once;
currently_testing = from_count;
- num_blocks = last_block;
+ num_blocks = last_block - 1;
if (!t_flag && (s_flag || v_flag)) {
fputs(_("Checking for bad blocks (read-only test): "), stderr);
if (v_flag <= 1)
num_blocks = 0;
alarm(0);
if (s_flag || v_flag)
- fputs(done_string, stderr);
+ fputs(_(done_string), stderr);
fflush (stderr);
free (blkbuf);
for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
pattern_fill(buffer, pattern[pat_idx],
blocks_at_once * block_size);
- num_blocks = last_block;
+ num_blocks = last_block - 1;
currently_testing = from_count;
if (s_flag && v_flag <= 1)
alarm_intr(SIGALRM);
num_blocks = 0;
alarm (0);
if (s_flag | v_flag)
- fputs(done_string, stderr);
+ fputs(_(done_string), stderr);
flush_bufs();
if (s_flag | v_flag)
fputs(_("Reading and comparing: "), stderr);
num_blocks = 0;
alarm (0);
if (s_flag | v_flag)
- fputs(done_string, stderr);
+ fputs(_(done_string), stderr);
flush_bufs();
}
uncapture_terminate();
save_ptr = save_base;
test_ptr = test_base;
currently_testing = from_count;
- num_blocks = last_block;
+ num_blocks = last_block - 1;
if (s_flag && v_flag <= 1)
alarm_intr(SIGALRM);
num_blocks = 0;
alarm(0);
if (s_flag || v_flag > 1)
- fputs(done_string, stderr);
+ fputs(_(done_string), stderr);
flush_bufs();
}
device_name);
return;
}
- if (!(mount_flags & EXT2_MF_MOUNTED))
- return;
+ if (mount_flags & EXT2_MF_MOUNTED) {
+ fprintf(stderr, _("%s is mounted; "), device_name);
+ if (force) {
+ fputs(_("badblocks forced anyway. "
+ "Hope /etc/mtab is incorrect.\n"), stderr);
+ return;
+ }
+ abort_badblocks:
+ fputs(_("it's not safe to run badblocks!\n"), stderr);
+ exit(1);
+ }
- fprintf(stderr, _("%s is mounted; "), device_name);
- if (force) {
- fputs(_("badblocks forced anyway. "
- "Hope /etc/mtab is incorrect.\n"), stderr);
- return;
+ if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
+ fprintf(stderr, _("%s is apparently in use by the system; "),
+ device_name);
+ if (force)
+ fputs(_("badblocks forced anyway.\n"), stderr);
+ else
+ goto abort_badblocks;
}
- fputs(_("it's not safe to run badblocks!\n"), stderr);
- exit(1);
+
}
if (argc && *argv)
program_name = *argv;
- while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:")) != EOF) {
+ while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:X")) != EOF) {
switch (c) {
case 'b':
block_size = strtoul (optarg, &tmp, 0);
break;
case 'w':
if (w_flag)
- usage();
+ exclusive_usage();
test_func = test_rw;
w_flag = 1;
break;
case 'n':
if (w_flag)
- usage();
+ exclusive_usage();
test_func = test_nd;
w_flag = 2;
break;
t_patts[t_flag++] = pattern;
}
break;
+ case 'X':
+ exclusive_ok++;
+ break;
default:
usage();
}
exit(1);
}
} else {
+ errno = 0;
last_block = strtoul (argv[optind], &tmp, 0);
- if (*tmp) {
- com_err (program_name, 0, _("bad blocks count - %s"),
+ printf("last_block = %d (%s)\n", last_block, argv[optind]);
+ if (*tmp || errno ||
+ (last_block == ULONG_MAX && errno == ERANGE)) {
+ com_err (program_name, 0, _("invalid blocks count - %s"),
argv[optind]);
exit (1);
}
+ last_block++;
optind++;
}
if (optind <= argc-1) {
+ errno = 0;
from_count = strtoul (argv[optind], &tmp, 0);
- if (*tmp) {
- com_err (program_name, 0, _("bad starting block - %s"),
+ printf("from_count = %d\n", from_count);
+ if (*tmp || errno ||
+ (from_count == ULONG_MAX && errno == ERANGE)) {
+ com_err (program_name, 0, _("invalid starting block - %s"),
argv[optind]);
exit (1);
}
} else from_count = 0;
if (from_count >= last_block) {
- com_err (program_name, 0, _("bad blocks range: %lu-%lu"),
+ com_err (program_name, 0, _("invalid starting block (%d): must be less than %lu"),
(unsigned long) from_count, (unsigned long) last_block);
exit (1);
}
errcode = ext2fs_badblocks_list_create(&bb_list,0);
if (errcode) {
com_err (program_name, errcode,
- _("creating in-memory bad blocks list"));
+ _("while creating in-memory bad blocks list"));
exit (1);
}
default:
errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
if (errcode) {
- com_err (program_name, errcode, _("adding to in-memory bad block list"));
+ com_err (program_name, errcode, _("while adding to in-memory bad block list"));
exit (1);
}
continue;