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>
@ -49,6 +48,7 @@
#include <pulsecore/protocol-native.h>
#include <pulsecore/pstream.h>
#include <pulsecore/pstream-util.h>
#include <pulsecore/database.h>
#include "module-stream-restore-symdef.h"
@ -81,7 +81,7 @@ struct userdata {
*source_output_new_hook_slot,
*connection_unlink_hook_slot;
pa_time_event *save_time_event;
GDBM_FILE gdbm_file;
pa_database* database;
pa_bool_t restore_device:1;
pa_bool_t restore_volume:1;
@ -123,7 +123,7 @@ 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.");
}
@ -153,28 +153,28 @@ static char *get_name(pa_proplist *p, const char *prefix) {
}
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)) {
if (data.size != sizeof(struct entry)) {
/* This is probably just a database upgrade, hence let's not
* consider this more than a debug message */
pa_log_debug("Database contains entry for stream %s of wrong size %lu != %lu. Probably due to uprade, ignoring.", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
pa_log_debug("Database contains entry for stream %s of wrong size %lu != %lu. Probably due to uprade, 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 stream %s doesn't match our version. Probably due to upgrade, ignoring.", name);
@ -205,7 +205,7 @@ static struct entry* read_entry(struct userdata *u, const char *name) {
fail:
pa_xfree(data.dptr);
pa_datum_free(&data);
return NULL;
}
@ -261,7 +261,7 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
struct userdata *u = userdata;
struct entry entry, *old;
char *name;
datum key, data;
pa_datum key, data;
pa_assert(c);
pa_assert(u);
@ -272,7 +272,7 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
return;
memset(&entry, 0, sizeof(entry));
pa_zero(entry);
entry.version = ENTRY_VERSION;
if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
@ -334,15 +334,15 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
pa_xfree(old);
}
key.dptr = name;
key.dsize = (int) strlen(name);
key.data = name;
key.size = strlen(name);
data.dptr = (void*) &entry;
data.dsize = sizeof(entry);
data.data = &entry;
data.size = sizeof(entry);
pa_log_info("Storing volume/mute/device for stream %s.", name);
gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
pa_database_set(u->database, &key, &data, TRUE);
pa_xfree(name);
@ -471,25 +471,6 @@ static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_ou
#define EXT_VERSION 1
static void clear_db(struct userdata *u) {
datum key;
pa_assert(u);
key = gdbm_firstkey(u->gdbm_file);
while (key.dptr) {
datum next_key;
next_key = gdbm_nextkey(u->gdbm_file, key);
gdbm_delete(u->gdbm_file, key);
pa_xfree(key.dptr);
key = next_key;
}
gdbm_reorganize(u->gdbm_file);
}
static void apply_entry(struct userdata *u, const char *name, struct entry *e) {
pa_sink_input *si;
pa_source_output *so;
@ -559,18 +540,20 @@ static void apply_entry(struct userdata *u, const char *name, struct entry *e) {
#if 0
static void dump_database(struct userdata *u) {
datum key;
pa_datum key;
pa_bool_t done;
key = gdbm_firstkey(u->gdbm_file);
while (key.dptr) {
datum next_key;
done = !pa_database_first(u->database, &key, NULL);
while (!done) {
pa_datum next_key;
struct entry *e;
char *name;
next_key = gdbm_nextkey(u->gdbm_file, key);
done = !pa_database_next(u->database, &key, &next_key, NULL);
name = pa_xstrndup(key.dptr, key.dsize);
pa_xfree(key.dptr);
name = pa_xstrndup(key.data, key.size);
pa_datum_free(&key);
if ((e = read_entry(u, name))) {
char t[256];
@ -618,21 +601,23 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
}
case SUBCOMMAND_READ: {
datum key;
pa_datum key;
pa_bool_t done;
if (!pa_tagstruct_eof(t))
goto fail;
key = gdbm_firstkey(u->gdbm_file);
while (key.dptr) {
datum next_key;
done = !pa_database_first(u->database, &key, NULL);
while (!done) {
pa_datum next_key;
struct entry *e;
char *name;
next_key = gdbm_nextkey(u->gdbm_file, key);
done = !pa_database_next(u->database, &key, &next_key, NULL);
name = pa_xstrndup(key.dptr, (size_t) key.dsize);
pa_xfree(key.dptr);
name = pa_xstrndup(key.data, key.size);
pa_datum_free(&key);
if ((e = read_entry(u, name))) {
pa_cvolume r;
@ -669,16 +654,15 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
goto fail;
if (mode == PA_UPDATE_SET)
clear_db(u);
pa_database_clear(u->database);
while (!pa_tagstruct_eof(t)) {
const char *name, *device;
pa_bool_t muted;
struct entry entry;
datum key, data;
int k;
pa_datum key, data;
memset(&entry, 0, sizeof(entry));
pa_zero(entry);
entry.version = ENTRY_VERSION;
if (pa_tagstruct_gets(t, &name) < 0 ||
@ -708,13 +692,13 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
!pa_namereg_is_valid_name(entry.device))
goto fail;
key.dptr = (void*) name;
key.dsize = (int) strlen(name);
key.data = (char*) name;
key.size = strlen(name);
data.dptr = (void*) &entry;
data.dsize = sizeof(entry);
data.data = &entry;
data.size = sizeof(entry);
if ((k = gdbm_store(u->gdbm_file, key, data, mode == PA_UPDATE_REPLACE ? GDBM_REPLACE : GDBM_INSERT)) == 0)
if (pa_database_set(u->database, &key, &data, mode == PA_UPDATE_REPLACE) == 0)
if (apply_immediately)
apply_entry(u, name, &entry);
}
@ -728,15 +712,15 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
while (!pa_tagstruct_eof(t)) {
const char *name;
datum key;
pa_datum key;
if (pa_tagstruct_gets(t, &name) < 0)
goto fail;
key.dptr = (void*) name;
key.dsize = (int) strlen(name);
key.data = (char*) name;
key.size = strlen(name);
gdbm_delete(u->gdbm_file, key);
pa_database_unset(u->database, &key);
}
trigger_save(u);
@ -786,12 +770,11 @@ static pa_hook_result_t connection_unlink_hook_cb(pa_native_protocol *p, pa_nati
int pa__init(pa_module*m) {
pa_modargs *ma = NULL;
struct userdata *u;
char *fname, *fn;
char *fname;
pa_sink_input *si;
pa_source_output *so;
uint32_t idx;
pa_bool_t restore_device = TRUE, restore_volume = TRUE, restore_muted = TRUE;
int gdbm_cache_size;
pa_assert(m);
@ -817,7 +800,7 @@ int pa__init(pa_module*m) {
u->restore_device = restore_device;
u->restore_volume = restore_volume;
u->restore_muted = restore_muted;
u->gdbm_file = NULL;
u->database = NULL;
u->subscribed = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
u->protocol = pa_native_protocol_get(m->core);
@ -835,27 +818,18 @@ int pa__init(pa_module*m) {
if (restore_volume || restore_muted)
u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_fixate_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("stream-volumes."CANONICAL_HOST".gdbm");
fname = pa_state_path(fn, TRUE);
pa_xfree(fn);
fname = pa_state_path("stream-volumes", TRUE);
if (!fname)
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);
@ -901,8 +875,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);
if (u->protocol) {
pa_native_protocol_remove_ext(u->protocol, m);