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