[glib/settings-portal] Wip: add support for system defaults to keyfile backend
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/settings-portal] Wip: add support for system defaults to keyfile backend
- Date: Thu, 8 Nov 2018 03:15:26 +0000 (UTC)
commit 2596dba57ebda63cf990e0423376b213a098cc85
Author: Matthias Clasen <mclasen redhat com>
Date: Mon Nov 5 16:07:55 2018 -0500
Wip: add support for system 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 proof-of-concept patch adds minimal support for the
same to the keyfile settings backend. We look for the
'mandatory profile' in /run/dconf/user, and if we find it,
read system-wide overrides and locks from the first system-db
specified therein. We treat the values from the system-wide
keyfile as overrides or defaults, depending on whether the
key is on the list of locks or not.
Writes always go to the per-user keyfile.
gio/gkeyfilesettingsbackend.c | 175 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 174 insertions(+), 1 deletion(-)
---
diff --git a/gio/gkeyfilesettingsbackend.c b/gio/gkeyfilesettingsbackend.c
index ab214d3d4..7484fb0fd 100644
--- a/gio/gkeyfilesettingsbackend.c
+++ b/gio/gkeyfilesettingsbackend.c
@@ -26,9 +26,11 @@
#include <stdio.h>
#include <string.h>
+#include <unistd.h>
#include "gfile.h"
#include "gfileinfo.h"
+#include "gfileenumerator.h"
#include "gfilemonitor.h"
#include "gsimplepermission.h"
#include "gsettingsbackendinternal.h"
@@ -60,6 +62,8 @@ typedef struct
GKeyFile *keyfile;
GPermission *permission;
gboolean writable;
+ GKeyFile *system_keyfile;
+ GHashTable *system_locks;
gchar *prefix;
gint prefix_len;
@@ -204,10 +208,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)
{
@@ -215,6 +228,8 @@ get_from_keyfile (GKeyfileSettingsBackend *kfsb,
g_free (str);
}
+ g_free (sysstr);
+
g_free (group);
g_free (name);
}
@@ -229,6 +244,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)
@@ -375,7 +393,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 *
@@ -521,6 +541,8 @@ g_keyfile_settings_backend_finalize (GObject *object)
g_key_file_free (kfsb->keyfile);
g_object_unref (kfsb->permission);
+ g_key_file_free (kfsb->system_keyfile);
+ g_hash_table_unref (kfsb->system_locks);
g_file_monitor_cancel (kfsb->file_monitor);
g_object_unref (kfsb->file_monitor);
@@ -569,10 +591,157 @@ dir_changed (GFileMonitor *monitor,
g_keyfile_settings_backend_keyfile_writable (kfsb);
}
+static char *
+find_system_settings (void)
+{
+ char *mandatory_profile;
+ char *contents;
+ char *name = NULL;
+ int i;
+
+ mandatory_profile = g_strdup_printf ("/run/dconf/user/%d", getuid ());
+ g_debug ("Looking for mandatory profile in %s", mandatory_profile);
+ if (g_file_get_contents (mandatory_profile, &contents, NULL, NULL))
+ {
+ char **lines;
+
+ lines = g_strsplit (contents, "\n", 0);
+ for (i = 0; lines[i]; i++)
+ {
+ if (g_str_has_prefix (lines[i], "system-db:"))
+ {
+ name = g_strdup (lines[i] + strlen ("system-db:"));
+ g_debug ("Using mandatory profile from %s, found system-db %s", mandatory_profile, name);
+ break;
+ }
+ }
+
+ g_strfreev (lines);
+ }
+
+ g_free (mandatory_profile);
+ g_free (contents);
+
+ return name;
+}
+
+static void
+load_system_settings (GKeyfileSettingsBackend *kfsb,
+ const char *db)
+{
+ GError *error = NULL;
+ char *db_path = g_strdup_printf ("/etc/dconf/db/%s.d", db);
+ GFile *file;
+ GFileEnumerator *enumerator;
+ GFile *lock_file;
+
+ kfsb->system_keyfile = g_key_file_new ();
+ kfsb->system_locks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+ if (!db)
+ return;
+
+ g_debug ("Loading settings from %s", db_path);
+
+ file = g_file_new_for_path (db_path);
+ enumerator = g_file_enumerate_children (file, "*", G_FILE_QUERY_INFO_NONE, NULL, NULL);
+ if (enumerator != NULL)
+ {
+ while (TRUE)
+ {
+ GFileInfo *info;
+ const char *name;
+ char *path;
+
+ info = g_file_enumerator_next_file (enumerator, NULL, NULL);
+ if (info == NULL)
+ break;
+
+ if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
+ continue;
+
+ name = g_file_info_get_name (info);
+ path = g_build_filename (db_path, name, NULL);
+
+ g_debug ("Loading system-wide settings from %s", path);
+
+ 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);
+ }
+ g_free (path);
+ g_object_unref (info);
+ }
+
+ g_object_unref (enumerator);
+ }
+
+ lock_file = g_file_get_child (file, "locks");
+
+ g_debug ("Loading locks from %s/locks", db_path);
+
+ enumerator = g_file_enumerate_children (lock_file, "*", G_FILE_QUERY_INFO_NONE, NULL, NULL);
+ if (enumerator != NULL)
+ {
+ while (TRUE)
+ {
+ GFileInfo *info;
+ const char *name;
+ char *path;
+ char *contents;
+ int i;
+ char **lines;
+
+ info = g_file_enumerator_next_file (enumerator, NULL, NULL);
+ if (info == NULL)
+ break;
+
+ name = g_file_info_get_name (info);
+ path = g_build_filename (db_path, "locks", name, NULL);
+
+ g_debug ("Loading system-wide locks from %s", path);
+
+ 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);
+ }
+
+ lines = g_strsplit (contents, "\n", 0);
+ for (i = 0; lines[i]; i++)
+ {
+ const char *line = lines[i];
+ if (line[0] == '#')
+ continue;
+ if (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);
+ g_object_unref (info);
+ }
+
+ g_object_unref (enumerator);
+ }
+
+ g_object_unref (lock_file);
+ g_object_unref (file);
+}
+
static void
g_keyfile_settings_backend_constructed (GObject *object)
{
GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
+ char *name;
if (kfsb->filename == NULL)
kfsb->filename = g_build_filename (g_get_user_config_dir (), "settings", NULL);
@@ -602,6 +771,10 @@ g_keyfile_settings_backend_constructed (GObject *object)
g_keyfile_settings_backend_keyfile_writable (kfsb);
g_keyfile_settings_backend_keyfile_reload (kfsb);
+
+ name = find_system_settings ();
+ load_system_settings (kfsb, name);
+ g_free (name);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]