From 70ddb0ce3980229064332fc548e8e7bdd985fb0d Mon Sep 17 00:00:00 2001 From: William JCM Date: Mon, 4 Apr 2022 09:21:31 +0200 Subject: [PATCH] UESaveFile: don't create a backup if the file is already temporary. Also improve error handling. --- src/UESaveFile/UESaveFile.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/UESaveFile/UESaveFile.cpp b/src/UESaveFile/UESaveFile.cpp index a5a2781..45b30fd 100644 --- a/src/UESaveFile/UESaveFile.cpp +++ b/src/UESaveFile/UESaveFile.cpp @@ -67,6 +67,13 @@ auto UESaveFile::props() -> Containers::ArrayView { } auto UESaveFile::saveToFile() -> bool { + bool temp_file = _filepath.hasSuffix(".tmp"); + + if(!temp_file && !Utility::Path::copy(_filepath, _filepath + ".bak"_s)) { + _lastError = "Couldn't create a backup for " + _filepath; + return false; + } + BinaryWriter writer{_filepath + ".tmp"_s}; if(!writer.open()) { @@ -125,12 +132,8 @@ auto UESaveFile::saveToFile() -> bool { writer.closeFile(); - if(!Utility::Path::copy(_filepath, _filepath + ".bak"_s)) { - return false; - } - - if(!Utility::Path::copy(_filepath + ".tmp"_s, _filepath)) { - Utility::Path::copy(_filepath + ".bak"_s, _filepath); + if(!temp_file && !Utility::Path::copy(_filepath + ".tmp"_s, _filepath)) { + _lastError = "Couldn't save the file properly."; return false; } else {