Rename dev_name and filename to iname/oname in preparation for enabling file outputs

This commit is contained in:
Imbus 2026-02-09 14:42:28 +01:00
parent 36a98b2630
commit d1d3b3cd45

View file

@ -73,8 +73,8 @@ const char help[] =
const char copyright[] = "Copyright (C) %s Imbus, BSD-2-Clause\n"; const char copyright[] = "Copyright (C) %s Imbus, BSD-2-Clause\n";
struct write_job { struct write_job {
char *filename; char *iname;
char *dev_name; char *oname;
char *buffer; char *buffer;
char *buffer2; /* For memcmp integrity checks */ char *buffer2; /* For memcmp integrity checks */
size_t bufsize; size_t bufsize;
@ -98,8 +98,8 @@ void int_handler(int signum) {
} }
int perform_write(write_job_t *job) { int perform_write(write_job_t *job) {
int block_fd = open(job->dev_name, O_RDWR); int block_fd = open(job->oname, O_RDWR);
int file_fd = open(job->filename, O_RDONLY); int file_fd = open(job->iname, O_RDONLY);
assert(block_fd >= 0); assert(block_fd >= 0);
assert(file_fd >= 0); assert(file_fd >= 0);
@ -128,7 +128,7 @@ int perform_write(write_job_t *job) {
} }
if (read_bytes < 0) { if (read_bytes < 0) {
fprintf(stderr, "%s: Read error\n", job->filename); fprintf(stderr, "%s: Read error\n", job->iname);
perror("Read"); perror("Read");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -138,7 +138,7 @@ int perform_write(write_job_t *job) {
if (job->flags & WI_WRITE) { if (job->flags & WI_WRITE) {
ssize_t written_bytes = write(block_fd, job->buffer, read_bytes); ssize_t written_bytes = write(block_fd, job->buffer, read_bytes);
if (written_bytes < 0) { if (written_bytes < 0) {
fprintf(stderr, "%s: Write error\n", job->dev_name); fprintf(stderr, "%s: Write error\n", job->oname);
perror("Write"); perror("Write");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -219,7 +219,7 @@ int main(int argc, char *argv[]) {
wjob.flags |= WI_VERIFY; wjob.flags |= WI_VERIFY;
wjob.flags &= ~WI_WRITE; wjob.flags &= ~WI_WRITE;
continue; continue;
case 'd': wjob.dev_name = optarg; continue; case 'd': wjob.oname = optarg; continue;
case 'h': break; case 'h': break;
case 'n': wjob.flags &= ~WI_ASK; continue; case 'n': wjob.flags &= ~WI_ASK; continue;
case 'V': exit(EXIT_SUCCESS); case 'V': exit(EXIT_SUCCESS);
@ -244,27 +244,27 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
wjob.filename = argv[0]; wjob.iname = argv[0];
struct stat file_stat = {0}; struct stat file_stat = {0};
if (0 != stat(wjob.filename, &file_stat)) { if (0 != stat(wjob.iname, &file_stat)) {
printf("File does not exist...\n"); printf("File does not exist...\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (NULL == wjob.dev_name) { if (NULL == wjob.oname) {
printf("You need to specify a device.\n"); printf("You need to specify a device.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (0 != strncmp(wjob.dev_name, "/dev/", 4)) { if (!(wjob.flags & WI_FILE) && 0 != strncmp(wjob.oname, "/dev/", 4)) {
printf("\"%s\" does not appear to be a block device...\n", wjob.dev_name); printf("\"%s\" does not appear to be a block device...\n", wjob.oname);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Seems to be the cleanest way to check for write perm on a blockdev */ /* Seems to be the cleanest way to check for write perm on a blockdev */
int fd = open(wjob.dev_name, O_WRONLY); int fd = open(wjob.oname, O_WRONLY);
if (fd < 0) { if (fd < 0) {
printf("Cannot write to \"%s\", do you have write permissions?\n", wjob.dev_name); printf("Cannot write to \"%s\", do you have write permissions?\n", wjob.oname);
exit(1); exit(1);
} }
close(fd); close(fd);
@ -273,10 +273,8 @@ int main(int argc, char *argv[]) {
assert(file_stat.st_size >= 0); assert(file_stat.st_size >= 0);
if (wjob.flags & WI_WRITE) if (wjob.flags & WI_WRITE)
printf("Writing \"%s\" (%.1f MiB) to \"%s\"\n", printf(
basename(wjob.filename), "Writing \"%s\" (%.1f MiB) to \"%s\"\n", basename(wjob.iname), BYTES_TO_MIB(wjob.total_bytes), wjob.oname);
BYTES_TO_MIB(wjob.total_bytes),
wjob.dev_name);
if ((wjob.flags & WI_ASK) && (wjob.flags & WI_WRITE)) { if ((wjob.flags & WI_ASK) && (wjob.flags & WI_WRITE)) {
printf("Is this okay? (y/N): "); printf("Is this okay? (y/N): ");