[glib/settings-portal: 3/4] settings: Add support for defaults to keyfile backend



commit f9affa31fefcf9a08b63e2cf5300283221236a1a
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Nov 5 16:07:55 2018 -0500

    settings: Add support for defaults to keyfile backend
    
    Stacked databases and locks are dconf features that allow
    management software like Fleet Commander to set system-wide
    defaults and overrides centrally for applications.
    
    This patch adds minimal support for the same to the keyfile
    backend. We look for a keyfile named 'defaults' and a
    lock-list named 'locks'.
    
    Suitable files can be produced from a dconf database with
    dconf dump and dconf list-locks, respectively.
    
    The default location for these files is /etc/glib-2.0/settings/.
    For test purposes, this can be overwritten with the
    GSETTINGS_DEFAULTS_DIR environment variable.
    
    Writes always go to the per-user keyfile.

 gio/gkeyfilesettingsbackend.c | 92 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 91 insertions(+), 1 deletion(-)
---
diff --git a/gio/gkeyfilesettingsbackend.c b/gio/gkeyfilesettingsbackend.c
index b1a4d165e..c71ba0c76 100644
--- a/gio/gkeyfilesettingsbackend.c
+++ b/gio/gkeyfilesettingsbackend.c
@@ -29,6 +29,7 @@
 
 #include "gfile.h"
 #include "gfileinfo.h"
+#include "gfileenumerator.h"
 #include "gfilemonitor.h"
 #include "gsimplepermission.h"
 #include "gsettingsbackendinternal.h"
@@ -58,6 +59,8 @@ typedef struct
   GKeyFile          *keyfile;
   GPermission       *permission;
   gboolean           writable;
+  GKeyFile          *system_keyfile;
+  GHashTable        *system_locks;
 
   gchar             *prefix;
   gint               prefix_len;
@@ -197,10 +200,19 @@ get_from_keyfile (GKeyfileSettingsBackend *kfsb,
   if (convert_path (kfsb, key, &group, &name))
     {
       gchar *str;
+      gchar *sysstr;
 
       g_assert (*name);
 
+      sysstr = g_key_file_get_value (kfsb->system_keyfile, group, name, NULL);
       str = g_key_file_get_value (kfsb->keyfile, group, name, NULL);
+      if (sysstr &&
+          (g_hash_table_contains (kfsb->system_locks, key) ||
+           str == NULL))
+        {
+          g_free (str);
+          str = g_strdup (sysstr);
+        }
 
       if (str)
         {
@@ -208,6 +220,8 @@ get_from_keyfile (GKeyfileSettingsBackend *kfsb,
           g_free (str);
         }
 
+      g_free (sysstr);
+
       g_free (group);
       g_free (name);
     }
@@ -222,6 +236,9 @@ set_to_keyfile (GKeyfileSettingsBackend *kfsb,
 {
   gchar *group, *name;
 
+  if (g_hash_table_contains (kfsb->system_locks, key))
+    return FALSE;
+
   if (convert_path (kfsb, key, &group, &name))
     {
       if (value)
@@ -369,7 +386,9 @@ g_keyfile_settings_backend_get_writable (GSettingsBackend *backend,
 {
   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
 
-  return kfsb->writable && path_is_valid (kfsb, name);
+  return kfsb->writable &&
+         !g_hash_table_contains (kfsb->system_locks, name) &&
+         path_is_valid (kfsb, name);
 }
 
 static GPermission *
@@ -515,6 +534,8 @@ g_keyfile_settings_backend_finalize (GObject *object)
 
   g_key_file_free (kfsb->keyfile);
   g_object_unref (kfsb->permission);
+  g_key_file_unref (kfsb->system_keyfile);
+  g_hash_table_unref (kfsb->system_locks);
 
   g_file_monitor_cancel (kfsb->file_monitor);
   g_object_unref (kfsb->file_monitor);
@@ -563,6 +584,73 @@ dir_changed (GFileMonitor       *monitor,
   g_keyfile_settings_backend_keyfile_writable (kfsb);
 }
 
+static void
+load_system_settings (GKeyfileSettingsBackend *kfsb)
+{
+  GError *error = NULL;
+  const char *dir;
+  char *path;
+  char *contents;
+
+  dir = g_getenv ("GSETTINGS_DEFAULTS_DIR");
+  if (dir == NULL)
+    dir = "/etc/glib-2.0/settings";
+
+  kfsb->system_keyfile = g_key_file_new ();
+  kfsb->system_locks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+  path = g_build_filename (dir, "defaults", NULL);
+
+  /* The defaults are in the same keyfile format that we use for the settings.
+   * It can be produced from a dconf database using: dconf dump
+   */
+  if (!g_key_file_load_from_file (kfsb->system_keyfile, path, G_KEY_FILE_NONE, &error))
+    {
+      if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+        g_warning ("Failed to read %s: %s", path, error->message);
+      g_clear_error (&error);
+    }
+  else
+    g_debug ("Loading default settings from %s", path);
+
+  g_free (path);
+
+  path = g_build_filename (dir, "locks", NULL);
+
+  /* The locks file is a text file containing a list paths to lock, one per line.
+   * It can be produced from a dconf database using: dconf list-locks
+   */
+  if (!g_file_get_contents (path, &contents, NULL, &error))
+    {
+      if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+        g_warning ("Failed to read %s: %s", path, error->message);
+      g_clear_error (&error);
+    }
+  else
+    {
+      char **lines;
+      gsize i;
+
+      g_debug ("Loading locks from %s", path);
+
+      lines = g_strsplit (contents, "\n", 0);
+      for (i = 0; lines[i]; i++)
+        {
+          const char *line = lines[i];
+          if (line[0] == '#' || line[0] == '\0')
+            continue;
+
+          g_debug ("Locking key %s", line);
+          g_hash_table_add (kfsb->system_locks, g_strdup (line));
+        }
+
+      g_strfreev (lines);
+    }
+  g_free (contents);
+
+  g_free (path);
+}
+
 static void
 g_keyfile_settings_backend_constructed (GObject *object)
 {
@@ -598,6 +686,8 @@ g_keyfile_settings_backend_constructed (GObject *object)
 
   g_keyfile_settings_backend_keyfile_writable (kfsb);
   g_keyfile_settings_backend_keyfile_reload (kfsb);
+
+  load_system_settings (kfsb);
 }
 
 static void


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