From 76efa02bebc18e3356b2430803d0c82405f46d9d Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Wed, 13 May 2026 03:23:49 +0200 Subject: [PATCH] Allow written files to be read by other applications (#1092) * Allow written files to be read by other applications Intended for the log file mainly. Hopefully fixes https://github.com/TwilitRealm/dusk/issues/966? * Consistency --- src/dusk/io.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/dusk/io.cpp b/src/dusk/io.cpp index 3c87a66acf..4f6ef16a9b 100644 --- a/src/dusk/io.cpp +++ b/src/dusk/io.cpp @@ -17,6 +17,8 @@ using namespace dusk::io; #else #define MODE(val) val #endif +#define _SH_DENYNO 0 +#define _SH_DENYWR 0 #endif static FILE* ThrowIfNotOpen(const FileStream& file) { @@ -31,19 +33,19 @@ static FILE* ThrowIfNotOpen(const FileStream& file) { throw std::system_error(std::make_error_code(static_cast(code))); } -static FILE* OpenCore(const std::filesystem::path& path, const MODE_TYPE* mode) { +static FILE* OpenCore(const std::filesystem::path& path, const MODE_TYPE* mode, int shareFlag) { FILE* file; int err; + errno = 0; #if _WIN32 static_assert(std::is_same_v); - err = _wfopen_s(&file, path.c_str(), mode); + file = _wfsopen(path.c_str(), mode, shareFlag); #else - errno = 0; static_assert(std::is_same_v); file = fopen(path.c_str(), mode); - err = errno; #endif + err = errno; if (!file) { ThrowForError(err); @@ -52,8 +54,8 @@ static FILE* OpenCore(const std::filesystem::path& path, const MODE_TYPE* mode) return file; } -static FILE* OpenCore(const char* path, const MODE_TYPE* mode) { - return OpenCore(reinterpret_cast(path), mode); +static FILE* OpenCore(const char* path, const MODE_TYPE* mode, int shareFlag) { + return OpenCore(reinterpret_cast(path), mode, shareFlag); } FileStream::FileStream() noexcept : file(nullptr) { @@ -76,19 +78,19 @@ FileStream::~FileStream() { } FileStream FileStream::OpenRead(const char* utf8Path) { - return FileStream(OpenCore(utf8Path, MODE("rb"))); + return FileStream(OpenCore(utf8Path, MODE("rb"), _SH_DENYWR)); } FileStream FileStream::OpenRead(const std::filesystem::path& path) { - return FileStream(OpenCore(path, MODE("rb"))); + return FileStream(OpenCore(path, MODE("rb"), _SH_DENYWR)); } FileStream FileStream::Create(const char* utf8Path) { - return FileStream(OpenCore(utf8Path, MODE("wb"))); + return FileStream(OpenCore(utf8Path, MODE("wb"), _SH_DENYWR)); } FileStream FileStream::Create(const std::filesystem::path& path) { - return FileStream(OpenCore(path, MODE("wb"))); + return FileStream(OpenCore(path, MODE("wb"), _SH_DENYWR)); } std::vector FileStream::ReadFull() {