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
30 /* Some versions of tdb lack inclusion of signal.h in the header files but use sigatomic_t */
34 #include <pulse/xmalloc.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/log.h>
40 #define MAKE_TDB_CONTEXT(x) ((struct tdb_context*) (x))
42 static inline TDB_DATA
* datum_to_tdb(TDB_DATA
*to
, const pa_datum
*from
) {
46 to
->dptr
= from
->data
;
47 to
->dsize
= from
->size
;
52 static inline pa_datum
* datum_from_tdb(pa_datum
*to
, const TDB_DATA
*from
) {
56 to
->data
= from
->dptr
;
57 to
->size
= from
->dsize
;
62 void pa_datum_free(pa_datum
*d
) {
65 free(d
->data
); /* tdb uses raw malloc/free hence we should do that here, too */
69 static struct tdb_context
*tdb_open_cloexec(
76 /* Mimics pa_open_cloexec() */
78 struct tdb_context
*c
;
81 open_flags
|= O_NOCTTY
;
86 if ((c
= tdb_open(name
, hash_size
, tdb_flags
, open_flags
| O_CLOEXEC
, mode
)))
94 if (!(c
= tdb_open(name
, hash_size
, tdb_flags
, open_flags
, mode
)))
98 pa_make_fd_cloexec(tdb_fd(c
));
102 pa_database
* pa_database_open(const char *fn
, pa_bool_t for_write
) {
103 struct tdb_context
*c
;
108 path
= pa_sprintf_malloc("%s.tdb", fn
);
109 if ((c
= tdb_open_cloexec(path
, 0, TDB_NOSYNC
|TDB_NOLOCK
, (for_write
? O_RDWR
|O_CREAT
: O_RDONLY
), 0644)))
110 pa_log_debug("Opened TDB database '%s'", path
);
120 return (pa_database
*) c
;
123 void pa_database_close(pa_database
*db
) {
126 tdb_close(MAKE_TDB_CONTEXT(db
));
129 pa_datum
* pa_database_get(pa_database
*db
, const pa_datum
*key
, pa_datum
* data
) {
130 TDB_DATA tdb_key
, tdb_data
;
136 tdb_data
= tdb_fetch(MAKE_TDB_CONTEXT(db
), *datum_to_tdb(&tdb_key
, key
));
138 return tdb_data
.dptr
?
139 datum_from_tdb(data
, &tdb_data
) :
143 int pa_database_set(pa_database
*db
, const pa_datum
*key
, const pa_datum
* data
, pa_bool_t overwrite
) {
144 TDB_DATA tdb_key
, tdb_data
;
150 return tdb_store(MAKE_TDB_CONTEXT(db
),
151 *datum_to_tdb(&tdb_key
, key
),
152 *datum_to_tdb(&tdb_data
, data
),
153 overwrite
? TDB_REPLACE
: TDB_INSERT
) != 0 ? -1 : 0;
156 int pa_database_unset(pa_database
*db
, const pa_datum
*key
) {
162 return tdb_delete(MAKE_TDB_CONTEXT(db
), *datum_to_tdb(&tdb_key
, key
)) != 0 ? -1 : 0;
165 int pa_database_clear(pa_database
*db
) {
168 return tdb_wipe_all(MAKE_TDB_CONTEXT(db
)) != 0 ? -1 : 0;
171 signed pa_database_size(pa_database
*db
) {
179 tdb_key
= tdb_firstkey(MAKE_TDB_CONTEXT(db
));
181 while (tdb_key
.dptr
) {
186 next
= tdb_nextkey(MAKE_TDB_CONTEXT(db
), tdb_key
);
194 pa_datum
* pa_database_first(pa_database
*db
, pa_datum
*key
, pa_datum
*data
) {
195 TDB_DATA tdb_key
, tdb_data
;
200 tdb_key
= tdb_firstkey(MAKE_TDB_CONTEXT(db
));
206 tdb_data
= tdb_fetch(MAKE_TDB_CONTEXT(db
), tdb_key
);
208 if (!tdb_data
.dptr
) {
213 datum_from_tdb(data
, &tdb_data
);
216 datum_from_tdb(key
, &tdb_key
);
221 pa_datum
* pa_database_next(pa_database
*db
, const pa_datum
*key
, pa_datum
*next
, pa_datum
*data
) {
222 TDB_DATA tdb_key
, tdb_data
;
227 tdb_key
= tdb_nextkey(MAKE_TDB_CONTEXT(db
), *datum_to_tdb(&tdb_key
, key
));
233 tdb_data
= tdb_fetch(MAKE_TDB_CONTEXT(db
), tdb_key
);
235 if (!tdb_data
.dptr
) {
240 datum_from_tdb(data
, &tdb_data
);
243 datum_from_tdb(next
, &tdb_key
);
248 int pa_database_sync(pa_database
*db
) {