From fc030c12c67d93cc5a6c11257a8c8f869bfabde9 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 18 Sep 2015 16:01:03 +0100 Subject: [PATCH 4/5] Add a DBusString-based version of _dbus_replace_install_prefix For the moment, this is just a wrapper around the version that uses static string buffers, but in the development branch it should be rewritten to use DBusString throughout. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=92028 --- dbus/dbus-sysdeps-util-unix.c | 14 ++++++++++++++ dbus/dbus-sysdeps-util-win.c | 33 +++++++++++++++++++++++++++++++++ dbus/dbus-sysdeps.h | 2 ++ test/manual-paths.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) diff --git a/dbus/dbus-sysdeps-util-unix.c b/dbus/dbus-sysdeps-util-unix.c index 9b724cc..1a4fa07 100644 --- a/dbus/dbus-sysdeps-util-unix.c +++ b/dbus/dbus-sysdeps-util-unix.c @@ -1285,6 +1285,20 @@ _dbus_replace_install_prefix (const char *configure_time_path) return configure_time_path; } +/** + * Replace the DBUS_PREFIX in the given path, in-place, by the + * current D-Bus installation directory. On Unix this function + * does nothing, successfully. + * + * @param path path to edit + * @return #FALSE on OOM + */ +dbus_bool_t +_dbus_replace_install_prefix_str (DBusString *path) +{ + return TRUE; +} + #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services" #define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services" diff --git a/dbus/dbus-sysdeps-util-win.c b/dbus/dbus-sysdeps-util-win.c index 096ffee..f2ed1a7 100644 --- a/dbus/dbus-sysdeps-util-win.c +++ b/dbus/dbus-sysdeps-util-win.c @@ -1520,6 +1520,39 @@ _dbus_replace_install_prefix (const char *configure_time_path) #endif } +/* FIXME: in the development branch, redo _dbus_replace_install_prefix + * and _dbus_get_install_root to be DBusString-based instead of using + * statically sized buffers */ +/** + * Replace the DBUS_PREFIX in the given path, in-place, by the + * current D-Bus installation directory. On Unix this function + * does nothing, successfully. + * + * @param path path to edit + * @return #FALSE on OOM + */ +dbus_bool_t +_dbus_replace_install_prefix_str (DBusString *path) +{ + const char *replaced; + DBusString replaced_str; + + replaced = _dbus_replace_install_prefix (_dbus_string_get_data (path)); + + /* fast-path: nothing to do */ + if (_dbus_string_equal_c_str (path, replaced)) + return TRUE; + + _dbus_string_init_const (&replaced_str, replaced); + + /* note unusual calling convention: source is first, then dest */ + if (!_dbus_string_replace_len (&replaced_str, 0, strlen (replaced), + path, 0, _dbus_string_get_length (path))) + return FALSE; + + return TRUE; +} + /** * return the relocated DATADIR * diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 79a6cc7..4afe065 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -657,6 +657,8 @@ void _dbus_flush_caches (void); const char * _dbus_replace_install_prefix (const char *configure_time_path); +dbus_bool_t _dbus_replace_install_prefix_str (DBusString *path); + /* Do not set this too high: it is a denial-of-service risk. * See * diff --git a/test/manual-paths.c b/test/manual-paths.c index e392c5c..5116c16 100644 --- a/test/manual-paths.c +++ b/test/manual-paths.c @@ -46,10 +46,39 @@ static dbus_bool_t print_service_dirs() static dbus_bool_t print_replace_install_prefix(const char *s) { + DBusString str; const char *s2 = _dbus_replace_install_prefix(s); if (!s2) return FALSE; + if (!_dbus_string_init (&str)) + { + _dbus_assert_not_reached ("out of memory"); + return FALSE; + } + + if (!_dbus_string_append (&str, s) || + !_dbus_replace_install_prefix_str (&str)) + { + _dbus_assert_not_reached ("out of memory"); + _dbus_string_free (&str); + return FALSE; + } + + if (!_dbus_string_equal_c_str (&str, s2)) + { + printf ("old API: length %lu \"%s\"\n", + (unsigned long) strlen (s2), s2); + printf ("new API: length %lu \"%s\"\n", + (unsigned long) _dbus_string_get_length (&str), + _dbus_string_get_const_data (&str)); + _dbus_assert_not_reached ("install prefix replaced differently"); + _dbus_string_free (&str); + return FALSE; + } + + _dbus_string_free (&str); + fprintf(stdout, "replaced '%s' by '%s'\n", s, s2); return TRUE; } -- 2.5.1