Rename dev_name and filename to iname/oname in preparation for enabling file outputs
This commit is contained in:
parent
36a98b2630
commit
d1d3b3cd45
1 changed files with 16 additions and 18 deletions
34
writeimg.c
34
writeimg.c
|
|
@ -73,8 +73,8 @@ const char help[] =
|
|||
const char copyright[] = "Copyright (C) %s Imbus, BSD-2-Clause\n";
|
||||
|
||||
struct write_job {
|
||||
char *filename;
|
||||
char *dev_name;
|
||||
char *iname;
|
||||
char *oname;
|
||||
char *buffer;
|
||||
char *buffer2; /* For memcmp integrity checks */
|
||||
size_t bufsize;
|
||||
|
|
@ -98,8 +98,8 @@ void int_handler(int signum) {
|
|||
}
|
||||
|
||||
int perform_write(write_job_t *job) {
|
||||
int block_fd = open(job->dev_name, O_RDWR);
|
||||
int file_fd = open(job->filename, O_RDONLY);
|
||||
int block_fd = open(job->oname, O_RDWR);
|
||||
int file_fd = open(job->iname, O_RDONLY);
|
||||
|
||||
assert(block_fd >= 0);
|
||||
assert(file_fd >= 0);
|
||||
|
|
@ -128,7 +128,7 @@ int perform_write(write_job_t *job) {
|
|||
}
|
||||
|
||||
if (read_bytes < 0) {
|
||||
fprintf(stderr, "%s: Read error\n", job->filename);
|
||||
fprintf(stderr, "%s: Read error\n", job->iname);
|
||||
perror("Read");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
|
@ -138,7 +138,7 @@ int perform_write(write_job_t *job) {
|
|||
if (job->flags & WI_WRITE) {
|
||||
ssize_t written_bytes = write(block_fd, job->buffer, read_bytes);
|
||||
if (written_bytes < 0) {
|
||||
fprintf(stderr, "%s: Write error\n", job->dev_name);
|
||||
fprintf(stderr, "%s: Write error\n", job->oname);
|
||||
perror("Write");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
|
@ -219,7 +219,7 @@ int main(int argc, char *argv[]) {
|
|||
wjob.flags |= WI_VERIFY;
|
||||
wjob.flags &= ~WI_WRITE;
|
||||
continue;
|
||||
case 'd': wjob.dev_name = optarg; continue;
|
||||
case 'd': wjob.oname = optarg; continue;
|
||||
case 'h': break;
|
||||
case 'n': wjob.flags &= ~WI_ASK; continue;
|
||||
case 'V': exit(EXIT_SUCCESS);
|
||||
|
|
@ -244,27 +244,27 @@ int main(int argc, char *argv[]) {
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
wjob.filename = argv[0];
|
||||
wjob.iname = argv[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");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (NULL == wjob.dev_name) {
|
||||
if (NULL == wjob.oname) {
|
||||
printf("You need to specify a device.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (0 != strncmp(wjob.dev_name, "/dev/", 4)) {
|
||||
printf("\"%s\" does not appear to be a block device...\n", wjob.dev_name);
|
||||
if (!(wjob.flags & WI_FILE) && 0 != strncmp(wjob.oname, "/dev/", 4)) {
|
||||
printf("\"%s\" does not appear to be a block device...\n", wjob.oname);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
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);
|
||||
}
|
||||
close(fd);
|
||||
|
|
@ -273,10 +273,8 @@ int main(int argc, char *argv[]) {
|
|||
assert(file_stat.st_size >= 0);
|
||||
|
||||
if (wjob.flags & WI_WRITE)
|
||||
printf("Writing \"%s\" (%.1f MiB) to \"%s\"\n",
|
||||
basename(wjob.filename),
|
||||
BYTES_TO_MIB(wjob.total_bytes),
|
||||
wjob.dev_name);
|
||||
printf(
|
||||
"Writing \"%s\" (%.1f MiB) to \"%s\"\n", basename(wjob.iname), BYTES_TO_MIB(wjob.total_bytes), wjob.oname);
|
||||
|
||||
if ((wjob.flags & WI_ASK) && (wjob.flags & WI_WRITE)) {
|
||||
printf("Is this okay? (y/N): ");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue