default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / pulsecore / database-gdbm.c
blobe65125d3387c019dbafafc8cbe1bfa587236016d
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <errno.h>
27 #include <gdbm.h>
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/log.h>
33 #include "database.h"
35 #define MAKE_GDBM_FILE(x) ((GDBM_FILE) (x))
37 static inline datum* datum_to_gdbm(datum *to, const pa_datum *from) {
38 pa_assert(from);
39 pa_assert(to);
41 to->dptr = from->data;
42 to->dsize = from->size;
44 return to;
47 static inline pa_datum* datum_from_gdbm(pa_datum *to, const datum *from) {
48 pa_assert(from);
49 pa_assert(to);
51 to->data = from->dptr;
52 to->size = from->dsize;
54 return to;
57 void pa_datum_free(pa_datum *d) {
58 pa_assert(d);
60 free(d->data); /* gdbm uses raw malloc/free hence we should do that here, too */
61 pa_zero(d);
64 pa_database* pa_database_open(const char *fn, pa_bool_t for_write) {
65 GDBM_FILE f;
66 int gdbm_cache_size;
67 char *path;
69 pa_assert(fn);
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);
75 errno = 0;
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);
82 if (f)
83 pa_log_debug("Opened GDBM database '%s'", path);
85 pa_xfree(path);
87 if (!f) {
88 if (errno == 0)
89 errno = EIO;
90 return NULL;
93 /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
94 gdbm_cache_size = 10;
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) {
101 pa_assert(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;
109 pa_assert(db);
110 pa_assert(key);
111 pa_assert(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) :
117 NULL;
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;
123 pa_assert(db);
124 pa_assert(key);
125 pa_assert(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) {
134 datum gdbm_key;
136 pa_assert(db);
137 pa_assert(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) {
143 datum gdbm_key;
145 pa_assert(db);
147 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
149 while (gdbm_key.dptr) {
150 datum next;
152 next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
154 gdbm_delete(MAKE_GDBM_FILE(db), gdbm_key);
156 free(gdbm_key.dptr);
157 gdbm_key = next;
160 return gdbm_reorganize(MAKE_GDBM_FILE(db)) == 0 ? 0 : -1;
163 signed pa_database_size(pa_database *db) {
164 datum gdbm_key;
165 unsigned n = 0;
167 pa_assert(db);
169 /* This sucks */
171 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
173 while (gdbm_key.dptr) {
174 datum next;
176 n++;
178 next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
179 free(gdbm_key.dptr);
180 gdbm_key = next;
183 return (signed) n;
186 pa_datum* pa_database_first(pa_database *db, pa_datum *key, pa_datum *data) {
187 datum gdbm_key, gdbm_data;
189 pa_assert(db);
190 pa_assert(key);
192 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
194 if (!gdbm_key.dptr)
195 return NULL;
197 if (data) {
198 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
200 if (!gdbm_data.dptr) {
201 free(gdbm_key.dptr);
202 return NULL;
205 datum_from_gdbm(data, &gdbm_data);
208 datum_from_gdbm(key, &gdbm_key);
210 return 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;
216 pa_assert(db);
217 pa_assert(key);
218 pa_assert(next);
220 if (!key)
221 return pa_database_first(db, next, data);
223 gdbm_key = gdbm_nextkey(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
225 if (!gdbm_key.dptr)
226 return NULL;
228 if (data) {
229 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
231 if (!gdbm_data.dptr) {
232 free(gdbm_key.dptr);
233 return NULL;
236 datum_from_gdbm(data, &gdbm_data);
239 datum_from_gdbm(next, &gdbm_key);
241 return next;
244 int pa_database_sync(pa_database *db) {
245 pa_assert(db);
247 gdbm_sync(MAKE_GDBM_FILE(db));
248 return 0;