Merge lp:~miguelaraujo/mysql-proxy/lib-location-hardcoding into lp:mysql-proxy/0.8

Proposed by Miguel Araújo
Status: Merged
Merged at revision: 1236
Proposed branch: lp:~miguelaraujo/mysql-proxy/lib-location-hardcoding
Merge into: lp:mysql-proxy/0.8
Diff against target: 158 lines (+77/-3)
5 files modified
src/Makefile.am (+2/-2)
src/chassis-frontend.c (+14/-0)
src/chassis-path.c (+40/-0)
src/chassis-path.h (+1/-0)
tests/unit/check_chassis_path.c (+20/-1)
To merge this branch: bzr merge lp:~miguelaraujo/mysql-proxy/lib-location-hardcoding
Reviewer Review Type Date Requested Status
Jan Kneschke (community) Approve
Review via email: mp+93630@code.launchpad.net

Description of the change

fixed hardcoding of plugin location:

    - exposed EXEC_PREFIX and PLUGINDIR on CFLAGS;
    - added gboolean chassis_path_string_is_parent_of(const char *parent, const char *child), to check if a path-string is inside another;
    - added stripping of EXEC_PREFIX from PLUGINDIR if necessary;
    - added builing of filename with correct path to installed libraries;

  added unit tests for chassis_path_string_is_parent_of();

To post a comment you must log in.
1236. By Miguel Araújo

added missing comments for gboolean chassis_path_string_is_parent_of(const char *parent, const char *child)

1237. By Miguel Araújo

Added missing else statement on chassis_frontend_init_plugin_dir();

Extended the parameters list of chassis_path_string_is_parent_of:

  - gboolean chassis_path_string_is_parent_of(const char *parent, gsize parent_len, const char *child, gsize child_len);
  - avoids using strlen();
  - we can get the size of the string from Gstring and static strings has the length at compile time;

1238. By Miguel Araújo

fixed temporary string comparing bug;

updated unit tests for chassis_path_string_is_parent_of;

Revision history for this message
Jan Kneschke (jan-kneschke) wrote :

* fix mix of space vs. tab for indenting
* fix coding style:
  if (...) {
   ...
  } else {
   ...
  }
* plugin_dir = g_build_filename(g_strdup(PLUGINDIR), NULL); doesn't need a g_build_filename(), g_strdup() does the same.
* g_build_filename(base_dir, g_strdup(rel_plugin_dir), NULL); creates a mem-leak as g_strdup() creates a copy which is never freed. The g_strdup() isn't needed in g_build_filename()
* add tests for chassis_path_string_is_parent_of(C(""), C("/foo"));

1239. By Miguel Araújo

fixed identation differences (spaces vs tabs);

fixed coding style for if/else statements;

replaced g_build_filename for g_strdup on chassis_frontend_init_plugin_dir;

removed unnecessary g_strdup on g_build_filename that leads to a mem-leak;

added missing verification if parent-path is empty on chassis_path_string_is_parent_of, and corresponding unit test;

Revision history for this message
Jan Kneschke (jan-kneschke) wrote :

add test for:
g_assert_cmpint(TRUE, ==, chassis_path_string_is_parent_of(C("/foo/bar/"), C("/foo/bar")));
g_assert_cmpint(FALSE, ==, chassis_path_string_is_parent_of(C("/foo/bar/"), C("/foo/bar2")));

review: Needs Fixing
Revision history for this message
Jan Kneschke (jan-kneschke) wrote :

referring to the previous comment:

  g_assert_cmpint(FALSE, ==, chassis_path_string_is_parent_of(C("/foo/bar/"), C("/foo/bar")));

as they the same, but not parent/child.

1240. By Miguel Araújo

added missing check for child lengh in order to compare correctly with parent;

added tests for:

  - child with same lenght as parent but different;
  - same but not parent( '/foo/bar/', '/foo/bar' );

1241. By Miguel Araújo

removed unnecessary g_assert_cmpint() on chassis_frontend_init_plugin_dir();

removed unncessary else statement on chassis_path_string_is_parent_of();

1242. By Miguel Araújo

optimizations on chassis_path_string_is_parent_of();

1243. By Miguel Araújo

added missing EXEC_PREFIX and PLUGINDIR exposed on CFLAGS;

fixed swapped vars with checking if exec-dir is parent of plugin-dir on chassis_frontend_init_plugin_dir();

Revision history for this message
Jan Kneschke (jan-kneschke) wrote :

looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Makefile.am'
2--- src/Makefile.am 2011-11-14 16:26:34 +0000
3+++ src/Makefile.am 2012-02-21 13:12:19 +0000
4@@ -22,7 +22,7 @@
5
6
7 BUILD_CPPFLAGS = $(LUA_CFLAGS) $(MYSQL_CFLAGS) $(GLIB_CFLAGS) $(GMODULE_CFLAGS) $(GTHREAD_CFLAGS)
8-BUILD_CFLAGS = -DLUAEXTDIR="\"$(luaextdir)\"" -DPLUGINDIR="\"$(plugindir)\""
9+BUILD_CFLAGS = -DLUAEXTDIR="\"$(luaextdir)\"" -DPLUGINDIR="\"$(plugindir)\"" -DEXEC_PREFIX="\"$(exec_prefix)\""
10 BUILD_LDADD = $(GLIB_LIBS) $(GMODULE_LIBS) libmysql-chassis.la $(GTHREAD_LIBS) libmysql-proxy.la libmysql-chassis-glibext.la
11
12 BUILT_SOURCES =
13@@ -99,7 +99,7 @@
14
15
16 libmysql_chassis_la_LDFLAGS = -export-dynamic -no-undefined -dynamic
17-libmysql_chassis_la_CPPFLAGS = $(MYSQL_CFLAGS) $(EVENT_CFLAGS) $(GLIB_CFLAGS) $(LUA_CFLAGS) $(GMODULE_CFLAGS) $(GTHREAD_CFLAGS)
18+libmysql_chassis_la_CPPFLAGS = $(MYSQL_CFLAGS) $(EVENT_CFLAGS) $(GLIB_CFLAGS) $(LUA_CFLAGS) $(GMODULE_CFLAGS) $(GTHREAD_CFLAGS) -DPLUGINDIR="\"$(plugindir)\"" -DEXEC_PREFIX="\"$(exec_prefix)\""
19 libmysql_chassis_la_LIBADD = $(EVENT_LIBS) $(GLIB_LIBS) $(LUA_LIBS) $(GMODULE_LIBS) $(GTHREAD_LIBS) libmysql-chassis-timing.la libmysql-chassis-glibext.la
20
21 lib_LTLIBRARIES += libmysql-proxy.la
22
23=== modified file 'src/chassis-frontend.c'
24--- src/chassis-frontend.c 2012-01-23 20:01:49 +0000
25+++ src/chassis-frontend.c 2012-02-21 13:12:19 +0000
26@@ -308,11 +308,25 @@
27
28 int chassis_frontend_init_plugin_dir(char **_plugin_dir, const char *base_dir) {
29 char *plugin_dir = *_plugin_dir;
30+ char *rel_plugin_dir;
31
32 if (plugin_dir) return 0;
33
34 #ifdef WIN32
35 plugin_dir = g_build_filename(base_dir, "bin", NULL);
36+
37+#elif defined(PLUGINDIR) && defined(EXEC_PREFIX)
38+
39+ if (chassis_path_string_is_parent_of(C(EXEC_PREFIX), C(PLUGINDIR)) {
40+ rel_plugin_dir = PLUGINDIR + sizeof(EXEC_PREFIX) - 1;
41+
42+ if (rel_plugin_dir[0] == G_DIR_SEPARATOR) rel_plugin_dir++; /* if plugindir starts with a /, skip it */
43+
44+ plugin_dir = g_build_filename(base_dir, rel_plugin_dir, NULL);
45+ } else {
46+ plugin_dir = g_strdup(PLUGINDIR);
47+ }
48+
49 #else
50 plugin_dir = g_build_filename(base_dir, "lib", PACKAGE, "plugins", NULL);
51 #endif
52
53=== modified file 'src/chassis-path.c'
54--- src/chassis-path.c 2010-04-06 14:26:51 +0000
55+++ src/chassis-path.c 2012-02-21 13:12:19 +0000
56@@ -125,4 +125,44 @@
57 return TRUE;
58 }
59
60+/**
61+ * Check if a path-string is parent of another
62+ * @returns TRUE if the path-string is parent, FALSE if don't
63+ */
64+gboolean chassis_path_string_is_parent_of(const char *parent, gsize parent_len, const char *child, gsize child_len) {
65+
66+ if (child_len < parent_len)
67+ return FALSE;
68+
69+ if(parent_len == 0)
70+ return FALSE;
71+
72+ /* On Unix systems '/' is parent of all directories' */
73+ if(0 == strcmp(parent, "/"))
74+ return TRUE;
75+
76+ /* The path-string child can start with the same prefix string however the directory is not correct,
77+ * e.g. parent: /foo/bar , child: /foo/bar-foo.
78+ * To make the correct comparison we need to check if the parent path-string ends with the directory separator '/',
79+ * and if not, add it before doing the comparison
80+ */
81+ if (parent[parent_len - 1] != G_DIR_SEPARATOR) {
82+ /* g_strndup adds the trailing '\0' at the end of the new string */
83+ char *temp_parent = g_strndup(parent, parent_len + 1);
84+ temp_parent[parent_len] = G_DIR_SEPARATOR;
85+
86+ if (0 == strncmp(temp_parent, child, parent_len + 1)) {
87+ g_free(temp_parent);
88+ return TRUE;
89+ }
90+
91+ g_free(temp_parent);
92+ return FALSE;
93+ }
94+
95+ if (0 == strncmp(parent, child, parent_len))
96+ return TRUE;
97+
98+ return FALSE;
99+}
100
101
102=== modified file 'src/chassis-path.h'
103--- src/chassis-path.h 2010-04-06 14:26:51 +0000
104+++ src/chassis-path.h 2012-02-21 13:12:19 +0000
105@@ -27,6 +27,7 @@
106
107 CHASSIS_API gboolean chassis_resolve_path(const char *base_dir, gchar **filename);
108 CHASSIS_API gchar *chassis_get_basedir(const gchar *prgname);
109+CHASSIS_API gboolean chassis_path_string_is_parent_of(const char *parent, gsize parent_len, const char *child, gsize child_len);
110
111 #endif
112
113
114=== modified file 'tests/unit/check_chassis_path.c'
115--- tests/unit/check_chassis_path.c 2010-04-06 14:26:51 +0000
116+++ tests/unit/check_chassis_path.c 2012-02-21 13:12:19 +0000
117@@ -28,6 +28,8 @@
118
119 #include "chassis-path.h"
120
121+#include <string.h>
122+
123 #if GLIB_CHECK_VERSION(2, 16, 0)
124 #define C(x) x, sizeof(x) - 1
125
126@@ -94,6 +96,22 @@
127 }
128 /*@}*/
129
130+void test_path_string_is_parent_of(void) {
131+ g_assert_cmpint(TRUE, ==, chassis_path_string_is_parent_of(C("/foo"), C("/foo/bar/")));
132+ g_assert_cmpint(TRUE, ==, chassis_path_string_is_parent_of(C("/"), C("/foo/")));
133+ g_assert_cmpint(TRUE, ==, chassis_path_string_is_parent_of(C("/"), C("/foo")));
134+ g_assert_cmpint(TRUE, ==, chassis_path_string_is_parent_of(C("/foo"), C("/foo/")));
135+ g_assert_cmpint(TRUE, ==, chassis_path_string_is_parent_of(C("/foo/bar"), C("/foo/bar/bar")));
136+ g_assert_cmpint(FALSE, ==, chassis_path_string_is_parent_of(C("/foo/bar"), C("/foo/bar-foo")));
137+ g_assert_cmpint(FALSE, ==, chassis_path_string_is_parent_of(C("/foo/bar2"), C("/foo/bar")));
138+ g_assert_cmpint(FALSE, ==, chassis_path_string_is_parent_of(C("/foo/bar"), C("/foo/bar2")));
139+ g_assert_cmpint(FALSE, ==, chassis_path_string_is_parent_of(C(""), C("/foo")));
140+ g_assert_cmpint(FALSE, ==, chassis_path_string_is_parent_of(C("/foo/bar/"), C("/foo/bar2")));
141+ g_assert_cmpint(FALSE, ==, chassis_path_string_is_parent_of(C("/foo/bar/"), C("/foo/bar")));
142+}
143+
144+/*@}*/
145+
146 int main(int argc, char **argv) {
147 g_thread_init(NULL);
148
149@@ -103,7 +121,8 @@
150 g_test_add_func("/core/basedir/relpath", test_path_basedir);
151 g_test_add_func("/core/basedir/nobasedir", test_no_basedir);
152 g_test_add_func("/core/basedir/abspath", test_abspath_basedir);
153-
154+ g_test_add_func("/core/path_string/is_parent_of", test_path_string_is_parent_of);
155+
156 return g_test_run();
157 }
158 #else

Subscribers

People subscribed via source and target branches