mirror of
https://github.com/ton-blockchain/ton
synced 2025-02-12 11:12:16 +00:00
Write config.json using temp file (#839)
Co-authored-by: SpyCheese <mikle98@yandex.ru>
This commit is contained in:
parent
b3be4283ff
commit
c8918f0c02
4 changed files with 33 additions and 9 deletions
|
@ -572,6 +572,12 @@ void DhtServer::load_config(td::Promise<td::Unit> promise) {
|
|||
config_file_ = db_root_ + "/config.json";
|
||||
}
|
||||
auto conf_data_R = td::read_file(config_file_);
|
||||
if (conf_data_R.is_error()) {
|
||||
conf_data_R = td::read_file(temp_config_file());
|
||||
if (conf_data_R.is_ok()) {
|
||||
td::rename(temp_config_file(), config_file_).ensure();
|
||||
}
|
||||
}
|
||||
if (conf_data_R.is_error()) {
|
||||
auto P = td::PromiseCreator::lambda(
|
||||
[name = local_config_, new_name = config_file_, promise = std::move(promise)](td::Result<td::Unit> R) {
|
||||
|
@ -620,12 +626,15 @@ void DhtServer::load_config(td::Promise<td::Unit> promise) {
|
|||
void DhtServer::write_config(td::Promise<td::Unit> promise) {
|
||||
auto s = td::json_encode<std::string>(td::ToJson(*config_.tl().get()), true);
|
||||
|
||||
auto S = td::write_file(config_file_, s);
|
||||
if (S.is_ok()) {
|
||||
promise.set_value(td::Unit());
|
||||
} else {
|
||||
auto S = td::write_file(temp_config_file(), s);
|
||||
if (S.is_error()) {
|
||||
td::unlink(temp_config_file()).ignore();
|
||||
promise.set_error(std::move(S));
|
||||
return;
|
||||
}
|
||||
td::unlink(config_file_).ignore();
|
||||
TRY_STATUS_PROMISE(promise, td::rename(temp_config_file(), config_file_));
|
||||
promise.set_value(td::Unit());
|
||||
}
|
||||
|
||||
td::Promise<ton::PublicKey> DhtServer::get_key_promise(td::MultiPromise::InitGuard &ig) {
|
||||
|
|
|
@ -109,6 +109,9 @@ class DhtServer : public td::actor::Actor {
|
|||
std::string local_config_ = "";
|
||||
std::string global_config_ = "ton-global.config";
|
||||
std::string config_file_;
|
||||
std::string temp_config_file() const {
|
||||
return config_file_ + ".tmp";
|
||||
}
|
||||
|
||||
std::string db_root_ = "/var/ton-work/db/";
|
||||
|
||||
|
|
|
@ -1595,6 +1595,12 @@ void ValidatorEngine::load_config(td::Promise<td::Unit> promise) {
|
|||
config_file_ = db_root_ + "/config.json";
|
||||
}
|
||||
auto conf_data_R = td::read_file(config_file_);
|
||||
if (conf_data_R.is_error()) {
|
||||
conf_data_R = td::read_file(temp_config_file());
|
||||
if (conf_data_R.is_ok()) {
|
||||
td::rename(temp_config_file(), config_file_).ensure();
|
||||
}
|
||||
}
|
||||
if (conf_data_R.is_error()) {
|
||||
auto P = td::PromiseCreator::lambda(
|
||||
[name = local_config_, new_name = config_file_, promise = std::move(promise)](td::Result<td::Unit> R) {
|
||||
|
@ -1643,12 +1649,15 @@ void ValidatorEngine::load_config(td::Promise<td::Unit> promise) {
|
|||
void ValidatorEngine::write_config(td::Promise<td::Unit> promise) {
|
||||
auto s = td::json_encode<std::string>(td::ToJson(*config_.tl().get()), true);
|
||||
|
||||
auto S = td::write_file(config_file_, s);
|
||||
if (S.is_ok()) {
|
||||
promise.set_value(td::Unit());
|
||||
} else {
|
||||
auto S = td::write_file(temp_config_file(), s);
|
||||
if (S.is_error()) {
|
||||
td::unlink(temp_config_file()).ignore();
|
||||
promise.set_error(std::move(S));
|
||||
return;
|
||||
}
|
||||
td::unlink(config_file_).ignore();
|
||||
TRY_STATUS_PROMISE(promise, td::rename(temp_config_file(), config_file_));
|
||||
promise.set_value(td::Unit());
|
||||
}
|
||||
|
||||
td::Promise<ton::PublicKey> ValidatorEngine::get_key_promise(td::MultiPromise::InitGuard &ig) {
|
||||
|
|
|
@ -152,6 +152,9 @@ class ValidatorEngine : public td::actor::Actor {
|
|||
std::string local_config_ = "";
|
||||
std::string global_config_ = "ton-global.config";
|
||||
std::string config_file_;
|
||||
std::string temp_config_file() const {
|
||||
return config_file_ + ".tmp";
|
||||
}
|
||||
|
||||
std::string fift_dir_ = "";
|
||||
|
||||
|
|
Loading…
Reference in a new issue