[glib] Add g_reload_user_special_dirs_cache (#541276)



commit 91bdccff7544ee19d329ec73db02fd5f11c617fd
Author: Alexander Larsson <alexl redhat com>
Date:   Mon Jun 15 13:18:22 2009 +0200

    Add g_reload_user_special_dirs_cache (#541276)
    
    This is useful for nautilus that changes the xdg-user-dirs files.

 glib/glib.symbols |    1 +
 glib/gutils.c     |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 glib/gutils.h     |    1 +
 3 files changed, 55 insertions(+), 0 deletions(-)
---
diff --git a/glib/glib.symbols b/glib/glib.symbols
index 7b4a886..a1d9c9e 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -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
diff --git a/glib/gutils.c b/glib/gutils.c
index 148d655..8b7bfe8 100644
--- a/glib/gutils.c
+++ b/glib/gutils.c
@@ -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
diff --git a/glib/gutils.h b/glib/gutils.h
index 68a27ed..da3da9a 100644
--- a/glib/gutils.h
+++ b/glib/gutils.h
@@ -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);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]