r14543 - gnucash/trunk/src/engine - Use glib filepath manipulation functions instead of our own manual methods. Necessary for non-Unix machines. Please keep an eye open for potential filename lookup problems.

Christian Stimming cstim at cvs.gnucash.org
Wed Jul 19 10:13:38 EDT 2006


Author: cstim
Date: 2006-07-19 10:13:37 -0400 (Wed, 19 Jul 2006)
New Revision: 14543
Trac: http://svn.gnucash.org/trac/changeset/14543

Modified:
   gnucash/trunk/src/engine/gnc-engine.c
   gnucash/trunk/src/engine/gnc-filepath-utils.c
   gnucash/trunk/src/engine/test/test-resolve-file-path.c
Log:
Use glib filepath manipulation functions instead of our own manual methods. Necessary for non-Unix machines. Please keep an eye open for potential filename lookup problems.

Modified: gnucash/trunk/src/engine/gnc-engine.c
===================================================================
--- gnucash/trunk/src/engine/gnc-engine.c	2006-07-18 15:41:55 UTC (rev 14542)
+++ gnucash/trunk/src/engine/gnc-engine.c	2006-07-19 14:13:37 UTC (rev 14543)
@@ -86,11 +86,15 @@
     { NULL, NULL, FALSE } }, *lib;
   gnc_engine_init_hook_t hook;
   GList * cur;
+  gchar *tracefilename;
 
   if (1 == engine_is_initialized) return;
 
   /* initialize logging to our file. */
-  qof_log_init_filename("/tmp/gnucash.trace");
+  tracefilename = g_build_filename(g_get_tmp_dir(), "gnucash.trace",
+				   (gchar *)NULL);
+  qof_log_init_filename(tracefilename);
+  g_free(tracefilename);
   /* Only set the core log_modules here the rest can be set locally.  */
   qof_log_set_level(GNC_MOD_ENGINE, QOF_LOG_WARNING);
   qof_log_set_level(GNC_MOD_IO, QOF_LOG_WARNING);

Modified: gnucash/trunk/src/engine/gnc-filepath-utils.c
===================================================================
--- gnucash/trunk/src/engine/gnc-filepath-utils.c	2006-07-18 15:41:55 UTC (rev 14542)
+++ gnucash/trunk/src/engine/gnc-filepath-utils.c	2006-07-19 14:13:37 UTC (rev 14543)
@@ -65,15 +65,15 @@
 {
   int rc;
   struct stat statbuf;
-  char *home;
+  const gchar *home;
   char *path;
   char *data;
 
   /* Punt. Can't figure out where home is. */
-  home = getenv ("HOME");
+  home = g_get_home_dir();
   if (!home) return;
 
-  path = g_strconcat(home, "/.gnucash", NULL);
+  path = g_build_filename(home, ".gnucash", (gchar *)NULL);
 
   rc = stat (path, &statbuf);
   if (rc)
@@ -85,7 +85,7 @@
     g_mkdir (path, S_IRWXU);   /* perms = S_IRWXU = 0700 */
   }
 
-  data = g_strconcat (path, "/data", NULL);
+  data = g_build_filename (path, "data", (gchar *)NULL);
   rc = stat (data, &statbuf);
   if (rc)
     g_mkdir (data, S_IRWXU);
@@ -97,28 +97,24 @@
 /* ====================================================================== */
 
 /* XXX hack alert -- we should be yanking this out of some config file */
+/* These are obviously meant to be hard-coded paths to the gnucash
+   data file. That is insane. These should be thrown out
+   altogether. On non-Unix systems (Windows) these paths would not
+   only have different directory separator characters but these
+   would certainly be completely different paths. I'd vote to
+   throw this out completely. -- cstim, 2006-07-19 */
 static char * searchpaths[] =
 {
-   "/usr/share/gnucash/data/",
-   "/usr/local/share/gnucash/data/",
-   "/usr/share/gnucash/accounts/",
-   "/usr/local/share/gnucash/accounts/",
+   "/usr/share/gnucash/data",
+   "/usr/local/share/gnucash/data",
+   "/usr/share/gnucash/accounts",
+   "/usr/local/share/gnucash/accounts",
    NULL,
 };
 
 typedef gboolean (*pathGenerator)(char *pathbuf, int which);
 
 static gboolean
-xaccAddEndPath(char *pathbuf, const char *ending, int len)
-{
-    if(len + strlen(pathbuf) >= PATH_MAX)
-        return FALSE;
-          
-    strcat (pathbuf, ending);
-    return TRUE;
-}
-
-static gboolean
 xaccCwdPathGenerator(char *pathbuf, int which)
 {
     if(which != 0)
@@ -131,7 +127,6 @@
         if (getcwd (pathbuf, PATH_MAX) == NULL)
             return FALSE;
 
-        strcat (pathbuf, "/");
         return TRUE;
     }
 }
@@ -139,23 +134,28 @@
 static gboolean
 xaccDataPathGenerator(char *pathbuf, int which)
 {
-    char *path;
-    
     if(which != 0)
     {
         return FALSE;
     }
     else
     {
-        path = getenv ("HOME");
-        if (!path)
+        const gchar *home;
+	gchar *tmppath;
+    
+        home = g_get_home_dir ();
+        if (!home)
             return FALSE;
 
-        if (PATH_MAX <= (strlen (path) + 20))
-            return FALSE;
+	tmppath = g_build_filename (home, ".gnucash", "data", (gchar *)NULL);
+        if (strlen(tmppath) >= PATH_MAX)
+	{
+	    g_free (tmppath);
+	    return FALSE;
+	}
 
-        strcpy (pathbuf, path);
-        strcat (pathbuf, "/.gnucash/data/");
+        g_strlcpy (pathbuf, tmppath, PATH_MAX);
+	g_free (tmppath);
         return TRUE;
     }
 }
