r14862 - gnucash/trunk - Implement automatic relocation of previously compiled-in paths.

Christian Stimming cstim at cvs.gnucash.org
Mon Sep 18 16:43:52 EDT 2006


Author: cstim
Date: 2006-09-18 16:43:50 -0400 (Mon, 18 Sep 2006)
New Revision: 14862
Trac: http://svn.gnucash.org/trac/changeset/14862

Added:
   gnucash/trunk/macros/binreloc.m4
   gnucash/trunk/src/engine/binreloc.c
   gnucash/trunk/src/engine/binreloc.h
Modified:
   gnucash/trunk/ChangeLog
   gnucash/trunk/Makefile.am
   gnucash/trunk/configure.in
   gnucash/trunk/src/bin/gnucash-bin.c
   gnucash/trunk/src/engine/Makefile.am
   gnucash/trunk/src/engine/gnc-engine.c
   gnucash/trunk/src/engine/gnc-path.c
   gnucash/trunk/src/engine/gncla-dir.h.in
Log:
Implement automatic relocation of previously compiled-in paths. 
With the exception of the env variables in src/bin/gnucash we're
fully relocatable now.



Modified: gnucash/trunk/ChangeLog
===================================================================
--- gnucash/trunk/ChangeLog	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/ChangeLog	2006-09-18 20:43:50 UTC (rev 14862)
@@ -1,3 +1,10 @@
+2006-09-18  Christian Stimming  <stimming at tuhh.de>
+
+	* src/engine/binreloc.[hc], src/engine/gnc-path.c,
+	src/bin/gnucash-bin.c, configure.in: Implement automatic
+	relocation of previously compiled-in paths. With the exception of
+	the env variables in src/bin/gnucash, we're fully relocatable now.
+
 2006-09-13  Christian Stimming <stimming at tuhh.de>
 
 	* src/import-export/hbci/gnc-plugin-hbci.c: Move the MT940

Modified: gnucash/trunk/Makefile.am
===================================================================
--- gnucash/trunk/Makefile.am	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/Makefile.am	2006-09-18 20:43:50 UTC (rev 14862)
@@ -46,6 +46,7 @@
   make-gnucash-potfiles.in \
   macros/aqbanking.m4 \
   macros/as-scrub-include.m4 \
+  macros/binreloc.m4 \
   macros/compiler-flags.m4 \
   macros/g-wrap.m4 \
   macros/gnome-guile-checks.m4 \

Modified: gnucash/trunk/configure.in
===================================================================
--- gnucash/trunk/configure.in	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/configure.in	2006-09-18 20:43:50 UTC (rev 14862)
@@ -152,6 +152,9 @@
 AC_CHECK_FUNCS(chown gethostname getppid getuid gettimeofday gmtime_r)
 AC_CHECK_FUNCS(gethostid link)
 
+# Init binreloc
+AM_BINRELOC
+
 ### --------------------------------------------------------------------------
 ### See if we have pkg-config
 

Added: gnucash/trunk/macros/binreloc.m4
===================================================================
--- gnucash/trunk/macros/binreloc.m4	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/macros/binreloc.m4	2006-09-18 20:43:50 UTC (rev 14862)
@@ -0,0 +1,77 @@
+# Check for binary relocation support.
+# Written by Hongli Lai
+# http://autopackage.org/
+
+AC_DEFUN([AM_BINRELOC],
+[
+	AC_ARG_ENABLE(binreloc,
+		[  --enable-binreloc       compile with binary relocation support
+                          (default=enable when available)],
+		enable_binreloc=$enableval,enable_binreloc=auto)
+
+	AC_ARG_ENABLE(binreloc-threads,
+		[  --enable-binreloc-threads      compile binary relocation with threads support
+	                         (default=yes)],
+		enable_binreloc_threads=$enableval,enable_binreloc_threads=yes)
+
+	BINRELOC_CFLAGS=
+	BINRELOC_LIBS=
+	if test "x$enable_binreloc" = "xauto"; then
+		AC_CHECK_FILE([/proc/self/maps])
+		AC_CACHE_CHECK([whether everything is installed to the same prefix],
+			       [br_cv_valid_prefixes], [
+				if test "$bindir" = '${exec_prefix}/bin' -a "$sbindir" = '${exec_prefix}/sbin' -a \
+					"$datadir" = '${prefix}/share' -a "$libdir" = '${exec_prefix}/lib' -a \
+					"$libexecdir" = '${exec_prefix}/libexec' -a "$sysconfdir" = '${prefix}/etc'
+				then
+					br_cv_valid_prefixes=yes
+				else
+					br_cv_valid_prefixes=no
+				fi
+				])
+	fi
+	AC_CACHE_CHECK([whether binary relocation support should be enabled],
+		       [br_cv_binreloc],
+		       [if test "x$enable_binreloc" = "xyes"; then
+		       	       br_cv_binreloc=yes
+		       elif test "x$enable_binreloc" = "xauto"; then
+			       if test "x$br_cv_valid_prefixes" = "xyes" -a \
+			       	       "x$ac_cv_file__proc_self_maps" = "xyes"; then
+				       br_cv_binreloc=yes
+			       else
+				       br_cv_binreloc=no
+			       fi
+		       else
+			       br_cv_binreloc=no
+		       fi])
+
+	if test "x$br_cv_binreloc" = "xyes"; then
+		BINRELOC_CFLAGS="-DENABLE_BINRELOC"
+		AC_DEFINE(ENABLE_BINRELOC,,[Use binary relocation?])
+		if test "x$enable_binreloc_threads" = "xyes"; then
+			AC_CHECK_LIB([pthread], [pthread_getspecific])
+		fi
+
+		AC_CACHE_CHECK([whether binary relocation should use threads],
+			       [br_cv_binreloc_threads],
+			       [if test "x$enable_binreloc_threads" = "xyes"; then
+					if test "x$ac_cv_lib_pthread_pthread_getspecific" = "xyes"; then
+						br_cv_binreloc_threads=yes
+					else
+						br_cv_binreloc_threads=no
+					fi
+			        else
+					br_cv_binreloc_threads=no
+				fi])
+
+		if test "x$br_cv_binreloc_threads" = "xyes"; then
+			BINRELOC_LIBS="-lpthread"
+			AC_DEFINE(BR_PTHREAD,1,[Include pthread support for binary relocation?])
+		else
+			BINRELOC_CFLAGS="$BINRELOC_CFLAGS -DBR_PTHREADS=0"
+			AC_DEFINE(BR_PTHREAD,0,[Include pthread support for binary relocation?])
+		fi
+	fi
+	AC_SUBST(BINRELOC_CFLAGS)
+	AC_SUBST(BINRELOC_LIBS)
+])

Modified: gnucash/trunk/src/bin/gnucash-bin.c
===================================================================
--- gnucash/trunk/src/bin/gnucash-bin.c	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/src/bin/gnucash-bin.c	2006-09-18 20:43:50 UTC (rev 14862)
@@ -34,6 +34,7 @@
 #include "glib.h"
 #include "gnc-module.h"
 #include "gnc-path.h"
+#include "binreloc.h"
 #include "gnc-version.h"
 #include "gnc-engine.h"
 #include "gnc-filepath-utils.h"
@@ -496,7 +497,14 @@
 
 int main(int argc, char ** argv)
 {
-    gchar *localedir = gnc_path_get_localedir ();
+    gchar *localedir;
+    GError *binreloc_error = NULL;
+
+    /* Init binreloc */
+    if (!gbr_init (&binreloc_error) ) {
+      printf("main: Error on gbr_init: %s\n", binreloc_error->message);
+    }
+    localedir = gnc_path_get_localedir ();
 #ifdef HAVE_GETTEXT
     /* setlocale (LC_ALL, ""); is already called by gtk_set_locale()
        via gtk_init(). */

Modified: gnucash/trunk/src/engine/Makefile.am
===================================================================
--- gnucash/trunk/src/engine/Makefile.am	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/src/engine/Makefile.am	2006-09-18 20:43:50 UTC (rev 14862)
@@ -28,6 +28,7 @@
   Split.c \
   TransLog.c \
   Transaction.c \
+  binreloc.c \
   cap-gains.c \
   cashobjects.c \
   gnc-associate-account.c \
@@ -67,6 +68,7 @@
   Split.h \
   TransLog.h \
   Transaction.h \
+  binreloc.h \
   cap-gains.h \
   cashobjects.h \
   engine-helpers.h \
@@ -113,6 +115,7 @@
   ${QOF_LIBS} \
   ${REGEX_LIBS} \
   ${GNUCASH_ENGINE_BASE_LIBS} \
+  ${BINRELOC_LIBS} \
   ${top_builddir}/lib/libc/libc-missing.la
 
 libgw_kvp_la_SOURCES = kvp-scm.c
@@ -229,8 +232,8 @@
 		-e 's#@-GCONF_SCHEMA_CONFIG_SOURCE_DIRONLY-@#${GCONF_SCHEMA_CONFIG_SOURCE_DIRONLY}#g' \
 		-e 's#@-pkglibdir-@#${pkglibdir}#g' \
 		-e 's#@-libdir-@#${libdir}#g' \
-		-e 's#@-pkgsysconfdir-@#${GNC_CONFIGDIR}#g' \
-		-e 's#@-pkgdatadir-@#${GNC_SHAREDIR}#g' \
+		-e 's#@-sysconfdir-@#${sysconfdir}#g' \
+		-e 's#@-datadir-@#${datadir}#g' \
 		-e 's#@-prefix-@#${prefix}#g'
 	mv $@.tmp $@
 

Added: gnucash/trunk/src/engine/binreloc.c
===================================================================
--- gnucash/trunk/src/engine/binreloc.c	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/src/engine/binreloc.c	2006-09-18 20:43:50 UTC (rev 14862)
@@ -0,0 +1,697 @@
+/*
+ * BinReloc - a library for creating relocatable executables
+ * Written by: Hongli Lai <h.lai at chello.nl>
+ * http://autopackage.org/
+ *
+ * This source code is public domain. You can relicense this code
+ * under whatever license you want.
+ *
+ * See http://autopackage.org/docs/binreloc/ for
+ * more information and how to use this.
+ */
+
+#ifndef __BINRELOC_C__
+#define __BINRELOC_C__
+#include "config.h"
+
+#ifdef ENABLE_BINRELOC
+	#include <sys/types.h>
+	#include <sys/stat.h>
+	#include <unistd.h>
+#endif /* ENABLE_BINRELOC */
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <string.h>
+#include "binreloc.h"
+
+G_BEGIN_DECLS
+
+
+/** @internal
+ * Find the canonical filename of the executable. Returns the filename
+ * (which must be freed) or NULL on error. If the parameter 'error' is
+ * not NULL, the error code will be stored there, if an error occured.
+ */
+static char *
+_br_find_exe (GbrInitError *error)
+{
+#ifndef ENABLE_BINRELOC
+	if (error)
+		*error = GBR_INIT_ERROR_DISABLED;
+	return NULL;
+#else
+	char *path, *path2, *line, *result;
+	size_t buf_size;
+	ssize_t size;
+	struct stat stat_buf;
+	FILE *f;
+
+	/* Read from /proc/self/exe (symlink) */
+	if (sizeof (path) > SSIZE_MAX)
+		buf_size = SSIZE_MAX - 1;
+	else
+		buf_size = PATH_MAX - 1;
+	path = (char *) g_try_malloc (buf_size);
+	if (path == NULL) {
+		/* Cannot allocate memory. */
+		if (error)
+			*error = GBR_INIT_ERROR_NOMEM;
+		return NULL;
+	}
+	path2 = (char *) g_try_malloc (buf_size);
+	if (path2 == NULL) {
+		/* Cannot allocate memory. */
+		if (error)
+			*error = GBR_INIT_ERROR_NOMEM;
+		g_free (path);
+		return NULL;
+	}
+
+	strncpy (path2, "/proc/self/exe", buf_size - 1);
+
+	while (1) {
+		int i;
+
+		size = readlink (path2, path, buf_size - 1);
+		if (size == -1) {
+			/* Error. */
+			g_free (path2);
+			break;
+		}
+
+		/* readlink() success. */
+		path[size] = '\0';
+
+		/* Check whether the symlink's target is also a symlink.
+		 * We want to get the final target. */
+		i = stat (path, &stat_buf);
+		if (i == -1) {
+			/* Error. */
+			g_free (path2);
+			break;
+		}
+
+		/* stat() success. */
+		if (!S_ISLNK (stat_buf.st_mode)) {
+			/* path is not a symlink. Done. */
+			g_free (path2);
+			return path;
+		}
+
+		/* path is a symlink. Continue loop and resolve this. */
+		strncpy (path, path2, buf_size - 1);
+	}
+
+
+	/* readlink() or stat() failed; this can happen when the program is
+	 * running in Valgrind 2.2. Read from /proc/self/maps as fallback. */
+
+	buf_size = PATH_MAX + 128;
+	line = (char *) g_try_realloc (path, buf_size);
+	if (line == NULL) {
+		/* Cannot allocate memory. */
+		g_free (path);
+		if (error)
+			*error = GBR_INIT_ERROR_NOMEM;
+		return NULL;
+	}
+
+	f = fopen ("/proc/self/maps", "r");
+	if (f == NULL) {
+		g_free (line);
+		if (error)
+			*error = GBR_INIT_ERROR_OPEN_MAPS;
+		return NULL;
+	}
+
+	/* The first entry should be the executable name. */
+	result = fgets (line, (int) buf_size, f);
+	if (result == NULL) {
+		fclose (f);
+		g_free (line);
+		if (error)
+			*error = GBR_INIT_ERROR_READ_MAPS;
+		return NULL;
+	}
+
+	/* Get rid of newline character. */
+	buf_size = strlen (line);
+	if (buf_size <= 0) {
+		/* Huh? An empty string? */
+		fclose (f);
+		g_free (line);
+		if (error)
+			*error = GBR_INIT_ERROR_INVALID_MAPS;
+		return NULL;
+	}
+	if (line[buf_size - 1] == 10)
+		line[buf_size - 1] = 0;
+
+	/* Extract the filename; it is always an absolute path. */
+	path = strchr (line, '/');
+
+	/* Sanity check. */
+	if (strstr (line, " r-xp ") == NULL || path == NULL) {
+		fclose (f);
+		g_free (line);
+		if (error)
+			*error = GBR_INIT_ERROR_INVALID_MAPS;
+		return NULL;
+	}
+
+	path = g_strdup (path);
+	g_free (line);
+	fclose (f);
+	return path;
+#endif /* ENABLE_BINRELOC */
+}
+
+
+/** @internal
+ * Find the canonical filename of the executable which owns symbol.
+ * Returns a filename which must be freed, or NULL on error.
+ */
+static char *
+_br_find_exe_for_symbol (const void *symbol, GbrInitError *error)
+{
+#ifndef ENABLE_BINRELOC
+	if (error)
+		*error = GBR_INIT_ERROR_DISABLED;
+	return (char *) NULL;
+#else
+	#define SIZE PATH_MAX + 100
+	FILE *f;
+	size_t address_string_len;
+	char *address_string, line[SIZE], *found;
+
+	if (symbol == NULL)
+		return (char *) NULL;
+
+	f = fopen ("/proc/self/maps", "r");
+	if (f == NULL)
+		return (char *) NULL;
+
+	address_string_len = 4;
+	address_string = (char *) g_try_malloc (address_string_len);
+	found = (char *) NULL;
+
+	while (!feof (f)) {
+		char *start_addr, *end_addr, *end_addr_end, *file;
+		void *start_addr_p, *end_addr_p;
+		size_t len;
+
+		if (fgets (line, SIZE, f) == NULL)
+			break;
+
+		/* Sanity check. */
+		if (strstr (line, " r-xp ") == NULL || strchr (line, '/') == NULL)
+			continue;
+
+		/* Parse line. */
+		start_addr = line;
+		end_addr = strchr (line, '-');
+		file = strchr (line, '/');
+
+		/* More sanity check. */
+		if (!(file > end_addr && end_addr != NULL && end_addr[0] == '-'))
+			continue;
+
+		end_addr[0] = '\0';
+		end_addr++;
+		end_addr_end = strchr (end_addr, ' ');
+		if (end_addr_end == NULL)
+			continue;
+
+		end_addr_end[0] = '\0';
+		len = strlen (file);
+		if (len == 0)
+			continue;
+		if (file[len - 1] == '\n')
+			file[len - 1] = '\0';
+
+		/* Get rid of "(deleted)" from the filename. */
+		len = strlen (file);
+		if (len > 10 && strcmp (file + len - 10, " (deleted)") == 0)
+			file[len - 10] = '\0';
+
+		/* I don't know whether this can happen but better safe than sorry. */
+		len = strlen (start_addr);
+		if (len != strlen (end_addr))
+			continue;
+
+
+		/* Transform the addresses into a string in the form of 0xdeadbeef,
+		 * then transform that into a pointer. */
+		if (address_string_len < len + 3) {
+			address_string_len = len + 3;
+			address_string = (char *) g_try_realloc (address_string, address_string_len);
+		}
+
+		memcpy (address_string, "0x", 2);
+		memcpy (address_string + 2, start_addr, len);
+		address_string[2 + len] = '\0';
+		sscanf (address_string, "%p", &start_addr_p);
+
+		memcpy (address_string, "0x", 2);
+		memcpy (address_string + 2, end_addr, len);
+		address_string[2 + len] = '\0';
+		sscanf (address_string, "%p", &end_addr_p);
+
+
+		if (symbol >= start_addr_p && symbol < end_addr_p) {
+			found = file;
+			break;
+		}
+	}
+
+	g_free (address_string);
+	fclose (f);
+
+	if (found == NULL)
+		return (char *) NULL;
+	else
+		return g_strdup (found);
+#endif /* ENABLE_BINRELOC */
+}
+
+
+static gchar *exe = NULL;
+
+static void set_gerror (GError **error, GbrInitError errcode);
+
+
+/** Initialize the BinReloc library (for applications).
+ *
+ * This function must be called before using any other BinReloc functions.
+ * It attempts to locate the application's canonical filename.
+ *
+ * @note If you want to use BinReloc for a library, then you should call
+ *       gbr_init_lib() instead.
+ *
+ * @param error  If BinReloc failed to initialize, then the error report will
+ *               be stored in this variable. Set to NULL if you don't want an
+ *               error report. See the #GbrInitError for a list of error
+ *               codes.
+ *
+ * @returns TRUE on success, FALSE if BinReloc failed to initialize.
+ */
+gboolean
+gbr_init (GError **error)
+{
+	GbrInitError errcode;
+
+	/* Locate the application's filename. */
+	exe = _br_find_exe (&errcode);
+	if (exe != NULL)
+		/* Success! */
+		return TRUE;
+	else {
+		/* Failed :-( */
+		set_gerror (error, errcode);
+		return FALSE;
+	}
+}
+
+
+/** Initialize the BinReloc library (for libraries).
+ *
+ * This function must be called before using any other BinReloc functions.
+ * It attempts to locate the calling library's canonical filename.
+ *
+ * @note The BinReloc source code MUST be included in your library, or this
+ *       function won't work correctly.
+ *
+ * @returns TRUE on success, FALSE if a filename cannot be found.
+ */
+gboolean
+gbr_init_lib (GError **error)
+{
+	GbrInitError errcode;
+
+	exe = _br_find_exe_for_symbol ((const void *) "", &errcode);
+	if (exe != NULL)
+		/* Success! */
+		return TRUE;
+	else {
+		/* Failed :-( */
+		set_gerror (error, errcode);
+		return exe != NULL;
+	}
+}
+
+
+static void
+set_gerror (GError **error, GbrInitError errcode)
+{
+	gchar *error_message;
+
+	if (error == NULL)
+		return;
+
+	switch (errcode) {
+	case GBR_INIT_ERROR_NOMEM:
+		error_message = "Cannot allocate memory.";
+		break;
+	case GBR_INIT_ERROR_OPEN_MAPS:
+		error_message = "Unable to open /proc/self/maps for reading.";
+		break;
+	case GBR_INIT_ERROR_READ_MAPS:
+		error_message = "Unable to read from /proc/self/maps.";
+		break;
+	case GBR_INIT_ERROR_INVALID_MAPS:
+		error_message = "The file format of /proc/self/maps is invalid.";
+		break;
+	case GBR_INIT_ERROR_DISABLED:
+		error_message = "Binary relocation support is disabled.";
+		break;
+	default:
+		error_message = "Unknown error.";
+		break;
+	};
+	g_set_error (error, g_quark_from_static_string ("GBinReloc"),
+		     errcode, "%s", error_message);
+}
+
+
+/** Find the canonical filename of the current application.
+ *
+ * @param default_exe  A default filename which will be used as fallback.
+ * @returns A string containing the application's canonical filename,
+ *          which must be freed when no longer necessary. If BinReloc is
+ *          not initialized, or if the initialization function failed,
+ *          then a copy of default_exe will be returned. If default_exe
+ *          is NULL, then NULL will be returned.
+ */
+gchar *
+gbr_find_exe (const gchar *default_exe)
+{
+	if (exe == NULL) {
+		/* BinReloc is not initialized. */
+		if (default_exe != NULL)
+			return g_strdup (default_exe);
+		else
+			return NULL;
+	}
+	return g_strdup (exe);
+}
+
+
+/** Locate the directory in which the current application is installed.
+ *
+ * The prefix is generated by the following pseudo-code evaluation:
+ * \code
+ * dirname(exename)
+ * \endcode
+ *
+ * @param default_dir  A default directory which will used as fallback.
+ * @return A string containing the directory, which must be freed when no
+ *         longer necessary. If BinReloc is not initialized, or if the
+ *         initialization function failed, then a copy of default_dir
+ *         will be returned. If default_dir is NULL, then NULL will be
+ *         returned.
+ */
+gchar *
+gbr_find_exe_dir (const gchar *default_dir)
+{
+	if (exe == NULL) {
+		/* BinReloc not initialized. */
+		if (default_dir != NULL)
+			return g_strdup (default_dir);
+		else
+			return NULL;
+	}
+
+	return g_path_get_dirname (exe);
+}
+
+
+/** Locate the prefix in which the current application is installed.
+ *
+ * The prefix is generated by the following pseudo-code evaluation:
+ * \code
+ * dirname(dirname(exename))
+ * \endcode
+ *
+ * @param default_prefix  A default prefix which will used as fallback.
+ * @return A string containing the prefix, which must be freed when no
+ *         longer necessary. If BinReloc is not initialized, or if the
+ *         initialization function failed, then a copy of default_prefix
+ *         will be returned. If default_prefix is NULL, then NULL will be
+ *         returned.
+ */
+gchar *
+gbr_find_prefix (const gchar *default_prefix)
+{
+	gchar *dir1, *dir2;
+
+	if (exe == NULL) {
+		/* BinReloc not initialized. */
+		if (default_prefix != NULL)
+			return g_strdup (default_prefix);
+		else
+			return NULL;
+	}
+
+	dir1 = g_path_get_dirname (exe);
+	dir2 = g_path_get_dirname (dir1);
+	g_free (dir1);
+	return dir2;
+}
+
+
+/** Locate the application's binary folder.
+ *
+ * The path is generated by the following pseudo-code evaluation:
+ * \code
+ * prefix + "/bin"
+ * \endcode
+ *
+ * @param default_bin_dir  A default path which will used as fallback.
+ * @return A string containing the bin folder's path, which must be freed when
+ *         no longer necessary. If BinReloc is not initialized, or if the
+ *         initialization function failed, then a copy of default_bin_dir will
+ *         be returned. If default_bin_dir is NULL, then NULL will be returned.
+ */
+gchar *
+gbr_find_bin_dir (const gchar *default_bin_dir)
+{
+	gchar *prefix, *dir;
+
+	prefix = gbr_find_prefix (NULL);
+	if (prefix == NULL) {
+		/* BinReloc not initialized. */
+		if (default_bin_dir != NULL)
+			return g_strdup (default_bin_dir);
+		else
+			return NULL;
+	}
+
+	dir = g_build_filename (prefix, "bin", NULL);
+	g_free (prefix);
+	return dir;
+}
+
+
+/** Locate the application's superuser binary folder.
+ *
+ * The path is generated by the following pseudo-code evaluation:
+ * \code
+ * prefix + "/sbin"
+ * \endcode
+ *
+ * @param default_sbin_dir  A default path which will used as fallback.
+ * @return A string containing the sbin folder's path, which must be freed when
+ *         no longer necessary. If BinReloc is not initialized, or if the
+ *         initialization function failed, then a copy of default_sbin_dir will
+ *         be returned. If default_bin_dir is NULL, then NULL will be returned.
+ */
+gchar *
+gbr_find_sbin_dir (const gchar *default_sbin_dir)
+{
+	gchar *prefix, *dir;
+
+	prefix = gbr_find_prefix (NULL);
+	if (prefix == NULL) {
+		/* BinReloc not initialized. */
+		if (default_sbin_dir != NULL)
+			return g_strdup (default_sbin_dir);
+		else
+			return NULL;
+	}
+
+	dir = g_build_filename (prefix, "sbin", NULL);
+	g_free (prefix);
+	return dir;
+}
+
+
+/** Locate the application's data folder.
+ *
+ * The path is generated by the following pseudo-code evaluation:
+ * \code
+ * prefix + "/share"
+ * \endcode
+ *
+ * @param default_data_dir  A default path which will used as fallback.
+ * @return A string containing the data folder's path, which must be freed when
+ *         no longer necessary. If BinReloc is not initialized, or if the
+ *         initialization function failed, then a copy of default_data_dir
+ *         will be returned. If default_data_dir is NULL, then NULL will be
+ *         returned.
+ */
+gchar *
+gbr_find_data_dir (const gchar *default_data_dir)
+{
+	gchar *prefix, *dir;
+
+	prefix = gbr_find_prefix (NULL);
+	if (prefix == NULL) {
+		/* BinReloc not initialized. */
+		if (default_data_dir != NULL)
+			return g_strdup (default_data_dir);
+		else
+			return NULL;
+	}
+
+	dir = g_build_filename (prefix, "share", NULL);
+	g_free (prefix);
+	return dir;
+}
+
+
+/** Locate the application's localization folder.
+ *
+ * The path is generated by the following pseudo-code evaluation:
+ * \code
+ * prefix + "/share/locale"
+ * \endcode
+ *
+ * @param default_locale_dir  A default path which will used as fallback.
+ * @return A string containing the localization folder's path, which must be freed when
+ *         no longer necessary. If BinReloc is not initialized, or if the
+ *         initialization function failed, then a copy of default_locale_dir will be returned.
+ *         If default_locale_dir is NULL, then NULL will be returned.
+ */
+gchar *
+gbr_find_locale_dir (const gchar *default_locale_dir)
+{
+	gchar *data_dir, *dir;
+
+	data_dir = gbr_find_data_dir (NULL);
+	if (data_dir == NULL) {
+		/* BinReloc not initialized. */
+		if (default_locale_dir != NULL)
+			return g_strdup (default_locale_dir);
+		else
+			return NULL;
+	}
+
+	dir = g_build_filename (data_dir, "locale", NULL);
+	g_free (data_dir);
+	return dir;
+}
+
+
+/** Locate the application's library folder.
+ *
+ * The path is generated by the following pseudo-code evaluation:
+ * \code
+ * prefix + "/lib"
+ * \endcode
+ *
+ * @param default_lib_dir  A default path which will used as fallback.
+ * @return A string containing the library folder's path, which must be freed when
+ *         no longer necessary. If BinReloc is not initialized, or if the
+ *         initialization function failed, then a copy of default_lib_dir will be returned.
+ *         If default_lib_dir is NULL, then NULL will be returned.
+ */
+gchar *
+gbr_find_lib_dir (const gchar *default_lib_dir)
+{
+	gchar *prefix, *dir;
+
+	prefix = gbr_find_prefix (NULL);
+	if (prefix == NULL) {
+		/* BinReloc not initialized. */
+		if (default_lib_dir != NULL)
+			return g_strdup (default_lib_dir);
+		else
+			return NULL;
+	}
+
+	dir = g_build_filename (prefix, "lib", NULL);
+	g_free (prefix);
+	return dir;
+}
+
+
+/** Locate the application's libexec folder.
+ *
+ * The path is generated by the following pseudo-code evaluation:
+ * \code
+ * prefix + "/libexec"
+ * \endcode
+ *
+ * @param default_libexec_dir  A default path which will used as fallback.
+ * @return A string containing the libexec folder's path, which must be freed when
+ *         no longer necessary. If BinReloc is not initialized, or if the initialization
+ *         function failed, then a copy of default_libexec_dir will be returned.
+ *         If default_libexec_dir is NULL, then NULL will be returned.
+ */
+gchar *
+gbr_find_libexec_dir (const gchar *default_libexec_dir)
+{
+	gchar *prefix, *dir;
+
+	prefix = gbr_find_prefix (NULL);
+	if (prefix == NULL) {
+		/* BinReloc not initialized. */
+		if (default_libexec_dir != NULL)
+			return g_strdup (default_libexec_dir);
+		else
+			return NULL;
+	}
+
+	dir = g_build_filename (prefix, "libexec", NULL);
+	g_free (prefix);
+	return dir;
+}
+
+
+/** Locate the application's configuration files folder.
+ *
+ * The path is generated by the following pseudo-code evaluation:
+ * \code
+ * prefix + "/etc"
+ * \endcode
+ *
+ * @param default_etc_dir  A default path which will used as fallback.
+ * @return A string containing the etc folder's path, which must be freed when
+ *         no longer necessary. If BinReloc is not initialized, or if the initialization
+ *         function failed, then a copy of default_etc_dir will be returned.
+ *         If default_etc_dir is NULL, then NULL will be returned.
+ */
+gchar *
+gbr_find_etc_dir (const gchar *default_etc_dir)
+{
+	gchar *prefix, *dir;
+
+	prefix = gbr_find_prefix (NULL);
+	if (prefix == NULL) {
+		/* BinReloc not initialized. */
+		if (default_etc_dir != NULL)
+			return g_strdup (default_etc_dir);
+		else
+			return NULL;
+	}
+
+	dir = g_build_filename (prefix, "etc", NULL);
+	g_free (prefix);
+	return dir;
+}
+
+
+G_END_DECLS
+
+#endif /* __BINRELOC_C__ */

Added: gnucash/trunk/src/engine/binreloc.h
===================================================================
--- gnucash/trunk/src/engine/binreloc.h	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/src/engine/binreloc.h	2006-09-18 20:43:50 UTC (rev 14862)
@@ -0,0 +1,68 @@
+/*
+ * BinReloc - a library for creating relocatable executables
+ * Written by: Hongli Lai <h.lai at chello.nl>
+ * http://autopackage.org/
+ *
+ * This source code is public domain. You can relicense this code
+ * under whatever license you want.
+ *
+ * See http://autopackage.org/docs/binreloc/ for
+ * more information and how to use this.
+ */
+
+#ifndef __BINRELOC_H__
+#define __BINRELOC_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+
+/** These error codes can be returned by br_init(), br_init_lib(), gbr_init() or gbr_init_lib(). */
+typedef enum {
+	/** Cannot allocate memory. */
+	GBR_INIT_ERROR_NOMEM,
+	/** Unable to open /proc/self/maps; see errno for details. */
+	GBR_INIT_ERROR_OPEN_MAPS,
+	/** Unable to read from /proc/self/maps; see errno for details. */
+	GBR_INIT_ERROR_READ_MAPS,
+	/** The file format of /proc/self/maps is invalid; kernel bug? */
+	GBR_INIT_ERROR_INVALID_MAPS,
+	/** BinReloc is disabled (the ENABLE_BINRELOC macro is not defined). */
+	GBR_INIT_ERROR_DISABLED
+} GbrInitError;
+
+
+#ifndef BINRELOC_RUNNING_DOXYGEN
+/* Mangle symbol names to avoid symbol collisions with other ELF objects. */
+	#define gbr_find_exe         ffEt66859784967989_gbr_find_exe
+	#define gbr_find_exe_dir     ffEt66859784967989_gbr_find_exe_dir
+	#define gbr_find_prefix      ffEt66859784967989_gbr_find_prefix
+	#define gbr_find_bin_dir     ffEt66859784967989_gbr_find_bin_dir
+	#define gbr_find_sbin_dir    ffEt66859784967989_gbr_find_sbin_dir
+	#define gbr_find_data_dir    ffEt66859784967989_gbr_find_data_dir
+	#define gbr_find_locale_dir  ffEt66859784967989_gbr_find_locale_dir
+	#define gbr_find_lib_dir     ffEt66859784967989_gbr_find_lib_dir
+	#define gbr_find_libexec_dir ffEt66859784967989_gbr_find_libexec_dir
+	#define gbr_find_etc_dir     ffEt66859784967989_gbr_find_etc_dir
+
+
+#endif
+gboolean gbr_init             (GError **error);
+gboolean gbr_init_lib         (GError **error);
+
+gchar   *gbr_find_exe         (const gchar *default_exe);
+gchar   *gbr_find_exe_dir     (const gchar *default_dir);
+gchar   *gbr_find_prefix      (const gchar *default_prefix);
+gchar   *gbr_find_bin_dir     (const gchar *default_bin_dir);
+gchar   *gbr_find_sbin_dir    (const gchar *default_sbin_dir);
+gchar   *gbr_find_data_dir    (const gchar *default_data_dir);
+gchar   *gbr_find_locale_dir  (const gchar *default_locale_dir);
+gchar   *gbr_find_lib_dir     (const gchar *default_lib_dir);
+gchar   *gbr_find_libexec_dir (const gchar *default_libexec_dir);
+gchar   *gbr_find_etc_dir     (const gchar *default_etc_dir);
+
+
+G_END_DECLS
+
+#endif /* __BINRELOC_H__ */

Modified: gnucash/trunk/src/engine/gnc-engine.c
===================================================================
--- gnucash/trunk/src/engine/gnc-engine.c	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/src/engine/gnc-engine.c	2006-09-18 20:43:50 UTC (rev 14862)
@@ -37,6 +37,7 @@
 #include "SchedXactionP.h"
 #include "FreqSpecP.h"
 #include "gnc-pricedb-p.h"
+#include "binreloc.h"
 
 /** gnc file backend library name */
 #define GNC_LIB_NAME "gnc-backend-file"
@@ -87,9 +88,21 @@
   GList * cur;
   gchar *tracefilename;
   gchar *libdir;
+#ifdef G_OS_WIN32
+  GError *binreloc_error = NULL;
+#endif
 
   if (1 == engine_is_initialized) return;
 
+#ifdef G_OS_WIN32
+  /* Init binreloc from a library. On Unix we don't need this - the
+     init in main() is enough. It's unclear whether we need this on
+     Windows here... */
+  if (!gbr_init_lib (&binreloc_error)) {
+    printf("gnc_engine_init: Error on gbr_init_lib: %s\n", binreloc_error->message);
+  }
+#endif
+
   /* initialize logging to our file. */
   tracefilename = g_build_filename(g_get_tmp_dir(), "gnucash.trace",
 				   (gchar *)NULL);

Modified: gnucash/trunk/src/engine/gnc-path.c
===================================================================
--- gnucash/trunk/src/engine/gnc-path.c	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/src/engine/gnc-path.c	2006-09-18 20:43:50 UTC (rev 14862)
@@ -22,10 +22,13 @@
 #include "config.h"
 #include "gnc-path.h"
 #include "gncla-dir.h"
+#include <stdio.h>
+#include "binreloc.h"
 
 gchar *gnc_path_get_prefix()
 {
-  return g_strdup (PREFIX);
+  //printf("Returning prefix %s\n", gbr_find_prefix (PREFIX));
+  return gbr_find_prefix (PREFIX);
 }
 
 /** Returns the libdir path, usually
@@ -34,7 +37,8 @@
  * @returns A newly allocated string. */
 gchar *gnc_path_get_libdir()
 {
-  return g_strdup (LIBDIR);
+  //printf("Returning libdir %s\n", gbr_find_lib_dir (LIBDIR));
+  return gbr_find_lib_dir (LIBDIR);
 }
 
 /** Returns the datadir path, usually
@@ -43,7 +47,11 @@
  * @returns A newly allocated string. */
 gchar *gnc_path_get_pkgdatadir()
 {
-  return g_strdup (PKGDATADIR);
+  gchar *datadir = gbr_find_data_dir (DATADIR);
+  gchar *result = g_build_filename (datadir, "gnucash", (char*)NULL);
+  g_free (datadir);
+  //printf("Returning pkgdatadir %s\n", result);
+  return result;
 }
 
 /** Returns the sysconfdir path, usually
@@ -52,7 +60,11 @@
  * @returns A newly allocated string. */
 gchar *gnc_path_get_pkgsysconfdir()
 {
-  return g_strdup (PKGSYSCONFDIR);
+  gchar *sysconfdir = gbr_find_etc_dir (SYSCONFDIR);
+  gchar *result = g_build_filename (sysconfdir, "gnucash", (char*)NULL);
+  g_free (sysconfdir);
+  //printf("Returning pkgsysconfdir %s\n", result);
+  return result;
 }
 
 
@@ -62,7 +74,11 @@
  * @returns A newly allocated string. */
 gchar *gnc_path_get_pkglibdir()
 {
-  return g_strdup (PKGLIBDIR);
+  gchar *libdir = gnc_path_get_libdir ();
+  gchar *result = g_build_filename (libdir, "gnucash", (char*)NULL);
+  g_free (libdir);
+  //printf("Returning pkglibdir %s\n", result);
+  return result;
 }
 
 /** Returns the glade file path, usually
@@ -71,13 +87,10 @@
  * @returns A newly allocated string. */
 gchar *gnc_path_get_gladedir()
 {
-  gchar *result;
-#ifdef G_OS_WIN32
-  result = g_win32_get_package_installation_subdirectory
-    (GETTEXT_PACKAGE, NULL, "share\\gnucash\\glade");
-#else
-  result = g_strdup (GNC_GLADE_DIR);
-#endif
+  gchar *pkgdatadir = gnc_path_get_pkgdatadir ();
+  gchar *result = g_build_filename (pkgdatadir, "glade", (char*)NULL);
+  g_free (pkgdatadir);
+  //printf("Returning gladedir %s\n", result);
   return result;
 }
 
@@ -87,7 +100,8 @@
  * @returns A newly allocated string. */
 gchar *gnc_path_get_localedir()
 {
-  return g_strdup (LOCALE_DIR);
+  //printf("Returning localedir %s\n", gbr_find_locale_dir (LOCALE_DIR));
+  return gbr_find_locale_dir (LOCALE_DIR);
 }
 
 /** Returns the glade file path, usually
@@ -96,14 +110,10 @@
  * @returns A newly allocated string. */
 gchar *gnc_path_get_accountsdir()
 {
-  gchar *result;
-#ifdef G_OS_WIN32
-  result = 
-    g_win32_get_package_installation_subdirectory
-    (GETTEXT_PACKAGE, NULL, "share\\gnucash\\accounts");
-#else
-  result = g_strdup (GNC_ACCOUNTS_DIR);
-#endif
+  gchar *pkgdatadir = gnc_path_get_pkgdatadir ();
+  gchar *result = g_build_filename (pkgdatadir, "accounts", (char*)NULL);
+  g_free (pkgdatadir);
+  //printf("Returning accountsdir %s\n", result);
   return result;
 }
 
@@ -113,6 +123,11 @@
  * @returns A newly allocated string. */
 gchar *gnc_path_get_gconfdir()
 {
-  return g_strdup (GNC_GCONF_DIR);
+  gchar *sysconfdir = gbr_find_etc_dir (SYSCONFDIR);
+  gchar *result = g_build_filename (sysconfdir, "gconf", 
+				    "gconf.xml.defaults", (char*)NULL);
+  g_free (sysconfdir);
+  //printf("Returning gconfdir %s\n", result);
+  return result;
 }
 

Modified: gnucash/trunk/src/engine/gncla-dir.h.in
===================================================================
--- gnucash/trunk/src/engine/gncla-dir.h.in	2006-09-18 19:57:32 UTC (rev 14861)
+++ gnucash/trunk/src/engine/gncla-dir.h.in	2006-09-18 20:43:50 UTC (rev 14862)
@@ -24,8 +24,8 @@
  */
 
 #define PREFIX "@-prefix-@"
-#define PKGDATADIR "@-pkgdatadir-@"
-#define PKGSYSCONFDIR "@-pkgsysconfdir-@"
+#define DATADIR "@-datadir-@"
+#define SYSCONFDIR "@-sysconfdir-@"
 
 #define LIBDIR "@-libdir-@"
 #define PKGLIBDIR "@-pkglibdir-@"



More information about the gnucash-changes mailing list