Coverage Report

Created: 2025-07-01 07:09

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