Add g_reload_user_special_dirs_cache (#541276)
authorAlexander Larsson <alexl@redhat.com>
Mon, 15 Jun 2009 11:18:22 +0000 (13:18 +0200)
committerAlexander Larsson <alexl@redhat.com>
Mon, 15 Jun 2009 11:18:22 +0000 (13:18 +0200)
This is useful for nautilus that changes the xdg-user-dirs files.

glib/glib.symbols
glib/gutils.c
glib/gutils.h

index 7b4a886..a1d9c9e 100644 (file)
@@ -1562,6 +1562,7 @@ g_get_tmp_dir_utf8
 g_get_user_cache_dir
 g_get_user_config_dir
 g_get_user_data_dir
+g_reload_user_special_dirs_cache
 g_get_user_special_dir
 #ifndef _WIN64
 g_get_user_name PRIVATE
index 148d655..8b7bfe8 100644 (file)
@@ -2471,6 +2471,59 @@ load_user_special_dirs (void)
 
 #endif /* G_OS_UNIX && !HAVE_CARBON */
 
+
+/**
+ * g_reload_user_special_dirs_cache:
+ *
+ * Resets the cache used for g_get_user_special_dir(), so
+ * that the latest on-disk version is used. Call this only
+ * if you just changed the data on disk yourself.
+ *
+ * Due to threadsafety issues this may cause leaking of strings
+ * that were previously returned from g_get_user_special_dir()
+ * that can't be freed. We ensure to only leak the data for
+ * the directories that actually changed value though.
+ *
+ * Since: 2.22
+ */
+void
+g_reload_user_special_dirs_cache (void)
+{
+  int i;
+
+  G_LOCK (g_utils_global);
+
+  if (g_user_special_dirs != NULL)
+    {
+      /* save a copy of the pointer, to check if some memory can be preserved */
+      char **old_g_user_special_dirs = g_user_special_dirs;
+      char *old_val;
+
+      /* recreate and reload our cache */
+      g_user_special_dirs = g_new0 (gchar *, G_USER_N_DIRECTORIES);
+      load_user_special_dirs ();
+
+      /* only leak changed directories */
+      for (i = 0; i < G_USER_N_DIRECTORIES; i++)
+        {
+         old_val = old_g_user_special_dirs[i];
+         if (g_strcmp0 (old_val, g_user_special_dirs[i]) == 0)
+            {
+             /* don't leak */
+             g_free (g_user_special_dirs[i]);
+             g_user_special_dirs[i] = old_val;
+            }
+         else
+            g_free (old_val);
+        }
+
+      /* free the old array */
+      g_free (old_g_user_special_dirs);
+    }
+
+  G_UNLOCK (g_utils_global);
+}
+
 /**
  * g_get_user_special_dir:
  * @directory: the logical id of special directory
index 68a27ed..da3da9a 100644 (file)
@@ -127,6 +127,7 @@ void                  g_set_prgname          (const gchar *prgname);
 G_CONST_RETURN gchar* g_get_application_name (void);
 void                  g_set_application_name (const gchar *application_name);
 
+void    g_reload_user_special_dirs_cache     (void);
 G_CONST_RETURN gchar*    g_get_user_data_dir      (void);
 G_CONST_RETURN gchar*    g_get_user_config_dir    (void);
 G_CONST_RETURN gchar*    g_get_user_cache_dir     (void);