Replace all hard-coded /etc path with sysconfdir
[dconf.git] / gvdb / gvdb-builder.c
blob2383e6002916b06ef5be376722df43b49f4b238d
1 /*
2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 #include "gvdb-builder.h"
21 #include "gvdb-format.h"
23 #include <glib.h>
24 #include <fcntl.h>
25 #if !defined(G_OS_WIN32) || !defined(_MSC_VER)
26 #include <unistd.h>
27 #endif
28 #include <string.h>
31 struct _GvdbItem
33 gchar *key;
34 guint32 hash_value;
35 guint32_le assigned_index;
36 GvdbItem *parent;
37 GvdbItem *sibling;
38 GvdbItem *next;
40 /* one of:
41 * this:
43 GVariant *value;
45 /* this: */
46 GHashTable *table;
48 /* or this: */
49 GvdbItem *child;
52 static void
53 gvdb_item_free (gpointer data)
55 GvdbItem *item = data;
57 g_free (item->key);
59 if (item->value)
60 g_variant_unref (item->value);
62 if (item->table)
63 g_hash_table_unref (item->table);
65 g_slice_free (GvdbItem, item);
68 GHashTable *
69 gvdb_hash_table_new (GHashTable *parent,
70 const gchar *name_in_parent)
72 GHashTable *table;
74 table = g_hash_table_new_full (g_str_hash, g_str_equal,
75 g_free, gvdb_item_free);
77 if (parent)
79 GvdbItem *item;
81 item = gvdb_hash_table_insert (parent, name_in_parent);
82 gvdb_item_set_hash_table (item, table);
85 return table;
88 static guint32
89 djb_hash (const gchar *key)
91 guint32 hash_value = 5381;
93 while (*key)
94 hash_value = hash_value * 33 + *(signed char *)key++;
96 return hash_value;
99 GvdbItem *
100 gvdb_hash_table_insert (GHashTable *table,
101 const gchar *key)
103 GvdbItem *item;
105 item = g_slice_new0 (GvdbItem);
106 item->key = g_strdup (key);
107 item->hash_value = djb_hash (key);
109 g_hash_table_insert (table, g_strdup (key), item);
111 return item;
114 void
115 gvdb_hash_table_insert_string (GHashTable *table,
116 const gchar *key,
117 const gchar *value)
119 GvdbItem *item;
121 item = gvdb_hash_table_insert (table, key);
122 gvdb_item_set_value (item, g_variant_new_string (value));
125 void
126 gvdb_item_set_value (GvdbItem *item,
127 GVariant *value)
129 g_return_if_fail (!item->value && !item->table && !item->child);
131 item->value = g_variant_ref_sink (value);
134 void
135 gvdb_item_set_hash_table (GvdbItem *item,
136 GHashTable *table)
138 g_return_if_fail (!item->value && !item->table && !item->child);
140 item->table = g_hash_table_ref (table);
143 void
144 gvdb_item_set_parent (GvdbItem *item,
145 GvdbItem *parent)
147 GvdbItem **node;
149 g_return_if_fail (g_str_has_prefix (item->key, parent->key));
150 g_return_if_fail (!parent->value && !parent->table);
151 g_return_if_fail (!item->parent && !item->sibling);
153 for (node = &parent->child; *node; node = &(*node)->sibling)
154 if (strcmp ((*node)->key, item->key) > 0)
155 break;
157 item->parent = parent;
158 item->sibling = *node;
159 *node = item;
162 typedef struct
164 GvdbItem **buckets;
165 gint n_buckets;
166 } HashTable;
168 static HashTable *
169 hash_table_new (gint n_buckets)
171 HashTable *table;
173 table = g_slice_new (HashTable);
174 table->buckets = g_new0 (GvdbItem *, n_buckets);
175 table->n_buckets = n_buckets;
177 return table;
180 static void
181 hash_table_free (HashTable *table)
183 g_free (table->buckets);
185 g_slice_free (HashTable, table);
188 static void
189 hash_table_insert (gpointer key,
190 gpointer value,
191 gpointer data)
193 guint32 hash_value, bucket;
194 HashTable *table = data;
195 GvdbItem *item = value;
197 hash_value = djb_hash (key);
198 bucket = hash_value % table->n_buckets;
199 item->next = table->buckets[bucket];
200 table->buckets[bucket] = item;
203 static guint32_le
204 item_to_index (GvdbItem *item)
206 if (item != NULL)
207 return item->assigned_index;
209 return guint32_to_le (-1u);
212 typedef struct
214 GQueue *chunks;
215 guint64 offset;
216 gboolean byteswap;
217 } FileBuilder;
219 typedef struct
221 gsize offset;
222 gsize size;
223 gpointer data;
224 } FileChunk;
226 static gpointer
227 file_builder_allocate (FileBuilder *fb,
228 guint alignment,
229 gsize size,
230 struct gvdb_pointer *pointer)
232 FileChunk *chunk;
234 if (size == 0)
235 return NULL;
237 fb->offset += (-fb->offset) & (alignment - 1);
238 chunk = g_slice_new (FileChunk);
239 chunk->offset = fb->offset;
240 chunk->size = size;
241 chunk->data = g_malloc (size);
243 pointer->start = guint32_to_le (fb->offset);
244 fb->offset += size;
245 pointer->end = guint32_to_le (fb->offset);
247 g_queue_push_tail (fb->chunks, chunk);
249 return chunk->data;
252 static void
253 file_builder_add_value (FileBuilder *fb,
254 GVariant *value,
255 struct gvdb_pointer *pointer)
257 GVariant *variant, *normal;
258 gpointer data;
259 gsize size;
261 if (fb->byteswap)
263 value = g_variant_byteswap (value);
264 variant = g_variant_new_variant (value);
265 g_variant_unref (value);
267 else
268 variant = g_variant_new_variant (value);
270 normal = g_variant_get_normal_form (variant);
271 g_variant_unref (variant);
273 size = g_variant_get_size (normal);
274 data = file_builder_allocate (fb, 8, size, pointer);
275 g_variant_store (normal, data);
276 g_variant_unref (normal);
279 static void
280 file_builder_add_string (FileBuilder *fb,
281 const gchar *string,
282 guint32_le *start,
283 guint16_le *size)
285 FileChunk *chunk;
286 gsize length;
288 length = strlen (string);
290 chunk = g_slice_new (FileChunk);
291 chunk->offset = fb->offset;
292 chunk->size = length;
293 chunk->data = g_malloc (length);
294 if (length != 0)
295 memcpy (chunk->data, string, length);
297 *start = guint32_to_le (fb->offset);
298 *size = guint16_to_le (length);
299 fb->offset += length;
301 g_queue_push_tail (fb->chunks, chunk);
304 static void
305 file_builder_allocate_for_hash (FileBuilder *fb,
306 gsize n_buckets,
307 gsize n_items,
308 guint bloom_shift,
309 gsize n_bloom_words,
310 guint32_le **bloom_filter,
311 guint32_le **hash_buckets,
312 struct gvdb_hash_item **hash_items,
313 struct gvdb_pointer *pointer)
315 guint32_le bloom_hdr, table_hdr;
316 guchar *data;
317 gsize size;
319 g_assert (n_bloom_words < (1u << 27));
321 bloom_hdr = guint32_to_le (bloom_shift << 27 | n_bloom_words);
322 table_hdr = guint32_to_le (n_buckets);
324 size = sizeof bloom_hdr + sizeof table_hdr +
325 n_bloom_words * sizeof (guint32_le) +
326 n_buckets * sizeof (guint32_le) +
327 n_items * sizeof (struct gvdb_hash_item);
329 data = file_builder_allocate (fb, 4, size, pointer);
331 #define chunk(s) (size -= (s), data += (s), data - (s))
332 memcpy (chunk (sizeof bloom_hdr), &bloom_hdr, sizeof bloom_hdr);
333 memcpy (chunk (sizeof table_hdr), &table_hdr, sizeof table_hdr);
334 *bloom_filter = (guint32_le *) chunk (n_bloom_words * sizeof (guint32_le));
335 *hash_buckets = (guint32_le *) chunk (n_buckets * sizeof (guint32_le));
336 *hash_items = (struct gvdb_hash_item *) chunk (n_items *
337 sizeof (struct gvdb_hash_item));
338 g_assert (size == 0);
339 #undef chunk
341 memset (*bloom_filter, 0, n_bloom_words * sizeof (guint32_le));
343 /* NOTE - the code to actually fill in the bloom filter here is missing.
344 * Patches welcome!
346 * http://en.wikipedia.org/wiki/Bloom_filter
347 * http://0pointer.de/blog/projects/bloom.html
351 static void
352 file_builder_add_hash (FileBuilder *fb,
353 GHashTable *table,
354 struct gvdb_pointer *pointer)
356 guint32_le *buckets, *bloom_filter;
357 struct gvdb_hash_item *items;
358 HashTable *mytable;
359 GvdbItem *item;
360 guint32 index;
361 gint bucket;
363 mytable = hash_table_new (g_hash_table_size (table));
364 g_hash_table_foreach (table, hash_table_insert, mytable);
365 index = 0;
367 for (bucket = 0; bucket < mytable->n_buckets; bucket++)
368 for (item = mytable->buckets[bucket]; item; item = item->next)
369 item->assigned_index = guint32_to_le (index++);
371 file_builder_allocate_for_hash (fb, mytable->n_buckets, index, 5, 0,
372 &bloom_filter, &buckets, &items, pointer);
374 index = 0;
375 for (bucket = 0; bucket < mytable->n_buckets; bucket++)
377 buckets[bucket] = guint32_to_le (index);
379 for (item = mytable->buckets[bucket]; item; item = item->next)
381 struct gvdb_hash_item *entry = items++;
382 const gchar *basename;
384 g_assert (index == guint32_from_le (item->assigned_index));
385 entry->hash_value = guint32_to_le (item->hash_value);
386 entry->parent = item_to_index (item->parent);
387 entry->unused = 0;
389 if (item->parent != NULL)
390 basename = item->key + strlen (item->parent->key);
391 else
392 basename = item->key;
394 file_builder_add_string (fb, basename,
395 &entry->key_start,
396 &entry->key_size);
398 if (item->value != NULL)
400 g_assert (item->child == NULL && item->table == NULL);
402 file_builder_add_value (fb, item->value, &entry->value.pointer);
403 entry->type = 'v';
406 if (item->child != NULL)
408 guint32 children = 0, i = 0;
409 guint32_le *offsets;
410 GvdbItem *child;
412 g_assert (item->table == NULL);
414 for (child = item->child; child; child = child->sibling)
415 children++;
417 offsets = file_builder_allocate (fb, 4, 4 * children,
418 &entry->value.pointer);
419 entry->type = 'L';
421 for (child = item->child; child; child = child->sibling)
422 offsets[i++] = child->assigned_index;
424 g_assert (children == i);
427 if (item->table != NULL)
429 entry->type = 'H';
430 file_builder_add_hash (fb, item->table, &entry->value.pointer);
433 index++;
437 hash_table_free (mytable);
440 static FileBuilder *
441 file_builder_new (gboolean byteswap)
443 FileBuilder *builder;
445 builder = g_slice_new (FileBuilder);
446 builder->chunks = g_queue_new ();
447 builder->offset = sizeof (struct gvdb_header);
448 builder->byteswap = byteswap;
450 return builder;
453 static GString *
454 file_builder_serialise (FileBuilder *fb,
455 struct gvdb_pointer root)
457 struct gvdb_header header = { { 0, }, };
458 GString *result;
460 if (fb->byteswap)
462 header.signature[0] = GVDB_SWAPPED_SIGNATURE0;
463 header.signature[1] = GVDB_SWAPPED_SIGNATURE1;
465 else
467 header.signature[0] = GVDB_SIGNATURE0;
468 header.signature[1] = GVDB_SIGNATURE1;
471 result = g_string_new (NULL);
473 header.root = root;
474 g_string_append_len (result, (gpointer) &header, sizeof header);
476 while (!g_queue_is_empty (fb->chunks))
478 FileChunk *chunk = g_queue_pop_head (fb->chunks);
480 if (result->len != chunk->offset)
482 gchar zero[8] = { 0, };
484 g_assert (chunk->offset > result->len);
485 g_assert (chunk->offset - result->len < 8);
487 g_string_append_len (result, zero, chunk->offset - result->len);
488 g_assert (result->len == chunk->offset);
491 g_string_append_len (result, chunk->data, chunk->size);
492 g_free (chunk->data);
494 g_slice_free (FileChunk, chunk);
497 g_queue_free (fb->chunks);
498 g_slice_free (FileBuilder, fb);
500 return result;
503 gboolean
504 gvdb_table_write_contents (GHashTable *table,
505 const gchar *filename,
506 gboolean byteswap,
507 GError **error)
509 struct gvdb_pointer root;
510 gboolean status;
511 FileBuilder *fb;
512 GString *str;
514 fb = file_builder_new (byteswap);
515 file_builder_add_hash (fb, table, &root);
516 str = file_builder_serialise (fb, root);
518 status = g_file_set_contents (filename, str->str, str->len, error);
519 g_string_free (str, TRUE);
521 return status;