Skip to content

Commit

Permalink
config_parser: Make paths absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkwah committed Jul 15, 2022
1 parent b6463a6 commit ec015e8
Show file tree
Hide file tree
Showing 22 changed files with 102 additions and 467 deletions.
1 change: 0 additions & 1 deletion libres/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pybind11_add_module(
config/config_content_node.cpp
config/config_error.cpp
config/config_path_elm.cpp
config/config_root_path.cpp
config/config_schema_item.cpp
config/config_settings.cpp
rms/rms_file.cpp
Expand Down
73 changes: 24 additions & 49 deletions libres/lib/config/config_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
#include <stdlib.h>

#include <filesystem>
#include <set>
#include <string>

Expand All @@ -28,15 +29,16 @@
#include <ert/config/config_content.hpp>
#include <ert/config/config_path_elm.hpp>
#include <ert/config/config_path_stack.hpp>
#include <ert/config/config_root_path.hpp>

namespace fs = std::filesystem;

#define CONFIG_CONTENT_TYPE_ID 6612520

struct config_content_struct {
UTIL_TYPE_ID_DECLARATION;
/** A set of config files which have been parsed - to protect against
* circular includes. */
std::set<std::string> *parsed_files;
std::set<std::string> parsed_files;
vector_type *nodes;
hash_type *items;
config_error_type *parse_errors;
Expand All @@ -47,17 +49,16 @@ struct config_content_struct {
char *config_path;

config_path_stack_type *path_stack;
config_root_path_type *invoke_path;
/** Absolute path to directory that contains current config */
fs::path invoke_path;
bool valid;
};

UTIL_IS_INSTANCE_FUNCTION(config_content, CONFIG_CONTENT_TYPE_ID)

config_content_type *config_content_alloc(const char *filename) {
config_content_type *content =
(config_content_type *)util_malloc(sizeof *content);
auto content = new config_content_type;
UTIL_TYPE_ID_INIT(content, CONFIG_CONTENT_TYPE_ID);
content->parsed_files = new std::set<std::string>();

content->valid = false;
content->items = hash_alloc();
Expand All @@ -70,7 +71,7 @@ config_content_type *config_content_alloc(const char *filename) {
content->config_file = util_alloc_string_copy(filename);
content->abs_path = util_alloc_abs_path(filename);
content->config_path = util_split_alloc_dirname(content->abs_path);
content->invoke_path = config_root_path_alloc(NULL);
content->invoke_path = fs::current_path();

return content;
}
Expand Down Expand Up @@ -128,8 +129,6 @@ void config_content_free(config_content_type *content) {
if (!content)
return;

delete content->parsed_files;

stringlist_free(content->warnings);
vector_free(content->nodes);
hash_free(content->items);
Expand All @@ -138,28 +137,21 @@ void config_content_free(config_content_type *content) {
free(content->config_file);
free(content->abs_path);
free(content->config_path);
if (content->invoke_path != NULL)
config_root_path_free(content->invoke_path);

config_path_stack_free(content->path_stack);
free(content);
delete content;
}

bool config_content_add_file(config_content_type *content,
const char *config_file) {
const auto iter = content->parsed_files->find(config_file);
if (iter == content->parsed_files->end()) {
content->parsed_files->insert(config_file);
const auto iter = content->parsed_files.find(config_file);
if (iter == content->parsed_files.end()) {
content->parsed_files.insert(config_file);
return true;
}
return false;
}

config_root_path_type *
config_content_get_invoke_path(config_content_type *content) {
return content->invoke_path;
}

/*
Here comes some xxx_get() functions - many of them will fail if
the item has not been added in the right way (this is to ensure that
Expand Down Expand Up @@ -331,14 +323,6 @@ config_content_get_value_as_abspath(const config_content_type *config,
return config_content_node_iget_as_abspath(node, 0);
}

const char *
config_content_get_value_as_relpath(const config_content_type *config,
const char *kw) {
config_content_node_type *node =
config_content_get_value_node__(config, kw);
return config_content_node_iget_as_relpath(node, 0);
}

const char *
config_content_get_value_as_executable(const config_content_type *config,
const char *kw) {
Expand Down Expand Up @@ -389,34 +373,25 @@ const char *config_content_get_config_file(const config_content_type *content,

config_path_elm_type *config_content_add_path_elm(config_content_type *content,
const char *path) {
const config_path_elm_type *current_path_elm;
const config_path_elm_type *current_path_elm{};

if (config_path_stack_size(content->path_stack) == 0)
current_path_elm = NULL;
else
current_path_elm = config_path_stack_get_last(content->path_stack);

{
config_path_elm_type *new_path_elm;

{
char *rel_path = NULL;
config_root_path_type *invoke_path =
config_content_get_invoke_path(content);
if (path != NULL) {
if (current_path_elm == NULL)
rel_path = util_alloc_rel_path(
config_root_path_get_abs_path(invoke_path), path);
else
rel_path =
config_path_elm_alloc_relpath(current_path_elm, path);
}
new_path_elm = config_path_elm_alloc(invoke_path, rel_path);
free(rel_path);
}
config_path_stack_append(content->path_stack, new_path_elm);
return new_path_elm;
config_path_elm_type *new_path_elm;
const auto &invoke_path = current_path_elm == nullptr
? content->invoke_path
: current_path_elm->path;
if (path != NULL) {
auto new_path = fs::absolute(invoke_path / path);
new_path_elm = config_path_elm_alloc(invoke_path, new_path.c_str());
} else {
new_path_elm = config_path_elm_alloc(invoke_path, nullptr);
}
config_path_stack_append(content->path_stack, new_path_elm);
return new_path_elm;
}

const char *config_content_get_config_path(const config_content_type *content) {
Expand Down
14 changes: 0 additions & 14 deletions libres/lib/config/config_content_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,6 @@ const char *config_content_node_iget_as_abspath(config_content_node_type *node,
}
}

const char *config_content_node_iget_as_relpath(config_content_node_type *node,
int index) {
config_schema_item_assure_type(node->schema, index,
CONFIG_PATH + CONFIG_EXISTING_PATH);
{
const char *config_value = config_content_node_iget(node, index);
char *path_value =
config_path_elm_alloc_relpath(node->cwd, config_value);
config_content_node_push_string(node, path_value);

return path_value;
}
}

const char *
config_content_node_iget_as_executable(config_content_node_type *node,
int index) {
Expand Down
125 changes: 21 additions & 104 deletions libres/lib/config/config_path_elm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
for more details.
*/

#include <filesystem>
#include <string>
#include <vector>

Expand All @@ -24,127 +25,43 @@
#include <ert/util/type_macros.hpp>

#include <ert/config/config_path_elm.hpp>
#include <ert/config/config_root_path.hpp>

#include <fmt/format.h>

namespace fs = std::filesystem;

#define CONFIG_PATH_ELM_TYPE_ID 7100063

struct config_path_elm_struct {
UTIL_TYPE_ID_DECLARATION;
/** This will always be absolute */
char *abs_path;
/** This will always be relative to the root path. */
char *rel_path;
const config_root_path_type *root_path;
};

static UTIL_SAFE_CAST_FUNCTION(config_path_elm, CONFIG_PATH_ELM_TYPE_ID)

config_path_elm_type *config_path_elm_alloc(
const config_root_path_type *root_path, const char *path) {
if (root_path != NULL) {
config_path_elm_type *path_elm =
(config_path_elm_type *)util_malloc(sizeof *path_elm);
UTIL_TYPE_ID_INIT(path_elm, CONFIG_PATH_ELM_TYPE_ID);
path_elm->root_path = root_path;
if (path == NULL) {
path_elm->rel_path = NULL;
path_elm->abs_path = util_alloc_string_copy(
config_root_path_get_abs_path(root_path));
} else {
if (util_is_abs_path(path)) {
path_elm->abs_path = util_alloc_string_copy(path);
path_elm->rel_path = util_alloc_rel_path(
config_root_path_get_abs_path(root_path), path);
} else {
{
char *tmp_abs_path = (char *)util_alloc_filename(
config_root_path_get_abs_path(root_path), path, NULL);
path_elm->abs_path = util_alloc_abs_path(tmp_abs_path);
free(tmp_abs_path);
}
path_elm->rel_path = util_alloc_string_copy(path);
}
}
return path_elm;
static UTIL_SAFE_CAST_FUNCTION(config_path_elm, CONFIG_PATH_ELM_TYPE_ID);

config_path_elm_type *config_path_elm_alloc(const fs::path &root_path,
const char *path) {
auto path_elm = new config_path_elm_type;
UTIL_TYPE_ID_INIT(path_elm, CONFIG_PATH_ELM_TYPE_ID);
if (path == NULL) {
path_elm->path = root_path;
} else {
util_abort("%s: root_path input argument == NULL - invalid \n",
__func__);
return NULL;
path_elm->path = root_path / path;
}
path_elm->path = fs::absolute(path_elm->path);
return path_elm;
}

void config_path_elm_free(config_path_elm_type *path_elm) {
free(path_elm->rel_path);
free(path_elm->abs_path);
free(path_elm);
}
void config_path_elm_free(config_path_elm_type *path_elm) { delete path_elm; }

void config_path_elm_free__(void *arg) {
config_path_elm_type *path_elm = config_path_elm_safe_cast(arg);
config_path_elm_free(path_elm);
}

const char *config_path_elm_get_relpath(const config_path_elm_type *path_elm) {
return path_elm->rel_path;
}

const char *config_path_elm_get_abspath(const config_path_elm_type *path_elm) {
return path_elm->abs_path;
return path_elm->path.c_str();
}

char *config_path_elm_alloc_path(const config_path_elm_type *path_elm,
const char *path) {
if (util_is_abs_path(path))
return util_alloc_string_copy(path);
else {
/* This will be relative or absolute depending on the relative/absolute
status of the root_path. */
const char *input_root =
config_root_path_get_input_path(path_elm->root_path);
std::string tmp_path;
char *return_path;
if (input_root == NULL)
tmp_path = util_alloc_filename(path_elm->rel_path, path, NULL);
else {
const std::vector<std::string> str_list{input_root,
path_elm->rel_path, path};

tmp_path =
fmt::format("{}", fmt::join(str_list, UTIL_PATH_SEP_STRING));
}

return_path = util_alloc_normal_path(tmp_path.c_str());

return return_path;
}
}

char *config_path_elm_alloc_relpath(const config_path_elm_type *path_elm,
const char *input_path) {
if (util_is_abs_path(input_path))
return util_alloc_rel_path(
config_root_path_get_rel_path(path_elm->root_path), input_path);
else {
char *abs_path = config_path_elm_alloc_abspath(path_elm, input_path);
char *rel_path = (char *)util_alloc_rel_path(
config_root_path_get_abs_path(path_elm->root_path), abs_path);
free(abs_path);
return rel_path;
}
}

char *config_path_elm_alloc_abspath(const config_path_elm_type *path_elm,
const char *input_path) {
if (util_is_abs_path(input_path))
return util_alloc_string_copy(input_path);
else {
char *abs_path1 =
(char *)util_alloc_filename(path_elm->abs_path, input_path, NULL);
char *abs_path = (char *)util_alloc_realpath__(
abs_path1); // The util_alloc_realpath__() will work also for nonexsting paths
free(abs_path1);
return abs_path;
}
const char *input_path) {
if (input_path[0] == '/')
return strdup(input_path);
auto path = (path_elm->path / input_path).lexically_normal();
return strdup(path.c_str());
}
Loading

0 comments on commit ec015e8

Please sign in to comment.