summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-01-18 14:10:55 +0100
committerDavid Robillard <d@drobilla.net>2020-01-18 14:10:55 +0100
commitc157f33ab2fd1ef5832e31eddfd339f72923810b (patch)
treed768188a18509025eec85479127712fa06653f83 /src
parenta9665722f49bd1be3e776dfa385da0d9ee0edfac (diff)
downloadlilv-c157f33ab2fd1ef5832e31eddfd339f72923810b.tar.gz
lilv-c157f33ab2fd1ef5832e31eddfd339f72923810b.tar.bz2
lilv-c157f33ab2fd1ef5832e31eddfd339f72923810b.zip
Ensure state directory path always ends in a separator
This can cause problems when resolving relative paths against the bundle directory, and it's simpler to ensure that the path always ends in a separator than deal with both cases in every place it is used.
Diffstat (limited to 'src')
-rw-r--r--src/lilv_internal.h1
-rw-r--r--src/state.c21
-rw-r--r--src/util.c19
3 files changed, 33 insertions, 8 deletions
diff --git a/src/lilv_internal.h b/src/lilv_internal.h
index 52e0870..368d7ed 100644
--- a/src/lilv_internal.h
+++ b/src/lilv_internal.h
@@ -382,6 +382,7 @@ char* lilv_strdup(const char* str);
char* lilv_get_lang(void);
char* lilv_expand(const char* path);
char* lilv_dirname(const char* path);
+char* lilv_dir_path(const char* path);
int lilv_copy_file(const char* src, const char* dst);
bool lilv_path_exists(const char* path, const void* ignored);
char* lilv_path_absolute(const char* path);
diff --git a/src/state.c b/src/state.c
index 35dd502..10f37ff 100644
--- a/src/state.c
+++ b/src/state.c
@@ -524,8 +524,11 @@ static void
set_state_dir_from_model(LilvState* state, const SordNode* graph)
{
if (!state->dir && graph) {
- const char* uri = (const char*)sord_node_get_string(graph);
- state->dir = lilv_file_uri_parse(uri, NULL);
+ const char* uri = (const char*)sord_node_get_string(graph);
+ char* path = lilv_file_uri_parse(uri, NULL);
+
+ state->dir = lilv_dir_path(path);
+ free(path);
}
assert(!state->dir || lilv_path_is_absolute(state->dir));
}
@@ -544,7 +547,7 @@ new_state_from_model(LilvWorld* world,
// Allocate state
LilvState* const state = (LilvState*)calloc(1, sizeof(LilvState));
- state->dir = lilv_strdup(dir);
+ state->dir = lilv_dir_path(dir);
state->atom_Path = map->map(map->handle, LV2_ATOM__Path);
state->uri = lilv_node_new_from_node(world, node);
@@ -710,12 +713,14 @@ lilv_state_new_from_file(LilvWorld* world,
? subject->node
: sord_node_from_serd_node(world->world, env, &node, NULL, NULL);
- char* dirname = lilv_dirname(path);
- char* real_path = lilv_realpath(dirname);
- LilvState* state = new_state_from_model(
- world, map, model, subject_node, real_path);
- free(dirname);
+ char* dirname = lilv_dirname(path);
+ char* real_path = lilv_realpath(dirname);
+ char* dir_path = lilv_dir_path(real_path);
+ LilvState* state =
+ new_state_from_model(world, map, model, subject_node, dir_path);
+ free(dir_path);
free(real_path);
+ free(dirname);
serd_node_free(&node);
free(abs_path);
diff --git a/src/util.c b/src/util.c
index 39f13c0..448b66d 100644
--- a/src/util.c
+++ b/src/util.c
@@ -281,6 +281,25 @@ lilv_dirname(const char* path)
}
}
+char*
+lilv_dir_path(const char* path)
+{
+ if (!path) {
+ return NULL;
+ }
+
+ const size_t len = strlen(path);
+
+ if (lilv_is_dir_sep(path[len - 1])) {
+ return lilv_strdup(path);
+ }
+
+ char* dir_path = (char*)calloc(len + 2, 1);
+ memcpy(dir_path, path, len);
+ dir_path[len] = LILV_DIR_SEP[0];
+ return dir_path;
+}
+
bool
lilv_path_exists(const char* path, const void* ignored)
{