Only call ensure_params_path once (#21229)

* Only call ensore_params_path once

* only ensure default params path once
old-commit-hash: fcf9f00a2a
This commit is contained in:
Willem Melching 2021-06-15 01:12:27 +02:00 committed by GitHub
parent ce94b4bc4b
commit 807c95a0f5
1 changed files with 12 additions and 3 deletions

View File

@ -78,7 +78,7 @@ int mkdir_p(std::string path) {
return 0;
}
bool ensure_params_path(const std::string &param_path, const std::string &key_path) {
static bool create_params_path(const std::string &param_path, const std::string &key_path) {
// Make sure params path exists
if (!util::file_exists(param_path) && mkdir_p(param_path) != 0) {
return false;
@ -117,6 +117,12 @@ bool ensure_params_path(const std::string &param_path, const std::string &key_pa
return chmod(key_path.c_str(), 0777) == 0;
}
static void ensure_params_path(const std::string &params_path) {
if (!create_params_path(params_path, params_path + "/d")) {
throw std::runtime_error(util::string_format("Failed to ensure params path, errno=%d", errno));
}
}
class FileLock {
public:
FileLock(const std::string& file_name, int op) : fn_(file_name), op_(op) {}
@ -225,9 +231,12 @@ std::unordered_map<std::string, uint32_t> keys = {
Params::Params(bool persistent_param) : Params(persistent_param ? persistent_params_path : default_params_path) {}
std::once_flag default_params_path_ensured;
Params::Params(const std::string &path) : params_path(path) {
if (!ensure_params_path(params_path, params_path + "/d")) {
throw std::runtime_error(util::string_format("Failed to ensure params path, errno=%d", errno));
if (path == default_params_path) {
std::call_once(default_params_path_ensured, ensure_params_path, path);
} else {
ensure_params_path(path);
}
}