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, see <http://www.gnu.org/licenses/>.
27 #include <pulse/xmalloc.h>
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/log.h>
33 #define MAKE_GDBM_FILE(x) ((GDBM_FILE) (x))
35 static inline datum* datum_to_gdbm(datum *to, const pa_datum *from) {
39 to->dptr = from->data;
40 to->dsize = from->size;
45 static inline pa_datum* datum_from_gdbm(pa_datum *to, const datum *from) {
49 to->data = from->dptr;
50 to->size = from->dsize;
55 void pa_datum_free(pa_datum *d) {
58 free(d->data); /* gdbm uses raw malloc/free hence we should do that here, too */
62 const char* pa_database_get_filename_suffix(void) {
66 pa_database* pa_database_open_internal(const char *path, bool for_write) {
74 /* We need to set the block size explicitly here, since otherwise
75 * gdbm takes the native block size of the underlying file system
76 * which might be incredibly large. */
77 f = gdbm_open((char*) path, 1024, GDBM_NOLOCK | (for_write ? GDBM_WRCREAT : GDBM_READER), 0644, NULL);
80 pa_log_debug("Opened GDBM database '%s'", path);
88 /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
90 gdbm_setopt(f, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size));
92 return (pa_database*) f;
95 void pa_database_close(pa_database *db) {
98 gdbm_close(MAKE_GDBM_FILE(db));
101 pa_datum* pa_database_get(pa_database *db, const pa_datum *key, pa_datum* data) {
102 datum gdbm_key, gdbm_data;
108 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
110 return gdbm_data.dptr ?
111 datum_from_gdbm(data, &gdbm_data) :
115 int pa_database_set(pa_database *db, const pa_datum *key, const pa_datum* data, bool overwrite) {
116 datum gdbm_key, gdbm_data;
122 return gdbm_store(MAKE_GDBM_FILE(db),
123 *datum_to_gdbm(&gdbm_key, key),
124 *datum_to_gdbm(&gdbm_data, data),
125 overwrite ? GDBM_REPLACE : GDBM_INSERT) != 0 ? -1 : 0;
128 int pa_database_unset(pa_database *db, const pa_datum *key) {
134 return gdbm_delete(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key)) != 0 ? -1 : 0;
137 int pa_database_clear(pa_database *db) {
142 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
144 while (gdbm_key.dptr) {
147 next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
149 gdbm_delete(MAKE_GDBM_FILE(db), gdbm_key);
155 return gdbm_reorganize(MAKE_GDBM_FILE(db)) == 0 ? 0 : -1;
158 signed pa_database_size(pa_database *db) {
166 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
168 while (gdbm_key.dptr) {
173 next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
181 pa_datum* pa_database_first(pa_database *db, pa_datum *key, pa_datum *data) {
182 datum gdbm_key, gdbm_data;
187 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
193 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
195 if (!gdbm_data.dptr) {
200 datum_from_gdbm(data, &gdbm_data);
203 datum_from_gdbm(key, &gdbm_key);
208 pa_datum* pa_database_next(pa_database *db, const pa_datum *key, pa_datum *next, pa_datum *data) {
209 datum gdbm_key, gdbm_data;
216 return pa_database_first(db, next, data);
218 gdbm_key = gdbm_nextkey(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
224 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
226 if (!gdbm_data.dptr) {
231 datum_from_gdbm(data, &gdbm_data);
234 datum_from_gdbm(next, &gdbm_key);
239 int pa_database_sync(pa_database *db) {
242 gdbm_sync(MAKE_GDBM_FILE(db));