Now includes a simple progress bar, percent only
This commit is contained in:
parent
9e96ebaca0
commit
21f2420d42
1 changed files with 28 additions and 1 deletions
29
writeimg.c
29
writeimg.c
|
|
@ -29,6 +29,23 @@
|
||||||
|
|
||||||
#define BYTES_TO_MIB(bts) ((double)bts / (1024 * 1024))
|
#define BYTES_TO_MIB(bts) ((double)bts / (1024 * 1024))
|
||||||
|
|
||||||
|
#define BAR_WIDTH 50
|
||||||
|
|
||||||
|
void print_progress(int current, int total) {
|
||||||
|
float fraction = (float)current / total;
|
||||||
|
int filled = (int)(fraction * BAR_WIDTH);
|
||||||
|
|
||||||
|
printf("\r[");
|
||||||
|
for (int i = 0; i < BAR_WIDTH; i++) {
|
||||||
|
if (i < filled)
|
||||||
|
putchar('#');
|
||||||
|
else
|
||||||
|
putchar(' ');
|
||||||
|
}
|
||||||
|
printf("] %3d%%", (int)(fraction * 100));
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
const char help[] =
|
const char help[] =
|
||||||
"Usage:\n"
|
"Usage:\n"
|
||||||
|
|
@ -80,13 +97,18 @@ int perform_write(write_job_t *job) {
|
||||||
/* TODO: Checks */
|
/* TODO: Checks */
|
||||||
assert(job->bufsize >= job->block_size);
|
assert(job->bufsize >= job->block_size);
|
||||||
|
|
||||||
int crc = crc32_init();
|
int crc = crc32_init();
|
||||||
|
size_t b_written = 0;
|
||||||
|
|
||||||
while (!job->verify_only) {
|
while (!job->verify_only) {
|
||||||
ssize_t read_bytes = read(file_fd, job->buffer, job->block_size);
|
ssize_t read_bytes = read(file_fd, job->buffer, job->block_size);
|
||||||
|
assert(read_bytes >= 0);
|
||||||
|
|
||||||
if (read_bytes == 0) {
|
if (read_bytes == 0) {
|
||||||
fsync(block_fd);
|
fsync(block_fd);
|
||||||
crc = crc32_finalize(crc);
|
crc = crc32_finalize(crc);
|
||||||
|
printf("\nWriting done...\n");
|
||||||
|
assert(job->total_bytes == b_written);
|
||||||
break; /* Finished */
|
break; /* Finished */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -104,6 +126,7 @@ int perform_write(write_job_t *job) {
|
||||||
perror("Write");
|
perror("Write");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
print_progress(b_written += written_bytes, job->total_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
lseek(block_fd, 0, SEEK_SET);
|
lseek(block_fd, 0, SEEK_SET);
|
||||||
|
|
@ -113,6 +136,7 @@ int perform_write(write_job_t *job) {
|
||||||
memset(job->buffer2, 0, BLOCKSIZE);
|
memset(job->buffer2, 0, BLOCKSIZE);
|
||||||
|
|
||||||
int crc_back = crc32_init();
|
int crc_back = crc32_init();
|
||||||
|
b_written = 0;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
ssize_t read_file = read(file_fd, job->buffer2, job->block_size);
|
ssize_t read_file = read(file_fd, job->buffer2, job->block_size);
|
||||||
|
|
@ -125,10 +149,13 @@ int perform_write(write_job_t *job) {
|
||||||
if (read_file == 0) {
|
if (read_file == 0) {
|
||||||
crc_back = crc32_finalize(crc_back);
|
crc_back = crc32_finalize(crc_back);
|
||||||
assert(crc_back == crc);
|
assert(crc_back == crc);
|
||||||
|
assert(job->total_bytes == b_written);
|
||||||
|
printf("\nVerification done!\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
crc_back = crc32_update(crc_back, job->buffer, read_file);
|
crc_back = crc32_update(crc_back, job->buffer, read_file);
|
||||||
|
print_progress(b_written += read_block, job->total_bytes);
|
||||||
|
|
||||||
int cmp = memcmp((const void *)job->buffer2, (const void *)job->buffer, (size_t)read_file);
|
int cmp = memcmp((const void *)job->buffer2, (const void *)job->buffer, (size_t)read_file);
|
||||||
if (0 != cmp) {
|
if (0 != cmp) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue