File: e-dbhash.c

package info (click to toggle)
evolution-data-server 3.12.9~git20141128.5242b0-2%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 58,708 kB
  • sloc: ansic: 286,597; sh: 11,359; makefile: 4,454; cpp: 472; xml: 410; perl: 204; python: 69
file content (308 lines) | stat: -rw-r--r-- 6,154 bytes parent folder | download | duplicates (2)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Author:
 *   JP Rosevear (jpr@ximian.com)
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 */

/**
 * SECTION: e-dbhash
 * @short_description: Simple DB-based hash table for strings
 *
 * An #EDbHash is a simple hash table of strings backed by a Berkeley DB
 * file for permanent storage.
 **/

#include <config.h>

#include "e-dbhash.h"

#include <string.h>
#include <fcntl.h>
#include "db.h"

struct _EDbHashPrivate {
	DB *db;
};

/**
 * e_dbhash_new:
 * @filename: path to a Berkeley DB file
 *
 * Creates a new #EDbHash structure and opens the given Berkeley DB file,
 * creating the DB file if necessary.
 *
 * Returns: a new #EDbHash
 **/
EDbHash *
e_dbhash_new (const gchar *filename)
{
	EDbHash *edbh;
	DB *db;
	gint rv;

	/* Attempt to open the database */
	rv = db_create (&db, NULL, 0);
	if (rv != 0) {
		return NULL;
	}

	rv = (*db->open) (db, NULL, filename, NULL, DB_HASH, 0, 0666);
	if (rv != 0) {
		/* Close and re-create the db handle to avoid memory leak */
		db->close (db, 0);
		rv = db_create (&db, NULL, 0);
		if (rv != 0) {
			return NULL;
		}

		rv = (*db->open) (
			db, NULL, filename, NULL, DB_HASH, DB_CREATE, 0666);

		if (rv != 0) {
			db->close (db, 0);
			return NULL;
		}
	}

	edbh = g_new (EDbHash, 1);
	edbh->priv = g_new (EDbHashPrivate, 1);
	edbh->priv->db = db;

	return edbh;
}

static void
string_to_dbt (const gchar *str,
               DBT *dbt)
{
	memset (dbt, 0, sizeof (DBT));
	dbt->data = (gpointer) str;
	dbt->size = strlen (str) + 1;
}

static void
md5_to_dbt (const guint8 str[16],
            DBT *dbt)
{
	memset (dbt, 0, sizeof (DBT));
	dbt->data = (gpointer) str;
	dbt->size = 16;
}

/**
 * e_dbhash_add:
 * @edbh: an #EDbHash
 * @key: a database key
 * @data: a database object for @key
 *
 * Adds a database object for @key.
 **/
void
e_dbhash_add (EDbHash *edbh,
              const gchar *key,
              const gchar *data)
{
	DB *db;
	DBT dkey;
	DBT ddata;
	GChecksum *checksum;
	guint8 *digest;
	gsize length;

	g_return_if_fail (edbh != NULL);
	g_return_if_fail (edbh->priv != NULL);
	g_return_if_fail (edbh->priv->db != NULL);
	g_return_if_fail (key != NULL);
	g_return_if_fail (data != NULL);

	length = g_checksum_type_get_length (G_CHECKSUM_MD5);
	digest = g_alloca (length);

	db = edbh->priv->db;

	/* Key dbt */
	string_to_dbt (key, &dkey);

	/* Compute MD5 checksum */
	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum, (guchar *) data, -1);
	g_checksum_get_digest (checksum, digest, &length);
	g_checksum_free (checksum);

	/* Data dbt */
	md5_to_dbt (digest, &ddata);

	/* Add to database */
	db->put (db, NULL, &dkey, &ddata, 0);
}

/**
 * e_dbhash_remove:
 * @edbh: an #EDbHash
 * @key: a database key
 *
 * Removes the database object corresponding to @key.
 **/
void
e_dbhash_remove (EDbHash *edbh,
                 const gchar *key)
{
	DB *db;
	DBT dkey;

	g_return_if_fail (edbh != NULL);
	g_return_if_fail (edbh->priv != NULL);
	g_return_if_fail (key != NULL);

	db = edbh->priv->db;

	/* Key dbt */
	string_to_dbt (key, &dkey);

	/* Remove from database */
	db->del (db, NULL, &dkey, 0);
}

