diff options
author | David Robillard <d@drobilla.net> | 2022-11-12 17:54:12 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-11-16 10:22:55 -0500 |
commit | ff9f776a48cdca700468f9d5a93625979d77e745 (patch) | |
tree | 51134aef6fd007f8a0b803a16cbf1ed06117189c /src | |
parent | 67a33e70aa2b3f2f78742e773bac5ccb7be95c20 (diff) | |
download | lilv-ff9f776a48cdca700468f9d5a93625979d77e745.tar.gz lilv-ff9f776a48cdca700468f9d5a93625979d77e745.tar.bz2 lilv-ff9f776a48cdca700468f9d5a93625979d77e745.zip |
Use zix_path_join()
Diffstat (limited to 'src')
-rw-r--r-- | src/filesystem.c | 57 | ||||
-rw-r--r-- | src/filesystem.h | 13 | ||||
-rw-r--r-- | src/node.c | 23 | ||||
-rw-r--r-- | src/state.c | 43 | ||||
-rw-r--r-- | src/util.c | 8 |
5 files changed, 53 insertions, 91 deletions
diff --git a/src/filesystem.c b/src/filesystem.c index 7897920..d8f42f3 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -5,6 +5,8 @@ #include "lilv_config.h" #include "lilv_internal.h" +#include "zix/path.h" + #ifdef _WIN32 # include <direct.h> # include <io.h> @@ -107,22 +109,12 @@ lilv_path_absolute(const char* path) } char* cwd = getcwd(NULL, 0); - char* abs_path = lilv_path_join(cwd, path); + char* abs_path = zix_path_join(NULL, cwd, path); free(cwd); return abs_path; } char* -lilv_path_absolute_child(const char* path, const char* parent) -{ - if (lilv_path_is_absolute(path)) { - return lilv_strdup(path); - } - - return lilv_path_join(parent, path); -} - -char* lilv_path_relative_to(const char* path, const char* base) { const size_t path_len = strlen(path); @@ -223,41 +215,6 @@ lilv_path_filename(const char* path) } char* -lilv_path_join(const char* a, const char* b) -{ - if (!a) { - return (b && b[0]) ? lilv_strdup(b) : NULL; - } - - const size_t a_len = strlen(a); - const size_t b_len = b ? strlen(b) : 0; - const bool a_end_is_sep = a_len > 0 && lilv_is_dir_sep(a[a_len - 1]); - const size_t pre_len = a_len - (a_end_is_sep ? 1 : 0); - char* path = (char*)calloc(1, a_len + b_len + 2); - memcpy(path, a, pre_len); - -#ifndef _WIN32 - path[pre_len] = '/'; -#else - // Use forward slash if it seems that the input paths do - const bool a_has_slash = strchr(a, '/'); - const bool b_has_slash = b && strchr(b, '/'); - if (a_has_slash || b_has_slash) { - path[pre_len] = '/'; - } else { - path[pre_len] = '\\'; - } -#endif - - if (b) { - memcpy(path + pre_len + 1, - b + (lilv_is_dir_sep(b[0]) ? 1 : 0), - lilv_is_dir_sep(b[0]) ? b_len - 1 : b_len); - } - return path; -} - -char* lilv_path_canonical(const char* path) { if (!path) { @@ -269,7 +226,7 @@ lilv_path_canonical(const char* path) GetFullPathName(path, MAX_PATH, out, NULL); return out; #else - char* real_path = realpath(path, NULL); + char* real_path = realpath(path, NULL); return real_path ? real_path : lilv_strdup(path); #endif } @@ -377,7 +334,7 @@ lilv_dir_for_each(const char* path, void (*f)(const char* path, const char* name, void* data)) { #ifdef _WIN32 - char* pat = lilv_path_join(path, "*"); + char* pat = zix_path_join(NULL, path, "*"); WIN32_FIND_DATA fd; HANDLE fh = FindFirstFile(pat, &fd); if (fh != INVALID_HANDLE_VALUE) { @@ -415,7 +372,7 @@ lilv_create_temporary_directory_in(const char* pattern, const char* parent) return NULL; } - char* const path_pattern = lilv_path_join(parent, pattern); + char* const path_pattern = zix_path_join(NULL, parent, pattern); const size_t path_pattern_len = strlen(path_pattern); char* const suffix = path_pattern + path_pattern_len - 6; @@ -431,7 +388,7 @@ lilv_create_temporary_directory_in(const char* pattern, const char* parent) return NULL; #else - char* const path_pattern = lilv_path_join(parent, pattern); + char* const path_pattern = zix_path_join(NULL, parent, pattern); return mkdtemp(path_pattern); // NOLINT (not a leak) #endif diff --git a/src/filesystem.h b/src/filesystem.h index 424afc6..db398f3 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -30,15 +30,6 @@ char* lilv_path_absolute(const char* path); /** - Return `path` as an absolute path relative to `parent`. - - If `path` is absolute, an identical copy of it is returned. Otherwise, the - returned path is relative to `parent`. -*/ -char* -lilv_path_absolute_child(const char* path, const char* parent); - -/** Return `path` relative to `base` if possible. If `path` is not within `base`, a copy is returned. Otherwise, an @@ -64,10 +55,6 @@ lilv_path_parent(const char* path); char* lilv_path_filename(const char* path); -/// Join path `a` and path `b` with a single directory separator between them -char* -lilv_path_join(const char* a, const char* b); - /** Return `path` as a canonicalized absolute path. @@ -1,12 +1,15 @@ // Copyright 2007-2019 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC -#include "filesystem.h" #include "lilv_internal.h" #include "lilv/lilv.h" #include "serd/serd.h" #include "sord/sord.h" +#include "zix/allocator.h" +#include "zix/filesystem.h" +#include "zix/path.h" +#include "zix/string_view.h" #include <math.h> #include <stdbool.h> @@ -147,13 +150,23 @@ lilv_new_uri(LilvWorld* world, const char* uri) LilvNode* lilv_new_file_uri(LilvWorld* world, const char* host, const char* path) { - char* abs_path = lilv_path_absolute(path); - SerdNode s = serd_node_new_file_uri( - (const uint8_t*)abs_path, (const uint8_t*)host, NULL, true); + SerdNode s = SERD_NODE_NULL; + if (zix_path_root_directory(path).length) { + s = serd_node_new_file_uri( + (const uint8_t*)path, (const uint8_t*)host, NULL, true); + } else { + char* const cwd = zix_current_path(NULL); + char* const abs_path = zix_path_join(NULL, cwd, path); + + s = serd_node_new_file_uri( + (const uint8_t*)abs_path, (const uint8_t*)host, NULL, true); + + zix_free(NULL, abs_path); + zix_free(NULL, cwd); + } LilvNode* ret = lilv_node_new(world, LILV_VALUE_URI, (const char*)s.buf); serd_node_free(&s); - free(abs_path); return ret; } diff --git a/src/state.c b/src/state.c index d215c15..2adb7bc 100644 --- a/src/state.c +++ b/src/state.c @@ -8,7 +8,9 @@ #include "serd/serd.h" #include "sord/sord.h" #include "sratom/sratom.h" +#include "zix/allocator.h" #include "zix/filesystem.h" +#include "zix/path.h" #include "zix/tree.h" #include "lv2/atom/atom.h" @@ -251,7 +253,7 @@ make_path(LV2_State_Make_Path_Handle handle, const char* path) LilvState* state = (LilvState*)handle; lilv_create_directories(state->dir); - return lilv_path_join(state->dir, path); + return zix_path_join(NULL, state->dir, path); } static char* @@ -287,7 +289,7 @@ abstract_path(LV2_State_Map_Path_Handle handle, const char* abs_path) "Error creating directory %s (%s)\n", state->copy_dir, strerror(st)); } - char* cpath = lilv_path_join(state->copy_dir, path); + char* cpath = zix_path_join(NULL, state->copy_dir, path); char* copy = lilv_get_latest_copy(real_path, cpath); if (!copy || !lilv_file_equals(real_path, copy)) { // No recent enough copy, make a new one @@ -298,7 +300,7 @@ abstract_path(LV2_State_Map_Path_Handle handle, const char* abs_path) } } free(real_path); - free(cpath); + zix_free(NULL, cpath); // Refer to the latest copy in plugin state real_path = copy; @@ -336,7 +338,7 @@ absolute_path(LV2_State_Map_Path_Handle handle, const char* state_path) path = lilv_strdup(state_path); } else if (state->dir) { // Relative path inside state directory - path = lilv_path_join(state->dir, state_path); + path = zix_path_join(NULL, state->dir, state_path); } else { // State has not been saved, unmap path = lilv_strdup(lilv_state_rel2abs(state, state_path)); @@ -382,7 +384,7 @@ static char* real_dir(const char* path) { char* abs_path = lilv_path_canonical(path); - char* base = lilv_path_join(abs_path, NULL); + char* base = zix_path_join(NULL, abs_path, NULL); free(abs_path); return base; } @@ -560,7 +562,7 @@ set_state_dir_from_model(LilvState* state, const SordNode* graph) const char* uri = (const char*)sord_node_get_string(graph); char* path = lilv_file_uri_parse(uri, NULL); - state->dir = lilv_path_join(path, NULL); + state->dir = zix_path_join(NULL, path, NULL); free(path); } assert(!state->dir || lilv_path_is_absolute(state->dir)); @@ -580,7 +582,7 @@ new_state_from_model(LilvWorld* world, // Allocate state LilvState* const state = (LilvState*)calloc(1, sizeof(LilvState)); - state->dir = lilv_path_join(dir, NULL); + state->dir = dir ? zix_path_join(NULL, dir, NULL) : NULL; state->atom_Path = map->map(map->handle, LV2_ATOM__Path); state->uri = lilv_node_new_from_node(world, node); @@ -745,10 +747,10 @@ lilv_state_new_from_file(LilvWorld* world, char* dirname = lilv_path_parent(path); char* real_path = lilv_path_canonical(dirname); - char* dir_path = lilv_path_join(real_path, NULL); + char* dir_path = zix_path_join(NULL, real_path, NULL); LilvState* state = new_state_from_model(world, map, model, subject_node, dir_path); - free(dir_path); + zix_free(NULL, dir_path); free(real_path); free(dirname); @@ -1167,16 +1169,17 @@ lilv_state_make_links(const LilvState* state, const char* dir) for (ZixTreeIter* i = zix_tree_begin(state->abs2rel); i != zix_tree_end(state->abs2rel); i = zix_tree_iter_next(i)) { - const PathMap* pm = (const PathMap*)zix_tree_get(i); + const PathMap* const pm = (const PathMap*)zix_tree_get(i); + char* const path = zix_path_join(NULL, dir, pm->rel); - char* path = lilv_path_absolute_child(pm->rel, dir); if (lilv_path_is_child(pm->abs, state->copy_dir) && strcmp(state->copy_dir, dir)) { // Link directly to snapshot in the copy directory maybe_symlink(pm->abs, path); } else if (!lilv_path_is_child(pm->abs, dir)) { const char* link_dir = state->link_dir ? state->link_dir : dir; - char* pat = lilv_path_absolute_child(pm->rel, link_dir); + char* pat = zix_path_join(NULL, link_dir, pm->rel); + if (!strcmp(dir, link_dir)) { // Link directory is save directory, make link at exact path remove(pat); @@ -1219,12 +1222,12 @@ lilv_state_save(LilvWorld* world, } char* abs_dir = real_dir(dir); - char* const path = lilv_path_join(abs_dir, filename); + char* const path = zix_path_join(NULL, abs_dir, filename); FILE* fd = fopen(path, "w"); if (!fd) { LILV_ERRORF("Failed to open %s (%s)\n", path, strerror(errno)); free(abs_dir); - free(path); + zix_free(NULL, path); return 4; } @@ -1240,7 +1243,7 @@ lilv_state_save(LilvWorld* world, lilv_state_write(world, map, unmap, state, ttl, (const char*)node.buf, dir); // Set saved dir and uri (FIXME: const violation) - free(state->dir); + zix_free(NULL, state->dir); lilv_node_free(state->uri); ((LilvState*)state)->dir = lilv_strdup(abs_dir); ((LilvState*)state)->uri = lilv_new_uri(world, (const char*)node.buf); @@ -1252,15 +1255,15 @@ lilv_state_save(LilvWorld* world, // Add entry to manifest if (!ret) { - char* const manifest = lilv_path_join(abs_dir, "manifest.ttl"); + char* const manifest = zix_path_join(NULL, abs_dir, "manifest.ttl"); ret = add_state_to_manifest(world, state->plugin_uri, manifest, uri, path); - free(manifest); + zix_free(NULL, manifest); } free(abs_dir); - free(path); + zix_free(NULL, path); return ret; } @@ -1372,9 +1375,9 @@ lilv_state_delete(LilvWorld* world, const LilvState* state) i != zix_tree_end(state->abs2rel); i = zix_tree_iter_next(i)) { const PathMap* pm = (const PathMap*)zix_tree_get(i); - char* path = lilv_path_join(state->dir, pm->rel); + char* path = zix_path_join(NULL, state->dir, pm->rel); try_unlink(state->dir, path); - free(path); + zix_free(NULL, path); } } else { // State loaded from model, get paths from loaded properties @@ -6,6 +6,8 @@ #include "lilv/lilv.h" #include "serd/serd.h" +#include "zix/allocator.h" +#include "zix/path.h" #include <sys/stat.h> @@ -237,13 +239,13 @@ static void update_latest(const char* path, const char* name, void* data) { Latest* latest = (Latest*)data; - char* entry_path = lilv_path_join(path, name); + char* entry_path = zix_path_join(NULL, path, name); unsigned num = 0; if (sscanf(entry_path, latest->pattern, &num) == 1) { struct stat st; if (!stat(entry_path, &st)) { if (st.st_mtime >= latest->time) { - free(latest->latest); + zix_free(NULL, latest->latest); latest->latest = entry_path; } } else { @@ -251,7 +253,7 @@ update_latest(const char* path, const char* name, void* data) } } if (entry_path != latest->latest) { - free(entry_path); + zix_free(NULL, entry_path); } } |