From 72218c5bc315c8c397925f80362d3228bf34939b Mon Sep 17 00:00:00 2001 From: Eduard Shishkin Date: Mon, 7 Oct 2024 12:38:58 +0200 Subject: [PATCH] zipl/src: Introduce misc_open_simulate() and misc_open_device() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduce misc_open_simulate() to mark individual files as "opened for write simulation". Introduce misc_open_device() to open a file either in "usual", or in "simulation" mode, depending on the passed argument Tested-by: Mikhail Zaslonko Reviewed-by: Mikhail Zaslonko Signed-off-by: Eduard Shishkin Signed-off-by: Jan Höppner --- zipl/include/misc.h | 2 ++ zipl/src/misc.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/zipl/include/misc.h b/zipl/include/misc.h index 991e659a..61148fcf 100644 --- a/zipl/include/misc.h +++ b/zipl/include/misc.h @@ -34,6 +34,8 @@ int misc_asprintf(char **out, const char *fmt, ...); void* misc_calloc(size_t n, size_t size); char* misc_strdup(const char* s); int misc_open_exclusive(const char* filename); +int misc_open_simulate(const char *filename, struct misc_fd *mfd); +int misc_open_device(const char *filename, struct misc_fd *mfd, int simulate); int misc_read(int fd, void* buffer, size_t count); int misc_read_file(const char* filename, char** buffer, size_t* size, int nil_terminate); diff --git a/zipl/src/misc.c b/zipl/src/misc.c index b69490c0..750fae50 100644 --- a/zipl/src/misc.c +++ b/zipl/src/misc.c @@ -101,6 +101,35 @@ misc_open_exclusive(const char* filename) return fd; } +/** + * Open file FILENAME for write simulation + */ +int misc_open_simulate(const char *filename, struct misc_fd *mfd) +{ + /* + * In write simulation mode actual data are not written to the file, + * so open it as read-only + */ + mfd->fd = open(filename, O_RDONLY); + mfd->simulate_write = 1; + if (mfd->fd == -1) + error_reason(strerror(errno)); + return mfd->fd; +} + +int misc_open_device(const char *filename, struct misc_fd *mfd, int simulate) +{ + if (simulate) { + /* + * open the file for "write simulation", when actual + * data are not written, and only current position in + * the file is updated + */ + return misc_open_simulate(filename, mfd); + } + mfd->fd = misc_open_exclusive(filename); + return mfd->fd; +} /* Read COUNT bytes of data from file identified by file descriptor FD to * memory at location BUFFER. Return 0 when all bytes were successfully read,