Git init
[framework/multimedia/pulseaudio.git] / src / pulsecore / database-tdb.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 <fcntl.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 /* Some versions of tdb lack inclusion of signal.h in the header files but use sigatomic_t */
31 #include <signal.h>
32 #include <tdb.h>
33
34 #include <pulse/xmalloc.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/log.h>
37
38 #include "database.h"
39
40 #define MAKE_TDB_CONTEXT(x) ((struct tdb_context*) (x))
41
42 static inline TDB_DATA* datum_to_tdb(TDB_DATA *to, const pa_datum *from) {
43     pa_assert(from);
44     pa_assert(to);
45
46     to->dptr = from->data;
47     to->dsize = from->size;
48
49     return to;
50 }
51
52 static inline pa_datum* datum_from_tdb(pa_datum *to, const TDB_DATA *from) {
53     pa_assert(from);
54     pa_assert(to);
55
56     to->data = from->dptr;
57     to->size = from->dsize;
58
59     return to;
60 }
61
62 void pa_datum_free(pa_datum *d) {
63     pa_assert(d);
64
65     free(d->data); /* tdb uses raw malloc/free hence we should do that here, too */
66     pa_zero(d);
67 }
68
69 pa_database* pa_database_open(const char *fn, pa_bool_t for_write) {
70     struct tdb_context *c;
71     char *path;
72
73     pa_assert(fn);
74
75     path = pa_sprintf_malloc("%s.tdb", fn);
76     errno = 0;
77     c = tdb_open(path, 0, TDB_NOSYNC|TDB_NOLOCK,
78                  (for_write ? O_RDWR|O_CREAT : O_RDONLY)|O_NOCTTY
79 #ifdef O_CLOEXEC
80                  |O_CLOEXEC
81 #endif
82                  , 0644);
83
84     if (c)
85         pa_log_debug("Opened TDB database '%s'", path);
86
87     pa_xfree(path);
88
89     if (!c) {
90         if (errno == 0)
91             errno = EIO;
92         return NULL;
93     }
94
95     return (pa_database*) c;
96 }
97
98 void pa_database_close(pa_database *db) {
99     pa_assert(db);
100
101     tdb_close(MAKE_TDB_CONTEXT(db));
102 }
103
104 pa_datum* pa_database_get(pa_database *db, const pa_datum *key, pa_datum* data) {
105     TDB_DATA tdb_key, tdb_data;
106
107     pa_assert(db);
108     pa_assert(key);
109     pa_assert(data);
110
111     tdb_data = tdb_fetch(MAKE_TDB_CONTEXT(db), *datum_to_tdb(&tdb_key, key));
112
113     return tdb_data.dptr ?
114         datum_from_tdb(data, &tdb_data) :
115         NULL;
116 }
117
118 int pa_database_set(pa_database *db, const pa_datum *key, const pa_datum* data, pa_bool_t overwrite) {
119     TDB_DATA tdb_key, tdb_data;
120
121     pa_assert(db);
122     pa_assert(key);
123     pa_assert(data);
124
125     return tdb_store(MAKE_TDB_CONTEXT(db),
126                       *datum_to_tdb(&tdb_key, key),
127                       *datum_to_tdb(&tdb_data, data),
128                      overwrite ? TDB_REPLACE : TDB_INSERT) != 0 ? -1 : 0;
129 }
130
131 int pa_database_unset(pa_database *db, const pa_datum *key) {
132     TDB_DATA tdb_key;
133
134     pa_assert(db);
135     pa_assert(key);
136
137     return tdb_delete(MAKE_TDB_CONTEXT(db), *datum_to_tdb(&tdb_key, key)) != 0 ? -1 : 0;
138 }
139
140 int pa_database_clear(pa_database *db) {
141     pa_assert(db);
142
143     return tdb_wipe_all(MAKE_TDB_CONTEXT(db)) != 0 ? -1 : 0;
144 }
145
146 signed pa_database_size(pa_database *db) {
147     TDB_DATA tdb_key;
148     unsigned n = 0;
149
150     pa_assert(db);
151
152     /* This sucks */
153
154     tdb_key = tdb_firstkey(MAKE_TDB_CONTEXT(db));
155
156     while (tdb_key.dptr) {
157         TDB_DATA next;
158
159         n++;
160
161         next = tdb_nextkey(MAKE_TDB_CONTEXT(db), tdb_key);
162         free(tdb_key.dptr);
163         tdb_key = next;
164     }
165
166     return (signed) n;
167 }
168
169 pa_datum* pa_database_first(pa_database *db, pa_datum *key, pa_datum *data) {
170     TDB_DATA tdb_key, tdb_data;
171
172     pa_assert(db);
173     pa_assert(key);
174
175     tdb_key = tdb_firstkey(MAKE_TDB_CONTEXT(db));
176
177     if (!tdb_key.dptr)
178         return NULL;
179
180     if (data) {
181         tdb_data = tdb_fetch(MAKE_TDB_CONTEXT(db), tdb_key);
182
183         if (!tdb_data.dptr) {
184             free(tdb_key.dptr);
185             return NULL;
186         }
187
188         datum_from_tdb(data, &tdb_data);
189     }
190
191     datum_from_tdb(key, &tdb_key);
192
193     return key;
194 }
195
196 pa_datum* pa_database_next(pa_database *db, const pa_datum *key, pa_datum *next, pa_datum *data) {
197     TDB_DATA tdb_key, tdb_data;
198
199     pa_assert(db);
200     pa_assert(key);
201
202     tdb_key = tdb_nextkey(MAKE_TDB_CONTEXT(db), *datum_to_tdb(&tdb_key, key));
203
204     if (!tdb_key.dptr)
205         return NULL;
206
207     if (data) {
208         tdb_data = tdb_fetch(MAKE_TDB_CONTEXT(db), tdb_key);
209
210         if (!tdb_data.dptr) {
211             free(tdb_key.dptr);
212             return NULL;
213         }
214
215         datum_from_tdb(data, &tdb_data);
216     }
217
218     datum_from_tdb(next, &tdb_key);
219
220     return next;
221 }
222
223 int pa_database_sync(pa_database *db) {
224     pa_assert(db);
225
226     return 0;
227 }