/**
 * e_dbhash_foreach_key:
 * @edbh: an #EDbHash
 * @func: a callback function
 * @user_data: data to pass to @func
 *
 * Calls @func for each database object.
 **/
void
e_dbhash_foreach_key (EDbHash *edbh,
                      EDbHashFunc func,
                      gpointer user_data)
{
	DB *db;
	DBT dkey;
	DBT ddata;
	DBC *dbc;
	gint db_error = 0;

	g_return_if_fail (edbh != NULL);
	g_return_if_fail (edbh->priv != NULL);
	g_return_if_fail (func != NULL);

	db = edbh->priv->db;

	db_error = db->cursor (db, NULL, &dbc, 0);

	if (db_error != 0) {
		return;
	}

	memset (&dkey, 0, sizeof (DBT));
	memset (&ddata, 0, sizeof (DBT));
	db_error = dbc->c_get (dbc, &dkey, &ddata, DB_FIRST);

	while (db_error == 0) {
		(*func) ((const gchar *) dkey.data, user_data);

		db_error = dbc->c_get (dbc, &dkey, &ddata, DB_NEXT);
	}
	dbc->c_close (dbc);
}

/**
 * e_dbhash_compare:
 * @edbh: an #EDbHash
 * @key: a database key
 * @compare_data: data to compare against the database
 *
 * Compares @compare_data to the database object corresponding to
 * @key using an MD5 checksum.  Returns #E_DBHASH_STATUS_SAME if the
 * checksums match, #E_DBHASH_STATUS_DIFFERENT if the checksums differ,
 * or #E_DBHASH_STATUS_NOT_FOUND if @key is not present in the database.
 *
 * Returns: a checksum comparison status
 **/
EDbHashStatus
e_dbhash_compare (EDbHash *edbh,
                  const gchar *key,
                  const gchar *compare_data)
{
	DB *db;
	DBT dkey;
	DBT ddata;
	guint8 compare_hash[16];
	gsize length = sizeof (compare_hash);

	g_return_val_if_fail (edbh != NULL, FALSE);
	g_return_val_if_fail (edbh->priv != NULL, FALSE);
	g_return_val_if_fail (key != NULL, FALSE);
	g_return_val_if_fail (compare_hash != NULL, FALSE);

	db = edbh->priv->db;

	/* Key dbt */
	string_to_dbt (key, &dkey);

	/* Lookup in database */
	memset (&ddata, 0, sizeof (DBT));
	db->get (db, NULL, &dkey, &ddata, 0);

	/* Compare */
	if (ddata.data) {
		GChecksum *checksum;

		checksum = g_checksum_new (G_CHECKSUM_MD5);
		g_checksum_update (checksum, (guchar *) compare_data, -1);
		g_checksum_get_digest (checksum, compare_hash, &length);
		g_checksum_free (checksum);

		if (memcmp (ddata.data, compare_hash, sizeof (guchar) * 16))
			return E_DBHASH_STATUS_DIFFERENT;
	} else {
		return E_DBHASH_STATUS_NOT_FOUND;
	}

	return E_DBHASH_STATUS_SAME;
}

/**
 * e_dbhash_write:
 * @edbh: an #EDbHash
 *
 * Flushes database changes to disk.
 **/
void
e_dbhash_write (EDbHash *edbh)
{
	DB *db;

	g_return_if_fail (edbh != NULL);
	g_return_if_fail (edbh->priv != NULL);

	db = edbh->priv->db;

	/* Flush database to disk */
	db->sync (db, 0);
}

/**
 * e_dbhash_destroy:
 * @edbh: an #EDbHash
 *
 * Closes the database file and frees the #EDbHash.
 **/
void
e_dbhash_destroy (EDbHash *edbh)
{
	DB *db;

	g_return_if_fail (edbh != NULL);
	g_return_if_fail (edbh->priv != NULL);

	db = edbh->priv->db;

	/* Close datbase */
	db->close (db, 0);

	g_free (edbh->priv);
	g_free (edbh);
}