diff --git a/cpp/src/Private/sweepstore/utils/file_handle.cpp b/cpp/src/Private/sweepstore/utils/file_handle.cpp index 25c182a..05b6d4a 100644 --- a/cpp/src/Private/sweepstore/utils/file_handle.cpp +++ b/cpp/src/Private/sweepstore/utils/file_handle.cpp @@ -69,11 +69,16 @@ void SweepstoreFileHandle::readSeek(std::streampos pos, std::ios::seekdir dir) { } #else #ifdef _WIN32 - // On Windows, flush output and sync to invalidate input buffer + // On Windows, flush and sync to disk, then invalidate buffers stream->flush(); - stream->seekp(pos, dir); // Sync put pointer + stream->sync(); + stream->clear(); + // Sync both pointers to same position + stream->seekp(pos, dir); + stream->seekg(pos, dir); + #else + stream->seekg(pos, dir); #endif - stream->seekg(pos, dir); #endif } @@ -84,7 +89,8 @@ void SweepstoreFileHandle::writeSeek(std::streampos pos, std::ios::seekdir dir) readSeek(pos, dir); #else #ifdef _WIN32 - stream->flush(); // Flush any pending writes + stream->flush(); + stream->sync(); #endif stream->seekp(pos, dir); #endif @@ -96,6 +102,12 @@ void SweepstoreFileHandle::readBytes(char* buffer, std::streamsize size) { unrealHandle->Read(reinterpret_cast(buffer), size); #else stream->read(buffer, size); + #ifdef _WIN32 + // Check for read errors on Windows + if (stream->fail() && !stream->eof()) { + stream->clear(); + } + #endif #endif } @@ -106,5 +118,11 @@ void SweepstoreFileHandle::writeBytes(const char* buffer, std::streamsize size) unrealHandle->Flush(); // Unreal requires explicit flush #else stream->write(buffer, size); + #ifdef _WIN32 + // Check for write errors on Windows + if (stream->fail()) { + stream->clear(); + } + #endif #endif } \ No newline at end of file diff --git a/cpp/src/Public/sweepstore/utils/file_handle.h b/cpp/src/Public/sweepstore/utils/file_handle.h index 5b32b04..efcf87c 100644 --- a/cpp/src/Public/sweepstore/utils/file_handle.h +++ b/cpp/src/Public/sweepstore/utils/file_handle.h @@ -39,10 +39,26 @@ public: void close(); // Windows-compatible I/O wrappers +#if defined(_WIN32) || defined(WITH_UNREAL) void readSeek(std::streampos pos, std::ios::seekdir dir = std::ios::beg); void writeSeek(std::streampos pos, std::ios::seekdir dir = std::ios::beg); void readBytes(char* buffer, std::streamsize size); void writeBytes(const char* buffer, std::streamsize size); +#else + // Inline for non-Windows to avoid overhead + inline void readSeek(std::streampos pos, std::ios::seekdir dir = std::ios::beg) { + stream->seekg(pos, dir); + } + inline void writeSeek(std::streampos pos, std::ios::seekdir dir = std::ios::beg) { + stream->seekp(pos, dir); + } + inline void readBytes(char* buffer, std::streamsize size) { + stream->read(buffer, size); + } + inline void writeBytes(const char* buffer, std::streamsize size) { + stream->write(buffer, size); + } +#endif SweepstoreFileHandle(SweepstoreFileHandle&&) noexcept = default; SweepstoreFileHandle& operator=(SweepstoreFileHandle&&) noexcept = default;