Imported Upstream version 2.61.2
[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 "giomodule-priv.h"
37 #include "gportalsupport.h"
38
39
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))
46
47
48 typedef GSettingsBackendClass GKeyfileSettingsBackendClass;
49
50 typedef enum {
51   PROP_FILENAME = 1,
52   PROP_ROOT_PATH,
53   PROP_ROOT_GROUP,
54   PROP_DEFAULTS_DIR
55 } GKeyfileSettingsBackendProperty;
56
57 typedef struct
58 {
59   GSettingsBackend   parent_instance;
60
61   GKeyFile          *keyfile;
62   GPermission       *permission;
63   gboolean           writable;
64   char              *defaults_dir;
65   GKeyFile          *system_keyfile;
66   GHashTable        *system_locks; /* Used as a set, owning the strings it contains */
67
68   gchar             *prefix;
69   gint               prefix_len;
70   gchar             *root_group;
71   gint               root_group_len;
72
73   GFile             *file;
74   GFileMonitor      *file_monitor;
75   guint8             digest[32];
76   GFile             *dir;
77   GFileMonitor      *dir_monitor;
78 } GKeyfileSettingsBackend;
79
80 #ifdef G_OS_WIN32
81 #define EXTENSION_PRIORITY 10
82 #else
83 #define EXTENSION_PRIORITY (glib_should_use_portal () && !glib_has_dconf_access_in_sandbox () ? 110 : 10)
84 #endif
85
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))
92
93 static void
94 compute_checksum (guint8        *digest,
95                   gconstpointer  contents,
96                   gsize          length)
97 {
98   GChecksum *checksum;
99   gsize len = 32;
100
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);
106 }
107
108 static void
109 g_keyfile_settings_backend_keyfile_write (GKeyfileSettingsBackend *kfsb)
110 {
111   gchar *contents;
112   gsize length;
113
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,
118                            NULL, NULL, NULL);
119
120   compute_checksum (kfsb->digest, contents, length);
121   g_free (contents);
122 }
123
124 static gboolean
125 group_name_matches (const gchar *group_name,
126                     const gchar *prefix)
127 {
128   /* sort of like g_str_has_prefix() except that it must be an exact
129    * match or the prefix followed by '/'.
130    *
131    * for example 'a' is a prefix of 'a' and 'a/b' but not 'ab'.
132    */
133   gint i;
134
135   for (i = 0; prefix[i]; i++)
136     if (prefix[i] != group_name[i])
137       return FALSE;
138
139   return group_name[i] == '\0' || group_name[i] == '/';
140 }
141
142 static gboolean
143 convert_path (GKeyfileSettingsBackend  *kfsb,
144               const gchar              *key,
145               gchar                   **group,
146               gchar                   **basename)
147 {
148   gint key_len = strlen (key);
149   gint i;
150
151   if (key_len < kfsb->prefix_len ||
152       memcmp (key, kfsb->prefix, kfsb->prefix_len) != 0)
153     return FALSE;
154
155   key_len -= kfsb->prefix_len;
156   key += kfsb->prefix_len;
157
158   for (i = key_len; i >= 0; i--)
159     if (key[i] == '/')
160       break;
161
162   if (kfsb->root_group)
163     {
164       /* if a root_group was specified, make sure the user hasn't given
165        * a path that ghosts that group name
166        */
167       if (i == kfsb->root_group_len && memcmp (key, kfsb->root_group, i) == 0)
168         return FALSE;
169     }
170   else
171     {
172       /* if no root_group was given, ensure that the user gave a path */
173       if (i == -1)
174         return FALSE;
175     }
176
177   if (group)
178     {
179       if (i >= 0)
180         {
181           *group = g_memdup (key, i + 1);
182           (*group)[i] = '\0';
183         }
184       else
185         *group = g_strdup (kfsb->root_group);
186     }
187
188   if (basename)
189     *basename = g_memdup (key + i + 1, key_len - i);
190
191   return TRUE;
192 }
193
194 static gboolean
195 path_is_valid (GKeyfileSettingsBackend *kfsb,
196                const gchar             *path)
197 {
198   return convert_path (kfsb, path, NULL, NULL);
199 }
200
201 static GVariant *
202 get_from_keyfile (GKeyfileSettingsBackend *kfsb,
203                   const GVariantType      *type,
204                   const gchar             *key)
205 {
206   GVariant *return_value = NULL;
207   gchar *group, *name;
208
209   if (convert_path (kfsb, key, &group, &name))
210     {
211       gchar *str;
212       gchar *sysstr;
213
214       g_assert (*name);
215
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);
218       if (sysstr &&
219           (g_hash_table_contains (kfsb->system_locks, key) ||
220            str == NULL))
221         {
222           g_free (str);
223           str = g_steal_pointer (&sysstr);
224         }
225
226       if (str)
227         {
228           return_value = g_variant_parse (type, str, NULL, NULL, NULL);
229
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) &&
235               str[0] != '\"')
236             {
237               GString *s = g_string_sized_new (strlen (str) + 2);
238               char *p = str;
239
240               g_string_append_c (s, '\"');
241               while (*p)
242                 {
243                   if (*p == '\"')
244                     g_string_append_c (s, '\\');
245                   g_string_append_c (s, *p);
246                   p++;
247                 }
248               g_string_append_c (s, '\"');
249               return_value = g_variant_parse (type, s->str, NULL, NULL, NULL);
250               g_string_free (s, TRUE);
251             }
252           g_free (str);
253         }
254
255       g_free (sysstr);
256
257       g_free (group);
258       g_free (name);
259     }
260
261   return return_value;
262 }
263
264 static gboolean
265 set_to_keyfile (GKeyfileSettingsBackend *kfsb,
266                 const gchar             *key,
267                 GVariant                *value)
268 {
269   gchar *group, *name;
270
271   if (g_hash_table_contains (kfsb->system_locks, key))
272     return FALSE;
273
274   if (convert_path (kfsb, key, &group, &name))
275     {
276       if (value)
277         {
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));
281           g_free (str);
282         }
283       else
284         {
285           if (*name == '\0')
286             {
287               gchar **groups;
288               gint i;
289
290               groups = g_key_file_get_groups (kfsb->keyfile, NULL);
291
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);
295
296               g_strfreev (groups);
297             }
298           else
299             g_key_file_remove_key (kfsb->keyfile, group, name, NULL);
300         }
301
302       g_free (group);
303       g_free (name);
304
305       return TRUE;
306     }
307
308   return FALSE;
309 }
310
311 static GVariant *
312 g_keyfile_settings_backend_read (GSettingsBackend   *backend,
313                                  const gchar        *key,
314                                  const GVariantType *expected_type,
315                                  gboolean            default_value)
316 {
317   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
318
319   if (default_value)
320     return NULL;
321
322   return get_from_keyfile (kfsb, expected_type, key);
323 }
324
325 typedef struct
326 {
327   GKeyfileSettingsBackend *kfsb;
328   gboolean failed;
329 } WriteManyData;
330
331 static gboolean
332 g_keyfile_settings_backend_write_one (gpointer key,
333                                       gpointer value,
334                                       gpointer user_data)
335 {
336   WriteManyData *data = user_data;
337   gboolean success G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;
338
339   success = set_to_keyfile (data->kfsb, key, value);
340   g_assert (success);
341
342   return FALSE;
343 }
344
345 static gboolean
346 g_keyfile_settings_backend_check_one (gpointer key,
347                                       gpointer value,
348                                       gpointer user_data)
349 {
350   WriteManyData *data = user_data;
351
352   return data->failed = g_hash_table_contains (data->kfsb->system_locks, key) ||
353                         !path_is_valid (data->kfsb, key);
354 }
355
356 static gboolean
357 g_keyfile_settings_backend_write_tree (GSettingsBackend *backend,
358                                        GTree            *tree,
359                                        gpointer          origin_tag)
360 {
361   WriteManyData data = { G_KEYFILE_SETTINGS_BACKEND (backend) };
362
363   if (!data.kfsb->writable)
364     return FALSE;
365
366   g_tree_foreach (tree, g_keyfile_settings_backend_check_one, &data);
367
368   if (data.failed)
369     return FALSE;
370
371   g_tree_foreach (tree, g_keyfile_settings_backend_write_one, &data);
372   g_keyfile_settings_backend_keyfile_write (data.kfsb);
373
374   g_settings_backend_changed_tree (backend, tree, origin_tag);
375
376   return TRUE;
377 }
378
379 static gboolean
380 g_keyfile_settings_backend_write (GSettingsBackend *backend,
381                                   const gchar      *key,
382                                   GVariant         *value,
383                                   gpointer          origin_tag)
384 {
385   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
386   gboolean success;
387
388   if (!kfsb->writable)
389     return FALSE;
390
391   success = set_to_keyfile (kfsb, key, value);
392
393   if (success)
394     {
395       g_settings_backend_changed (backend, key, origin_tag);
396       g_keyfile_settings_backend_keyfile_write (kfsb);
397     }
398
399   return success;
400 }
401
402 static void
403 g_keyfile_settings_backend_reset (GSettingsBackend *backend,
404                                   const gchar      *key,
405                                   gpointer          origin_tag)
406 {
407   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
408
409   if (set_to_keyfile (kfsb, key, NULL))
410     g_keyfile_settings_backend_keyfile_write (kfsb);
411
412   g_settings_backend_changed (backend, key, origin_tag);
413 }
414
415 static gboolean
416 g_keyfile_settings_backend_get_writable (GSettingsBackend *backend,
417                                          const gchar      *name)
418 {
419   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
420
421   return kfsb->writable &&
422          !g_hash_table_contains (kfsb->system_locks, name) &&
423          path_is_valid (kfsb, name);
424 }
425
426 static GPermission *
427 g_keyfile_settings_backend_get_permission (GSettingsBackend *backend,
428                                            const gchar      *path)
429 {
430   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
431
432   return g_object_ref (kfsb->permission);
433 }
434
435 static void
436 keyfile_to_tree (GKeyfileSettingsBackend *kfsb,
437                  GTree                   *tree,
438                  GKeyFile                *keyfile,
439                  gboolean                 dup_check)
440 {
441   gchar **groups;
442   gint i;
443
444   groups = g_key_file_get_groups (keyfile, NULL);
445   for (i = 0; groups[i]; i++)
446     {
447       gboolean is_root_group;
448       gchar **keys;
449       gint j;
450
451       is_root_group = g_strcmp0 (kfsb->root_group, groups[i]) == 0;
452
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], "//")))
457         continue;
458
459       keys = g_key_file_get_keys (keyfile, groups[i], NULL, NULL);
460       g_assert (keys != NULL);
461
462       for (j = 0; keys[j]; j++)
463         {
464           gchar *path, *value;
465
466           /* reject key names with slashes in them */
467           if (strchr (keys[j], '/'))
468             continue;
469
470           if (is_root_group)
471             path = g_strdup_printf ("%s%s", kfsb->prefix, keys[j]);
472           else
473             path = g_strdup_printf ("%s%s/%s", kfsb->prefix, groups[i], keys[j]);
474
475           value = g_key_file_get_value (keyfile, groups[i], keys[j], NULL);
476
477           if (dup_check && g_strcmp0 (g_tree_lookup (tree, path), value) == 0)
478             {
479               g_tree_remove (tree, path);
480               g_free (value);
481               g_free (path);
482             }
483           else
484             g_tree_insert (tree, path, value);
485         }
486
487       g_strfreev (keys);
488     }
489   g_strfreev (groups);
490 }
491
492 static void
493 g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
494 {
495   guint8 digest[32];
496   gchar *contents;
497   gsize length;
498
499   contents = NULL;
500   length = 0;
501
502   g_file_load_contents (kfsb->file, NULL, &contents, &length, NULL, NULL);
503   compute_checksum (digest, contents, length);
504
505   if (memcmp (kfsb->digest, digest, sizeof digest) != 0)
506     {
507       GKeyFile *keyfiles[2];
508       GTree *tree;
509
510       tree = g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
511                               g_free, g_free);
512
513       keyfiles[0] = kfsb->keyfile;
514       keyfiles[1] = g_key_file_new ();
515
516       if (length > 0)
517         g_key_file_load_from_data (keyfiles[1], contents, length,
518                                    G_KEY_FILE_KEEP_COMMENTS |
519                                    G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
520
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];
525
526       if (g_tree_nnodes (tree) > 0)
527         g_settings_backend_changed_tree (&kfsb->parent_instance, tree, NULL);
528
529       g_tree_unref (tree);
530
531       memcpy (kfsb->digest, digest, sizeof digest);
532     }
533
534   g_free (contents);
535 }
536
537 static void
538 g_keyfile_settings_backend_keyfile_writable (GKeyfileSettingsBackend *kfsb)
539 {
540   GFileInfo *fileinfo;
541   gboolean writable;
542
543   fileinfo = g_file_query_info (kfsb->dir, "access::*", 0, NULL, NULL);
544
545   if (fileinfo)
546     {
547       writable =
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);
551     }
552   else
553     writable = FALSE;
554
555   if (writable != kfsb->writable)
556     {
557       kfsb->writable = writable;
558       g_settings_backend_path_writable_changed (&kfsb->parent_instance, "/");
559     }
560 }
561
562 static void
563 g_keyfile_settings_backend_finalize (GObject *object)
564 {
565   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
566
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);
572
573   g_file_monitor_cancel (kfsb->file_monitor);
574   g_object_unref (kfsb->file_monitor);
575   g_object_unref (kfsb->file);
576
577   g_file_monitor_cancel (kfsb->dir_monitor);
578   g_object_unref (kfsb->dir_monitor);
579   g_object_unref (kfsb->dir);
580
581   g_free (kfsb->root_group);
582   g_free (kfsb->prefix);
583
584   G_OBJECT_CLASS (g_keyfile_settings_backend_parent_class)
585     ->finalize (object);
586 }
587
588 static void
589 g_keyfile_settings_backend_init (GKeyfileSettingsBackend *kfsb)
590 {
591 }
592
593 static void
594 file_changed (GFileMonitor      *monitor,
595               GFile             *file,
596               GFile             *other_file,
597               GFileMonitorEvent  event_type,
598               gpointer           user_data)
599 {
600   GKeyfileSettingsBackend *kfsb = user_data;
601
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);
605 }
606
607 static void
608 dir_changed (GFileMonitor       *monitor,
609               GFile             *file,
610               GFile             *other_file,
611               GFileMonitorEvent  event_type,
612               gpointer           user_data)
613 {
614   GKeyfileSettingsBackend *kfsb = user_data;
615
616   g_keyfile_settings_backend_keyfile_writable (kfsb);
617 }
618
619 static void
620 load_system_settings (GKeyfileSettingsBackend *kfsb)
621 {
622   GError *error = NULL;
623   const char *dir = "/etc/glib-2.0/settings";
624   char *path;
625   char *contents;
626
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);
629
630   if (kfsb->defaults_dir)
631     dir = kfsb->defaults_dir;
632
633   path = g_build_filename (dir, "defaults", NULL);
634
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
637    */
638   if (!g_key_file_load_from_file (kfsb->system_keyfile, path, G_KEY_FILE_NONE, &error))
639     {
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);
643     }
644   else
645     g_debug ("Loading default settings from %s", path);
646
647   g_free (path);
648
649   path = g_build_filename (dir, "locks", NULL);
650
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
653    */
654   if (!g_file_get_contents (path, &contents, NULL, &error))
655     {
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);
659     }
660   else
661     {
662       char **lines;
663       gsize i;
664
665       g_debug ("Loading locks from %s", path);
666
667       lines = g_strsplit (contents, "\n", 0);
668       for (i = 0; lines[i]; i++)
669         {
670           char *line = lines[i];
671           if (line[0] == '#' || line[0] == '\0')
672             {
673               g_free (line);
674               continue;
675             }
676
677           g_debug ("Locking key %s", line);
678           g_hash_table_add (kfsb->system_locks, g_steal_pointer (&line));
679         }
680
681       g_free (lines);
682     }
683   g_free (contents);
684
685   g_free (path);
686 }
687
688 static void
689 g_keyfile_settings_backend_constructed (GObject *object)
690 {
691   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
692
693   if (kfsb->file == NULL)
694     {
695       char *filename = g_build_filename (g_get_user_config_dir (),
696                                          "glib-2.0", "settings", "keyfile",
697                                          NULL);
698       kfsb->file = g_file_new_for_path (filename);
699       g_free (filename);
700     }
701
702   if (kfsb->prefix == NULL)
703     {
704       kfsb->prefix = g_strdup ("/");
705       kfsb->prefix_len = 1;
706     }
707   
708   kfsb->keyfile = g_key_file_new ();
709   kfsb->permission = g_simple_permission_new (TRUE);
710
711   kfsb->dir = g_file_get_parent (kfsb->file);
712   g_mkdir_with_parents (g_file_peek_path (kfsb->dir), 0700);
713
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);
716
717   compute_checksum (kfsb->digest, NULL, 0);
718
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);
723
724   g_keyfile_settings_backend_keyfile_writable (kfsb);
725   g_keyfile_settings_backend_keyfile_reload (kfsb);
726
727   load_system_settings (kfsb);
728 }
729
730 static void
731 g_keyfile_settings_backend_set_property (GObject      *object,
732                                          guint         prop_id,
733                                          const GValue *value,
734                                          GParamSpec   *pspec)
735 {
736   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
737
738   switch ((GKeyfileSettingsBackendProperty)prop_id)
739     {
740     case PROP_FILENAME:
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));
745       break;
746
747     case PROP_ROOT_PATH:
748       /* Construct only. */
749       g_assert (kfsb->prefix == NULL);
750       kfsb->prefix = g_value_dup_string (value);
751       if (kfsb->prefix)
752         kfsb->prefix_len = strlen (kfsb->prefix);
753       break;
754
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);
761       break;
762
763     case PROP_DEFAULTS_DIR:
764       /* Construct only. */
765       g_assert (kfsb->defaults_dir == NULL);
766       kfsb->defaults_dir = g_value_dup_string (value);
767       break;
768
769     default:
770       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
771       break;
772     }
773 }
774
775 static void
776 g_keyfile_settings_backend_get_property (GObject    *object,
777                                          guint       prop_id,
778                                          GValue     *value,
779                                          GParamSpec *pspec)
780 {
781   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
782
783   switch ((GKeyfileSettingsBackendProperty)prop_id)
784     {
785     case PROP_FILENAME:
786       g_value_set_string (value, g_file_peek_path (kfsb->file));
787       break;
788
789     case PROP_ROOT_PATH:
790       g_value_set_string (value, kfsb->prefix);
791       break;
792
793     case PROP_ROOT_GROUP:
794       g_value_set_string (value, kfsb->root_group);
795       break;
796
797     case PROP_DEFAULTS_DIR:
798       g_value_set_string (value, kfsb->defaults_dir);
799       break;
800
801     default:
802       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
803       break;
804     }
805 }
806
807 static void
808 g_keyfile_settings_backend_class_init (GKeyfileSettingsBackendClass *class)
809 {
810   GObjectClass *object_class = G_OBJECT_CLASS (class);
811
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;
816
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
825    * big win.
826    */
827
828   /**
829    * GKeyfileSettingsBackend:filename:
830    *
831    * The location where the settings are stored on disk.
832    *
833    * Defaults to `$XDG_CONFIG_HOME/glib-2.0/settings/keyfile`.
834    */
835   g_object_class_install_property (object_class,
836                                    PROP_FILENAME,
837                                    g_param_spec_string ("filename",
838                                                         P_("Filename"),
839                                                         P_("The filename"),
840                                                         NULL,
841                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
842                                                         G_PARAM_STATIC_STRINGS));
843
844   /**
845    * GKeyfileSettingsBackend:root-path:
846    *
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 "/".
850    * 
851    * Defaults to "/".
852    */
853   g_object_class_install_property (object_class,
854                                    PROP_ROOT_PATH,
855                                    g_param_spec_string ("root-path",
856                                                         P_("Root path"),
857                                                         P_("The root path"),
858                                                         NULL,
859                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
860                                                         G_PARAM_STATIC_STRINGS));
861
862   /**
863    * GKeyfileSettingsBackend:root-group:
864    *
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.
867    *
868    * Defaults to NULL.
869    */
870   g_object_class_install_property (object_class,
871                                    PROP_ROOT_GROUP,
872                                    g_param_spec_string ("root-group",
873                                                         P_("Root group"),
874                                                         P_("The root group"),
875                                                         NULL,
876                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
877                                                         G_PARAM_STATIC_STRINGS));
878
879   /**
880    * GKeyfileSettingsBackend:default-dir:
881    *
882    * The directory where the system defaults and locks are located.
883    *
884    * Defaults to `/etc/glib-2.0/settings`.
885    */
886   g_object_class_install_property (object_class,
887                                    PROP_DEFAULTS_DIR,
888                                    g_param_spec_string ("defaults-dir",
889                                                         P_("Default dir"),
890                                                         P_("Defaults dir"),
891                                                         NULL,
892                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
893                                                         G_PARAM_STATIC_STRINGS));
894 }
895
896 /**
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
902  *
903  * Creates a keyfile-backed #GSettingsBackend.
904  *
905  * The filename of the keyfile to use is given by @filename.
906  *
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 "/".
910  *
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:
916  *
917  * |[
918  *   [toplevel]
919  *   enabled=true
920  * ]|
921  *
922  * If @root_group is %NULL then it is not permitted to store keys
923  * directly below the @root_path.
924  *
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:
930  *
931  * |[
932  *   [profiles/default]
933  *   font-size=12
934  * ]|
935  *
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
941  * writable).
942  *
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
946  * trouble.
947  *
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
951  * the same location.
952  *
953  * Returns: (transfer full): a keyfile-backed #GSettingsBackend
954  **/
955 GSettingsBackend *
956 g_keyfile_settings_backend_new (const gchar *filename,
957                                 const gchar *root_path,
958                                 const gchar *root_group)
959 {
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);
965
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,
970                                            NULL));
971 }