@@ -176,7 +176,7 @@
         if (PATH_MAX <= strlen(path))
             return FALSE;
 
-        strcpy (pathbuf, path);
+        g_strlcpy (pathbuf, path, PATH_MAX);
         return TRUE;
     }
 }
@@ -206,14 +206,12 @@
   /* OK, now we try to find or build an absolute file path */
 
   /* check for an absolute file path */
-  if (*filefrag == '/')
+  if (g_path_is_absolute(filefrag))
     return g_strdup (filefrag);
 
   if (!g_ascii_strncasecmp(filefrag, "file:", 5))
   {
-      char *ret = g_new(char, strlen(filefrag) - 5 + 1);
-      strcpy(ret, filefrag + 5);
-      return ret;
+      return g_strdup(filefrag + 5);
   }
 
   /* get conservative on the length so that sprintf(getpid()) works ... */
@@ -230,14 +228,14 @@
       int j;
       for(j = 0; gens[i](pathbuf, j) ; j++)
       {
-          if(xaccAddEndPath(pathbuf, filefrag, namelen))
-          {
-              int rc = stat (pathbuf, &statbuf);
-              if ((!rc) && (S_ISREG(statbuf.st_mode)))
-              {
-                  return (g_strdup (pathbuf));
-              }
+	  gchar *fullpath = g_build_filename(pathbuf, filefrag, (gchar *)NULL);
+
+	  int rc = stat (fullpath, &statbuf);
+	  if ((!rc) && (S_ISREG(statbuf.st_mode)))
+	  {
+	      return fullpath;
           }
+	  g_free (fullpath);
       }
   }
   /* OK, we didn't find the file. */
@@ -262,22 +260,20 @@
   /* Lets try creating a new file in $HOME/.gnucash/data */
   if (xaccDataPathGenerator(pathbuf, 0))
   {
-      if(xaccAddEndPath(pathbuf, filefrag_dup, namelen))
-      {
-          g_free (filefrag_dup);
-          return (g_strdup (pathbuf));
-      }
+      gchar *result;
+      result = g_build_filename(pathbuf, filefrag_dup, (gchar *)NULL);
+      g_free (filefrag_dup);
+      return result;
   } 
 
   /* OK, we still didn't find the file */
   /* Lets try creating a new file in the cwd */
   if (xaccCwdPathGenerator(pathbuf, 0))
   {
-      if(xaccAddEndPath(pathbuf, filefrag_dup, namelen))
-      {
-          g_free (filefrag_dup);
-          return (g_strdup (pathbuf));
-      }
+      gchar *result;
+      result = g_build_filename(pathbuf, filefrag_dup, (gchar *)NULL);
+      g_free (filefrag_dup);
+      return result;
   }
 
   g_free (filefrag_dup);

Modified: gnucash/trunk/src/engine/test/test-resolve-file-path.c
===================================================================
--- gnucash/trunk/src/engine/test/test-resolve-file-path.c	2006-07-18 15:41:55 UTC (rev 14542)
+++ gnucash/trunk/src/engine/test/test-resolve-file-path.c	2006-07-19 14:13:37 UTC (rev 14543)
@@ -39,10 +39,14 @@
 typedef struct test_strings_struct test_strings;
 
 test_strings strs[] = {
-    { "/.gnucash/test-account-name", "/.gnucash/test-account-name", 1 },
-    { "/tmp/test-account-name2", "/tmp/test-account-name2", 0 },
-    { "postgres://localhost/foo/bar", "/.gnucash/data/postgres:,,localhost,foo,bar", 2 },
-    { "file:/tmp/test-account-name3", "/tmp/test-account-name3", 0 },
+    { G_DIR_SEPARATOR_S ".gnucash" G_DIR_SEPARATOR_S "test-account-name",
+      G_DIR_SEPARATOR_S ".gnucash" G_DIR_SEPARATOR_S "test-account-name", 1 },
+    { G_DIR_SEPARATOR_S "tmp" G_DIR_SEPARATOR_S "test-account-name2",
+      G_DIR_SEPARATOR_S "tmp" G_DIR_SEPARATOR_S "test-account-name2", 0 },
+    { "postgres://localhost/foo/bar",
+      G_DIR_SEPARATOR_S ".gnucash" G_DIR_SEPARATOR_S "data" G_DIR_SEPARATOR_S "postgres:,,localhost,foo,bar", 2 },
+    { "file:" G_DIR_SEPARATOR_S "tmp" G_DIR_SEPARATOR_S "test-account-name3",
+      G_DIR_SEPARATOR_S "tmp" G_DIR_SEPARATOR_S "test-account-name3", 0 },
     { NULL, NULL, 0 },
 };
 
@@ -59,15 +63,16 @@
         
         if(strs[i].prefix_home == 1) 
         {
-            dain = g_strdup_printf("%s/%s", g_get_home_dir(), strs[i].input);
-            wantout = g_strdup_printf("%s/%s", g_get_home_dir(),
-                                      strs[i].output);
+            dain = g_build_filename(g_get_home_dir(), strs[i].input,
+				    (gchar *)NULL);
+            wantout = g_build_filename(g_get_home_dir(), strs[i].output,
+				       (gchar *)NULL);
         }
         else if(strs[i].prefix_home == 2)
         {
             dain = g_strdup(strs[i].input);
-            wantout = g_strdup_printf("%s%s", g_get_home_dir(),
-                                      strs[i].output);
+            wantout = g_build_filename(g_get_home_dir(), strs[i].output,
+				       (gchar *)NULL);
         }
          else
         {



More information about the gnucash-changes mailing list