From d9dc49199f7cd1cb33f81d28cd87fdaf246700a2 Mon Sep 17 00:00:00 2001 From: staphen Date: Sat, 1 Feb 2025 22:39:57 -0500 Subject: [PATCH] Do not depend on FileExists() when opening save files for writing --- Source/mpq/mpq_writer.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Source/mpq/mpq_writer.cpp b/Source/mpq/mpq_writer.cpp index 201d502e1..c425772f6 100644 --- a/Source/mpq/mpq_writer.cpp +++ b/Source/mpq/mpq_writer.cpp @@ -91,9 +91,9 @@ MpqWriter::MpqWriter(const char *path) const std::string dir = std::string(Dirname(path)); RecursivelyCreateDir(dir.c_str()); LogVerbose("Opening {}", path); + bool isNewFile = false; std::string error; - bool exists = FileExists(path); - if (!exists) { + if (!FileExists(path)) { // FileExists() may return false in the case of an error // so we use "ab" instead of "wb" to avoid accidentally // truncating an existing file @@ -112,6 +112,7 @@ MpqWriter::MpqWriter(const char *path) goto on_error; } size_ = static_cast(fileSize); + isNewFile = size_ == 0; LogVerbose("GetFileSize(\"{}\") = {}", path, size_); if (!stream_.Open(path, "r+b")) { @@ -124,7 +125,7 @@ MpqWriter::MpqWriter(const char *path) if (blockTable_ == nullptr || hashTable_ == nullptr) { MpqFileHeader fhdr; - if (!exists) { + if (isNewFile) { InitDefaultMpqHeader(&fhdr); } else if (!ReadMPQHeader(&fhdr)) { error = "Failed to read MPQ header"; @@ -162,7 +163,7 @@ MpqWriter::MpqWriter(const char *path) // Write garbage header and tables because some platforms cannot `Seekp` beyond EOF. // The data is incorrect at this point, it will be overwritten on Close. - if (!exists) + if (isNewFile) WriteHeaderAndTables(); #endif }