Skip to content

Commit

Permalink
[bb] progress bar and cancellation support
Browse files Browse the repository at this point in the history
  • Loading branch information
pbatard committed Dec 6, 2011
1 parent 480986b commit 192c3d6
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 72 deletions.
2 changes: 2 additions & 0 deletions configure
Expand Up @@ -3360,6 +3360,8 @@ $as_echo "#define _GNU_SOURCE /**/" >>confdefs.h
# AC_MSG_ERROR([unsupported development environment])
#esac
# Clang needs an explicit WIN32_WINNT defined else it produces warnings
# in msapi_utf8.h - including winver.h only doesn't work
AM_CFLAGS="${AM_CFLAGS} -DWINVER=0x501 -D_WIN32_WINNT=0x501 -D_WIN32_IE=0x501"
AM_LDFLAGS="${AM_LDFLAGS} -Wl,-no-undefined"
Expand Down
2 changes: 2 additions & 0 deletions configure.ac
Expand Up @@ -31,6 +31,8 @@ AC_DEFINE([_GNU_SOURCE], [], [Use GNU extensions])
# AC_MSG_ERROR([unsupported development environment])
#esac

# Clang needs an explicit WIN32_WINNT defined else it produces warnings
# in msapi_utf8.h - including winver.h only doesn't work
AM_CFLAGS="${AM_CFLAGS} -DWINVER=0x501 -D_WIN32_WINNT=0x501 -D_WIN32_IE=0x501"
AM_LDFLAGS="${AM_LDFLAGS} -Wl,-no-undefined"

