Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI wallet: avoid directly overwriting wallet file on exit #1109 #1195

Merged
merged 11 commits into from
Aug 17, 2018
29 changes: 28 additions & 1 deletion libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ class wallet_api_impl
wlog( "saving wallet to file ${fn}", ("fn", wallet_filename) );

string data = fc::json::to_pretty_string( _wallet );

try
{
enable_umask_protection();
Expand All @@ -808,14 +809,40 @@ class wallet_api_impl
//
// http://en.wikipedia.org/wiki/Most_vexing_parse
//
fc::ofstream outfile{ fc::path( wallet_filename ) };
std::string tmp_wallet_filename = wallet_filename + ".tmp";
fc::ofstream outfile{ fc::path( tmp_wallet_filename ) };
outfile.write( data.c_str(), data.length() );
outfile.flush();
outfile.close();

wlog( "saved successfully wallet to tmp file ${fn}", ("fn", tmp_wallet_filename) );

std::string wallet_file_content;
fc::read_file_contents(tmp_wallet_filename, wallet_file_content);

if (wallet_file_content == data) {
wlog( "validated successfully tmp wallet file ${fn}", ("fn", tmp_wallet_filename) );

fc::rename( tmp_wallet_filename, wallet_filename );

wlog( "renamed successfully tmp wallet file ${fn}", ("fn", tmp_wallet_filename) );
}
else
{
FC_THROW("tmp wallet file cannot be validated ${fn}", ("fn", tmp_wallet_filename) );
}

wlog( "successfully saved wallet to file ${fn}", ("fn", wallet_filename) );

disable_umask_protection();
}
catch(...)
{
string ws_password = _wallet.ws_password;
_wallet.ws_password = "";
wlog("wallet file content is next: ${data}", ("data", fc::json::to_pretty_string( _wallet ) ) );
_wallet.ws_password = ws_password;

disable_umask_protection();
throw;
}
Expand Down