Enhance Windows file handling with improved sync and error checking in read/write operations
This commit is contained in:
@@ -69,11 +69,16 @@ void SweepstoreFileHandle::readSeek(std::streampos pos, std::ios::seekdir dir) {
|
|||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#ifdef _WIN32
|
#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->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
|
#endif
|
||||||
stream->seekg(pos, dir);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +89,8 @@ void SweepstoreFileHandle::writeSeek(std::streampos pos, std::ios::seekdir dir)
|
|||||||
readSeek(pos, dir);
|
readSeek(pos, dir);
|
||||||
#else
|
#else
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
stream->flush(); // Flush any pending writes
|
stream->flush();
|
||||||
|
stream->sync();
|
||||||
#endif
|
#endif
|
||||||
stream->seekp(pos, dir);
|
stream->seekp(pos, dir);
|
||||||
#endif
|
#endif
|
||||||
@@ -96,6 +102,12 @@ void SweepstoreFileHandle::readBytes(char* buffer, std::streamsize size) {
|
|||||||
unrealHandle->Read(reinterpret_cast<uint8*>(buffer), size);
|
unrealHandle->Read(reinterpret_cast<uint8*>(buffer), size);
|
||||||
#else
|
#else
|
||||||
stream->read(buffer, size);
|
stream->read(buffer, size);
|
||||||
|
#ifdef _WIN32
|
||||||
|
// Check for read errors on Windows
|
||||||
|
if (stream->fail() && !stream->eof()) {
|
||||||
|
stream->clear();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,5 +118,11 @@ void SweepstoreFileHandle::writeBytes(const char* buffer, std::streamsize size)
|
|||||||
unrealHandle->Flush(); // Unreal requires explicit flush
|
unrealHandle->Flush(); // Unreal requires explicit flush
|
||||||
#else
|
#else
|
||||||
stream->write(buffer, size);
|
stream->write(buffer, size);
|
||||||
|
#ifdef _WIN32
|
||||||
|
// Check for write errors on Windows
|
||||||
|
if (stream->fail()) {
|
||||||
|
stream->clear();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -39,10 +39,26 @@ public:
|
|||||||
void close();
|
void close();
|
||||||
|
|
||||||
// Windows-compatible I/O wrappers
|
// Windows-compatible I/O wrappers
|
||||||
|
#if defined(_WIN32) || defined(WITH_UNREAL)
|
||||||
void readSeek(std::streampos pos, std::ios::seekdir dir = std::ios::beg);
|
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 writeSeek(std::streampos pos, std::ios::seekdir dir = std::ios::beg);
|
||||||
void readBytes(char* buffer, std::streamsize size);
|
void readBytes(char* buffer, std::streamsize size);
|
||||||
void writeBytes(const 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(SweepstoreFileHandle&&) noexcept = default;
|
||||||
SweepstoreFileHandle& operator=(SweepstoreFileHandle&&) noexcept = default;
|
SweepstoreFileHandle& operator=(SweepstoreFileHandle&&) noexcept = default;
|
||||||
|
|||||||
Reference in New Issue
Block a user