Only call ensure_params_path once (#21229)

* Only call ensore_params_path once

* only ensure default params path once
pull/21270/head
Willem Melching 4 years ago committed by GitHub
parent 04ef4eeeda
commit fcf9f00a2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      selfdrive/common/params.cc

@ -78,7 +78,7 @@ int mkdir_p(std::string path) {
return 0; 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 // Make sure params path exists
if (!util::file_exists(param_path) && mkdir_p(param_path) != 0) { if (!util::file_exists(param_path) && mkdir_p(param_path) != 0) {
return false; 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; 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 { class FileLock {
public: public:
FileLock(const std::string& file_name, int op) : fn_(file_name), op_(op) {} 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) {} 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) { Params::Params(const std::string &path) : params_path(path) {
if (!ensure_params_path(params_path, params_path + "/d")) { if (path == default_params_path) {
throw std::runtime_error(util::string_format("Failed to ensure params path, errno=%d", errno)); std::call_once(default_params_path_ensured, ensure_params_path, path);
} else {
ensure_params_path(path);
} }
} }

Loading…
Cancel
Save