Skip to content

Commit d6103ee

Browse files
committed
Add support to relative paths in configurations.
1 parent a0f4a11 commit d6103ee

File tree

12 files changed

+457
-52
lines changed

12 files changed

+457
-52
lines changed

source/configuration/include/configuration/configuration_object.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ CONFIGURATION_API configuration configuration_object_initialize(const char *name
7676
*/
7777
CONFIGURATION_API int configuration_object_childs(configuration config, vector childs, set storage);
7878

79+
/**
80+
* @brief
81+
* Get an absolute path from the value @v which is a string representing a path,
82+
* if the path is absolute, store it in @path as it is, otherwise, join the @config
83+
* path to the value string @v and make it canonical
84+
*
85+
* @param[in] config
86+
* Pointer to configuration object
87+
*
88+
* @param[in] v
89+
* The value representing the path
90+
*
91+
* @param[out] path
92+
* The string where it is going to be store the path
93+
*
94+
* @return
95+
* Returns the size of the path
96+
*
97+
*/
98+
CONFIGURATION_API size_t configuration_object_child_path(configuration config, value v, char *path);
99+
79100
/**
80101
* @brief
81102
* Set value of configuration object @config

source/configuration/source/configuration_impl.c

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,15 @@ int configuration_impl_initialize(const char *name)
6969
int configuration_impl_load(configuration config, void *allocator)
7070
{
7171
configuration_impl_singleton singleton = configuration_impl_singleton_instance();
72-
73-
set storage;
74-
7572
vector queue, childs;
76-
77-
storage = set_create(&hash_callback_str, &comparable_callback_str);
73+
set storage = set_create(&hash_callback_str, &comparable_callback_str);
74+
int result = 1;
7875

7976
if (storage == NULL)
8077
{
8178
log_write("metacall", LOG_LEVEL_ERROR, "Invalid configuration implementation load set allocation");
8279

83-
return 1;
80+
goto alloc_storage_error;
8481
}
8582

8683
queue = vector_create(sizeof(configuration));
@@ -89,9 +86,7 @@ int configuration_impl_load(configuration config, void *allocator)
8986
{
9087
log_write("metacall", LOG_LEVEL_ERROR, "Invalid configuration implementation load queue allocation");
9188

92-
set_destroy(storage);
93-
94-
return 1;
89+
goto alloc_queue_error;
9590
}
9691

9792
childs = vector_create(sizeof(configuration));
@@ -100,11 +95,7 @@ int configuration_impl_load(configuration config, void *allocator)
10095
{
10196
log_write("metacall", LOG_LEVEL_ERROR, "Invalid configuration implementation load childs allocation");
10297

103-
set_destroy(storage);
104-
105-
vector_destroy(queue);
106-
107-
return 1;
98+
goto alloc_childs_error;
10899
}
109100

110101
vector_push_back(queue, &config);
@@ -131,21 +122,20 @@ int configuration_impl_load(configuration config, void *allocator)
131122
{
132123
log_write("metacall", LOG_LEVEL_ERROR, "Invalid configuration implementation load (childs) <%p>", current);
133124

134-
set_destroy(storage);
135-
136-
vector_destroy(queue);
137-
138-
vector_destroy(childs);
139-
140-
return 1;
125+
goto load_error;
141126
}
142127
}
143128

144129
configuration_object_instantiate(current, v);
145130

146131
vector_clear(childs);
147132

148-
if (configuration_object_childs(current, childs, storage) == 0 && vector_size(childs) > 0)
133+
if (configuration_object_childs(current, childs, storage) != 0)
134+
{
135+
goto load_error;
136+
}
137+
138+
if (vector_size(childs) > 0)
149139
{
150140
size_t iterator;
151141

@@ -160,13 +150,7 @@ int configuration_impl_load(configuration config, void *allocator)
160150
log_write("metacall", LOG_LEVEL_ERROR, "Invalid configuration implementation child singleton insertion (%s, %s)",
161151
configuration_object_name(child), configuration_object_path(child));
162152

163-
set_destroy(storage);
164-
165-
vector_destroy(queue);
166-
167-
vector_destroy(childs);
168-
169-
return 1;
153+
goto load_error;
170154
}
171155

172156
vector_push_back(queue, &child);
@@ -176,25 +160,22 @@ int configuration_impl_load(configuration config, void *allocator)
176160
{
177161
log_write("metacall", LOG_LEVEL_ERROR, "Invalid configuration implementation child set insertion");
178162

179-
set_destroy(storage);
180-
181-
vector_destroy(queue);
182-
183-
vector_destroy(childs);
184-
185-
return 1;
163+
goto load_error;
186164
}
187165
}
188166
}
189167
}
190168

191-
set_destroy(storage);
192-
193-
vector_destroy(queue);
169+
result = 0;
194170

171+
load_error:
195172
vector_destroy(childs);
196-
197-
return 0;
173+
alloc_childs_error:
174+
vector_destroy(queue);
175+
alloc_queue_error:
176+
set_destroy(storage);
177+
alloc_storage_error:
178+
return result;
198179
}
199180

200181
int configuration_impl_destroy(void)

source/configuration/source/configuration_object.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include <log/log.h>
1515

16+
#include <portability/portability_path.h>
17+
1618
#include <string.h>
1719

1820
/* -- Member Data -- */
@@ -102,6 +104,8 @@ configuration configuration_object_initialize(const char *name, const char *path
102104

103105
if (config->source == NULL)
104106
{
107+
log_write("metacall", LOG_LEVEL_ERROR, "Failed to load configuration %s from %s", name, path);
108+
105109
free(config);
106110

107111
return NULL;
@@ -216,6 +220,31 @@ int configuration_object_childs_valid(set_key key, set_value val)
216220
return 1;
217221
}
218222

223+
size_t configuration_object_child_path(configuration config, value v, char *path)
224+
{
225+
const char *value_path = value_to_string(v);
226+
size_t size = value_type_size(v);
227+
228+
if (portability_path_is_absolute(value_path, size) == 0)
229+
{
230+
memcpy(path, value_path, size);
231+
}
232+
else
233+
{
234+
char absolute_path[PORTABILITY_PATH_SIZE];
235+
236+
size_t absolute_path_size = portability_path_get_directory(config->path, strnlen(config->path, PORTABILITY_PATH_SIZE), absolute_path, PORTABILITY_PATH_SIZE);
237+
238+
char join_path[PORTABILITY_PATH_SIZE];
239+
240+
size_t join_path_size = portability_path_join(absolute_path, absolute_path_size, value_path, size, join_path, PORTABILITY_PATH_SIZE);
241+
242+
size = portability_path_canonical(join_path, join_path_size, path, PORTABILITY_PATH_SIZE);
243+
}
244+
245+
return size;
246+
}
247+
219248
int configuration_object_childs(configuration config, vector childs, set storage)
220249
{
221250
struct set_iterator_type it;
@@ -229,11 +258,13 @@ int configuration_object_childs(configuration config, vector childs, set storage
229258
{
230259
if (set_get(storage, key) == NULL)
231260
{
232-
value v = val;
261+
char path[PORTABILITY_PATH_SIZE];
262+
263+
configuration child;
233264

234-
const char *path = value_to_string(v);
265+
configuration_object_child_path(config, val, path);
235266

236-
configuration child = configuration_object_initialize(key, path, config);
267+
child = configuration_object_initialize(key, path, config);
237268

238269
if (child == NULL)
239270
{

source/loader/source/loader_impl.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <log/log.h>
3636

3737
#include <configuration/configuration.h>
38+
#include <configuration/configuration_object.h>
3839

3940
#include <portability/portability_library_path.h>
4041

@@ -282,16 +283,13 @@ void loader_impl_configuration_execution_paths(loader_impl_interface iface, load
282283
{
283284
if (value_type_id(execution_paths_array[iterator]) == TYPE_STRING)
284285
{
285-
const char *str = value_to_string(execution_paths_array[iterator]);
286-
size_t str_size = value_type_size(execution_paths_array[iterator]);
286+
loader_path execution_path;
287287

288-
if (str != NULL)
289-
{
290-
loader_path execution_path;
291-
292-
strncpy(execution_path, str, str_size > LOADER_PATH_SIZE ? LOADER_PATH_SIZE : str_size);
288+
configuration_object_child_path(impl->config, execution_paths_array[iterator], execution_path);
293289

294-
iface->execution_path(impl, execution_path);
290+
if (iface->execution_path(impl, execution_path) != 0)
291+
{
292+
log_write("metacall", LOG_LEVEL_ERROR, "Failed to load execution path %s in configuration %s", execution_path, configuration_object_name(impl->config));
295293
}
296294
}
297295
}

source/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ add_subdirectory(metacall_inspect_test)
158158
add_subdirectory(metacall_integration_test)
159159
add_subdirectory(metacall_depends_test)
160160
add_subdirectory(metacall_configuration_exec_path_test)
161+
add_subdirectory(metacall_configuration_exec_relative_path_test)
161162
add_subdirectory(metacall_configuration_default_test)
162163
add_subdirectory(metacall_clear_test)
163164
add_subdirectory(metacall_python_test)

0 commit comments

Comments
 (0)