summaryrefslogtreecommitdiff
path: root/service/dconf-writer.c
blob: 4cf800e7d47cbc3d7522675d1004bdf9b033cc1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * Copyright © 2010 Codethink Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "dconf-writer.h"

#include "../shm/dconf-shm.h"

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>

#include "dconf-generated.h"
#include "../common/dconf-changeset.h"
#include "../gvdb/gvdb-builder.h"
#include "../gvdb/gvdb-reader.h"

typedef struct
{
  DConfDBusWriterSkeleton parent_instance;
  gchar *filename;
  gboolean native;
  gchar *name;
  guint64 tag;

  GHashTable *uncommited_values;
  GHashTable *commited_values;

  GQueue uncommited_changes;
  GQueue commited_changes;
} DConfWriter;

typedef struct
{
  DConfChangeset *changeset;
  gchar          *tag;
} TaggedChange;

typedef struct
{
  DConfDBusWriterSkeletonClass parent_instance;

  gboolean (* begin)  (DConfWriter     *writer,
                       GError         **error);
  void     (* change) (DConfWriter     *writer,
                       DConfChangeset  *changeset,
                       const gchar     *tag);
  gboolean (* commit) (DConfWriter     *writer,
                       GError         **error);
  void     (* end)    (DConfWriter     *writer);
} DConfWriterClass;


static void dconf_writer_iface_init (DConfDBusWriterIface *iface);

G_DEFINE_TYPE_WITH_CODE (DConfWriter, dconf_writer, DCONF_DBUS_TYPE_WRITER_SKELETON,
                         G_IMPLEMENT_INTERFACE (DCONF_DBUS_TYPE_WRITER, dconf_writer_iface_init))

static GvdbItem *
dconf_writer_get_parent (GHashTable  *table,
                         const gchar *key)
{
  GvdbItem *grandparent, *parent;
  gchar *parent_name;
  gint len;

  if (g_str_equal (key, "/"))
    return NULL;

  len = strlen (key);
  if (key[len - 1] == '/')
    len--;

  while (key[len - 1] != '/')
    len--;

  parent_name = g_strndup (key, len);
  parent = g_hash_table_lookup (table, parent_name);

  if (parent == NULL)
    {
      parent = gvdb_hash_table_insert (table, parent_name);

      grandparent = dconf_writer_get_parent (table, parent_name);

      if (grandparent != NULL)
        gvdb_item_set_parent (parent, grandparent);
    }

  g_free (parent_name);

  return parent;
}

static GHashTable *
dconf_writer_new_value_table (GHashTable *copy_from)
{
  GHashTable *table;

  table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);

  if (copy_from)
    {
      GHashTableIter iter;
      gpointer key, value;

      g_hash_table_iter_init (&iter, copy_from);
      while (g_hash_table_iter_next (&iter, &key, &value))
        g_hash_table_insert (table, g_strdup (key), g_variant_ref (value));
    }

  return table;
}

static gchar *
dconf_writer_get_tag (DConfWriter *writer)
{
  GDBusConnection *connection;

  connection = g_dbus_interface_skeleton_get_connection (G_DBUS_INTERFACE_SKELETON (writer));

  return g_strdup_printf ("%s:%s:%" G_GUINT64_FORMAT,
                          g_dbus_connection_get_unique_name (connection),
                          writer->name, writer->tag++);
}

