From 9e96ebaca075bef04d24eb51c486c0bfd6abdc0f Mon Sep 17 00:00:00 2001 From: Imbus Date: Sat, 7 Feb 2026 14:54:25 +0100 Subject: [PATCH] Assertion on file descriptor write access --- writeimg.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/writeimg.c b/writeimg.c index 9d3c47d..99d6b01 100644 --- a/writeimg.c +++ b/writeimg.c @@ -74,6 +74,9 @@ int perform_write(write_job_t *job) { int block_fd = open(job->dev_name, O_RDWR); int file_fd = open(job->filename, O_RDONLY); + assert(block_fd >= 0); + assert(file_fd >= 0); + /* TODO: Checks */ assert(job->bufsize >= job->block_size); @@ -202,10 +205,20 @@ 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.dev_name, O_WRONLY); + if (fd < 0) { + printf("Cannot write to \"%s\", do you have write permissions?\n", wjob.dev_name); + exit(1); + } + close(fd); + printf("Writing %s to %s\n", wjob.filename, wjob.dev_name); wjob.buffer = malloc(BLOCKSIZE); wjob.buffer2 = malloc(BLOCKSIZE); + assert(wjob.buffer); + assert(wjob.buffer2); wjob.bufsize = BLOCKSIZE; wjob.block_size = BLOCKSIZE;