From 2e02a056cc510ef7276cd675a3ae35347e46db5d Mon Sep 17 00:00:00 2001 From: Imbus Date: Sat, 21 Feb 2026 03:57:30 +0100 Subject: [PATCH] Now also reports the size of the block device before asking to continue --- writeimg.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) 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): ");