mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Replaced ifstreams with file read inside the config and patch config read. (#220)
This commit is contained in:
58
src/conf.cpp
58
src/conf.cpp
@@ -16,6 +16,8 @@ namespace conf
|
||||
// Stores the initial startup mode of the node.
|
||||
ROLE startup_mode;
|
||||
|
||||
constexpr int FILE_PERMS = 0644;
|
||||
|
||||
constexpr const char *ROLE_OBSERVER = "observer";
|
||||
constexpr const char *ROLE_VALIDATOR = "validator";
|
||||
constexpr const char *PUBLIC = "public";
|
||||
@@ -208,19 +210,24 @@ namespace conf
|
||||
int read_config(contract_config &cfg)
|
||||
{
|
||||
// Read the config file into json document object.
|
||||
std::string buf;
|
||||
if (util::read_from_fd(ctx.config_fd, buf) == -1)
|
||||
{
|
||||
std::cerr << "Error reading from the config file. " << errno << '\n';
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::ifstream ifs(ctx.config_file);
|
||||
jsoncons::ojson d;
|
||||
try
|
||||
{
|
||||
d = jsoncons::ojson::parse(ifs, jsoncons::strict_json_parsing());
|
||||
d = jsoncons::ojson::parse(buf, jsoncons::strict_json_parsing());
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cerr << "Invalid config file format. " << e.what() << '\n';
|
||||
return -1;
|
||||
}
|
||||
ifs.close();
|
||||
buf.clear();
|
||||
|
||||
try
|
||||
{
|
||||
@@ -658,20 +665,34 @@ namespace conf
|
||||
if (!util::is_file_exists(path))
|
||||
return 0;
|
||||
|
||||
const int fd = open(path.data(), O_RDONLY);
|
||||
if (fd == -1)
|
||||
{
|
||||
std::cerr << "Error opening the patch config file. " << errno << '\n';
|
||||
return -1;
|
||||
}
|
||||
|
||||
// If patch file exist, read the patch file values to a json doc and then persist the values into hp.cfg.
|
||||
std::ifstream ifs(path);
|
||||
std::string buf;
|
||||
if (util::read_from_fd(fd, buf) == -1)
|
||||
{
|
||||
std::cerr << "Error reading from the patch config file. " << errno << '\n';
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
jsoncons::ojson jdoc;
|
||||
try
|
||||
{
|
||||
jdoc = jsoncons::ojson::parse(ifs, jsoncons::strict_json_parsing());
|
||||
jdoc = jsoncons::ojson::parse(buf, jsoncons::strict_json_parsing());
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
ifs.close();
|
||||
std::cerr << "Invalid patch config file format. " << e.what() << '\n';
|
||||
return -1;
|
||||
}
|
||||
ifs.close();
|
||||
buf.clear();
|
||||
|
||||
// Persist new changes to HP config file.
|
||||
if (parse_contract_section_json(cfg.contract, jdoc, false) == -1 ||
|
||||
@@ -869,20 +890,33 @@ namespace conf
|
||||
*/
|
||||
int write_json_file(const std::string &file_path, const jsoncons::ojson &d)
|
||||
{
|
||||
std::ofstream ofs(file_path);
|
||||
std::string json;
|
||||
// Convert json object to a string.
|
||||
try
|
||||
{
|
||||
jsoncons::json_options options;
|
||||
options.object_array_line_splits(jsoncons::line_split_kind::multi_line);
|
||||
ofs << jsoncons::pretty_print(d, options);
|
||||
std::ostringstream os;
|
||||
os << jsoncons::pretty_print(d, options);
|
||||
json = os.str();
|
||||
os.clear();
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cerr << "Writing file failed. " << ctx.config_file << std::endl;
|
||||
ofs.close();
|
||||
std::cerr << "Converting json to string failed. " << file_path << std::endl;
|
||||
return -1;
|
||||
}
|
||||
ofs.close();
|
||||
|
||||
// O_TRUNC flag is used to trucate existing content from the file.
|
||||
const int fd = open(file_path.data(), O_CREAT | O_RDWR | O_TRUNC, FILE_PERMS);
|
||||
if (fd == -1 || write(fd, json.data(), json.size()) == -1)
|
||||
{
|
||||
std::cerr << "Writing file failed. " << file_path << std::endl;
|
||||
if (fd != -1)
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user