    umask(prev_mask);
    return 0;
  }
  
  static int ensure_dir_exists(std::string path) {
    // TODO: replace by std::filesystem::create_directories
    return mkdir_p(path.c_str());
  }
  
  
  Params::Params(bool persistent_param){
    params_path = persistent_param ? persistent_params_path : default_params_path;
  }
 
</s> remove Params::Params(bool persistent_param){
  params_path = persistent_param ? persistent_params_path : default_params_path;
 </s> add   // Ensure permissions are correct in case we didn't create the symlink
  return chmod(key_path.c_str(), 0777) == 0; </s> remove   // Make sure params path exists
  result = ensure_dir_exists(params_path);
  if (result < 0) {
    goto cleanup;
  }

  // See if the symlink exists, otherwise create it
  path = params_path + "/d";
  struct stat st;
  if (stat(path.c_str(), &st) == -1) {
    // Create temp folder
    path = params_path + "/.tmp_XXXXXX";

    char *t = mkdtemp((char*)path.c_str());
    if (t == NULL){
      goto cleanup;
    }
    std::string tmp_dir(t);

    // Set permissions
    result = chmod(tmp_dir.c_str(), 0777);
    if (result < 0) {
      goto cleanup;
    }

    // Symlink it to temp link
    tmp_path = tmp_dir + ".link";
    result = symlink(tmp_dir.c_str(), tmp_path.c_str());
    if (result < 0) {
      goto cleanup;
    }

    // Move symlink to <params>/d
    path = params_path + "/d";
    result = rename(tmp_path.c_str(), path.c_str());
    if (result < 0) {
      goto cleanup;
    }
  } else {
    // Ensure permissions are correct in case we didn't create the symlink
    result = chmod(path.c_str(), 0777);
    if (result < 0) {
      goto cleanup;
    }
  }

 </s> add  </s> remove Params::Params(const std::string &path) {
  params_path = path;
 </s> add Params::Params(bool persistent_param) : Params(persistent_param ? persistent_params_path : default_params_path) {}

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));
  }
static bool ensure_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;
  }

  // See if the symlink exists, otherwise create it
  if (!util::file_exists(key_path)) {
    // 1) Create temp folder
    // 2) Set permissions
    // 3) Symlink it to temp link
    // 4) Move symlink to <params>/d

    std::string tmp_path = param_path + "/.tmp_XXXXXX";
    // this should be OK since mkdtemp just replaces characters in place
    char *tmp_dir = mkdtemp((char *)tmp_path.c_str());
    if (tmp_dir == NULL) {
      return false;
    }

    if (chmod(tmp_dir, 0777) != 0) {
      return false;
    }

    std::string link_path = std::string(tmp_dir) + ".link";
    if (symlink(tmp_dir, link_path.c_str()) != 0) {
      return false;
    }
    umask(prev_mask);
    return 0;
  }
  
static bool ensure_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;
  }

  // See if the symlink exists, otherwise create it
  if (!util::file_exists(key_path)) {
    // 1) Create temp folder
    // 2) Set permissions
    // 3) Symlink it to temp link
    // 4) Move symlink to <params>/d

    std::string tmp_path = param_path + "/.tmp_XXXXXX";
    // this should be OK since mkdtemp just replaces characters in place
    char *tmp_dir = mkdtemp((char *)tmp_path.c_str());
    if (tmp_dir == NULL) {
      return false;
    }

    if (chmod(tmp_dir, 0777) != 0) {
      return false;
    }

    std::string link_path = std::string(tmp_dir) + ".link";
    if (symlink(tmp_dir, link_path.c_str()) != 0) {
      return false;
    }
static bool ensure_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;
  }

  // See if the symlink exists, otherwise create it
  if (!util::file_exists(key_path)) {
    // 1) Create temp folder
    // 2) Set permissions
    // 3) Symlink it to temp link
    // 4) Move symlink to <params>/d

    std::string tmp_path = param_path + "/.tmp_XXXXXX";
    // this should be OK since mkdtemp just replaces characters in place
    char *tmp_dir = mkdtemp((char *)tmp_path.c_str());
    if (tmp_dir == NULL) {
      return false;
    }

    if (chmod(tmp_dir, 0777) != 0) {
      return false;
    }

    std::string link_path = std::string(tmp_dir) + ".link";
    if (symlink(tmp_dir, link_path.c_str()) != 0) {
      return false;
    }
static bool ensure_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;
  }

  // See if the symlink exists, otherwise create it
  if (!util::file_exists(key_path)) {
    // 1) Create temp folder
    // 2) Set permissions
    // 3) Symlink it to temp link
    // 4) Move symlink to <params>/d

    std::string tmp_path = param_path + "/.tmp_XXXXXX";
    // this should be OK since mkdtemp just replaces characters in place
    char *tmp_dir = mkdtemp((char *)tmp_path.c_str());
    if (tmp_dir == NULL) {
      return false;
    }

    if (chmod(tmp_dir, 0777) != 0) {
      return false;
    }

    std::string link_path = std::string(tmp_dir) + ".link";
    if (symlink(tmp_dir, link_path.c_str()) != 0) {
      return false;
    }
static bool ensure_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;
  }

  // See if the symlink exists, otherwise create it
  if (!util::file_exists(key_path)) {
    // 1) Create temp folder
    // 2) Set permissions
    // 3) Symlink it to temp link
    // 4) Move symlink to <params>/d

    std::string tmp_path = param_path + "/.tmp_XXXXXX";
    // this should be OK since mkdtemp just replaces characters in place
    char *tmp_dir = mkdtemp((char *)tmp_path.c_str());
    if (tmp_dir == NULL) {
      return false;
    }

    if (chmod(tmp_dir, 0777) != 0) {
      return false;
    }

    std::string link_path = std::string(tmp_dir) + ".link";
    if (symlink(tmp_dir, link_path.c_str()) != 0) {
      return false;
    }
  
  
  Params::Params(bool persistent_param){
    params_path = persistent_param ? persistent_params_path : default_params_path;
  }
keep keep keep keep replace replace replace replace keep keep keep keep keep
 Params: ensure path and symlink in constructor (#20538)

* ensure path and symlink in constructor

* better comments

* global Params g_params

* const params_path

* Revert "global Params g_params"

This reverts commit 3439894334d69a8c8c19516b9865f9a71ab163da.

* Revert "const params_path"

This reverts commit d3de2fc22ce0f32ffdcdba5595db9c35b21b84a1.

* cleanup

* add todo

* concat string

* add comment

* more robust

* cleanup 
selfdrive/common/params.cc