]> code.delx.au - pulseaudio/blob - src/pulsecore/database-gdbm.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[pulseaudio] / src / pulsecore / database-gdbm.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Lennart Poettering
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <gdbm.h>
28
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/log.h>
32
33 #include "database.h"
34
35 #define MAKE_GDBM_FILE(x) ((GDBM_FILE) (x))
36
37 static inline datum* datum_to_gdbm(datum *to, const pa_datum *from) {
38 pa_assert(from);
39 pa_assert(to);
40
41 to->dptr = from->data;
42 to->dsize = from->size;
43
44 return to;
45 }
46
47 static inline pa_datum* datum_from_gdbm(pa_datum *to, const datum *from) {
48 pa_assert(from);
49 pa_assert(to);
50
51 to->data = from->dptr;
52 to->size = from->dsize;
53
54 return to;
55 }
56
57 void pa_datum_free(pa_datum *d) {
58 pa_assert(d);
59
60 free(d->data); /* gdbm uses raw malloc/free hence we should do that here, too */
61 pa_zero(d);
62 }
63
64 pa_database* pa_database_open(const char *fn, bool for_write) {
65 GDBM_FILE f;
66 int gdbm_cache_size;
67 char *path;
68
69 pa_assert(fn);
70
71 /* We include the host identifier in the file name because gdbm
72 * files are CPU dependent, 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;
76
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);
81
82 if (f)
83 pa_log_debug("Opened GDBM database '%s'", path);
84
85 pa_xfree(path);
86
87 if (!f) {
88 if (errno == 0)
89 errno = EIO;
90 return NULL;
91 }
92
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));
96
97 return (pa_database*) f;
98 }
99
100 void pa_database_close(pa_database *db) {
101 pa_assert(db);
102
103 gdbm_close(MAKE_GDBM_FILE(db));
104 }
105
106 pa_datum* pa_database_get(pa_database *db, const pa_datum *key, pa_datum* data) {
107 datum gdbm_key, gdbm_data;
108
109 pa_assert(db);
110 pa_assert(key);
111 pa_assert(data);
112
113 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
114
115 return gdbm_data.dptr ?
116 datum_from_gdbm(data, &gdbm_data) :
117 NULL;
118 }
119
120 int pa_database_set(pa_database *db, const pa_datum *key, const pa_datum* data, bool overwrite) {
121 datum gdbm_key, gdbm_data;
122
123 pa_assert(db);
124 pa_assert(key);
125 pa_assert(data);
126
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;
131 }
132
133 int pa_database_unset(pa_database *db, const pa_datum *key) {
134 datum gdbm_key;
135
136 pa_assert(db);
137 pa_assert(key);
138
139 return gdbm_delete(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key)) != 0 ? -1 : 0;
140 }
141
142 int pa_database_clear(pa_database *db) {
143 datum gdbm_key;
144
145 pa_assert(db);
146
147 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
148
149 while (gdbm_key.dptr) {
150 datum next;
151
152 next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
153
154 gdbm_delete(MAKE_GDBM_FILE(db), gdbm_key);
155
156 free(gdbm_key.dptr);
157 gdbm_key = next;
158 }
159
160 return gdbm_reorganize(MAKE_GDBM_FILE(db)) == 0 ? 0 : -1;
161 }
162
163 signed pa_database_size(pa_database *db) {
164 datum gdbm_key;
165 unsigned n = 0;
166
167 pa_assert(db);
168
169 /* This sucks */
170
171 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
172
173 while (gdbm_key.dptr) {
174 datum next;
175
176 n++;
177
178 next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
179 free(gdbm_key.dptr);
180 gdbm_key = next;
181 }
182
183 return (signed) n;
184 }
185
186 pa_datum* pa_database_first(pa_database *db, pa_datum *key, pa_datum *data) {
187 datum gdbm_key, gdbm_data;
188
189 pa_assert(db);
190 pa_assert(key);
191
192 gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
193
194 if (!gdbm_key.dptr)
195 return NULL;
196
197 if (data) {
198 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
199
200 if (!gdbm_data.dptr) {
201 free(gdbm_key.dptr);
202 return NULL;
203 }
204
205 datum_from_gdbm(data, &gdbm_data);
206 }
207
208 datum_from_gdbm(key, &gdbm_key);
209
210 return key;
211 }
212
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;
215
216 pa_assert(db);
217 pa_assert(key);
218 pa_assert(next);
219
220 if (!key)
221 return pa_database_first(db, next, data);
222
223 gdbm_key = gdbm_nextkey(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
224
225 if (!gdbm_key.dptr)
226 return NULL;
227
228 if (data) {
229 gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
230
231 if (!gdbm_data.dptr) {
232 free(gdbm_key.dptr);
233 return NULL;
234 }
235
236 datum_from_gdbm(data, &gdbm_data);
237 }
238
239 datum_from_gdbm(next, &gdbm_key);
240
241 return next;
242 }
243
244 int pa_database_sync(pa_database *db) {
245 pa_assert(db);
246
247 gdbm_sync(MAKE_GDBM_FILE(db));
248 return 0;
249 }