Guard for verify only

This commit is contained in:
Imbus 2026-02-07 17:23:27 +01:00
parent 19bf88b2e6
commit c08ee6a738

View file

@ -100,15 +100,20 @@ int perform_write(write_job_t *job) {
int crc = crc32_init(); int crc = crc32_init();
size_t b_written = 0; size_t b_written = 0;
while (!job->verify_only) { while (42) {
fsync(block_fd);
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); assert(read_bytes >= 0);
if (read_bytes == 0) { if (read_bytes == 0) {
fsync(block_fd);
crc = crc32_finalize(crc); crc = crc32_finalize(crc);
printf("\nWriting done...\n");
assert(job->total_bytes == b_written); if (!job->verify_only) {
printf("\nWriting done...\n");
assert(job->total_bytes == b_written);
} else
assert(0 == b_written);
break; /* Finished */ break; /* Finished */
} }
@ -120,13 +125,15 @@ int perform_write(write_job_t *job) {
crc = crc32_update(crc, job->buffer, read_bytes); crc = crc32_update(crc, job->buffer, read_bytes);
ssize_t written_bytes = write(block_fd, job->buffer, read_bytes); if (!job->verify_only) {
if (written_bytes < 0) { ssize_t written_bytes = write(block_fd, job->buffer, read_bytes);
fprintf(stderr, "%s: Write error\n", job->dev_name); if (written_bytes < 0) {
perror("Write"); fprintf(stderr, "%s: Write error\n", job->dev_name);
exit(EXIT_FAILURE); perror("Write");
} exit(EXIT_FAILURE);
print_progress(b_written += written_bytes, job->total_bytes); }
print_progress(b_written += written_bytes, job->total_bytes);
} /* Else maybe give some helpful insights? */
} }
lseek(block_fd, 0, SEEK_SET); lseek(block_fd, 0, SEEK_SET);
@ -173,6 +180,7 @@ static const struct option longopts[] = {
{"help", 0, 0, 'h'}, {"help", 0, 0, 'h'},
{"version", 0, 0, 'V'}, {"version", 0, 0, 'V'},
{"device", 1, 0, 'd'}, {"device", 1, 0, 'd'},
{"noconfirm", 0, 0, 'n'},
{NULL, 0, 0, 0}, {NULL, 0, 0, 0},
}; };
@ -240,12 +248,16 @@ int main(int argc, char *argv[]) {
} }
close(fd); close(fd);
printf("Writing %s to %s\n", wjob.filename, wjob.dev_name); if (!wjob.verify_only)
printf("Writing %s to %s\n", wjob.filename, wjob.dev_name);
printf("Is this okay? (y/n): "); if (ask_permission && !wjob.verify_only) {
fflush(stdout); printf("Is this okay? (y/n): ");
if ('y' != getchar()) { fflush(stdout);
exit(0); if ('y' != getchar()) {
printf("Aborting...\n");
exit(EXIT_SUCCESS);
}
} }
wjob.buffer = malloc(BLOCKSIZE); wjob.buffer = malloc(BLOCKSIZE);
@ -266,7 +278,7 @@ int main(int argc, char *argv[]) {
if (wjob.buffer2) if (wjob.buffer2)
free(wjob.buffer2); free(wjob.buffer2);
printf("\n%.1f MiB's written and verified.\nAll good!\n", BYTES_TO_MIB(wjob.total_bytes)); printf("\n%.1f MiB's verified.\nAll good!\n", BYTES_TO_MIB(wjob.total_bytes));
exit(0);
return 0; exit(EXIT_SUCCESS);
} }