END_TEST

START_TEST (module_check)
{
	char *testbed = setup_testbed(NULL);
	
	OSyncError *error = NULL;
	OSyncModule *module = osync_module_new(&error);
	fail_unless(module != NULL, NULL);
	fail_unless(error == NULL, NULL);
	
	char *curdir = g_get_current_dir();
	char *path = g_strdup_printf("%s/formats/mock-format.%s", curdir, G_MODULE_SUFFIX);
	fail_unless(osync_module_load(module, path, &error), NULL);
	fail_unless(error == NULL, NULL);
	g_free(path);
	g_free(curdir);
	
	fail_unless(osync_module_check(module, &error), NULL);
	fail_unless(error == NULL, NULL);
	
	osync_module_unload(module);
	
	osync_module_unref(module);
	
	destroy_testbed(testbed);
}
osync_bool osync_plugin_env_load_module(OSyncPluginEnv *env, const char *filename, OSyncError **error)
{
	OSyncModule *module = NULL;
	
	osync_trace(TRACE_ENTRY, "%s(%p, %s, %p)", __func__, env, filename, error);
	osync_assert(env);
	osync_assert(filename);
	
	module = osync_module_new(error);
	if (!module)
		goto error;
	
	if (!osync_module_load(module, filename, error)) {
		osync_trace(TRACE_INTERNAL, "Unable to load module %s: %s", filename, osync_error_print(error));
		osync_module_free(module);
	} else {
		if (!osync_module_check(module, error)) {
			if (osync_error_is_set(error)) {
				osync_trace(TRACE_INTERNAL, "Module check error for %s: %s", filename, osync_error_print(error));
			}
			osync_module_unload(module);
			osync_module_free(module);
			osync_trace(TRACE_EXIT, "%s: Unable to load module", __func__);
			return FALSE;
		}
		
		if (!osync_module_get_sync_info(module, env, error)) {
			if (osync_error_is_set(error))
				goto error_free_module;
			
			osync_module_unload(module);
			osync_module_free(module);
			osync_trace(TRACE_EXIT, "%s: No get_sync_info function", __func__);
			return FALSE;
		}
		env->modules = g_list_append(env->modules, module);
	}
	
	osync_trace(TRACE_EXIT, "%s", __func__);
	return TRUE;

error_free_module:
	osync_module_unload(module);
	osync_module_free(module);
error:
	osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error));
	return FALSE;
}
Example #3
0
static osync_bool osync_format_env_load_modules(OSyncFormatEnv *env, const char *path, osync_bool must_exist, OSyncError **error)
{
  GDir *dir = NULL;
  GError *gerror = NULL;
  char *filename = NULL;
  OSyncModule *module = NULL;
  const gchar *de = NULL;
  GList *m = NULL;
	
  osync_trace(TRACE_ENTRY, "%s(%p, %s, %i, %p)", __func__, env, path, must_exist, error);
  osync_assert(env);
  osync_assert(path);
	
  //Load all available shared libraries (plugins)
  if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
    if (must_exist) {
      osync_error_set(error, OSYNC_ERROR_GENERIC, "Path is not loadable");
      goto error;
    } else {
      osync_trace(TRACE_EXIT, "%s: Directory does not exist (non-fatal)", __func__);
      return TRUE;
    }
  }
	
  dir = g_dir_open(path, 0, &gerror);
  if (!dir) {
    osync_error_set(error, OSYNC_ERROR_IO_ERROR, "Unable to open directory %s: %s", path, gerror->message);
    g_error_free(gerror);
    goto error;
  }
	
  while ((de = g_dir_read_name(dir))) {
    filename = g_strdup_printf ("%s%c%s", path, G_DIR_SEPARATOR, de);
		
    if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR) || !g_pattern_match_simple("*."G_MODULE_SUFFIX, filename)) {
      g_free(filename);
      continue;
    }
		
    module = osync_module_new(error);
    if (!module)
      goto error_free_filename;
		
    if (!osync_module_load(module, filename, error)) {
      osync_trace(TRACE_INTERNAL, "Unable to load module %s: %s", filename, osync_error_print(error));
      osync_module_free(module);
      g_free(filename);
      continue;
    }
		
    if (!osync_module_check(module, error)) {
      if (osync_error_is_set(error)) {
        osync_trace(TRACE_INTERNAL, "Module check error for %s: %s", filename, osync_error_print(error));
      }
      osync_module_free(module);
      g_free(filename);
      continue;
    }
	
    if (!osync_module_get_format_info(module, env, error) && !osync_module_get_function(module, "get_conversion_info", NULL)) {
      if (osync_error_is_set(error)) {
        osync_trace(TRACE_ERROR, "Module load format plugin error for %s: %s", filename, osync_error_print(error));
      }
      osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to load format plugin %s. Neither a converter nor a format could be initialized.", __NULLSTR(filename));
      osync_trace(TRACE_ERROR, "%s", osync_error_print(error));
      osync_module_free(module);
      g_free(filename);
      continue;
    }
		
    env->modules = g_list_append(env->modules, module);
		
    g_free(filename);
  }
	
  g_dir_close(dir);
	
  /* Load the converters, filters, etc */
  for (m = env->modules; m; m = m->next) {
    module = m->data;
    if (!osync_module_get_conversion_info(module, env, error)) {
      osync_trace(TRACE_INTERNAL, "Module get conversion error %s", osync_error_print(error));
      osync_error_unref(error);
    }
  }
	
  osync_trace(TRACE_EXIT, "%s", __func__);
  return TRUE;

 error_free_filename:
  g_free(filename);
  g_dir_close(dir);
 error:
  osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error));
  return FALSE;
}