From 1c18b0ba64be0e03867b95dff80bed3b4568c57b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 8 Jan 2012 04:18:00 +0000 Subject: Move all non-portable stuff to util.c. git-svn-id: http://svn.drobilla.net/lad/trunk/lilv@3921 a436a847-0d15-0410-975c-d299462d15a1 --- src/lilv_internal.h | 3 +++ src/state.c | 54 +++++++++-------------------------------------------- src/util.c | 39 ++++++++++++++++++++++++++++++++++++-- wscript | 6 ------ 4 files changed, 49 insertions(+), 53 deletions(-) diff --git a/src/lilv_internal.h b/src/lilv_internal.h index 43f92cd..be3f5de 100644 --- a/src/lilv_internal.h +++ b/src/lilv_internal.h @@ -349,6 +349,9 @@ char* lilv_get_latest_copy(const char* path); char* lilv_path_relative_to(const char* path, const char* base); bool lilv_path_is_child(const char* path, const char* dir); int lilv_flock(FILE* file, bool lock); +char* lilv_realpath(const char* path); +int lilv_symlink(const char* oldpath, const char* newpath); +int lilv_mkdir_p(const char* path); char* lilv_find_free_path(const char* in_path, diff --git a/src/state.c b/src/state.c index 512cccf..80815ec 100644 --- a/src/state.c +++ b/src/state.c @@ -14,8 +14,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#define _BSD_SOURCE 1 /* for realpath, symlink */ - #include #include #include @@ -27,13 +25,6 @@ # include "lv2/lv2plug.in/ns/ext/state/state.h" #endif -#ifdef HAVE_MKDIR -# include -# include -#endif - -#include - #define NS_ATOM "http://lv2plug.in/ns/ext/atom#" #define NS_PSET "http://lv2plug.in/ns/ext/presets#" #define NS_STATE "http://lv2plug.in/ns/ext/state#" @@ -200,7 +191,7 @@ abstract_path(LV2_State_Map_Path_Handle handle, LilvState* state = (LilvState*)handle; const size_t file_dir_len = state->file_dir ? strlen(state->file_dir) : 0; char* path = NULL; - char* real_path = realpath(absolute_path, NULL); + char* real_path = lilv_realpath(absolute_path); const PathMap key = { (char*)real_path, NULL }; ZixTreeIter* iter = NULL; @@ -294,7 +285,7 @@ lilv_state_new_from_instance(const LilvPlugin* plugin, state->plugin_uri = lilv_node_duplicate(lilv_plugin_get_uri(plugin)); state->abs2rel = zix_tree_new(false, abs_cmp, NULL, path_rel_free); state->rel2abs = zix_tree_new(false, rel_cmp, NULL, NULL); - state->file_dir = dir ? realpath(dir, NULL) : NULL; + state->file_dir = dir ? lilv_realpath(dir) : NULL; state->state_Path = map->map(map->handle, LV2_STATE_PATH_URI); #ifdef HAVE_LV2_STATE @@ -614,7 +605,7 @@ lilv_state_new_from_file(LilvWorld* world, : sord_node_from_serd_node(world->world, env, &base, NULL, NULL); char* dirname = lilv_dirname(path); - char* real_path = realpath(dirname, NULL); + char* real_path = lilv_realpath(dirname); LilvState* state = new_state_from_model( world, map, model, subject_node, real_path); free(dirname); @@ -776,7 +767,7 @@ add_state_to_manifest(const LilvNode* plugin_uri, static char* pathify(const char* in) { - const size_t in_len = strlen(in); + const size_t in_len = strlen(in); char* out = calloc(in_len + 1, 1); for (size_t i = 0; i < in_len; ++i) { @@ -791,32 +782,9 @@ pathify(const char* in) return out; } -static int -mkdir_p(const char* dir_path) -{ - char* path = lilv_strdup(dir_path); - const size_t path_len = strlen(path); - for (size_t i = 1; i <= path_len; ++i) { - if (path[i] == LILV_DIR_SEP[0] || path[i] == '\0') { - path[i] = '\0'; - if (mkdir(path, 0755) && errno != EEXIST) { - LILV_ERRORF("Failed to create %s (%s)\n", - path, strerror(errno)); - free(path); - return 1; - } - path[i] = LILV_DIR_SEP[0]; - } - } - - free(path); - return 0; -} - static char* lilv_default_state_dir(LilvWorld* world) { -#ifdef HAVE_MKDIR // Use environment variable or default value if it is unset char* state_bundle = getenv("LV2_STATE_BUNDLE"); if (!state_bundle) { @@ -825,10 +793,6 @@ lilv_default_state_dir(LilvWorld* world) // Expand any variables and create if necessary return lilv_expand(state_bundle); -#else - LILV_ERROR("Save to default state path but mkdir is unavailable.\n"); - return NULL; -#endif } LILV_API @@ -846,7 +810,7 @@ lilv_state_save(LilvWorld* world, if (!dir) { dir = default_dir = lilv_default_state_dir(world); } - if (mkdir_p(dir)) { + if (lilv_mkdir_p(dir)) { free(default_dir); return 1; } @@ -950,12 +914,12 @@ lilv_state_save(LilvWorld* world, i = zix_tree_iter_next(i)) { const PathMap* pm = (const PathMap*)zix_tree_get(i); - char* real_dir = lilv_strjoin(realpath(dir, NULL), "/", NULL); + char* real_dir = lilv_strjoin(lilv_realpath(dir), "/", NULL); char* rel_path = lilv_strjoin(dir, "/", pm->rel, NULL); char* target_path = lilv_path_is_child(pm->abs, state->file_dir) ? lilv_path_relative_to(pm->abs, real_dir) : lilv_strdup(pm->abs); - if (symlink(target_path, rel_path)) { + if (lilv_symlink(target_path, rel_path)) { LILV_ERRORF("Failed to link `%s' => `%s' (%s)\n", pm->abs, pm->rel, strerror(errno)); } @@ -1109,8 +1073,8 @@ lilv_state_equals(const LilvState* a, const LilvState* b) if (ap->type == a->state_Path) { const char* const a_abs = lilv_state_rel2abs(a, ap->value); const char* const b_abs = lilv_state_rel2abs(b, bp->value); - char* const a_real = realpath(a_abs, NULL); - char* const b_real = realpath(b_abs, NULL); + char* const a_real = lilv_realpath(a_abs); + char* const b_real = lilv_realpath(b_abs); const int cmp = strcmp(a_real, b_real); free(a_real); free(b_real); diff --git a/src/util.c b/src/util.c index 86300a1..526bf9b 100644 --- a/src/util.c +++ b/src/util.c @@ -15,6 +15,7 @@ */ #define _POSIX_SOURCE 1 /* for wordexp, fileno */ +#define _BSD_SOURCE 1 /* for realpath, symlink */ #include #include @@ -27,12 +28,12 @@ #include #include +#include "lilv_internal.h" + #if defined(HAVE_FLOCK) && defined(HAVE_FILENO) # include #endif -#include "lilv_internal.h" - #ifdef HAVE_WORDEXP # include #endif @@ -305,6 +306,18 @@ lilv_get_latest_copy(const char* path) return latest.latest; } +char* +lilv_realpath(const char* path) +{ + return realpath(path, NULL); +} + +int +lilv_symlink(const char* oldpath, const char* newpath) +{ + return symlink(oldpath, newpath); +} + char* lilv_path_relative_to(const char* path, const char* base) { @@ -378,3 +391,25 @@ lilv_dir_for_each(const char* path, closedir(dir); } } + +int +lilv_mkdir_p(const char* dir_path) +{ + char* path = lilv_strdup(dir_path); + const size_t path_len = strlen(path); + for (size_t i = 1; i <= path_len; ++i) { + if (path[i] == LILV_DIR_SEP[0] || path[i] == '\0') { + path[i] = '\0'; + if (mkdir(path, 0755) && errno != EEXIST) { + LILV_ERRORF("Failed to create %s (%s)\n", + path, strerror(errno)); + free(path); + return 1; + } + path[i] = LILV_DIR_SEP[0]; + } + } + + free(path); + return 0; +} diff --git a/wscript b/wscript index 730c814..e712038 100644 --- a/wscript +++ b/wscript @@ -97,12 +97,6 @@ def configure(conf): define_name='HAVE_FILENO', mandatory=False) - conf.check_cc(function_name='mkdir', - header_name=['sys/stat.h','sys/types.h'], - defines='_POSIX_SOURCE', - define_name='HAVE_MKDIR', - mandatory=False) - autowaf.define(conf, 'LILV_VERSION', LILV_VERSION) if Options.options.dyn_manifest: autowaf.define(conf, 'LILV_DYN_MANIFEST', 1) -- cgit v1.2.1