1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-14 12:12:21 +00:00

Write config.json using temp file (#839)

Co-authored-by: SpyCheese <mikle98@yandex.ru>
This commit is contained in:
EmelyanenkoK 2023-12-27 15:50:09 +03:00 committed by GitHub
parent b3be4283ff
commit c8918f0c02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 9 deletions

View file

@ -572,6 +572,12 @@ void DhtServer::load_config(td::Promise<td::Unit> promise) {
config_file_ = db_root_ + "/config.json"; config_file_ = db_root_ + "/config.json";
} }
auto conf_data_R = td::read_file(config_file_); 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()) { if (conf_data_R.is_error()) {
auto P = td::PromiseCreator::lambda( auto P = td::PromiseCreator::lambda(
[name = local_config_, new_name = config_file_, promise = std::move(promise)](td::Result<td::Unit> R) { [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) { 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::json_encode<std::string>(td::ToJson(*config_.tl().get()), true);
auto S = td::write_file(config_file_, s); auto S = td::write_file(temp_config_file(), s);
if (S.is_ok()) { if (S.is_error()) {
promise.set_value(td::Unit()); td::unlink(temp_config_file()).ignore();
} else {
promise.set_error(std::move(S)); 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) { td::Promise<ton::PublicKey> DhtServer::get_key_promise(td::MultiPromise::InitGuard &ig) {

View file

@ -109,6 +109,9 @@ class DhtServer : public td::actor::Actor {
std::string local_config_ = ""; std::string local_config_ = "";
std::string global_config_ = "ton-global.config"; std::string global_config_ = "ton-global.config";
std::string config_file_; std::string config_file_;
std::string temp_config_file() const {
return config_file_ + ".tmp";
}
std::string db_root_ = "/var/ton-work/db/"; std::string db_root_ = "/var/ton-work/db/";

View file

@ -1595,6 +1595,12 @@ void ValidatorEngine::load_config(td::Promise<td::Unit> promise) {
config_file_ = db_root_ + "/config.json"; config_file_ = db_root_ + "/config.json";
} }
auto conf_data_R = td::read_file(config_file_); 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()) { if (conf_data_R.is_error()) {
auto P = td::PromiseCreator::lambda( auto P = td::PromiseCreator::lambda(
[name = local_config_, new_name = config_file_, promise = std::move(promise)](td::Result<td::Unit> R) { [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) { 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::json_encode<std::string>(td::ToJson(*config_.tl().get()), true);
auto S = td::write_file(config_file_, s); auto S = td::write_file(temp_config_file(), s);
if (S.is_ok()) { if (S.is_error()) {
promise.set_value(td::Unit()); td::unlink(temp_config_file()).ignore();
} else {
promise.set_error(std::move(S)); 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) { td::Promise<ton::PublicKey> ValidatorEngine::get_key_promise(td::MultiPromise::InitGuard &ig) {

View file

@ -1,4 +1,4 @@
/* /*
This file is part of TON Blockchain source code. This file is part of TON Blockchain source code.
TON Blockchain is free software; you can redistribute it and/or TON Blockchain is free software; you can redistribute it and/or
@ -152,6 +152,9 @@ class ValidatorEngine : public td::actor::Actor {
std::string local_config_ = ""; std::string local_config_ = "";
std::string global_config_ = "ton-global.config"; std::string global_config_ = "ton-global.config";
std::string config_file_; std::string config_file_;
std::string temp_config_file() const {
return config_file_ + ".tmp";
}
std::string fift_dir_ = ""; std::string fift_dir_ = "";