From e2084e1e9dff6f6e816ac63f02c49fb8ae9178b2 Mon Sep 17 00:00:00 2001 From: def Date: Mon, 20 Dec 2021 10:54:46 +0100 Subject: [PATCH] Use temporary file when saving editor file (fixes #4476) --- src/game/editor/io.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/game/editor/io.cpp b/src/game/editor/io.cpp index cbda8576d..d44ab6a42 100644 --- a/src/game/editor/io.cpp +++ b/src/game/editor/io.cpp @@ -221,13 +221,15 @@ int CEditor::Save(const char *pFilename) int CEditorMap::Save(class IStorage *pStorage, const char *pFileName) { - char aBuf[256]; - str_format(aBuf, sizeof(aBuf), "saving to '%s'...", pFileName); + char aFileNameTmp[IO_MAX_PATH_LENGTH]; + str_format(aFileNameTmp, sizeof(aFileNameTmp), "%s.%d.tmp", pFileName, pid()); + char aBuf[IO_MAX_PATH_LENGTH]; + str_format(aBuf, sizeof(aBuf), "saving to '%s'...", aFileNameTmp); m_pEditor->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "editor", aBuf); CDataFileWriter df; - if(!df.Open(pStorage, pFileName)) + if(!df.Open(pStorage, aFileNameTmp)) { - str_format(aBuf, sizeof(aBuf), "failed to open file '%s'...", pFileName); + str_format(aBuf, sizeof(aBuf), "failed to open file '%s'...", aFileNameTmp); m_pEditor->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "editor", aBuf); return 0; } @@ -553,6 +555,13 @@ int CEditorMap::Save(class IStorage *pStorage, const char *pFileName) df.Finish(); m_pEditor->Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "editor", "saving done"); + str_format(aBuf, sizeof(aBuf), "moving '%s' to '%s'", aFileNameTmp, pFileName); + m_pEditor->Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "editor", aBuf); + if(!pStorage->RenameFile(aFileNameTmp, pFileName, IStorage::TYPE_SAVE)) + { + return 0; + } + // send rcon.. if we can if(m_pEditor->Client()->RconAuthed()) {