Merge pull request #542

9b4f8b4 wallet: save to a temporary file, then rename (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2015-12-18 12:14:26 +02:00
commit 45249d92a7
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
2 changed files with 20 additions and 1 deletions

View File

@ -48,6 +48,7 @@ using namespace epee;
#include "cryptonote_protocol/blobdatatype.h" #include "cryptonote_protocol/blobdatatype.h"
#include "mnemonics/electrum-words.h" #include "mnemonics/electrum-words.h"
#include "common/dns_utils.h" #include "common/dns_utils.h"
#include "common/util.h"
#include "rapidjson/document.h" #include "rapidjson/document.h"
#include "rapidjson/writer.h" #include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
@ -1283,8 +1284,20 @@ void wallet2::store()
std::string buf; std::string buf;
bool r = ::serialization::dump_binary(cache_file_data, buf); bool r = ::serialization::dump_binary(cache_file_data, buf);
r = r && epee::file_io_utils::save_string_to_file(m_wallet_file, buf);
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_wallet_file); THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_wallet_file);
// save to new file, rename main to old, rename new to main
// at all times, there should be a valid file on disk
const std::string new_file = m_wallet_file + ".new";
const std::string old_file = m_wallet_file + ".old";
r = epee::file_io_utils::save_string_to_file(new_file, buf);
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, new_file);
boost::filesystem::remove(old_file); // probably does not exist
std::error_code e = tools::replace_file(m_wallet_file, old_file);
THROW_WALLET_EXCEPTION_IF(e, error::file_save_error, m_wallet_file, e);
e = tools::replace_file(new_file, m_wallet_file);
THROW_WALLET_EXCEPTION_IF(e, error::file_save_error, m_wallet_file, e);
boost::filesystem::remove(old_file);
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
uint64_t wallet2::unlocked_balance() const uint64_t wallet2::unlocked_balance() const

View File

@ -192,6 +192,12 @@ namespace tools
{ {
} }
explicit file_error_base(std::string&& loc, const std::string& file, const std::error_code &e)
: wallet_logic_error(std::move(loc), std::string(file_error_messages[msg_index]) + " \"" + file + "\": " + e.message())
, m_file(file)
{
}
const std::string& file() const { return m_file; } const std::string& file() const { return m_file; }
std::string to_string() const { return wallet_logic_error::to_string(); } std::string to_string() const { return wallet_logic_error::to_string(); }