Imported Upstream version 2.66.6
[platform/upstream/glib.git] / gio / gkeyfilesettingsbackend.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  * Copyright © 2010 Novell, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  * Authors: Vincent Untz <vuntz@gnome.org>
19  *          Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include <glib.h>
25 #include <glibintl.h>
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "gfile.h"
31 #include "gfileinfo.h"
32 #include "gfileenumerator.h"
33 #include "gfilemonitor.h"
34 #include "gsimplepermission.h"
35 #include "gsettingsbackendinternal.h"
36 #include "gstrfuncsprivate.h"
37 #include "giomodule-priv.h"
38 #include "gportalsupport.h"
39
40
41 #define G_TYPE_KEYFILE_SETTINGS_BACKEND      (g_keyfile_settings_backend_get_type ())
42 #define G_KEYFILE_SETTINGS_BACKEND(inst)     (G_TYPE_CHECK_INSTANCE_CAST ((inst),      \
43                                               G_TYPE_KEYFILE_SETTINGS_BACKEND,         \
44                                               GKeyfileSettingsBackend))
45 #define G_IS_KEYFILE_SETTINGS_BACKEND(inst)  (G_TYPE_CHECK_INSTANCE_TYPE ((inst),      \
46                                               G_TYPE_KEYFILE_SETTINGS_BACKEND))
47
48
49 typedef GSettingsBackendClass GKeyfileSettingsBackendClass;
50
51 typedef enum {
52   PROP_FILENAME = 1,
53   PROP_ROOT_PATH,
54   PROP_ROOT_GROUP,
55   PROP_DEFAULTS_DIR
56 } GKeyfileSettingsBackendProperty;
57
58 typedef struct
59 {
60   GSettingsBackend   parent_instance;
61
62   GKeyFile          *keyfile;
63   GPermission       *permission;
64   gboolean           writable;
65   char              *defaults_dir;
66   GKeyFile          *system_keyfile;
67   GHashTable        *system_locks; /* Used as a set, owning the strings it contains */
68
69   gchar             *prefix;
70   gint               prefix_len;
71   gchar             *root_group;
72   gint               root_group_len;
73
74   GFile             *file;
75   GFileMonitor      *file_monitor;
76   guint8             digest[32];
77   GFile             *dir;
78   GFileMonitor      *dir_monitor;
79 } GKeyfileSettingsBackend;
80
81 #ifdef G_OS_WIN32
82 #define EXTENSION_PRIORITY 10
83 #else
84 #define EXTENSION_PRIORITY (glib_should_use_portal () && !glib_has_dconf_access_in_sandbox () ? 110 : 10)
85 #endif
86
87 G_DEFINE_TYPE_WITH_CODE (GKeyfileSettingsBackend,
88                          g_keyfile_settings_backend,
89                          G_TYPE_SETTINGS_BACKEND,
90                          _g_io_modules_ensure_extension_points_registered ();
91                          g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
92                                                          g_define_type_id, "keyfile", EXTENSION_PRIORITY))
93
94 static void
95 compute_checksum (guint8        *digest,
96                   gconstpointer  contents,
97                   gsize          length)
98 {
99   GChecksum *checksum;
100   gsize len = 32;
101
102   checksum = g_checksum_new (G_CHECKSUM_SHA256);
103   g_checksum_update (checksum, contents, length);
104   g_checksum_get_digest (checksum, digest, &len);
105   g_checksum_free (checksum);
106   g_assert (len == 32);
107 }
108
109 static void
110 g_keyfile_settings_backend_keyfile_write (GKeyfileSettingsBackend *kfsb)
111 {
112   gchar *contents;
113   gsize length;
114
115   contents = g_key_file_to_data (kfsb->keyfile, &length, NULL);
116   g_file_replace_contents (kfsb->file, contents, length, NULL, FALSE,
117                            G_FILE_CREATE_REPLACE_DESTINATION |
118                            G_FILE_CREATE_PRIVATE,
119                            NULL, NULL, NULL);
120
121   compute_checksum (kfsb->digest, contents, length);
122   g_free (contents);
123 }
124
125 static gboolean
126 group_name_matches (const gchar *group_name,
127                     const gchar *prefix)
128 {
129   /* sort of like g_str_has_prefix() except that it must be an exact
130    * match or the prefix followed by '/'.
131    *
132    * for example 'a' is a prefix of 'a' and 'a/b' but not 'ab'.
133    */
134   gint i;
135
136   for (i = 0; prefix[i]; i++)
137     if (prefix[i] != group_name[i])
138       return FALSE;
139
140   return group_name[i] == '\0' || group_name[i] == '/';
141 }
142
143 static gboolean
144 convert_path (GKeyfileSettingsBackend  *kfsb,
145               const gchar              *key,
146               gchar                   **group,
147               gchar                   **basename)
148 {
149   gsize key_len = strlen (key);
150   const gchar *last_slash;
151
152   if (key_len < kfsb->prefix_len ||
153       memcmp (key, kfsb->prefix, kfsb->prefix_len) != 0)
154     return FALSE;
155
156   key_len -= kfsb->prefix_len;
157   key += kfsb->prefix_len;
158
159   last_slash = strrchr (key, '/');
160
161   if (kfsb->root_group)
162     {
163       /* if a root_group was specified, make sure the user hasn't given
164        * a path that ghosts that group name
165        */
166       if (last_slash != NULL && (last_slash - key) == kfsb->root_group_len && memcmp (key, kfsb->root_group, last_slash - key) == 0)
167         return FALSE;
168     }
169   else
170     {
171       /* if no root_group was given, ensure that the user gave a path */
172       if (last_slash == NULL)
173         return FALSE;
174     }
175
176   if (group)
177     {
178       if (last_slash != NULL)
179         {
180           *group = g_memdup2 (key, (last_slash - key) + 1);
181           (*group)[(last_slash - key)] = '\0';
182         }
183       else
184         *group = g_strdup (kfsb->root_group);
185     }
186
187   if (basename)
188     *basename = g_memdup2 (last_slash + 1, key_len - (last_slash - key));
189
190   return TRUE;
191 }
192
193 static gboolean
194 path_is_valid (GKeyfileSettingsBackend *kfsb,
195                const gchar             *path)
196 {
197   return convert_path (kfsb, path, NULL, NULL);
198 }
199
200 static GVariant *
201 get_from_keyfile (GKeyfileSettingsBackend *kfsb,
202                   const GVariantType      *type,
203                   const gchar             *key)
204 {
205   GVariant *return_value = NULL;
206   gchar *group, *name;
207
208   if (convert_path (kfsb, key, &group, &name))
209     {
210       gchar *str;
211       gchar *sysstr;
212
213       g_assert (*name);
214
215       sysstr = g_key_file_get_value (kfsb->system_keyfile, group, name, NULL);
216       str = g_key_file_get_value (kfsb->keyfile, group, name, NULL);
217       if (sysstr &&
218           (g_hash_table_contains (kfsb->system_locks, key) ||
219            str == NULL))
220         {
221           g_free (str);
222           str = g_steal_pointer (&sysstr);
223         }
224
225       if (str)
226         {
227           return_value = g_variant_parse (type, str, NULL, NULL, NULL);
228
229           /* As a special case, support values of type %G_VARIANT_TYPE_STRING
230            * not being quoted, since users keep forgetting to do it and then
231            * getting confused. */
232           if (return_value == NULL &&
233               g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
234               str[0] != '\"')
235             {
236               GString *s = g_string_sized_new (strlen (str) + 2);
237               char *p = str;
238
239               g_string_append_c (s, '\"');
240               while (*p)
241                 {
242                   if (*p == '\"')
243                     g_string_append_c (s, '\\');
244                   g_string_append_c (s, *p);
245                   p++;
246                 }
247               g_string_append_c (s, '\"');
248               return_value = g_variant_parse (type, s->str, NULL, NULL, NULL);
249               g_string_free (s, TRUE);
250             }
251           g_free (str);
252         }
253
254       g_free (sysstr);
255
256       g_free (group);
257       g_free (name);
258     }
259
260   return return_value;
261 }
262
263 static gboolean
264 set_to_keyfile (GKeyfileSettingsBackend *kfsb,
265                 const gchar             *key,
266                 GVariant                *value)
267 {
268   gchar *group, *name;
269
270   if (g_hash_table_contains (kfsb->system_locks, key))
271     return FALSE;
272
273   if (convert_path (kfsb, key, &group, &name))
274     {
275       if (value)
276         {
277           gchar *str = g_variant_print (value, FALSE);
278           g_key_file_set_value (kfsb->keyfile, group, name, str);
279           g_variant_unref (g_variant_ref_sink (value));
280           g_free (str);
281         }
282       else
283         {
284           if (*name == '\0')
285             {
286               gchar **groups;
287               gint i;
288
289               groups = g_key_file_get_groups (kfsb->keyfile, NULL);
290
291               for (i = 0; groups[i]; i++)
292                 if (group_name_matches (groups[i], group))
293                   g_key_file_remove_group (kfsb->keyfile, groups[i], NULL);
294
295               g_strfreev (groups);
296             }
297           else
298             g_key_file_remove_key (kfsb->keyfile, group, name, NULL);
299         }
300
301       g_free (group);
302       g_free (name);
303
304       return TRUE;
305     }
306
307   return FALSE;
308 }
309
310 static GVariant *
311 g_keyfile_settings_backend_read (GSettingsBackend   *backend,
312                                  const gchar        *key,
313                                  const GVariantType *expected_type,
314                                  gboolean            default_value)
315 {
316   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
317
318   if (default_value)
319     return NULL;
320
321   return get_from_keyfile (kfsb, expected_type, key);
322 }
323
324 typedef struct
325 {
326   GKeyfileSettingsBackend *kfsb;
327   gboolean failed;
328 } WriteManyData;
329
330 static gboolean
331 g_keyfile_settings_backend_write_one (gpointer key,
332                                       gpointer value,
333                                       gpointer user_data)
334 {
335   WriteManyData *data = user_data;
336   gboolean success G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;
337
338   success = set_to_keyfile (data->kfsb, key, value);
339   g_assert (success);
340
341   return FALSE;
342 }
343
344 static gboolean
345 g_keyfile_settings_backend_check_one (gpointer key,
346                                       gpointer value,
347                                       gpointer user_data)
348 {
349   WriteManyData *data = user_data;
350
351   return data->failed = g_hash_table_contains (data->kfsb->system_locks, key) ||
352                         !path_is_valid (data->kfsb, key);
353 }
354
355 static gboolean
356 g_keyfile_settings_backend_write_tree (GSettingsBackend *backend,
357                                        GTree            *tree,
358                                        gpointer          origin_tag)
359 {
360   WriteManyData data = { G_KEYFILE_SETTINGS_BACKEND (backend) };
361
362   if (!data.kfsb->writable)
363     return FALSE;
364
365   g_tree_foreach (tree, g_keyfile_settings_backend_check_one, &data);
366
367   if (data.failed)
368     return FALSE;
369
370   g_tree_foreach (tree, g_keyfile_settings_backend_write_one, &data);
371   g_keyfile_settings_backend_keyfile_write (data.kfsb);
372
373   g_settings_backend_changed_tree (backend, tree, origin_tag);
374
375   return TRUE;
376 }
377
378 static gboolean
379 g_keyfile_settings_backend_write (GSettingsBackend *backend,
380                                   const gchar      *key,
381                                   GVariant         *value,
382                                   gpointer          origin_tag)
383 {
384   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
385   gboolean success;
386
387   if (!kfsb->writable)
388     return FALSE;
389
390   success = set_to_keyfile (kfsb, key, value);
391
392   if (success)
393     {
394       g_settings_backend_changed (backend, key, origin_tag);
395       g_keyfile_settings_backend_keyfile_write (kfsb);
396     }
397
398   return success;
399 }
400
401 static void
402 g_keyfile_settings_backend_reset (GSettingsBackend *backend,
403                                   const gchar      *key,
404                                   gpointer          origin_tag)
405 {
406   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
407
408   if (set_to_keyfile (kfsb, key, NULL))
409     g_keyfile_settings_backend_keyfile_write (kfsb);
410
411   g_settings_backend_changed (backend, key, origin_tag);
412 }
413
414 static gboolean
415 g_keyfile_settings_backend_get_writable (GSettingsBackend *backend,
416                                          const gchar      *name)
417 {
418   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
419
420   return kfsb->writable &&
421          !g_hash_table_contains (kfsb->system_locks, name) &&
422          path_is_valid (kfsb, name);
423 }
424
425 static GPermission *
426 g_keyfile_settings_backend_get_permission (GSettingsBackend *backend,
427                                            const gchar      *path)
428 {
429   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
430
431   return g_object_ref (kfsb->permission);
432 }
433
434 static void
435 keyfile_to_tree (GKeyfileSettingsBackend *kfsb,
436                  GTree                   *tree,
437                  GKeyFile                *keyfile,
438                  gboolean                 dup_check)
439 {
440   gchar **groups;
441   gint i;
442
443   groups = g_key_file_get_groups (keyfile, NULL);
444   for (i = 0; groups[i]; i++)
445     {
446       gboolean is_root_group;
447       gchar **keys;
448       gint j;
449
450       is_root_group = g_strcmp0 (kfsb->root_group, groups[i]) == 0;
451
452       /* reject group names that will form invalid key names */
453       if (!is_root_group &&
454           (g_str_has_prefix (groups[i], "/") ||
455            g_str_has_suffix (groups[i], "/") || strstr (groups[i], "//")))
456         continue;
457
458       keys = g_key_file_get_keys (keyfile, groups[i], NULL, NULL);
459       g_assert (keys != NULL);
460
461       for (j = 0; keys[j]; j++)
462         {
463           gchar *path, *value;
464
465           /* reject key names with slashes in them */
466           if (strchr (keys[j], '/'))
467             continue;
468
469           if (is_root_group)
470             path = g_strdup_printf ("%s%s", kfsb->prefix, keys[j]);
471           else
472             path = g_strdup_printf ("%s%s/%s", kfsb->prefix, groups[i], keys[j]);
473
474           value = g_key_file_get_value (keyfile, groups[i], keys[j], NULL);
475
476           if (dup_check && g_strcmp0 (g_tree_lookup (tree, path), value) == 0)
477             {
478               g_tree_remove (tree, path);
479               g_free (value);
480               g_free (path);
481             }
482           else
483             g_tree_insert (tree, path, value);
484         }
485
486       g_strfreev (keys);
487     }
488   g_strfreev (groups);
489 }
490
491 static void
492 g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
493 {
494   guint8 digest[32];
495   gchar *contents;
496   gsize length;
497
498   contents = NULL;
499   length = 0;
500
501   g_file_load_contents (kfsb->file, NULL, &contents, &length, NULL, NULL);
502   compute_checksum (digest, contents, length);
503
504   if (memcmp (kfsb->digest, digest, sizeof digest) != 0)
505     {
506       GKeyFile *keyfiles[2];
507       GTree *tree;
508
509       tree = g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
510                               g_free, g_free);
511
512       keyfiles[0] = kfsb->keyfile;
513       keyfiles[1] = g_key_file_new ();
514
515       if (length > 0)
516         g_key_file_load_from_data (keyfiles[1], contents, length,
517                                    G_KEY_FILE_KEEP_COMMENTS |
518                                    G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
519
520       keyfile_to_tree (kfsb, tree, keyfiles[0], FALSE);
521       keyfile_to_tree (kfsb, tree, keyfiles[1], TRUE);
522       g_key_file_free (keyfiles[0]);
523       kfsb->keyfile = keyfiles[1];
524
525       if (g_tree_nnodes (tree) > 0)
526         g_settings_backend_changed_tree (&kfsb->parent_instance, tree, NULL);
527
528       g_tree_unref (tree);
529
530       memcpy (kfsb->digest, digest, sizeof digest);
531     }
532
533   g_free (contents);
534 }
535
536 static void
537 g_keyfile_settings_backend_keyfile_writable (GKeyfileSettingsBackend *kfsb)
538 {
539   GFileInfo *fileinfo;
540   gboolean writable;
541
542   fileinfo = g_file_query_info (kfsb->dir, "access::*", 0, NULL, NULL);
543
544   if (fileinfo)
545     {
546       writable =
547         g_file_info_get_attribute_boolean (fileinfo, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE) &&
548         g_file_info_get_attribute_boolean (fileinfo, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE);
549       g_object_unref (fileinfo);
550     }
551   else
552     writable = FALSE;
553
554   if (writable != kfsb->writable)
555     {
556       kfsb->writable = writable;
557       g_settings_backend_path_writable_changed (&kfsb->parent_instance, "/");
558     }
559 }
560
561 static void
562 g_keyfile_settings_backend_finalize (GObject *object)
563 {
564   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
565
566   g_key_file_free (kfsb->keyfile);
567   g_object_unref (kfsb->permission);
568   g_key_file_unref (kfsb->system_keyfile);
569   g_hash_table_unref (kfsb->system_locks);
570   g_free (kfsb->defaults_dir);
571
572   g_file_monitor_cancel (kfsb->file_monitor);
573   g_object_unref (kfsb->file_monitor);
574   g_object_unref (kfsb->file);
575
576   g_file_monitor_cancel (kfsb->dir_monitor);
577   g_object_unref (kfsb->dir_monitor);
578   g_object_unref (kfsb->dir);
579
580   g_free (kfsb->root_group);
581   g_free (kfsb->prefix);
582
583   G_OBJECT_CLASS (g_keyfile_settings_backend_parent_class)
584     ->finalize (object);
585 }
586
587 static void
588 g_keyfile_settings_backend_init (GKeyfileSettingsBackend *kfsb)
589 {
590 }
591
592 static void
593 file_changed (GFileMonitor      *monitor,
594               GFile             *file,
595               GFile             *other_file,
596               GFileMonitorEvent  event_type,
597               gpointer           user_data)
598 {
599   GKeyfileSettingsBackend *kfsb = user_data;
600
601   /* Ignore file deletions, let the GKeyFile content remain in tact. */
602   if (event_type != G_FILE_MONITOR_EVENT_DELETED)
603     g_keyfile_settings_backend_keyfile_reload (kfsb);
604 }
605
606 static void
607 dir_changed (GFileMonitor       *monitor,
608               GFile             *file,
609               GFile             *other_file,
610               GFileMonitorEvent  event_type,
611               gpointer           user_data)
612 {
613   GKeyfileSettingsBackend *kfsb = user_data;
614
615   g_keyfile_settings_backend_keyfile_writable (kfsb);
616 }
617
618 static void
619 load_system_settings (GKeyfileSettingsBackend *kfsb)
620 {
621   GError *error = NULL;
622   const char *dir = "/etc/glib-2.0/settings";
623   char *path;
624   char *contents;
625
626   kfsb->system_keyfile = g_key_file_new ();
627   kfsb->system_locks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
628
629   if (kfsb->defaults_dir)
630     dir = kfsb->defaults_dir;
631
632   path = g_build_filename (dir, "defaults", NULL);
633
634   /* The defaults are in the same keyfile format that we use for the settings.
635    * It can be produced from a dconf database using: dconf dump
636    */
637   if (!g_key_file_load_from_file (kfsb->system_keyfile, path, G_KEY_FILE_NONE, &error))
638     {
639       if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
640         g_warning ("Failed to read %s: %s", path, error->message);
641       g_clear_error (&error);
642     }
643   else
644     g_debug ("Loading default settings from %s", path);
645
646   g_free (path);
647
648   path = g_build_filename (dir, "locks", NULL);
649
650   /* The locks file is a text file containing a list paths to lock, one per line.
651    * It can be produced from a dconf database using: dconf list-locks
652    */
653   if (!g_file_get_contents (path, &contents, NULL, &error))
654     {
655       if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
656         g_warning ("Failed to read %s: %s", path, error->message);
657       g_clear_error (&error);
658     }
659   else
660     {
661       char **lines;
662       gsize i;
663
664       g_debug ("Loading locks from %s", path);
665
666       lines = g_strsplit (contents, "\n", 0);
667       for (i = 0; lines[i]; i++)
668         {
669           char *line = lines[i];
670           if (line[0] == '#' || line[0] == '\0')
671             {
672               g_free (line);
673               continue;
674             }
675
676           g_debug ("Locking key %s", line);
677           g_hash_table_add (kfsb->system_locks, g_steal_pointer (&line));
678         }
679
680       g_free (lines);
681     }
682   g_free (contents);
683
684   g_free (path);
685 }
686
687 static void
688 g_keyfile_settings_backend_constructed (GObject *object)
689 {
690   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
691
692   if (kfsb->file == NULL)
693     {
694       char *filename = g_build_filename (g_get_user_config_dir (),
695                                          "glib-2.0", "settings", "keyfile",
696                                          NULL);
697       kfsb->file = g_file_new_for_path (filename);
698       g_free (filename);
699     }
700
701   if (kfsb->prefix == NULL)
702     {
703       kfsb->prefix = g_strdup ("/");
704       kfsb->prefix_len = 1;
705     }
706   
707   kfsb->keyfile = g_key_file_new ();
708   kfsb->permission = g_simple_permission_new (TRUE);
709
710   kfsb->dir = g_file_get_parent (kfsb->file);
711   g_mkdir_with_parents (g_file_peek_path (kfsb->dir), 0700);
712
713   kfsb->file_monitor = g_file_monitor (kfsb->file, G_FILE_MONITOR_NONE, NULL, NULL);
714   kfsb->dir_monitor = g_file_monitor (kfsb->dir, G_FILE_MONITOR_NONE, NULL, NULL);
715
716   compute_checksum (kfsb->digest, NULL, 0);
717
718   g_signal_connect (kfsb->file_monitor, "changed",
719                     G_CALLBACK (file_changed), kfsb);
720   g_signal_connect (kfsb->dir_monitor, "changed",
721                     G_CALLBACK (dir_changed), kfsb);
722
723   g_keyfile_settings_backend_keyfile_writable (kfsb);
724   g_keyfile_settings_backend_keyfile_reload (kfsb);
725
726   load_system_settings (kfsb);
727 }
728
729 static void
730 g_keyfile_settings_backend_set_property (GObject      *object,
731                                          guint         prop_id,
732                                          const GValue *value,
733                                          GParamSpec   *pspec)
734 {
735   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
736
737   switch ((GKeyfileSettingsBackendProperty)prop_id)
738     {
739     case PROP_FILENAME:
740       /* Construct only. */
741       g_assert (kfsb->file == NULL);
742       if (g_value_get_string (value))
743         kfsb->file = g_file_new_for_path (g_value_get_string (value));
744       break;
745
746     case PROP_ROOT_PATH:
747       /* Construct only. */
748       g_assert (kfsb->prefix == NULL);
749       kfsb->prefix = g_value_dup_string (value);
750       if (kfsb->prefix)
751         kfsb->prefix_len = strlen (kfsb->prefix);
752       break;
753
754     case PROP_ROOT_GROUP:
755       /* Construct only. */
756       g_assert (kfsb->root_group == NULL);
757       kfsb->root_group = g_value_dup_string (value);
758       if (kfsb->root_group)
759         kfsb->root_group_len = strlen (kfsb->root_group);
760       break;
761
762     case PROP_DEFAULTS_DIR:
763       /* Construct only. */
764       g_assert (kfsb->defaults_dir == NULL);
765       kfsb->defaults_dir = g_value_dup_string (value);
766       break;
767
768     default:
769       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
770       break;
771     }
772 }
773
774 static void
775 g_keyfile_settings_backend_get_property (GObject    *object,
776                                          guint       prop_id,
777                                          GValue     *value,
778                                          GParamSpec *pspec)
779 {
780   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
781
782   switch ((GKeyfileSettingsBackendProperty)prop_id)
783     {
784     case PROP_FILENAME:
785       g_value_set_string (value, g_file_peek_path (kfsb->file));
786       break;
787
788     case PROP_ROOT_PATH:
789       g_value_set_string (value, kfsb->prefix);
790       break;
791
792     case PROP_ROOT_GROUP:
793       g_value_set_string (value, kfsb->root_group);
794       break;
795
796     case PROP_DEFAULTS_DIR:
797       g_value_set_string (value, kfsb->defaults_dir);
798       break;
799
800     default:
801       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
802       break;
803     }
804 }
805
806 static void
807 g_keyfile_settings_backend_class_init (GKeyfileSettingsBackendClass *class)
808 {
809   GObjectClass *object_class = G_OBJECT_CLASS (class);
810
811   object_class->finalize = g_keyfile_settings_backend_finalize;
812   object_class->constructed = g_keyfile_settings_backend_constructed;
813   object_class->get_property = g_keyfile_settings_backend_get_property;
814   object_class->set_property = g_keyfile_settings_backend_set_property;
815
816   class->read = g_keyfile_settings_backend_read;
817   class->write = g_keyfile_settings_backend_write;
818   class->write_tree = g_keyfile_settings_backend_write_tree;
819   class->reset = g_keyfile_settings_backend_reset;
820   class->get_writable = g_keyfile_settings_backend_get_writable;
821   class->get_permission = g_keyfile_settings_backend_get_permission;
822   /* No need to implement subscribed/unsubscribe: the only point would be to
823    * stop monitoring the file when there's no GSettings anymore, which is no
824    * big win.
825    */
826
827   /**
828    * GKeyfileSettingsBackend:filename:
829    *
830    * The location where the settings are stored on disk.
831    *
832    * Defaults to `$XDG_CONFIG_HOME/glib-2.0/settings/keyfile`.
833    */
834   g_object_class_install_property (object_class,
835                                    PROP_FILENAME,
836                                    g_param_spec_string ("filename",
837                                                         P_("Filename"),
838                                                         P_("The filename"),
839                                                         NULL,
840                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
841                                                         G_PARAM_STATIC_STRINGS));
842
843   /**
844    * GKeyfileSettingsBackend:root-path:
845    *
846    * All settings read to or written from the backend must fall under the
847    * path given in @root_path (which must start and end with a slash and
848    * not contain two consecutive slashes).  @root_path may be "/".
849    * 
850    * Defaults to "/".
851    */
852   g_object_class_install_property (object_class,
853                                    PROP_ROOT_PATH,
854                                    g_param_spec_string ("root-path",
855                                                         P_("Root path"),
856                                                         P_("The root path"),
857                                                         NULL,
858                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
859                                                         G_PARAM_STATIC_STRINGS));
860
861   /**
862    * GKeyfileSettingsBackend:root-group:
863    *
864    * If @root_group is non-%NULL then it specifies the name of the keyfile
865    * group used for keys that are written directly below the root path.
866    *
867    * Defaults to NULL.
868    */
869   g_object_class_install_property (object_class,
870                                    PROP_ROOT_GROUP,
871                                    g_param_spec_string ("root-group",
872                                                         P_("Root group"),
873                                                         P_("The root group"),
874                                                         NULL,
875                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
876                                                         G_PARAM_STATIC_STRINGS));
877
878   /**
879    * GKeyfileSettingsBackend:default-dir:
880    *
881    * The directory where the system defaults and locks are located.
882    *
883    * Defaults to `/etc/glib-2.0/settings`.
884    */
885   g_object_class_install_property (object_class,
886                                    PROP_DEFAULTS_DIR,
887                                    g_param_spec_string ("defaults-dir",
888                                                         P_("Default dir"),
889                                                         P_("Defaults dir"),
890                                                         NULL,
891                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
892                                                         G_PARAM_STATIC_STRINGS));
893 }
894
895 /**
896  * g_keyfile_settings_backend_new:
897  * @filename: the filename of the keyfile
898  * @root_path: the path under which all settings keys appear
899  * @root_group: (nullable): the group name corresponding to
900  *              @root_path, or %NULL
901  *
902  * Creates a keyfile-backed #GSettingsBackend.
903  *
904  * The filename of the keyfile to use is given by @filename.
905  *
906  * All settings read to or written from the backend must fall under the
907  * path given in @root_path (which must start and end with a slash and
908  * not contain two consecutive slashes).  @root_path may be "/".
909  *
910  * If @root_group is non-%NULL then it specifies the name of the keyfile
911  * group used for keys that are written directly below @root_path.  For
912  * example, if @root_path is "/apps/example/" and @root_group is
913  * "toplevel", then settings the key "/apps/example/enabled" to a value
914  * of %TRUE will cause the following to appear in the keyfile:
915  *
916  * |[
917  *   [toplevel]
918  *   enabled=true
919  * ]|
920  *
921  * If @root_group is %NULL then it is not permitted to store keys
922  * directly below the @root_path.
923  *
924  * For keys not stored directly below @root_path (ie: in a sub-path),
925  * the name of the subpath (with the final slash stripped) is used as
926  * the name of the keyfile group.  To continue the example, if
927  * "/apps/example/profiles/default/font-size" were set to
928  * 12 then the following would appear in the keyfile:
929  *
930  * |[
931  *   [profiles/default]
932  *   font-size=12
933  * ]|
934  *
935  * The backend will refuse writes (and return writability as being
936  * %FALSE) for keys outside of @root_path and, in the event that
937  * @root_group is %NULL, also for keys directly under @root_path.
938  * Writes will also be refused if the backend detects that it has the
939  * inability to rewrite the keyfile (ie: the containing directory is not
940  * writable).
941  *
942  * There is no checking done for your key namespace clashing with the
943  * syntax of the key file format.  For example, if you have '[' or ']'
944  * characters in your path names or '=' in your key names you may be in
945  * trouble.
946  *
947  * The backend reads default values from a keyfile called `defaults` in
948  * the directory specified by the #GKeyfileSettingsBackend:defaults-dir property,
949  * and a list of locked keys from a text file with the name `locks` in
950  * the same location.
951  *
952  * Returns: (transfer full): a keyfile-backed #GSettingsBackend
953  **/
954 GSettingsBackend *
955 g_keyfile_settings_backend_new (const gchar *filename,
956                                 const gchar *root_path,
957                                 const gchar *root_group)
958 {
959   g_return_val_if_fail (filename != NULL, NULL);
960   g_return_val_if_fail (root_path != NULL, NULL);
961   g_return_val_if_fail (g_str_has_prefix (root_path, "/"), NULL);
962   g_return_val_if_fail (g_str_has_suffix (root_path, "/"), NULL);
963   g_return_val_if_fail (strstr (root_path, "//") == NULL, NULL);
964
965   return G_SETTINGS_BACKEND (g_object_new (G_TYPE_KEYFILE_SETTINGS_BACKEND,
966                                            "filename", filename,
967                                            "root-path", root_path,
968                                            "root-group", root_group,
969                                            NULL));
970 }