static gboolean
dconf_writer_real_begin (DConfWriter  *writer,
                         GError      **error)
{
  /* If this is the first time, populate the value table with the
   * existing values.
   */
  if (writer->commited_values == NULL)
    {
      GError *my_error = NULL;
      GvdbTable *table;
      gchar **names;
      gint n_names;
      gint i;

      table = gvdb_table_new (writer->filename, FALSE, &my_error);

      /* It is perfectly fine if the file does not exist -- then it's
       * just empty.
       */
      if (g_error_matches (my_error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
        g_clear_error (&my_error);

      /* Otherwise, we should report errors to prevent ourselves from
       * overwriting the database in other situations...
       */
      if (my_error)
        {
          g_propagate_prefixed_error (error, my_error, "Cannot open dconf database: ");
          return FALSE;
        }

      /* Only initialise once we know we are in a non-error situation */
      writer->commited_values = dconf_writer_new_value_table (NULL);

      /* Fill the table up with the initial state */
      names = gvdb_table_get_names (table, &n_names);
      for (i = 0; i < n_names; i++)
        {
          if (dconf_is_key (names[i], NULL))
            {
              GVariant *value;

              value = gvdb_table_get_value (table, names[i]);

              if (value != NULL)
                {
                  g_hash_table_insert (writer->commited_values, names[i], value);
                  names[i] = NULL;
                }
            }

          g_free (names[i]);
        }

      gvdb_table_unref (table);
      g_free (names);
    }

  writer->uncommited_values = dconf_writer_new_value_table (writer->commited_values);

  return TRUE;
}

static void
dconf_writer_real_change (DConfWriter    *writer,
                          DConfChangeset *changeset,
                          const gchar    *tag)
{
  const gchar *prefix;
  const gchar * const *keys;
  GVariant * const *values;
  int n_items;
  gint i;

  n_items = dconf_changeset_describe (changeset, &prefix, &keys, &values);

  for (i = 0; i < n_items; i++)
    {
      gchar *path = g_strconcat (prefix, keys[i], NULL);

      /* Check if we are performing a path reset */
      if (g_str_has_suffix (path, "/"))
        {
          GHashTableIter iter;
          gpointer key;

          g_assert (values[i] == NULL);

          /* A path reset is really a request to delete all keys that
           * have a name starting with the reset path.
           */
          g_hash_table_iter_init (&iter, writer->uncommited_values);
          while (g_hash_table_iter_next (&iter, &key, NULL))
            if (g_str_has_prefix (key, path))
              g_hash_table_iter_remove (&iter);
        }

      if (values[i] != NULL)
        g_hash_table_insert (writer->uncommited_values, g_strdup (path), g_variant_ref (values[i]));
      else
        g_hash_table_remove (writer->uncommited_values, path);

      g_free (path);
    }

  if (tag)
    {
      TaggedChange *change;

      change = g_slice_new (TaggedChange);
      change->changeset = dconf_changeset_ref (changeset);
      change->tag = g_strdup (tag);

      g_queue_push_tail (&writer->uncommited_changes, change);
    }
}

static gboolean
dconf_writer_real_commit (DConfWriter  *writer,
                          GError      **error)
{
  GHashTableIter iter;
  gpointer key, value;
  GHashTable *gvdb;
  gboolean success;

  gvdb = gvdb_hash_table_new (NULL, NULL);

  g_hash_table_iter_init (&iter, writer->uncommited_values);
  while (g_hash_table_iter_next (&iter, &key, &value))
    {
      GvdbItem *item;

      g_assert (g_hash_table_lookup (gvdb, key) == NULL);
      item = gvdb_hash_table_insert (gvdb, key);
      gvdb_item_set_parent (item, dconf_writer_get_parent (gvdb, key));
      gvdb_item_set_value (item, value);
    }

  success = gvdb_table_write_contents (gvdb, writer->filename, FALSE, error);

  if (success && writer->native)
    dconf_shm_flag (writer->name);

  if (writer->commited_values)
    g_hash_table_unref (writer->commited_values);
  writer->commited_values = g_hash_table_ref (writer->uncommited_values);

  {
    GQueue empty_queue = G_QUEUE_INIT;

    g_assert (g_queue_is_empty (&writer->commited_changes));
    writer->commited_changes = writer->uncommited_changes;
    writer->uncommited_changes = empty_queue;
  }

  return success;
}

static void
dconf_writer_real_end (DConfWriter *writer)
{
  while (!g_queue_is_empty (&writer->uncommited_changes))
    {
      TaggedChange *change = g_queue_pop_head (&writer->commited_changes);
      g_free (change->tag);
      g_slice_free (TaggedChange, change);
    }

  while (!g_queue_is_empty (&writer->commited_changes))
    {
      TaggedChange *change = g_queue_pop_head (&writer->commited_changes);
      const gchar *prefix;
      const gchar * const *paths;

      dconf_changeset_describe (change->changeset, &prefix, &paths, NULL);
      dconf_dbus_writer_emit_notify_signal (DCONF_DBUS_WRITER (writer), prefix, paths, change->tag);
      dconf_changeset_unref (change->changeset);
      g_free (change->tag);
      g_slice_free (TaggedChange, change);
    }

  g_clear_pointer (&writer->uncommited_values, g_hash_table_unref);
}

gboolean
dconf_writer_begin (DConfWriter  *writer,
                    GError      **error)
{
  return DCONF_WRITER_GET_CLASS (writer)->begin (writer, error);
}

void
dconf_writer_change (DConfWriter    *writer,
                     DConfChangeset *changeset,
                     const gchar    *tag)
{
  DCONF_WRITER_GET_CLASS (writer)->change (writer, changeset, tag);
}

gboolean
dconf_writer_commit (DConfWriter  *writer,
                     GError      **error)
{
  return DCONF_WRITER_GET_CLASS (writer)->commit (writer, error);
}

void
dconf_writer_end (DConfWriter *writer)
{
  return DCONF_WRITER_GET_CLASS (writer)->end (writer);
}

static gboolean
dconf_writer_handle_init (DConfDBusWriter       *dbus_writer,
                          GDBusMethodInvocation *invocation)
{
  DConfWriter *writer = DCONF_WRITER (dbus_writer);
  GError *error = NULL;

  dconf_blame_record (invocation);

  dconf_writer_begin (writer, &error) && dconf_writer_commit (writer, &error);

  if (error)
    {
      g_dbus_method_invocation_return_gerror (invocation, error);
      g_error_free (error);
    }

  else
    g_dbus_method_invocation_return_value (invocation, NULL);

  dconf_writer_end (writer);

  return TRUE;
}

static gboolean
dconf_writer_handle_change (DConfDBusWriter       *dbus_writer,
                            GDBusMethodInvocation *invocation,
                            GVariant              *blob)
{
  DConfWriter *writer = DCONF_WRITER (dbus_writer);
  DConfChangeset *changeset;
  GError *error = NULL;
  GVariant *tmp, *args;
  gchar *tag;

  dconf_blame_record (invocation);

  tmp = g_variant_new_from_data (G_VARIANT_TYPE ("a{smv}"),
                                 g_variant_get_data (blob), g_variant_get_size (blob), FALSE,
                                 (GDestroyNotify) g_variant_unref, g_variant_ref (blob));
  g_variant_ref_sink (tmp);
  args = g_variant_get_normal_form (tmp);
  g_variant_unref (tmp);

  changeset = dconf_changeset_deserialise (args);
  g_variant_unref (args);

  tag = dconf_writer_get_tag (writer);

  if (!dconf_writer_begin (writer, &error))
    goto out;

  dconf_writer_change (writer, changeset, tag);

  if (!dconf_writer_commit (writer, &error))
    goto out;

out:
  if (error)
    {
      g_dbus_method_invocation_return_gerror (invocation, error);
      g_error_free (error);
    }

  else
    g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", tag));

  g_free (tag);

  dconf_writer_end (writer);

  return TRUE;
}

static void
dconf_writer_iface_init (DConfDBusWriterIface *iface)
{
  iface->handle_init = dconf_writer_handle_init;
  iface->handle_change = dconf_writer_handle_change;
}

static void
dconf_writer_init (DConfWriter *writer)
{
  writer->native = TRUE;
}

static void
dconf_writer_set_property (GObject *object, guint prop_id,
                           const GValue *value, GParamSpec *pspec)
{
  DConfWriter *writer = DCONF_WRITER (object);

  g_assert_cmpint (prop_id, ==, 1);

  g_assert (!writer->name);
  writer->name = g_value_dup_string (value);

  if (writer->native)
    writer->filename = g_build_filename (g_get_user_config_dir (), "dconf", writer->name, NULL);
  else
    writer->filename = g_build_filename (g_get_user_runtime_dir (), "dconf", writer->name, NULL);
}

static void
dconf_writer_class_init (DConfWriterClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->set_property = dconf_writer_set_property;

  class->begin = dconf_writer_real_begin;
  class->change = dconf_writer_real_change;
  class->commit = dconf_writer_real_commit;
  class->end = dconf_writer_real_end;

  g_object_class_install_property (object_class, 1,
                                   g_param_spec_string ("name", "name", "name", NULL,
                                                        G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_WRITABLE));
}

const gchar *
dconf_writer_get_name (DConfWriter *writer)
{
  return writer->name;
}

GDBusInterfaceSkeleton *
dconf_writer_new (const gchar *name)
{
  return g_object_new (DCONF_TYPE_WRITER, "name", name, NULL);
}