Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

badblocks: fix status output when bb found #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 52 additions & 39 deletions misc/badblocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ static unsigned int d_flag; /* delay factor between reads */
static struct timeval time_start;

#define T_INC 32
#define S_BUF 128

static unsigned int sys_page_size = 4096;

Expand Down Expand Up @@ -148,43 +149,6 @@ static void *allocate_buffer(size_t size)
return ret;
}

/*
* This routine reports a new bad block. If the bad block has already
* been seen before, then it returns 0; otherwise it returns 1.
*/
static int bb_output (blk_t bad, enum error_types error_type)
{
errcode_t errcode;

if (ext2fs_badblocks_list_test(bb_list, bad))
return 0;

fprintf(out, "%lu\n", (unsigned long) bad);
fflush(out);

errcode = ext2fs_badblocks_list_add (bb_list, bad);
if (errcode) {
com_err (program_name, errcode, "adding to in-memory bad block list");
exit (1);
}

/* kludge:
increment the iteration through the bb_list if
an element was just added before the current iteration
position. This should not cause next_bad to change. */
if (bb_iter && bad < next_bad)
ext2fs_badblocks_list_iterate (bb_iter, &next_bad);

if (error_type == READ_ERROR) {
num_read_errors++;
} else if (error_type == WRITE_ERROR) {
num_write_errors++;
} else if (error_type == CORRUPTION_ERROR) {
num_corruption_errors++;
}
return 1;
}

static char *time_diff_format(struct timeval *tv1,
struct timeval *tv2, char *buf)
{
Expand Down Expand Up @@ -218,9 +182,9 @@ static float calc_percent(unsigned long current, unsigned long total) {
static void print_status(void)
{
struct timeval time_end;
char diff_buf[32], line_buf[128];
char diff_buf[32], line_buf[S_BUF];
#ifdef HAVE_MBSTOWCS
wchar_t wline_buf[128];
wchar_t wline_buf[S_BUF];
#endif
int len;

Expand Down Expand Up @@ -485,6 +449,55 @@ static void flush_bufs(void)
_("during ext2fs_sync_device"));
}

/*
* This routine reports a new bad block. If the bad block has already
* been seen before, then it returns 0; otherwise it returns 1.
*/
static int bb_output (blk_t bad, enum error_types error_type)
{
errcode_t errcode;
char status_buf[S_BUF*2];

if (ext2fs_badblocks_list_test(bb_list, bad))
return 0;

if (v_flag > 1) {
memset(status_buf, ' ', S_BUF-1);
memset(status_buf+S_BUF-1, '\b', S_BUF-1);
status_buf[S_BUF*2-2] = 0;
fputs(status_buf, stderr);
fflush(stderr);
}

fprintf(out, "%lu\n", (unsigned long) bad);
fflush(out);

if (v_flag > 1)
print_status();

errcode = ext2fs_badblocks_list_add (bb_list, bad);
if (errcode) {
com_err (program_name, errcode, "adding to in-memory bad block list");
exit (1);
}

/* kludge:
increment the iteration through the bb_list if
an element was just added before the current iteration
position. This should not cause next_bad to change. */
if (bb_iter && bad < next_bad)
ext2fs_badblocks_list_iterate (bb_iter, &next_bad);

if (error_type == READ_ERROR) {
num_read_errors++;
} else if (error_type == WRITE_ERROR) {
num_write_errors++;
} else if (error_type == CORRUPTION_ERROR) {
num_corruption_errors++;
}
return 1;
}

static unsigned int test_ro (int dev, blk_t last_block,
int block_size, blk_t first_block,
unsigned int blocks_at_once)
Expand Down