gplugin/gplugin

Make the search paths always have trailing slashes
develop
2020-08-29, Gary Kramlich
22eacbf0c2f8
Parents e38f74f1b4d8
Children 91d639a0209b
Make the search paths always have trailing slashes

Make sure the internal representation of search paths always have a trailing slash.

Testing Done:
Compiled and ran unit tests.

Reviewed at https://reviews.imfreedom.org/r/97/
--- a/gplugin/gplugin-manager.c Wed Aug 26 03:12:40 2020 -0500
+++ b/gplugin/gplugin-manager.c Sat Aug 29 03:48:20 2020 -0500
@@ -254,6 +254,33 @@
}
}
+static gchar *
+gplugin_manager_normalize_path(const gchar *path)
+{
+ if(g_str_has_suffix(path, G_DIR_SEPARATOR_S)) {
+ return g_strdup(path);
+ }
+
+ return g_strdup_printf("%s%s", path, G_DIR_SEPARATOR_S);
+}
+
+static gint
+gplugin_manager_compare_paths(gconstpointer a, gconstpointer b)
+{
+ gchar *keya = NULL, *keyb = NULL;
+ gint r = 0;
+
+ keya = g_utf8_collate_key_for_filename((const gchar *)a, -1);
+ keyb = g_utf8_collate_key_for_filename((const gchar *)b, -1);
+
+ r = strcmp(keya, keyb);
+
+ g_free(keya);
+ g_free(keyb);
+
+ return r;
+}
+
/******************************************************************************
* Manager implementation
*****************************************************************************/
@@ -261,53 +288,74 @@
gplugin_manager_real_append_path(GPluginManager *manager, const gchar *path)
{
GList *l = NULL;
+ gchar *normalized = NULL;
- if(!path)
+ if(!path) {
return;
+ }
+
+ normalized = gplugin_manager_normalize_path(path);
- for(l = manager->paths->head; l; l = l->next)
- if(strcmp(l->data, path) == 0)
- return;
-
- g_queue_push_tail(manager->paths, g_strdup(path));
+ l = g_queue_find_custom(
+ manager->paths,
+ normalized,
+ gplugin_manager_compare_paths);
+ if(l == NULL) {
+ g_queue_push_tail(manager->paths, normalized);
+ } else {
+ g_free(normalized);
+ }
}
static void
gplugin_manager_real_prepend_path(GPluginManager *manager, const gchar *path)
{
GList *l = NULL;
+ gchar *normalized = NULL;
- if(!path)
+ if(!path) {
return;
+ }
+
+ normalized = gplugin_manager_normalize_path(path);
- for(l = manager->paths->head; l; l = l->next)
- if(strcmp(l->data, path) == 0)
- return;
-
- g_queue_push_head(manager->paths, g_strdup(path));
+ l = g_queue_find_custom(
+ manager->paths,
+ normalized,
+ gplugin_manager_compare_paths);
+ if(l == NULL) {
+ g_queue_push_head(manager->paths, normalized);
+ } else {
+ g_free(normalized);
+ }
}
static void
gplugin_manager_real_remove_path(GPluginManager *manager, const gchar *path)
{
- GList *l = NULL, *link = NULL;
+ GList *l = NULL;
+ gchar *normalized = NULL;
g_return_if_fail(path != NULL);
- for(l = manager->paths->head; l; l = l->next)
- if(strcmp(l->data, path) == 0) {
- g_free(l->data);
- link = l;
- }
+ normalized = gplugin_manager_normalize_path(path);
- if(link)
- g_queue_delete_link(manager->paths, link);
+ l = g_queue_find_custom(
+ manager->paths,
+ normalized,
+ gplugin_manager_compare_paths);
+ if(l != NULL) {
+ g_free(l->data);
+ g_queue_delete_link(manager->paths, l);
+ }
+
+ g_free(normalized);
}
static void
gplugin_manager_real_remove_paths(GPluginManager *manager)
{
- g_queue_clear(manager->paths);
+ g_queue_clear_full(manager->paths, g_free);
}
static GList *
--- a/gplugin/tests/test-option-group.c Wed Aug 26 03:12:40 2020 -0500
+++ b/gplugin/tests/test-option-group.c Sat Aug 29 03:48:20 2020 -0500
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2011-2014 Gary Kramlich <grim@reaperworld.com>
+ * Copyright (C) 2011-2020 Gary Kramlich <grim@reaperworld.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -69,10 +69,14 @@
GList *paths = NULL;
gchar *path = NULL;
- path = g_build_filename(PREFIX, LIBDIR, "gplugin", NULL);
+ path = g_build_filename(PREFIX, LIBDIR, "gplugin", G_DIR_SEPARATOR_S, NULL);
paths = g_list_prepend(paths, path);
- path = g_build_filename(g_get_user_config_dir(), "gplugin", NULL);
+ path = g_build_filename(
+ g_get_user_config_dir(),
+ "gplugin",
+ G_DIR_SEPARATOR_S,
+ NULL);
paths = g_list_prepend(paths, path);
return paths;
@@ -96,7 +100,7 @@
gchar *args = "test-option-group,-p,foo";
expected = test_gplugin_manager_default_paths();
- expected = g_list_prepend(expected, g_strdup("foo"));
+ expected = g_list_prepend(expected, g_strdup("foo/"));
test_gplugin_option_group_paths(args, expected);
}
@@ -108,9 +112,9 @@
gchar *args = "test-option-group,-p,foo,-p,bar,-p,baz";
expected = test_gplugin_manager_default_paths();
- expected = g_list_prepend(expected, g_strdup("foo"));
- expected = g_list_prepend(expected, g_strdup("bar"));
- expected = g_list_prepend(expected, g_strdup("baz"));
+ expected = g_list_prepend(expected, g_strdup("foo/"));
+ expected = g_list_prepend(expected, g_strdup("bar/"));
+ expected = g_list_prepend(expected, g_strdup("baz/"));
test_gplugin_option_group_paths(args, expected);
}
@@ -137,7 +141,7 @@
GList *expected = NULL;
gchar *args = "test-option-group,-D,-p,foo";
- expected = g_list_prepend(expected, g_strdup("foo"));
+ expected = g_list_prepend(expected, g_strdup("foo/"));
test_gplugin_option_group_paths(args, expected);
}
@@ -148,9 +152,9 @@
GList *expected = NULL;
gchar *args = "test-option-group,-D,-p,foo,-p,bar,-p,baz";
- expected = g_list_prepend(expected, g_strdup("foo"));
- expected = g_list_prepend(expected, g_strdup("bar"));
- expected = g_list_prepend(expected, g_strdup("baz"));
+ expected = g_list_prepend(expected, g_strdup("foo/"));
+ expected = g_list_prepend(expected, g_strdup("bar/"));
+ expected = g_list_prepend(expected, g_strdup("baz/"));
test_gplugin_option_group_paths(args, expected);
}
@@ -158,7 +162,7 @@
static void
test_gplugin_option_group_add_path_short(void)
{
- GList *expected = g_list_prepend(NULL, g_strdup("foo"));
+ GList *expected = g_list_prepend(NULL, g_strdup("foo/"));
gchar *args = "test-option-group,-D,-p,foo";
test_gplugin_option_group_paths(args, expected);
@@ -167,7 +171,7 @@
static void
test_gplugin_option_group_add_path_long(void)
{
- GList *expected = g_list_prepend(NULL, g_strdup("foo"));
+ GList *expected = g_list_prepend(NULL, g_strdup("foo/"));
gchar *args = "test-option-group,-D,--path,foo";
test_gplugin_option_group_paths(args, expected);
--- a/gplugin/tests/test-plugin-manager-paths.c Wed Aug 26 03:12:40 2020 -0500
+++ b/gplugin/tests/test-plugin-manager-paths.c Sat Aug 29 03:48:20 2020 -0500
@@ -89,6 +89,42 @@
}
static void
+test_gplugin_manager_paths_unicode(void)
+{
+ test_path_count(0);
+
+ gplugin_manager_append_path("/home/🐦/.plugins");
+ test_path_count(1);
+
+ gplugin_manager_append_path("/home/user/.plugins");
+ test_path_count(2);
+
+ gplugin_manager_remove_path("/home/🐦/.plugins");
+ test_path_count(1);
+
+ gplugin_manager_remove_path("/home/user/.plugins");
+ test_path_count(0);
+}
+
+static void
+test_gplugin_manager_add_multiple_mixed_trailing_slashes(void)
+{
+ test_path_count(0);
+
+ gplugin_manager_append_path("/home/user1/.plugins");
+ test_path_count(1);
+
+ gplugin_manager_append_path("/home/user2/.plugins/");
+ test_path_count(2);
+
+ gplugin_manager_remove_path("/home/user1/.plugins/");
+ test_path_count(1);
+
+ gplugin_manager_remove_path("/home/user2/.plugins");
+ test_path_count(0);
+}
+
+static void
test_gplugin_manager_add_default_paths(void)
{
GHashTable *req = NULL;
@@ -100,10 +136,14 @@
req = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
/* create and add the paths we are expecting to the table */
- path = g_build_filename(PREFIX, LIBDIR, "gplugin", NULL);
+ path = g_build_filename(PREFIX, LIBDIR, "gplugin", G_DIR_SEPARATOR_S, NULL);
g_hash_table_insert(req, path, GINT_TO_POINTER(FALSE));
- path = g_build_filename(g_get_user_config_dir(), "gplugin", NULL);
+ path = g_build_filename(
+ g_get_user_config_dir(),
+ "gplugin",
+ G_DIR_SEPARATOR_S,
+ NULL);
g_hash_table_insert(req, path, GINT_TO_POINTER(FALSE));
/* now tell the plugin manager to add the default paths */
@@ -135,10 +175,15 @@
/* build our table of required paths */
req = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
- path = g_build_filename(prefix, LIBDIR, appname, NULL);
+ path = g_build_filename(prefix, LIBDIR, appname, G_DIR_SEPARATOR_S, NULL);
g_hash_table_insert(req, path, GINT_TO_POINTER(FALSE));
- path = g_build_filename(g_get_user_config_dir(), appname, "plugins", NULL);
+ path = g_build_filename(
+ g_get_user_config_dir(),
+ appname,
+ "plugins",
+ G_DIR_SEPARATOR_S,
+ NULL);
g_hash_table_insert(req, path, GINT_TO_POINTER(FALSE));
/* now add the app paths */
@@ -186,6 +231,14 @@
test_gplugin_manager_paths_multiple_filo);
g_test_add_func(
+ "/plugins/paths/add_multiple_unicode",
+ test_gplugin_manager_paths_unicode);
+
+ g_test_add_func(
+ "/plugins/paths/add_multiple_mixed_trailing_slashes",
+ test_gplugin_manager_add_multiple_mixed_trailing_slashes);
+
+ g_test_add_func(
"/plugins/paths/add_default_paths",
test_gplugin_manager_add_default_paths);
g_test_add_func(