From 665d4c9b6e3ff5e36439e33dc5a0ee2a7c84ccff Mon Sep 17 00:00:00 2001 From: Lieuwe Rooijakkers Date: Sun, 21 Jul 2024 21:06:49 +0200 Subject: read_file: openOptions --- src/io/read_file.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/io/read_file.c') diff --git a/src/io/read_file.c b/src/io/read_file.c index c344f5b..414a6d8 100644 --- a/src/io/read_file.c +++ b/src/io/read_file.c @@ -13,7 +13,11 @@ return NULL; \ } -struct filebuf *stream_to_filebuf(FILE *restrict stream) { +struct filebuf *stream_to_filebuf(FILE *restrict stream, int openOptions) { + if (openOptions & O_NOALLOWSPONGE) { + return NULL; + } + size_t sz = 0; size_t cap = 4096; char *buf = malloc(cap); @@ -56,12 +60,19 @@ static void *fd_to_mmap(const int fd, struct stat sb) { return addr; } -struct filebuf *file_to_filebuf(char *fname) { +struct filebuf *file_to_filebuf(char *fname, int openOptions) { const int fd = open(fname, O_RDONLY); + if (fd == -1) { + return NULL; + } + + if (openOptions & O_NOALLOWMAP) { + goto sponge; + } struct stat sb; if (fstat(fd, &sb) == -1) { - goto stream; + goto sponge; } void *addr; @@ -73,9 +84,9 @@ struct filebuf *file_to_filebuf(char *fname) { return res; } -stream: +sponge: FILE *stream = fdopen(fd, "rb"); - struct filebuf *res = stream_to_filebuf(stream); + struct filebuf *res = stream_to_filebuf(stream, openOptions); fclose(stream); return res; } -- cgit v1.2.3-70-g09d2