database: port restore modules to new database API

This commit is contained in:
Lennart Poettering 2009-05-14 01:24:26 +02:00
parent 003e03d233
commit b7e2223186
5 changed files with 197 additions and 168 deletions

View file

@ -30,7 +30,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <gdbm.h>
#include <pulse/xmalloc.h>
#include <pulse/volume.h>
@ -45,6 +44,7 @@
#include <pulsecore/core-subscribe.h>
#include <pulsecore/card.h>
#include <pulsecore/namereg.h>
#include <pulsecore/database.h>
#include "module-card-restore-symdef.h"
@ -65,7 +65,7 @@ struct userdata {
pa_subscription *subscription;
pa_hook_slot *card_new_hook_slot;
pa_time_event *save_time_event;
GDBM_FILE gdbm_file;
pa_database *database;
};
#define ENTRY_VERSION 1
@ -87,31 +87,31 @@ static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct
u->core->mainloop->time_free(u->save_time_event);
u->save_time_event = NULL;
gdbm_sync(u->gdbm_file);
pa_database_sync(u->database);
pa_log_info("Synced.");
}
static struct entry* read_entry(struct userdata *u, const char *name) {
datum key, data;
pa_datum key, data;
struct entry *e;
pa_assert(u);
pa_assert(name);
key.dptr = (char*) name;
key.dsize = (int) strlen(name);
key.data = (char*) name;
key.size = strlen(name);
data = gdbm_fetch(u->gdbm_file, key);
pa_zero(data);
if (!data.dptr)
if (!pa_database_get(u->database, &key, &data))
goto fail;
if (data.dsize != sizeof(struct entry)) {
pa_log_debug("Database contains entry for card %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
if (data.size != sizeof(struct entry)) {
pa_log_debug("Database contains entry for card %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry));
goto fail;
}
e = (struct entry*) data.dptr;
e = (struct entry*) data.data;
if (e->version != ENTRY_VERSION) {
pa_log_debug("Version of database entry for card %s doesn't match our version. Probably due to upgrade, ignoring.", name);
@ -127,7 +127,7 @@ static struct entry* read_entry(struct userdata *u, const char *name) {
fail:
pa_xfree(data.dptr);
pa_datum_free(&data);
return NULL;
}
@ -145,7 +145,7 @@ static void trigger_save(struct userdata *u) {
static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
struct userdata *u = userdata;
struct entry entry, *old;
datum key, data;
pa_datum key, data;
pa_card *card;
pa_assert(c);
@ -155,7 +155,7 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE))
return;
memset(&entry, 0, sizeof(entry));
pa_zero(entry);
entry.version = ENTRY_VERSION;
if (!(card = pa_idxset_get_by_index(c->cards, idx)))
@ -176,15 +176,15 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
pa_xfree(old);
}
key.dptr = card->name;
key.dsize = (int) strlen(card->name);
key.data = card->name;
key.size = strlen(card->name);
data.dptr = (void*) &entry;
data.dsize = sizeof(entry);
data.data = &entry;
data.size = sizeof(entry);
pa_log_info("Storing profile for card %s.", card->name);
gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
pa_database_set(u->database, &key, &data, TRUE);
trigger_save(u);
}
@ -211,10 +211,9 @@ static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new
int pa__init(pa_module*m) {
pa_modargs *ma = NULL;
struct userdata *u;
char *fname, *fn;
char *fname;
pa_card *card;
uint32_t idx;
int gdbm_cache_size;
pa_assert(m);
@ -227,33 +226,21 @@ int pa__init(pa_module*m) {
u->core = m->core;
u->module = m;
u->save_time_event = NULL;
u->gdbm_file = NULL;
u->database = NULL;
u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_CARD, subscribe_callback, u);
u->card_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CARD_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) card_new_hook_callback, u);
/* We include the host identifier in the file name because gdbm
* files are CPU dependant, and we don't want things to go wrong
* if we are on a multiarch system. */
fn = pa_sprintf_malloc("card-database."CANONICAL_HOST".gdbm");
fname = pa_state_path(fn, TRUE);
pa_xfree(fn);
if (!fname)
if (!(fname = pa_state_path("card-database", TRUE)))
goto fail;
if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT|GDBM_NOLOCK, 0600, NULL))) {
pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno));
if (!(u->database = pa_database_open(fname, TRUE))) {
pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
pa_xfree(fname);
goto fail;
}
/* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
gdbm_cache_size = 10;
gdbm_setopt(u->gdbm_file, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size));
pa_log_info("Sucessfully opened database file '%s'.", fname);
pa_xfree(fname);
@ -289,8 +276,8 @@ void pa__done(pa_module*m) {
if (u->save_time_event)
u->core->mainloop->time_free(u->save_time_event);
if (u->gdbm_file)
gdbm_close(u->gdbm_file);
if (u->database)
pa_database_close(u->database);
pa_xfree(u);
}