Expand Down
Empty file removed rufus.rc
Empty file.
114 changes: 57 additions & 57 deletions src/badblocks.c
Expand Up @@ -272,10 +272,11 @@ static void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
/*
* from e2fsprogs/misc/badblocks.c
*/
static int v_flag = 2; /* verbose */
static int v_flag = 1; /* verbose */
static int s_flag = 1; /* show progress of test */
static int t_flag = 0; /* number of test patterns */
static unsigned int *t_patts = NULL; /* test patterns */
static int cancel_ops = 0; /* abort current operation */
/* Abort test if more than this number of bad blocks has been encountered */
static unsigned int max_bb = EXT2_BAD_BLOCKS_THRESHOLD;
static DWORD time_start;
Expand Down Expand Up @@ -354,23 +355,32 @@ static float calc_percent(unsigned long current, unsigned long total) {

static void print_status(void)
{
static DWORD msecs = 0;
DWORD time_end;
float percent;

// TODO: use GetTickCount64 on Vista and later
time_end = GetTickCount();
/* update status every second */
if (time_end - time_start >= msecs) {
uprintf("%6.2f%% done, %0.2f elapsed. "
"(%d/%d/%d errors)",
calc_percent((unsigned long) currently_testing,
(unsigned long) num_blocks),
(time_end - time_start)/1000.0,
num_read_errors,
num_write_errors,
num_corruption_errors);
msecs += 1000;
percent = calc_percent((unsigned long) currently_testing,
(unsigned long) num_blocks);
uprintf("%6.2f%% done, %4.0fs elapsed. "
"(%d/%d/%d errors)",
percent,
(time_end - time_start)/1000.0,
num_read_errors,
num_write_errors,
num_corruption_errors);
PostMessage(hMainDialog, UM_FORMAT_PROGRESS, (WPARAM)(DWORD)percent, (LPARAM)0);
}

static void CALLBACK alarm_intr(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
if (!num_blocks)
return;
if (FormatStatus) {
uprintf("Interrupting at block %llu\n",
(unsigned long long) currently_testing);
cancel_ops = -1;
}
print_status();
}

static void pattern_fill(unsigned char *buffer, unsigned int pattern,
Expand Down Expand Up @@ -413,10 +423,6 @@ static int do_read (HANDLE hDrive, unsigned char * buffer, int tryout, int block
{
long got;

#if 0
printf("do_read: block %d, try %d\n", current_block, tryout);
#endif

if (v_flag > 1)
print_status();

Expand All @@ -439,9 +445,6 @@ static int do_write(HANDLE hDrive, unsigned char * buffer, int tryout, int block
{
long got;

#if 0
printf("do_write: block %lu, try %d\n", current_block, tryout);
#endif
if (v_flag > 1)
print_status();

Expand Down Expand Up @@ -483,8 +486,8 @@ static unsigned int test_ro (HANDLE hDrive, blk_t last_block,
}
if (!blkbuf)
{
// TODO: err
uprintf("could not allocate buffers\n");
cancel_ops = -1;
return 0;
}
if (t_flag) {
Expand All @@ -505,6 +508,7 @@ static unsigned int test_ro (HANDLE hDrive, blk_t last_block,
if (s_flag || v_flag) {
uprintf("Too many bad blocks, aborting test\n");
}
cancel_ops = -1;
break;
}
if (next_bad) {
Expand Down Expand Up @@ -559,7 +563,7 @@ static unsigned int test_ro (HANDLE hDrive, blk_t last_block,

static unsigned int test_rw(HANDLE hDrive, blk_t last_block, int block_size, blk_t first_block, unsigned int blocks_at_once)
{
unsigned char *buffer, *read_buffer;
unsigned char *buffer = NULL, *read_buffer;
const unsigned int patterns[] = {0xaa}; // {0xaa, 0x55, 0xff, 0x00};
const unsigned int *pattern;
int i, tryout, got, nr_pattern, pat_idx;
Expand All @@ -571,6 +575,7 @@ static unsigned int test_rw(HANDLE hDrive, blk_t last_block, int block_size, blk

if (!buffer) {
uprintf("Error while allocating buffers");
cancel_ops = -1;
return 0;
}

Expand All @@ -583,24 +588,24 @@ static unsigned int test_rw(HANDLE hDrive, blk_t last_block, int block_size, blk
pattern = patterns;
nr_pattern = ARRAYSIZE(patterns);
}
// TODO: allow cancellation

for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
if (cancel_ops) goto out;
pattern_fill(buffer, pattern[pat_idx], blocks_at_once * block_size);
num_blocks = last_block - 1;
currently_testing = first_block;
// if (s_flag && v_flag <= 1)
// alarm_intr(SIGALRM);
if (s_flag | v_flag)
uprintf("Writing\n");
tryout = blocks_at_once;
while (currently_testing < last_block) {
if (max_bb && bb_count >= max_bb) {
if (s_flag || v_flag) {
// TODO: this abort blows
uprintf("Too many bad blocks, aborting test\n");
}
cancel_ops = -1;
break;
}
if (cancel_ops) goto out;
if (currently_testing + tryout > last_block)
tryout = last_block - currently_testing;
got = do_write(hDrive, buffer, tryout, block_size, currently_testing);
Expand All @@ -623,19 +628,14 @@ static unsigned int test_rw(HANDLE hDrive, blk_t last_block, int block_size, blk
}

num_blocks = 0;
// alarm (0);
// if (s_flag | v_flag)
// fputs(_(done_string), stderr);
if (s_flag | v_flag)
// TODO: status
uprintf("Reading and comparing\n");
num_blocks = last_block;
currently_testing = first_block;
// if (s_flag && v_flag <= 1)
// alarm_intr(SIGALRM);

tryout = blocks_at_once;
while (currently_testing < last_block) {
if (cancel_ops) goto out;
if (max_bb && bb_count >= max_bb) {
if (s_flag || v_flag) {
uprintf("Too many bad blocks, aborting test\n");
Expand Down Expand Up @@ -670,10 +670,8 @@ static unsigned int test_rw(HANDLE hDrive, blk_t last_block, int block_size, blk
}

num_blocks = 0;
// alarm (0);
// if (s_flag | v_flag)
// fputs(_(done_string), stderr);
}
out:
free_buffer(buffer);
return bb_count;
}
Expand Down Expand Up @@ -721,8 +719,8 @@ static unsigned int test_nd(HANDLE hDrive, blk_t last_block,
test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
if (!blkbuf || !test_record) {
uprintf("Error while allocating buffers");
// TODO
exit (1);
cancel_ops = -1;
return 0;
}

save_base = blkbuf;
Expand Down Expand Up @@ -782,6 +780,7 @@ static unsigned int test_nd(HANDLE hDrive, blk_t last_block,
if (s_flag || v_flag) {
uprintf("Too many bad blocks, aborting test\n");
}
cancel_ops = -1;
break;
}
tryout = granularity - buf_used;
Expand Down Expand Up @@ -927,18 +926,20 @@ static unsigned int test_nd(HANDLE hDrive, blk_t last_block,
return bb_count;
}

int BadBlocks(HANDLE hPhysicalDrive, ULONGLONG disk_size, int block_size, int test_type)
BOOL BadBlocks(HANDLE hPhysicalDrive, ULONGLONG disk_size, int block_size,
int test_type, badblocks_report *report)
{
errcode_t error_code;
unsigned int (*test_func)(HANDLE, blk_t, int, blk_t, unsigned int);
int num_passes = 0;
int passes_clean = 0;
blk_t bb_count = 0, first_block = 0, last_block = (blk_t)disk_size/block_size;
blk_t first_block = 0, last_block = (blk_t)disk_size/block_size;

if (report == NULL) return FALSE;
report->bb_count = 0;

error_code = ext2fs_badblocks_list_create(&bb_list, 0);
if (error_code) {
uprintf("Error %d while creating in-memory bad blocks list", error_code);
return -1;
return FALSE;
}

switch(test_type) {
Expand All @@ -953,21 +954,20 @@ int BadBlocks(HANDLE hPhysicalDrive, ULONGLONG disk_size, int block_size, int te
break;
}
time_start = GetTickCount();
do {
bb_count = test_func(hPhysicalDrive, last_block, block_size, first_block, EXT2_BLOCKS_AT_ONCE);
if (bb_count)
passes_clean = 0;
else
++passes_clean;

if (v_flag)
uprintf("Pass completed, %u bad blocks found. (%d/%d/%d errors)\n",
bb_count, num_read_errors, num_write_errors, num_corruption_errors);

} while (passes_clean < num_passes);
cancel_ops = 0;
/* use a timer to update status every second */
SetTimer(hMainDialog, EXT2_TIMER_ID, 1000, alarm_intr);
report->bb_count = test_func(hPhysicalDrive, last_block, block_size, first_block, EXT2_BLOCKS_AT_ONCE);
KillTimer(hMainDialog, EXT2_TIMER_ID);
free(t_patts);
free(bb_list->list);
free(bb_list);

return bb_count;
// TODO: report first problem block for each error
report->num_read_errors = num_read_errors;
report->num_write_errors = num_write_errors;
report->num_corruption_errors = num_corruption_errors;

if (cancel_ops)
return FALSE;
return TRUE;
}
4 changes: 3 additions & 1 deletion src/badblocks.h
Expand Up @@ -41,6 +41,7 @@ typedef struct ext2_struct_u32_iterate *ext2_u32_iterate;
#define EXT2_BAD_BLOCKS_THRESHOLD 32
#define EXT2_BLOCKS_AT_ONCE 64
#define EXT2_SYS_PAGE_SIZE 4096
#define EXT2_TIMER_ID 0x1000

enum test_type {
BADBLOCKS_RO, /* Read-only */
Expand All @@ -62,4 +63,5 @@ typedef struct {
/*
* Shared prototypes
*/
int BadBlocks(HANDLE hPhysicalDrive, ULONGLONG disk_size, int block_size, int test_type);
BOOL BadBlocks(HANDLE hPhysicalDrive, ULONGLONG disk_size, int block_size,
int test_type, badblocks_report *report);
17 changes: 12 additions & 5 deletions src/format.c
Expand Up @@ -43,6 +43,7 @@
* Globals
*/
DWORD FormatStatus;
badblocks_report report;

/*
* FormatEx callback. Return FALSE to halt operations
Expand Down Expand Up @@ -341,14 +342,20 @@ void __cdecl FormatThread(void* param)
goto out;
}

if (BadBlocks(hPhysicalDrive, SelectedDrive.DiskSize,
SelectedDrive.Geometry.BytesPerSector, BADBLOCKS_RW)) {
// TODO: report block failure number, etc
if (!BadBlocks(hPhysicalDrive, SelectedDrive.DiskSize,
SelectedDrive.Geometry.BytesPerSector, BADBLOCKS_RW, &report)) {
uprintf("Bad blocks check failed.\n");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_BADBLOCKS);
if (!FormatStatus)
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|
APPERR(ERROR_BADBLOCKS_FAILURE);
// TODO: should probably ClearMBR here as well
goto out;
}
uprintf("Check completed, %u bad blocks found. (%d/%d/%d errors)\n",
report.bb_count, report.num_read_errors, report.num_write_errors, report.num_corruption_errors);
safe_unlockclose(hLogicalVolume);

// TODO: check bb_count and ask user if they want to continue
}

// Especially after destructive badblocks test, you must zero the MBR completely
Expand Down Expand Up @@ -382,7 +389,7 @@ void __cdecl FormatThread(void* param)

if (!FormatDrive(drive_name[0])) {
// Error will be set by FormatDrive() in FormatStatus
uprintf("Format error: 0x%08X\n", FormatStatus);
uprintf("Format error: %s\n", StrError(FormatStatus));
goto out;
}

Expand Down
4 changes: 4 additions & 0 deletions src/rufus.c
Expand Up @@ -703,11 +703,13 @@ static INT_PTR CALLBACK MainCallback(HWND hDlg, UINT message, WPARAM wParam, LPA
if (MessageBoxA(hMainDialog, str, "Rufus", MB_OKCANCEL|MB_ICONWARNING) == IDOK) {
// Disable all controls except cancel
EnableControls(FALSE);
#if 0
// Handle marquee progress bar on quickformat
SetWindowLongPtr(hProgress, GWL_STYLE, ProgressStyle | (IsChecked(IDC_QUICKFORMAT)?PBS_MARQUEE:0));
if (IsChecked(IDC_QUICKFORMAT)) {
SendMessage(hProgress, PBM_SETMARQUEE, TRUE, 0);
}
#endif
DeviceNum = (DWORD)ComboBox_GetItemData(hDeviceList, nDeviceIndex);
FormatStatus = 0;
format_thid = _beginthread(FormatThread, 0, (void*)(uintptr_t)DeviceNum);
Expand Down Expand Up @@ -739,6 +741,7 @@ static INT_PTR CALLBACK MainCallback(HWND hDlg, UINT message, WPARAM wParam, LPA
format_thid = -1L;
// Close the cancel MessageBox if active
SendMessage(FindWindowA(MAKEINTRESOURCEA(32770), RUFUS_CANCELBOX_TITLE), WM_COMMAND, IDNO, 0);
#if 0
if (IsChecked(IDC_QUICKFORMAT)) {
SendMessage(hProgress, PBM_SETMARQUEE, FALSE, 0);
SetWindowLongPtr(hProgress, GWL_STYLE, ProgressStyle);
Expand All @@ -747,6 +750,7 @@ static INT_PTR CALLBACK MainCallback(HWND hDlg, UINT message, WPARAM wParam, LPA
SendMessage(hProgress, PBM_SETPOS, 101, 0);
SendMessage(hProgress, PBM_SETRANGE, 0, 100<<16);
}
#endif
SendMessage(hProgress, PBM_SETPOS, FormatStatus?0:100, 0);
EnableControls(TRUE);
GetUSBDevices();
Expand Down
2 changes: 1 addition & 1 deletion src/rufus.h
Expand Up @@ -183,4 +183,4 @@ typedef struct {
#define ERROR_INVALID_CLUSTER_SIZE 0x1203
#define ERROR_INVALID_VOLUME_SIZE 0x1204
#define ERROR_CANT_START_THREAD 0x1205
#define ERROR_BADBLOCKS 0x1206
#define ERROR_BADBLOCKS_FAILURE 0x1206

0 comments on commit 192c3d6

Please sign in to comment.