From e81533b73da641f12ca90f364843624b5b86f3b5 Mon Sep 17 00:00:00 2001 From: Xadhoom <> Date: Tue, 19 Mar 2019 08:30:44 +0000 Subject: [PATCH] Fix miniwin/misc_io.cpp --- SourceX/miniwin/misc_io.cpp | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/SourceX/miniwin/misc_io.cpp b/SourceX/miniwin/misc_io.cpp index 79070352e..0cb447c4c 100644 --- a/SourceX/miniwin/misc_io.cpp +++ b/SourceX/miniwin/misc_io.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "devilution.h" #include "stubs.h" @@ -129,19 +130,23 @@ WINBOOL CloseHandle(HANDLE hObject) return true; std::unique_ptr ufile(file); // ensure that delete file is // called on returning - bool ret = true; - std::ofstream filestream(file->path + ".tmp", std::ios::binary); - if (filestream.fail()) - ret = false; - filestream.write(file->buf.data(), file->buf.size()); - if (filestream.fail()) - ret = false; - if (std::rename((file->path + ".tmp").c_str(), file->path.c_str())) - ret = false; - if(!ret) { + files.erase(file); + try { + std::ofstream filestream(file->path + ".tmp", std::ios::binary | std::ios::trunc); + if (filestream.fail()) + throw std::runtime_error("ofstream"); + filestream.write(file->buf.data(), file->buf.size()); + if (filestream.fail()) + throw std::runtime_error("ofstream::write"); + std::remove(file->path.c_str()); + if (std::rename((file->path + ".tmp").c_str(), file->path.c_str())) + throw std::runtime_error("rename"); + return true; + } catch (std::runtime_error e) { + // log DialogBoxParam(ghInst, DVL_MAKEINTRESOURCE(IDD_DIALOG7), ghMainWnd, (DLGPROC)FuncDlg, (LPARAM)file->path.c_str()); + return false; } - return ret; } } // namespace dvl