2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/log.h>
35 #define MAKE_GDBM_FILE(x) ((GDBM_FILE) (x))
37 static inline datum* datum_to_gdbm(datum *to, const pa_datum *from) {
41 to->dptr = from->data;
42 to->dsize = from->size;
47 static inline pa_datum* datum_from_gdbm(pa_datum *to, const datum *from) {
51 to->data = from->dptr;
52 to->size = from->dsize;
57 void pa_datum_free(pa_datum *d) {
60 free(d->data); /* gdbm uses raw malloc/free hence we should do that here, too */
64 pa_database* pa_database_open(const char *fn, pa_bool_t for_write) {
71 /* We include the host identifier in the file name because gdbm
72 * files are CPU dependant, and we don't want things to go wrong
73 * if we are on a multiarch system. */
75 path = pa_sprintf_malloc("%s."CANONICAL_HOST".gdbm", fn);
77 f = gdbm_open((char*) path, 0, GDBM_NOLOCK | (for_write ? GDBM_WRCREAT : GDBM_READER), 0644, NULL);
80 pa_log_debug("Opened GDBM database '%s'", path);
90 /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
92 gdbm_setopt(f, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size));
94 return (pa_database*) f;
97 void pa_database_close(pa_database *db) {
100 gdbm_close(MAKE_GDBM_FILE(db));
103 pa_datum* pa_database_get(pa_database *db, const pa_datum *key, pa_datum* data) {
104 datum gdbm_key, gdbm_data;
110 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
112 return gdbm_data.dptr ?
113 datum_from_gdbm(data, &gdbm_data) :
117 int pa_database_set(pa_database *db, const pa_datum *key, const pa_datum* data, pa_bool_t overwrite) {
118 datum gdbm_key, gdbm_data;
124 return gdbm_store(MAKE_GDBM_FILE(db),
125 *datum_to_gdbm(&gdbm_key, key),
126 *datum_to_gdbm(&gdbm_data, data),
127 overwrite ? GDBM_REPLACE : GDBM_INSERT) != 0 ? -1 : 0;
130 int pa_database_unset(pa_database *db, const pa_datum *key) {
136 return gdbm_delete(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key)) != 0 ? -1 : 0;
139 int pa_database_clear(pa_database *db) {
144 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
146 while (gdbm_key.dptr) {
149 next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
151 gdbm_delete(MAKE_GDBM_FILE(db), gdbm_key);
157 return gdbm_reorganize(MAKE_GDBM_FILE(db)) == 0 ? 0 : -1;
160 signed pa_database_size(pa_database *db) {
168 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
170 while (gdbm_key.dptr) {
175 next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
183 pa_datum* pa_database_first(pa_database *db, pa_datum *key, pa_datum *data) {
184 datum gdbm_key, gdbm_data;
189 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
195 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
197 if (!gdbm_data.dptr) {
202 datum_from_gdbm(data, &gdbm_data);
205 datum_from_gdbm(key, &gdbm_key);
210 pa_datum* pa_database_next(pa_database *db, const pa_datum *key, pa_datum *next, pa_datum *data) {
211 datum gdbm_key, gdbm_data;
218 return pa_database_first(db, next, data);
220 gdbm_key = gdbm_nextkey(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
226 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
228 if (!gdbm_data.dptr) {
233 datum_from_gdbm(data, &gdbm_data);
236 datum_from_gdbm(next, &gdbm_key);
241 int pa_database_sync(pa_database *db) {
244 gdbm_sync(MAKE_GDBM_FILE(db));