Enhance Windows file handling with improved sync and error checking in read/write operations

This commit is contained in:
ImBenji
2025-12-02 15:15:47 +00:00
parent 7adca647a6
commit 5d9b34e030

View File

@@ -55,6 +55,7 @@ void SweepstoreFileHandle::close() {
#endif
}
#if defined(_WIN32) || defined(WITH_UNREAL)
// readSeek
void SweepstoreFileHandle::readSeek(std::streampos pos, std::ios::seekdir dir) {
#ifdef WITH_UNREAL
@@ -68,17 +69,14 @@ void SweepstoreFileHandle::readSeek(std::streampos pos, std::ios::seekdir dir) {
unrealHandle->SeekFromEnd(unrealPos);
}
#else
#ifdef _WIN32
// On Windows, flush and sync to disk, then invalidate buffers
stream->flush();
stream->sync();
stream->clear();
// Sync both pointers to same position
stream->seekp(pos, dir);
stream->seekg(pos, dir);
#else
stream->seekg(pos, dir);
#endif
// Windows
// On Windows, flush and sync to disk, then invalidate buffers
stream->flush();
stream->sync();
stream->clear();
// Sync both pointers to same position
stream->seekp(pos, dir);
stream->seekg(pos, dir);
#endif
}
@@ -88,10 +86,9 @@ void SweepstoreFileHandle::writeSeek(std::streampos pos, std::ios::seekdir dir)
// Same as readSeek for Unreal
readSeek(pos, dir);
#else
#ifdef _WIN32
stream->flush();
stream->sync();
#endif
// Windows
stream->flush();
stream->sync();
stream->seekp(pos, dir);
#endif
}
@@ -101,13 +98,12 @@ void SweepstoreFileHandle::readBytes(char* buffer, std::streamsize size) {
#ifdef WITH_UNREAL
unrealHandle->Read(reinterpret_cast<uint8*>(buffer), size);
#else
// Windows
stream->read(buffer, size);
#ifdef _WIN32
// Check for read errors on Windows
if (stream->fail() && !stream->eof()) {
stream->clear();
}
#endif
// Check for read errors on Windows
if (stream->fail() && !stream->eof()) {
stream->clear();
}
#endif
}
@@ -117,12 +113,12 @@ void SweepstoreFileHandle::writeBytes(const char* buffer, std::streamsize size)
unrealHandle->Write(reinterpret_cast<const uint8*>(buffer), size);
unrealHandle->Flush(); // Unreal requires explicit flush
#else
// Windows
stream->write(buffer, size);
#ifdef _WIN32
// Check for write errors on Windows
if (stream->fail()) {
stream->clear();
}
#endif
// Check for write errors on Windows
if (stream->fail()) {
stream->clear();
}
#endif
}
}
#endif // _WIN32 || WITH_UNREAL