From 2b10bb09beb03380c8b815a6f6be268f188ac78d Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 11 Jun 2023 23:03:21 +0200 Subject: libio: add open handle functions to istream/ostream For the backends, this simplifies the code as both paths (open file and open stdio) use the same basic code. Even when merging them only in the backend, it would be done in a similar way. Making the functions public allows other uses as well. Signed-off-by: David Oberhollenzer --- include/io/file.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'include/io') diff --git a/include/io/file.h b/include/io/file.h index 8c6e851..f3723b2 100644 --- a/include/io/file.h +++ b/include/io/file.h @@ -10,10 +10,52 @@ #include "io/istream.h" #include "io/ostream.h" +#if defined(_WIN32) || defined(__WINDOWS__) +#include + +typedef HANDLE os_file_t; +#else +typedef int os_file_t; +#endif + #ifdef __cplusplus extern "C" { #endif +/** + * @brief Create an input stream for an OS native file handle. + * + * @memberof istream_t + * + * The functions takes up ownership of the file handle and takes care + * of cleaning it up. On failure, the handle remains usable, and ownership + * remains with the caller. + * + * @param path The name to associate with the handle. + * @param fd A native file handle. + * + * @return A pointer to an output stream on success, NULL on failure. + */ +SQFS_INTERNAL istream_t *istream_open_handle(const char *path, os_file_t fd); + +/** + * @brief Create an output stream that writes to an OS native file handle. + * + * @memberof ostream_t + * + * If the flag OSTREAM_OPEN_SPARSE is set, the underlying implementation tries + * to use seek/truncate style API to create sparse output files. If the flag + * is not set, holes will always be filled with zero bytes. + * + * @param path The name to associate with the handle. + * @param fd A native file handle. + * @param flags A combination of flags. + * + * @return A pointer to an output stream on success, NULL on failure. + */ +SQFS_INTERNAL ostream_t *ostream_open_handle(const char *path, os_file_t hnd, + int flags); + /** * @brief Create an input stream that reads from a file. * -- cgit v1.2.3