2 * Copyright © 2010 Codethink Limited
3 * Copyright © 2010 Novell, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Authors: Vincent Untz <vuntz@gnome.org>
19 * Ryan Lortie <desrt@desrt.ca>
31 #include "gfileinfo.h"
32 #include "gfileenumerator.h"
33 #include "gfilemonitor.h"
34 #include "gsimplepermission.h"
35 #include "gsettingsbackendinternal.h"
36 #include "giomodule-priv.h"
37 #include "gportalsupport.h"
40 #define G_TYPE_KEYFILE_SETTINGS_BACKEND (g_keyfile_settings_backend_get_type ())
41 #define G_KEYFILE_SETTINGS_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
42 G_TYPE_KEYFILE_SETTINGS_BACKEND, \
43 GKeyfileSettingsBackend))
44 #define G_IS_KEYFILE_SETTINGS_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
45 G_TYPE_KEYFILE_SETTINGS_BACKEND))
48 typedef GSettingsBackendClass GKeyfileSettingsBackendClass;
55 } GKeyfileSettingsBackendProperty;
59 GSettingsBackend parent_instance;
62 GPermission *permission;
65 GKeyFile *system_keyfile;
66 GHashTable *system_locks; /* Used as a set, owning the strings it contains */
74 GFileMonitor *file_monitor;
77 GFileMonitor *dir_monitor;
78 } GKeyfileSettingsBackend;
81 #define EXTENSION_PRIORITY 10
83 #define EXTENSION_PRIORITY (glib_should_use_portal () && !glib_has_dconf_access_in_sandbox () ? 110 : 10)
86 G_DEFINE_TYPE_WITH_CODE (GKeyfileSettingsBackend,
87 g_keyfile_settings_backend,
88 G_TYPE_SETTINGS_BACKEND,
89 _g_io_modules_ensure_extension_points_registered ();
90 g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
91 g_define_type_id, "keyfile", EXTENSION_PRIORITY))
94 compute_checksum (guint8 *digest,
95 gconstpointer contents,
101 checksum = g_checksum_new (G_CHECKSUM_SHA256);
102 g_checksum_update (checksum, contents, length);
103 g_checksum_get_digest (checksum, digest, &len);
104 g_checksum_free (checksum);
105 g_assert (len == 32);
109 g_keyfile_settings_backend_keyfile_write (GKeyfileSettingsBackend *kfsb)
114 contents = g_key_file_to_data (kfsb->keyfile, &length, NULL);
115 g_file_replace_contents (kfsb->file, contents, length, NULL, FALSE,
116 G_FILE_CREATE_REPLACE_DESTINATION |
117 G_FILE_CREATE_PRIVATE,
120 compute_checksum (kfsb->digest, contents, length);
125 group_name_matches (const gchar *group_name,
128 /* sort of like g_str_has_prefix() except that it must be an exact
129 * match or the prefix followed by '/'.
131 * for example 'a' is a prefix of 'a' and 'a/b' but not 'ab'.
135 for (i = 0; prefix[i]; i++)
136 if (prefix[i] != group_name[i])
139 return group_name[i] == '\0' || group_name[i] == '/';
143 convert_path (GKeyfileSettingsBackend *kfsb,
148 gint key_len = strlen (key);
151 if (key_len < kfsb->prefix_len ||
152 memcmp (key, kfsb->prefix, kfsb->prefix_len) != 0)
155 key_len -= kfsb->prefix_len;
156 key += kfsb->prefix_len;
158 for (i = key_len; i >= 0; i--)
162 if (kfsb->root_group)
164 /* if a root_group was specified, make sure the user hasn't given
165 * a path that ghosts that group name
167 if (i == kfsb->root_group_len && memcmp (key, kfsb->root_group, i) == 0)
172 /* if no root_group was given, ensure that the user gave a path */
181 *group = g_memdup (key, i + 1);
185 *group = g_strdup (kfsb->root_group);
189 *basename = g_memdup (key + i + 1, key_len - i);
195 path_is_valid (GKeyfileSettingsBackend *kfsb,
198 return convert_path (kfsb, path, NULL, NULL);
202 get_from_keyfile (GKeyfileSettingsBackend *kfsb,
203 const GVariantType *type,
206 GVariant *return_value = NULL;
209 if (convert_path (kfsb, key, &group, &name))
216 sysstr = g_key_file_get_value (kfsb->system_keyfile, group, name, NULL);
217 str = g_key_file_get_value (kfsb->keyfile, group, name, NULL);
219 (g_hash_table_contains (kfsb->system_locks, key) ||
223 str = g_steal_pointer (&sysstr);
228 return_value = g_variant_parse (type, str, NULL, NULL, NULL);
230 /* As a special case, support values of type %G_VARIANT_TYPE_STRING
231 * not being quoted, since users keep forgetting to do it and then
232 * getting confused. */
233 if (return_value == NULL &&
234 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
237 GString *s = g_string_sized_new (strlen (str) + 2);
240 g_string_append_c (s, '\"');
244 g_string_append_c (s, '\\');
245 g_string_append_c (s, *p);
248 g_string_append_c (s, '\"');
249 return_value = g_variant_parse (type, s->str, NULL, NULL, NULL);
250 g_string_free (s, TRUE);
265 set_to_keyfile (GKeyfileSettingsBackend *kfsb,
271 if (g_hash_table_contains (kfsb->system_locks, key))
274 if (convert_path (kfsb, key, &group, &name))
278 gchar *str = g_variant_print (value, FALSE);
279 g_key_file_set_value (kfsb->keyfile, group, name, str);
280 g_variant_unref (g_variant_ref_sink (value));
290 groups = g_key_file_get_groups (kfsb->keyfile, NULL);
292 for (i = 0; groups[i]; i++)
293 if (group_name_matches (groups[i], group))
294 g_key_file_remove_group (kfsb->keyfile, groups[i], NULL);
299 g_key_file_remove_key (kfsb->keyfile, group, name, NULL);
312 g_keyfile_settings_backend_read (GSettingsBackend *backend,
314 const GVariantType *expected_type,
315 gboolean default_value)
317 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
322 return get_from_keyfile (kfsb, expected_type, key);
327 GKeyfileSettingsBackend *kfsb;
332 g_keyfile_settings_backend_write_one (gpointer key,
336 WriteManyData *data = user_data;
337 gboolean success G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
339 success = set_to_keyfile (data->kfsb, key, value);
346 g_keyfile_settings_backend_check_one (gpointer key,
350 WriteManyData *data = user_data;
352 return data->failed = g_hash_table_contains (data->kfsb->system_locks, key) ||
353 !path_is_valid (data->kfsb, key);
357 g_keyfile_settings_backend_write_tree (GSettingsBackend *backend,
361 WriteManyData data = { G_KEYFILE_SETTINGS_BACKEND (backend) };
363 if (!data.kfsb->writable)
366 g_tree_foreach (tree, g_keyfile_settings_backend_check_one, &data);
371 g_tree_foreach (tree, g_keyfile_settings_backend_write_one, &data);
372 g_keyfile_settings_backend_keyfile_write (data.kfsb);
374 g_settings_backend_changed_tree (backend, tree, origin_tag);
380 g_keyfile_settings_backend_write (GSettingsBackend *backend,
385 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
391 success = set_to_keyfile (kfsb, key, value);
395 g_settings_backend_changed (backend, key, origin_tag);
396 g_keyfile_settings_backend_keyfile_write (kfsb);
403 g_keyfile_settings_backend_reset (GSettingsBackend *backend,
407 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
409 if (set_to_keyfile (kfsb, key, NULL))
410 g_keyfile_settings_backend_keyfile_write (kfsb);
412 g_settings_backend_changed (backend, key, origin_tag);
416 g_keyfile_settings_backend_get_writable (GSettingsBackend *backend,
419 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
421 return kfsb->writable &&
422 !g_hash_table_contains (kfsb->system_locks, name) &&
423 path_is_valid (kfsb, name);
427 g_keyfile_settings_backend_get_permission (GSettingsBackend *backend,
430 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
432 return g_object_ref (kfsb->permission);
436 keyfile_to_tree (GKeyfileSettingsBackend *kfsb,
444 groups = g_key_file_get_groups (keyfile, NULL);
445 for (i = 0; groups[i]; i++)
447 gboolean is_root_group;
451 is_root_group = g_strcmp0 (kfsb->root_group, groups[i]) == 0;
453 /* reject group names that will form invalid key names */
454 if (!is_root_group &&
455 (g_str_has_prefix (groups[i], "/") ||
456 g_str_has_suffix (groups[i], "/") || strstr (groups[i], "//")))
459 keys = g_key_file_get_keys (keyfile, groups[i], NULL, NULL);
460 g_assert (keys != NULL);
462 for (j = 0; keys[j]; j++)
466 /* reject key names with slashes in them */
467 if (strchr (keys[j], '/'))
471 path = g_strdup_printf ("%s%s", kfsb->prefix, keys[j]);
473 path = g_strdup_printf ("%s%s/%s", kfsb->prefix, groups[i], keys[j]);
475 value = g_key_file_get_value (keyfile, groups[i], keys[j], NULL);
477 if (dup_check && g_strcmp0 (g_tree_lookup (tree, path), value) == 0)
479 g_tree_remove (tree, path);
484 g_tree_insert (tree, path, value);
493 g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
502 g_file_load_contents (kfsb->file, NULL, &contents, &length, NULL, NULL);
503 compute_checksum (digest, contents, length);
505 if (memcmp (kfsb->digest, digest, sizeof digest) != 0)
507 GKeyFile *keyfiles[2];
510 tree = g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
513 keyfiles[0] = kfsb->keyfile;
514 keyfiles[1] = g_key_file_new ();
517 g_key_file_load_from_data (keyfiles[1], contents, length,
518 G_KEY_FILE_KEEP_COMMENTS |
519 G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
521 keyfile_to_tree (kfsb, tree, keyfiles[0], FALSE);
522 keyfile_to_tree (kfsb, tree, keyfiles[1], TRUE);
523 g_key_file_free (keyfiles[0]);
524 kfsb->keyfile = keyfiles[1];
526 if (g_tree_nnodes (tree) > 0)
527 g_settings_backend_changed_tree (&kfsb->parent_instance, tree, NULL);
531 memcpy (kfsb->digest, digest, sizeof digest);
538 g_keyfile_settings_backend_keyfile_writable (GKeyfileSettingsBackend *kfsb)
543 fileinfo = g_file_query_info (kfsb->dir, "access::*", 0, NULL, NULL);
548 g_file_info_get_attribute_boolean (fileinfo, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE) &&
549 g_file_info_get_attribute_boolean (fileinfo, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE);
550 g_object_unref (fileinfo);
555 if (writable != kfsb->writable)
557 kfsb->writable = writable;
558 g_settings_backend_path_writable_changed (&kfsb->parent_instance, "/");
563 g_keyfile_settings_backend_finalize (GObject *object)
565 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
567 g_key_file_free (kfsb->keyfile);
568 g_object_unref (kfsb->permission);
569 g_key_file_unref (kfsb->system_keyfile);
570 g_hash_table_unref (kfsb->system_locks);
571 g_free (kfsb->defaults_dir);
573 g_file_monitor_cancel (kfsb->file_monitor);
574 g_object_unref (kfsb->file_monitor);
575 g_object_unref (kfsb->file);
577 g_file_monitor_cancel (kfsb->dir_monitor);
578 g_object_unref (kfsb->dir_monitor);
579 g_object_unref (kfsb->dir);
581 g_free (kfsb->root_group);
582 g_free (kfsb->prefix);
584 G_OBJECT_CLASS (g_keyfile_settings_backend_parent_class)
589 g_keyfile_settings_backend_init (GKeyfileSettingsBackend *kfsb)
594 file_changed (GFileMonitor *monitor,
597 GFileMonitorEvent event_type,
600 GKeyfileSettingsBackend *kfsb = user_data;
602 /* Ignore file deletions, let the GKeyFile content remain in tact. */
603 if (event_type != G_FILE_MONITOR_EVENT_DELETED)
604 g_keyfile_settings_backend_keyfile_reload (kfsb);
608 dir_changed (GFileMonitor *monitor,
611 GFileMonitorEvent event_type,
614 GKeyfileSettingsBackend *kfsb = user_data;
616 g_keyfile_settings_backend_keyfile_writable (kfsb);
620 load_system_settings (GKeyfileSettingsBackend *kfsb)
622 GError *error = NULL;
623 const char *dir = "/etc/glib-2.0/settings";
627 kfsb->system_keyfile = g_key_file_new ();
628 kfsb->system_locks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
630 if (kfsb->defaults_dir)
631 dir = kfsb->defaults_dir;
633 path = g_build_filename (dir, "defaults", NULL);
635 /* The defaults are in the same keyfile format that we use for the settings.
636 * It can be produced from a dconf database using: dconf dump
638 if (!g_key_file_load_from_file (kfsb->system_keyfile, path, G_KEY_FILE_NONE, &error))
640 if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
641 g_warning ("Failed to read %s: %s", path, error->message);
642 g_clear_error (&error);
645 g_debug ("Loading default settings from %s", path);
649 path = g_build_filename (dir, "locks", NULL);
651 /* The locks file is a text file containing a list paths to lock, one per line.
652 * It can be produced from a dconf database using: dconf list-locks
654 if (!g_file_get_contents (path, &contents, NULL, &error))
656 if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
657 g_warning ("Failed to read %s: %s", path, error->message);
658 g_clear_error (&error);
665 g_debug ("Loading locks from %s", path);
667 lines = g_strsplit (contents, "\n", 0);
668 for (i = 0; lines[i]; i++)
670 char *line = lines[i];
671 if (line[0] == '#' || line[0] == '\0')
677 g_debug ("Locking key %s", line);
678 g_hash_table_add (kfsb->system_locks, g_steal_pointer (&line));
689 g_keyfile_settings_backend_constructed (GObject *object)
691 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
693 if (kfsb->file == NULL)
695 char *filename = g_build_filename (g_get_user_config_dir (),
696 "glib-2.0", "settings", "keyfile",
698 kfsb->file = g_file_new_for_path (filename);
702 if (kfsb->prefix == NULL)
704 kfsb->prefix = g_strdup ("/");
705 kfsb->prefix_len = 1;
708 kfsb->keyfile = g_key_file_new ();
709 kfsb->permission = g_simple_permission_new (TRUE);
711 kfsb->dir = g_file_get_parent (kfsb->file);
712 g_mkdir_with_parents (g_file_peek_path (kfsb->dir), 0700);
714 kfsb->file_monitor = g_file_monitor (kfsb->file, G_FILE_MONITOR_NONE, NULL, NULL);
715 kfsb->dir_monitor = g_file_monitor (kfsb->dir, G_FILE_MONITOR_NONE, NULL, NULL);
717 compute_checksum (kfsb->digest, NULL, 0);
719 g_signal_connect (kfsb->file_monitor, "changed",
720 G_CALLBACK (file_changed), kfsb);
721 g_signal_connect (kfsb->dir_monitor, "changed",
722 G_CALLBACK (dir_changed), kfsb);
724 g_keyfile_settings_backend_keyfile_writable (kfsb);
725 g_keyfile_settings_backend_keyfile_reload (kfsb);
727 load_system_settings (kfsb);
731 g_keyfile_settings_backend_set_property (GObject *object,
736 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
738 switch ((GKeyfileSettingsBackendProperty)prop_id)
741 /* Construct only. */
742 g_assert (kfsb->file == NULL);
743 if (g_value_get_string (value))
744 kfsb->file = g_file_new_for_path (g_value_get_string (value));
748 /* Construct only. */
749 g_assert (kfsb->prefix == NULL);
750 kfsb->prefix = g_value_dup_string (value);
752 kfsb->prefix_len = strlen (kfsb->prefix);
755 case PROP_ROOT_GROUP:
756 /* Construct only. */
757 g_assert (kfsb->root_group == NULL);
758 kfsb->root_group = g_value_dup_string (value);
759 if (kfsb->root_group)
760 kfsb->root_group_len = strlen (kfsb->root_group);
763 case PROP_DEFAULTS_DIR:
764 /* Construct only. */
765 g_assert (kfsb->defaults_dir == NULL);
766 kfsb->defaults_dir = g_value_dup_string (value);
770 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
776 g_keyfile_settings_backend_get_property (GObject *object,
781 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
783 switch ((GKeyfileSettingsBackendProperty)prop_id)
786 g_value_set_string (value, g_file_peek_path (kfsb->file));
790 g_value_set_string (value, kfsb->prefix);
793 case PROP_ROOT_GROUP:
794 g_value_set_string (value, kfsb->root_group);
797 case PROP_DEFAULTS_DIR:
798 g_value_set_string (value, kfsb->defaults_dir);
802 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
808 g_keyfile_settings_backend_class_init (GKeyfileSettingsBackendClass *class)
810 GObjectClass *object_class = G_OBJECT_CLASS (class);
812 object_class->finalize = g_keyfile_settings_backend_finalize;
813 object_class->constructed = g_keyfile_settings_backend_constructed;
814 object_class->get_property = g_keyfile_settings_backend_get_property;
815 object_class->set_property = g_keyfile_settings_backend_set_property;
817 class->read = g_keyfile_settings_backend_read;
818 class->write = g_keyfile_settings_backend_write;
819 class->write_tree = g_keyfile_settings_backend_write_tree;
820 class->reset = g_keyfile_settings_backend_reset;
821 class->get_writable = g_keyfile_settings_backend_get_writable;
822 class->get_permission = g_keyfile_settings_backend_get_permission;
823 /* No need to implement subscribed/unsubscribe: the only point would be to
824 * stop monitoring the file when there's no GSettings anymore, which is no
829 * GKeyfileSettingsBackend:filename:
831 * The location where the settings are stored on disk.
833 * Defaults to `$XDG_CONFIG_HOME/glib-2.0/settings/keyfile`.
835 g_object_class_install_property (object_class,
837 g_param_spec_string ("filename",
841 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
842 G_PARAM_STATIC_STRINGS));
845 * GKeyfileSettingsBackend:root-path:
847 * All settings read to or written from the backend must fall under the
848 * path given in @root_path (which must start and end with a slash and
849 * not contain two consecutive slashes). @root_path may be "/".
853 g_object_class_install_property (object_class,
855 g_param_spec_string ("root-path",
859 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
860 G_PARAM_STATIC_STRINGS));
863 * GKeyfileSettingsBackend:root-group:
865 * If @root_group is non-%NULL then it specifies the name of the keyfile
866 * group used for keys that are written directly below the root path.
870 g_object_class_install_property (object_class,
872 g_param_spec_string ("root-group",
874 P_("The root group"),
876 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
877 G_PARAM_STATIC_STRINGS));
880 * GKeyfileSettingsBackend:default-dir:
882 * The directory where the system defaults and locks are located.
884 * Defaults to `/etc/glib-2.0/settings`.
886 g_object_class_install_property (object_class,
888 g_param_spec_string ("defaults-dir",
892 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
893 G_PARAM_STATIC_STRINGS));
897 * g_keyfile_settings_backend_new:
898 * @filename: the filename of the keyfile
899 * @root_path: the path under which all settings keys appear
900 * @root_group: (nullable): the group name corresponding to
901 * @root_path, or %NULL
903 * Creates a keyfile-backed #GSettingsBackend.
905 * The filename of the keyfile to use is given by @filename.
907 * All settings read to or written from the backend must fall under the
908 * path given in @root_path (which must start and end with a slash and
909 * not contain two consecutive slashes). @root_path may be "/".
911 * If @root_group is non-%NULL then it specifies the name of the keyfile
912 * group used for keys that are written directly below @root_path. For
913 * example, if @root_path is "/apps/example/" and @root_group is
914 * "toplevel", then settings the key "/apps/example/enabled" to a value
915 * of %TRUE will cause the following to appear in the keyfile:
922 * If @root_group is %NULL then it is not permitted to store keys
923 * directly below the @root_path.
925 * For keys not stored directly below @root_path (ie: in a sub-path),
926 * the name of the subpath (with the final slash stripped) is used as
927 * the name of the keyfile group. To continue the example, if
928 * "/apps/example/profiles/default/font-size" were set to
929 * 12 then the following would appear in the keyfile:
936 * The backend will refuse writes (and return writability as being
937 * %FALSE) for keys outside of @root_path and, in the event that
938 * @root_group is %NULL, also for keys directly under @root_path.
939 * Writes will also be refused if the backend detects that it has the
940 * inability to rewrite the keyfile (ie: the containing directory is not
943 * There is no checking done for your key namespace clashing with the
944 * syntax of the key file format. For example, if you have '[' or ']'
945 * characters in your path names or '=' in your key names you may be in
948 * The backend reads default values from a keyfile called `defaults` in
949 * the directory specified by the #GKeyfileSettingsBackend:defaults-dir property,
950 * and a list of locked keys from a text file with the name `locks` in
953 * Returns: (transfer full): a keyfile-backed #GSettingsBackend
956 g_keyfile_settings_backend_new (const gchar *filename,
957 const gchar *root_path,
958 const gchar *root_group)
960 g_return_val_if_fail (filename != NULL, NULL);
961 g_return_val_if_fail (root_path != NULL, NULL);
962 g_return_val_if_fail (g_str_has_prefix (root_path, "/"), NULL);
963 g_return_val_if_fail (g_str_has_suffix (root_path, "/"), NULL);
964 g_return_val_if_fail (strstr (root_path, "//") == NULL, NULL);
966 return G_SETTINGS_BACKEND (g_object_new (G_TYPE_KEYFILE_SETTINGS_BACKEND,
967 "filename", filename,
968 "root-path", root_path,
969 "root-group", root_group,