diff --git a/writeimg.c b/writeimg.c index 245e6c4..e839930 100644 --- a/writeimg.c +++ b/writeimg.c @@ -268,20 +268,34 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } - /* Seems to be the cleanest way to check for write perm on a blockdev */ - int fd = open(wjob.oname, O_WRONLY); - if (fd < 0) { - printf("Cannot write to \"%s\", do you have write permissions?\n", wjob.oname); - exit(1); + uint64_t device_size; + { + /* Seems to be the cleanest way to check for write perm on a blockdev */ + int fd = open(wjob.oname, O_WRONLY); + if (fd < 0) { + printf("Cannot write to \"%s\", do you have write permissions?\n", wjob.oname); + close(fd); + exit(EXIT_FAILURE); + } + + if (ioctl(fd, BLKGETSIZE64, &device_size) < 0) { + perror("ioctl"); + close(fd); + exit(EXIT_FAILURE); + } + + close(fd); } - close(fd); wjob.total_bytes = file_stat.st_size; assert(file_stat.st_size >= 0); if (wjob.flags & WI_WRITE) - printf( - "Writing \"%s\" (%.1f MiB) to \"%s\"\n", basename(wjob.iname), BYTES_TO_MIB(wjob.total_bytes), wjob.oname); + printf("Writing \"%s\" (%.1f MiB) to \"%s\" (%.1f MiB)\n", + basename(wjob.iname), + BYTES_TO_MIB(wjob.total_bytes), + wjob.oname, + BYTES_TO_MIB(device_size)); if ((wjob.flags & WI_ASK) && (wjob.flags & WI_WRITE)) { printf("Is this okay? (y/N): ");