From a1c54beffed03bfd1f52e4834db3810faed5c06d Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 23 Dec 2025 10:52:20 +0800 Subject: [PATCH] Fix fopen on windows share files with read and share permissions. --- stream.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/stream.h b/stream.h index 38022bb..622b180 100644 --- a/stream.h +++ b/stream.h @@ -6,6 +6,18 @@ #include #include #include "cstr_util.h" +#if _WIN32 +#include +#ifndef _SH_DENYWR +#define _SH_DENYWR 0x20 +#endif +#ifndef _O_BINARY +#define _O_BINARY 0x8000 +#endif +#ifndef _O_RDONLY +#define _O_RDONLY 0x0000 +#endif +#endif class ReadStream { public: @@ -93,10 +105,24 @@ public: * @param filename File name (on Windows, UTF-8 encoding is supported) */ FileReadStream(const char* filename) { +#if _WIN32 + int fd = 0; + int re = fileop::open(filename, fd, _O_RDONLY | _O_BINARY, _SH_DENYWR); + if (re != 0) { + errored = true; + return; + } + fp = fileop::fdopen(fd, "rb"); + if (!fp) { + errored = true; + fileop::close(fd); + } +#else fp = fileop::fopen(filename, "rb"); if (!fp) { errored = true; } +#endif } virtual size_t read(uint8_t* buf, size_t size) { if (!fp) return 0;