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. */
74 path = pa_sprintf_malloc("%s."CANONICAL_HOST".gdbm", fn);
77 /* We need to set the block size explicitly here, since otherwise
78 * gdbm takes the native block size of the underlying file system
79 * which might be incredibly large. */
80 f = gdbm_open((char*) path, 1024, GDBM_NOLOCK | (for_write ? GDBM_WRCREAT : GDBM_READER), 0644, NULL);
83 pa_log_debug("Opened GDBM database '%s'", path);
93 /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
95 gdbm_setopt(f, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size));
97 return (pa_database*) f;
100 void pa_database_close(pa_database *db) {
103 gdbm_close(MAKE_GDBM_FILE(db));
106 pa_datum* pa_database_get(pa_database *db, const pa_datum *key, pa_datum* data) {
107 datum gdbm_key, gdbm_data;
113 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
115 return gdbm_data.dptr ?
116 datum_from_gdbm(data, &gdbm_data) :
120 int pa_database_set(pa_database *db, const pa_datum *key, const pa_datum* data, pa_bool_t overwrite) {
121 datum gdbm_key, gdbm_data;
127 return gdbm_store(MAKE_GDBM_FILE(db),
128 *datum_to_gdbm(&gdbm_key, key),
129 *datum_to_gdbm(&gdbm_data, data),
130 overwrite ? GDBM_REPLACE : GDBM_INSERT) != 0 ? -1 : 0;
133 int pa_database_unset(pa_database *db, const pa_datum *key) {
139 return gdbm_delete(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key)) != 0 ? -1 : 0;
142 int pa_database_clear(pa_database *db) {
147 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
149 while (gdbm_key.dptr) {
152 next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
154 gdbm_delete(MAKE_GDBM_FILE(db), gdbm_key);
160 return gdbm_reorganize(MAKE_GDBM_FILE(db)) == 0 ? 0 : -1;
163 signed pa_database_size(pa_database *db) {
171 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
173 while (gdbm_key.dptr) {
178 next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
186 pa_datum* pa_database_first(pa_database *db, pa_datum *key, pa_datum *data) {
187 datum gdbm_key, gdbm_data;
192 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
198 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
200 if (!gdbm_data.dptr) {
205 datum_from_gdbm(data, &gdbm_data);
208 datum_from_gdbm(key, &gdbm_key);
213 pa_datum* pa_database_next(pa_database *db, const pa_datum *key, pa_datum *next, pa_datum *data) {
214 datum gdbm_key, gdbm_data;
221 return pa_database_first(db, next, data);
223 gdbm_key = gdbm_nextkey(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
229 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
231 if (!gdbm_data.dptr) {
236 datum_from_gdbm(data, &gdbm_data);
239 datum_from_gdbm(next, &gdbm_key);
244 int pa_database_sync(pa_database *db) {
247 gdbm_sync(MAKE_GDBM_FILE(db));