Skip to content

Commit

Permalink
Write config.json using temp file (#839)
Browse files Browse the repository at this point in the history
Co-authored-by: SpyCheese <[email protected]>
  • Loading branch information
EmelyanenkoK and SpyCheese authored Dec 27, 2023
1 parent b3be428 commit c8918f0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
17 changes: 13 additions & 4 deletions dht-server/dht-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions dht-server/dht-server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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/";

Expand Down
17 changes: 13 additions & 4 deletions validator-engine/validator-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion validator-engine/validator-engine.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
This file is part of TON Blockchain source code.
TON Blockchain is free software; you can redistribute it and/or
Expand Down Expand Up @@ -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_ = "";

Expand Down

0 comments on commit c8918f0

Please sign in to comment.