Assertion on file descriptor write access

This commit is contained in:
Imbus 2026-02-07 14:54:25 +01:00
parent 804a6b1485
commit 9e96ebaca0

View file

@ -74,6 +74,9 @@ int perform_write(write_job_t *job) {
int block_fd = open(job->dev_name, O_RDWR); int block_fd = open(job->dev_name, O_RDWR);
int file_fd = open(job->filename, O_RDONLY); int file_fd = open(job->filename, O_RDONLY);
assert(block_fd >= 0);
assert(file_fd >= 0);
/* TODO: Checks */ /* TODO: Checks */
assert(job->bufsize >= job->block_size); assert(job->bufsize >= job->block_size);
@ -202,10 +205,20 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); 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); printf("Writing %s to %s\n", wjob.filename, wjob.dev_name);
wjob.buffer = malloc(BLOCKSIZE); wjob.buffer = malloc(BLOCKSIZE);
wjob.buffer2 = malloc(BLOCKSIZE); wjob.buffer2 = malloc(BLOCKSIZE);
assert(wjob.buffer);
assert(wjob.buffer2);
wjob.bufsize = BLOCKSIZE; wjob.bufsize = BLOCKSIZE;
wjob.block_size = BLOCKSIZE; wjob.block_size = BLOCKSIZE;