summaryrefslogtreecommitdiff
path: root/service/dconf-rebuilder.c
blob: 65271cdb99dce19b1dbedcb0b2743a6b3e64d6ec (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
/*
 * 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-rebuilder.h"

#include <string.h>

#include "gvdb-reader.h"
#include "gvdb-builder.h"

typedef struct
{
  const gchar *prefix;
  gint prefix_len;

  GHashTable *table;
  const gchar *const*keys;
  GVariant *const*values;
  gint n_items;
  gint index;

  gchar name[4096];
  gint name_len;
} DConfRebuilderState;

static GvdbItem *
dconf_rebuilder_get_parent (GHashTable *table,
                            gchar      *key,
                            gint        length)
{
  GvdbItem *grandparent, *parent;

  if (length == 1)
    return NULL;

  while (key[--length - 1] != '/');
  key[length] = '\0';

  parent = g_hash_table_lookup (table, key);

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

      grandparent = dconf_rebuilder_get_parent (table, key, length);

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

  return parent;
}

static void
dconf_rebuilder_insert (GHashTable  *table,
                        const gchar *key,
                        GVariant    *value)
{
  GvdbItem *item;
  gchar *mykey;
  gint length;

  length = strlen (key);
  mykey = g_alloca (length);
  memcpy (mykey, key, length);

  g_assert (g_hash_table_lookup (table, key) == NULL);
  item = gvdb_hash_table_insert (table, key);

  gvdb_item_set_parent (item,
                        dconf_rebuilder_get_parent (table, mykey, length));

  gvdb_item_set_value (item, value);
}

static void
dconf_rebuilder_put_item (DConfRebuilderState *state)
{
  if (state->values[state->index] != NULL)
    {
      gchar *fullname;

      fullname = g_strconcat (state->prefix, state->keys[state->index], NULL);
      dconf_rebuilder_insert (state->table, fullname, state->values[state->index]);
      g_free (fullname);
    }

  state->index++;
}

static gboolean
dconf_rebuilder_walk_name (DConfRebuilderState *state,
                           const gchar         *name,
                           gsize                name_len)
{
  gint cmp;

  g_assert (state->name_len + name_len < sizeof state->name - 1);
  memcpy (state->name + state->name_len, name, name_len);
  state->name[state->name_len + name_len] = '\0';

  if (state->index == state->n_items)
    return TRUE;

  if (state->name_len + name_len < state->prefix_len ||
      memcmp (state->name, state->prefix, state->prefix_len) != 0)
    return TRUE;

  while ((cmp = strcmp (state->name + state->prefix_len,
                        state->keys[state->index])) > 0)
    {
      dconf_rebuilder_put_item (state);

      if (state->index == state->n_items)
        return TRUE;
    }

  return cmp != 0;
}

static void
dconf_rebuilder_walk_value (const gchar *name,
                            gsize        name_len,
                            GVariant    *value,
                            gpointer     user_data)
{
  DConfRebuilderState *state = user_data;

  if (dconf_rebuilder_walk_name (state, name, name_len))
    dconf_rebuilder_insert (state->table, state->name, value);

  else
    dconf_rebuilder_put_item (state);
}

static gboolean
dconf_rebuilder_walk_open (const gchar *name,
                           gsize        name_len,
                           gpointer     user_data)
{
  DConfRebuilderState *state = user_data;

  if (dconf_rebuilder_walk_name (state, name, name_len))
    {
      state->name_len += name_len;
      return TRUE;
    }

  return FALSE;
}

static void
dconf_rebuilder_walk_close (gsize    name_len,
                            gpointer user_data)
{
  DConfRebuilderState *state = user_data;

  state->name_len -= name_len;
}

gboolean
dconf_rebuilder_rebuild (const gchar  *filename,
                         const gchar  *prefix,
                         const gchar *const*keys,
                         GVariant    *const*values,
                         int           n_items,
                         GError      **error)
{
  DConfRebuilderState state = { prefix, strlen (prefix),
                                0, keys, values, n_items };
  gboolean success;
  GvdbTable *old;

  state.table = gvdb_hash_table_new (NULL, NULL);

  if ((old = gvdb_table_new (filename, FALSE, NULL)))
    {
      gvdb_table_walk (old, "/",
                       dconf_rebuilder_walk_open,
                       dconf_rebuilder_walk_value,
                       dconf_rebuilder_walk_close,
                       &state);
      gvdb_table_unref (old);
    }

  while (state.index != state.n_items)
    dconf_rebuilder_put_item (&state);

  success = gvdb_table_write_contents (state.table, filename, FALSE, error);
  g_hash_table_unref (state.table);

  return success;
}