Sending translation for Portuguese
[platform/upstream/pulseaudio.git] / 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, pa_bool_t 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 dependant, and we don't want things to go wrong
73      * if we are on a multiarch system. */
74
75     path = pa_sprintf_malloc("%s."CANONICAL_HOST".gdbm", fn);
76     errno = 0;
77     f = gdbm_open((char*) path, 0, GDBM_NOLOCK | (for_write ? GDBM_WRCREAT : GDBM_READER), 0644, NULL);
78
79     if (f)
80         pa_log_debug("Opened GDBM database '%s'", path);
81
82     pa_xfree(path);
83
84     if (!f) {
85         if (errno == 0)
86             errno = EIO;
87         return NULL;
88     }
89
90     /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
91     gdbm_cache_size = 10;
92     gdbm_setopt(f, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size));
93
94     return (pa_database*) f;
95 }
96
97 void pa_database_close(pa_database *db) {
98     pa_assert(db);
99
100     gdbm_close(MAKE_GDBM_FILE(db));
101 }
102
103 pa_datum* pa_database_get(pa_database *db, const pa_datum *key, pa_datum* data) {
104     datum gdbm_key, gdbm_data;
105
106     pa_assert(db);
107     pa_assert(key);
108     pa_assert(data);
109
110     gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
111
112     return gdbm_data.dptr ?
113         datum_from_gdbm(data, &gdbm_data) :
114         NULL;
115 }
116
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;
119
120     pa_assert(db);
121     pa_assert(key);
122     pa_assert(data);
123
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;
128 }
129
130 int pa_database_unset(pa_database *db, const pa_datum *key) {
131     datum gdbm_key;
132
133     pa_assert(db);
134     pa_assert(key);
135
136     return gdbm_delete(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key)) != 0 ? -1 : 0;
137 }
138
139 int pa_database_clear(pa_database *db) {
140     datum gdbm_key;
141
142     pa_assert(db);
143
144     gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
145
146     while (gdbm_key.dptr) {
147         datum next;
148
149         next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
150
151         gdbm_delete(MAKE_GDBM_FILE(db), gdbm_key);
152
153         free(gdbm_key.dptr);
154         gdbm_key = next;
155     }
156
157     return gdbm_reorganize(MAKE_GDBM_FILE(db)) == 0 ? 0 : -1;
158 }
159
160 signed pa_database_size(pa_database *db) {
161     datum gdbm_key;
162     unsigned n = 0;
163
164     pa_assert(db);
165
166     /* This sucks */
167
168     gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
169
170     while (gdbm_key.dptr) {
171         datum next;
172
173         n++;
174
175         next = gdbm_nextkey(MAKE_GDBM_FILE(db), gdbm_key);
176         free(gdbm_key.dptr);
177         gdbm_key = next;
178     }
179
180     return (signed) n;
181 }
182
183 pa_datum* pa_database_first(pa_database *db, pa_datum *key, pa_datum *data) {
184     datum gdbm_key, gdbm_data;
185
186     pa_assert(db);
187     pa_assert(key);
188
189     gdbm_key = gdbm_firstkey(MAKE_GDBM_FILE(db));
190
191     if (!gdbm_key.dptr)
192         return NULL;
193
194     if (data) {
195         gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
196
197         if (!gdbm_data.dptr) {
198             free(gdbm_key.dptr);
199             return NULL;
200         }
201
202         datum_from_gdbm(data, &gdbm_data);
203     }
204
205     datum_from_gdbm(key, &gdbm_key);
206
207     return key;
208 }
209
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;
212
213     pa_assert(db);
214     pa_assert(key);
215     pa_assert(next);
216
217     if (!key)
218         return pa_database_first(db, next, data);
219
220     gdbm_key = gdbm_nextkey(MAKE_GDBM_FILE(db), *datum_to_gdbm(&gdbm_key, key));
221
222     if (!gdbm_key.dptr)
223         return NULL;
224
225     if (data) {
226         gdbm_data = gdbm_fetch(MAKE_GDBM_FILE(db), gdbm_key);
227
228         if (!gdbm_data.dptr) {
229             free(gdbm_key.dptr);
230             return NULL;
231         }
232
233         datum_from_gdbm(data, &gdbm_data);
234     }
235
236     datum_from_gdbm(next, &gdbm_key);
237
238     return next;
239 }
240
241 int pa_database_sync(pa_database *db) {
242     pa_assert(db);
243
244     gdbm_sync(MAKE_GDBM_FILE(db));
245     return 0;
246 }