Browse Source

Fix miniwin/misc_io.cpp

pull/44/head
Xadhoom 7 years ago committed by Anders Jenbo
parent
commit
e81533b73d
  1. 27
      SourceX/miniwin/misc_io.cpp

27
SourceX/miniwin/misc_io.cpp

@ -6,6 +6,7 @@
#include <algorithm>
#include <fstream>
#include <memory>
#include <stdexcept>
#include "devilution.h"
#include "stubs.h"
@ -129,19 +130,23 @@ WINBOOL CloseHandle(HANDLE hObject)
return true;
std::unique_ptr<memfile> 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

Loading…
Cancel
Save