22 #include "opensync_internals.h"
53 static const char *osync_env_query_option(OSyncEnv *env,
const char *name)
56 value = g_hash_table_lookup(env->options, name);
60 gchar *env_name = g_strdup_printf(
"OSYNC_%s", name);
61 value = getenv(env_name);
70 static osync_bool osync_env_query_option_bool(OSyncEnv *env,
const char *name)
72 const char *get_value;
73 if (!(get_value = osync_env_query_option(env, name)))
75 if (!strcmp(get_value,
"TRUE"))
85 int num_modules = g_list_length(env->modules);
88 gchar **path_array = g_malloc0(
sizeof(gchar*)*(num_modules + 1));
90 for (i = 0; i < num_modules; i++) {
91 GModule *module = g_list_nth_data(env->modules, i);
92 const gchar *path = g_module_name(module);
99 path_array[i] = (gchar*)path;
103 gchar *list_str = g_strjoinv(
":", path_array);
104 setenv(
"OSYNC_FORMAT_LIST", list_str, 1);
108 static void export_option_to_env(gpointer key, gpointer data, gpointer user_data)
110 const char *name = (
const char*)key;
111 const char *value = (
const char*)data;
112 gchar *env_name = g_strdup_printf(
"OSYNC_%s", name);
113 setenv(env_name, value, 1);
122 g_hash_table_foreach(env->options, export_option_to_env, NULL);
125 static void free_hash(
char *key,
char *value,
void *data)
141 char *filename = NULL;
147 filename = g_strdup_printf(
"%s/group%lli", env->groupsdir, i);
148 }
while (g_file_test(filename, G_FILE_TEST_EXISTS));
188 OSyncEnv *env = g_malloc0(
sizeof(OSyncEnv));
189 env->is_initialized = FALSE;
190 env->options = g_hash_table_new(g_str_hash, g_str_equal);
210 g_hash_table_foreach(env->options, (GHFunc)free_hash, NULL);
211 g_hash_table_destroy(env->options);
225 g_hash_table_insert(env->options, g_strdup(name), g_strdup(value));
227 g_hash_table_remove(env->options, name);
245 if (env->is_initialized) {
246 osync_error_set(error, OSYNC_ERROR_INITIALIZATION,
"Cannot initialize the same environment twice");
252 if (osync_env_query_option_bool(env,
"LOAD_PLUGINS")) {
260 if (osync_env_query_option_bool(env,
"LOAD_FORMATS")) {
268 if (osync_env_query_option_bool(env,
"LOAD_GROUPS")) {
275 env->is_initialized = TRUE;
294 if (!env->is_initialized) {
295 osync_error_set(error, OSYNC_ERROR_INITIALIZATION,
"Environment has to be initialized before");
302 GList *plugins = g_list_copy(env->plugins);
304 for (p = plugins; p; p = p->next) {
308 g_list_free(plugins);
311 GList *modules = g_list_copy(env->modules);
312 for (p = modules; p; p = p->next) {
313 GModule *module = p->data;
316 g_list_free(modules);
337 osync_bool must_exist = TRUE;
340 path = OPENSYNC_FORMATSDIR;
366 osync_bool must_exist = TRUE;
369 path = OPENSYNC_PLUGINDIR;
398 if (g_ascii_strcasecmp(plugin->info.name, name) == 0) {
415 return g_list_length(env->plugins);
429 return (
OSyncPlugin *)g_list_nth_data(env->plugins, nth);
446 osync_error_set(error, OSYNC_ERROR_PLUGIN_NOT_FOUND,
"Unable to find plugin \"%s\". This can be caused by unresolved symbols", pluginname);
451 if (plugin->info.functions.is_available) {
452 osync_bool ret = plugin->info.functions.is_available(error);
475 GError *gerror = NULL;
476 char *filename = NULL;
477 char *real_path = NULL;
478 char *path = g_strdup(p);
486 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
487 if (mkdir(path, 0700) == -1) {
488 osync_error_set(error, OSYNC_ERROR_GENERIC,
"Unable to create group directory at %s: %s", path, strerror(errno));
492 char *enginepath = g_strdup_printf(
"%s/engines", path);
493 if (mkdir(enginepath, 0700) == -1) {
494 osync_error_set(error, OSYNC_ERROR_GENERIC,
"Unable to create engine group directory at %s: %s", enginepath, strerror(errno));
500 osync_debug(
"OSGRP", 3,
"Created groups configdir %s\n", path);
502 osync_user_free(user);
505 if (!g_path_is_absolute(path)) {
506 real_path = g_strdup_printf(
"%s/%s", g_get_current_dir(), path);
508 real_path = g_strdup(path);
511 if (!g_file_test(real_path, G_FILE_TEST_IS_DIR)) {
512 osync_debug(
"OSGRP", 0,
"%s exists, but is no dir", real_path);
513 osync_error_set(error, OSYNC_ERROR_INITIALIZATION,
"%s exists, but is no dir", real_path);
519 dir = g_dir_open(real_path, 0, &gerror);
521 osync_debug(
"OSGRP", 0,
"Unable to open main configdir %s: %s", real_path, gerror->message);
522 osync_error_set(error, OSYNC_ERROR_IO_ERROR,
"Unable to open main configdir %s: %s", real_path, gerror->message);
523 g_error_free (gerror);
529 const gchar *de = NULL;
530 while ((de = g_dir_read_name(dir))) {
531 filename = g_strdup_printf (
"%s/%s", real_path, de);
533 if (!g_file_test(filename, G_FILE_TEST_IS_DIR) || g_file_test(filename, G_FILE_TEST_IS_SYMLINK) || !g_pattern_match_simple(
"group*", de)) {
541 osync_debug(
"OSGRP", 0,
"Unable to load group from %s: %s", filename, error->
message);
550 env->groupsdir = path;
569 if (g_ascii_strcasecmp(group->name, name) == 0) {
573 osync_debug(
"OSPLG", 0,
"Couldnt find the group with the name %s", name);
587 env->groups = g_list_append(env->groups, group);
600 env->groups = g_list_remove(env->groups, group);
613 return g_list_length(env->groups);
627 return (
OSyncGroup *)g_list_nth_data(env->groups, nth);;
654 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
655 osync_debug(
"OSXML", 1,
"File %s does not exist", path);
656 osync_error_set(error, OSYNC_ERROR_IO_ERROR,
"File %s does not exist", path);
660 *doc = xmlParseFile(path);
663 osync_debug(
"OSXML", 1,
"Could not open: %s", path);
664 osync_error_set(error, OSYNC_ERROR_IO_ERROR,
"Could not open: %s", path);
668 *cur = xmlDocGetRootElement(*doc);
671 osync_debug(
"OSXML", 0,
"%s seems to be empty", path);
672 osync_error_set(error, OSYNC_ERROR_IO_ERROR,
"%s seems to be empty", path);
677 if (xmlStrcmp((*cur)->name, (
const xmlChar *) topentry)) {
678 osync_debug(
"OSXML", 0,
"%s seems not to be a valid configfile.\n", path);
679 osync_error_set(error, OSYNC_ERROR_IO_ERROR,
"%s seems not to be a valid configfile.\n", path);
684 *cur = (*cur)->xmlChildrenNode;
702 osync_bool ret = FALSE;
703 GError *error = NULL;
704 GIOChannel *chan = g_io_channel_new_file(filename,
"w", &error);
706 osync_debug(
"OSYNC", 3,
"Unable to open file %s for writing: %s", filename, error->message);
707 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR,
"Unable to open file %s for writing: %s", filename, error->message);
711 int fd = g_io_channel_unix_get_fd(chan);
712 if (fchmod(fd, mode)) {
713 osync_debug(
"OSYNC", 3,
"Unable to set file permissions %i for file %s", mode, filename);
714 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR,
"Unable to set file permissions %i for file %s", mode, filename);
719 g_io_channel_set_encoding(chan, NULL, NULL);
720 if (g_io_channel_write_chars(chan, data, size, &writen, &error) != G_IO_STATUS_NORMAL) {
721 osync_debug(
"OSYNC", 3,
"Unable to write contents of file %s: %s", filename, error->message);
722 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR,
"Unable to write contents of file %s: %s", filename, error->message);
724 g_io_channel_flush(chan, NULL);
727 g_io_channel_shutdown(chan, FALSE, NULL);
728 g_io_channel_unref(chan);
745 osync_bool ret = FALSE;
746 GError *error = NULL;
751 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR,
"No file to open specified");
754 GIOChannel *chan = g_io_channel_new_file(filename,
"r", &error);
756 osync_debug(
"OSYNC", 3,
"Unable to read file %s: %s", filename, error->message);
757 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR,
"Unable to open file %s for reading: %s", filename, error->message);
760 g_io_channel_set_encoding(chan, NULL, NULL);
761 if (g_io_channel_read_to_end(chan, data, &sz, &error) != G_IO_STATUS_NORMAL) {
762 osync_debug(
"OSYNC", 3,
"Unable to read contents of file %s: %s", filename, error->message);
763 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR,
"Unable to read contents of file %s: %s", filename, error->message);
768 g_io_channel_shutdown(chan, FALSE, NULL);
769 g_io_channel_unref(chan);
798 void *result = g_try_malloc(size);
803 memset(result, 0, size);
807 char *osync_strreplace(
const char *input,
const char *delimiter,
const char *replacement)
809 osync_return_val_if_fail(input != NULL, NULL);
810 osync_return_val_if_fail(delimiter != NULL, NULL);
811 osync_return_val_if_fail(replacement != NULL, NULL);
813 gchar **array = g_strsplit(input, delimiter, 0);
814 gchar *ret = g_strjoinv(replacement, array);
830 if (!g_thread_supported ()) g_thread_init (NULL);
832 thread->started_mutex = g_mutex_new();
833 thread->started = g_cond_new();
834 thread->context = context;
836 g_main_context_ref(thread->context);
837 thread->loop = g_main_loop_new(thread->context, FALSE);
850 osync_assert(thread);
852 if (thread->started_mutex)
853 g_mutex_free(thread->started_mutex);
856 g_cond_free(thread->started);
859 g_main_loop_unref(thread->loop);
862 g_main_context_unref(thread->context);
881 static gboolean osyncThreadStopCallback(gpointer data)
885 g_main_loop_quit(thread->loop);
904 static gboolean osyncThreadStartCallback(gpointer data)
908 g_mutex_lock(thread->started_mutex);
909 g_cond_signal(thread->started);
910 g_mutex_unlock(thread->started_mutex);
918 g_mutex_lock(thread->started_mutex);
919 GSource *idle = g_idle_source_new();
920 g_source_set_callback(idle, osyncThreadStartCallback, thread, NULL);
921 g_source_attach(idle, thread->context);
922 thread->thread = g_thread_create ((GThreadFunc)g_main_loop_run, thread->loop, TRUE, NULL);
923 g_cond_wait(thread->started, thread->started_mutex);
924 g_mutex_unlock(thread->started_mutex);
932 osync_assert(thread);
934 GSource *source = g_idle_source_new();
935 g_source_set_callback(source, osyncThreadStopCallback, thread, NULL);
936 g_source_attach(source, thread->context);
938 g_thread_join(thread->thread);
939 thread->thread = NULL;
941 g_source_unref(source);
946 osync_bool osync_pattern_match(
const char *pattern,
const char *data,
int size)
948 GPatternSpec *spec = g_pattern_spec_new(pattern);
949 osync_bool result = g_pattern_match(spec, size, data, NULL);
950 g_pattern_spec_free(spec);