From 18e975fc5ef68a89875c77974570884b9214a009 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 20 Jul 2009 17:34:17 +0200 Subject: merged --- src/modules/module-card-restore.c | 85 ++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 50 deletions(-) (limited to 'src/modules/module-card-restore.c') diff --git a/src/modules/module-card-restore.c b/src/modules/module-card-restore.c index 0afb9353..7dea94f7 100644 --- a/src/modules/module-card-restore.c +++ b/src/modules/module-card-restore.c @@ -30,12 +30,12 @@ #include #include #include -#include #include #include #include #include +#include #include #include @@ -45,6 +45,7 @@ #include #include #include +#include #include "module-card-restore-symdef.h" @@ -53,7 +54,7 @@ PA_MODULE_DESCRIPTION("Automatically restore profile of cards"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(TRUE); -#define SAVE_INTERVAL 10 +#define SAVE_INTERVAL (10 * PA_USEC_PER_SEC) static const char* const valid_modargs[] = { NULL @@ -65,7 +66,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 @@ -75,43 +76,42 @@ struct entry { char profile[PA_NAME_MAX]; } PA_GCC_PACKED ; -static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) { +static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) { struct userdata *u = userdata; pa_assert(a); pa_assert(e); - pa_assert(tv); pa_assert(u); pa_assert(e == u->save_time_event); 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,25 +127,21 @@ static struct entry* read_entry(struct userdata *u, const char *name) { fail: - pa_xfree(data.dptr); + pa_datum_free(&data); return NULL; } static void trigger_save(struct userdata *u) { - struct timeval tv; - if (u->save_time_event) return; - pa_gettimeofday(&tv); - tv.tv_sec += SAVE_INTERVAL; - u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u); + u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, 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,12 +151,15 @@ 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))) return; + if (!card->save_profile) + return; + pa_strlcpy(entry.profile, card->active_profile ? card->active_profile->name : "", sizeof(entry.profile)); if ((old = read_entry(u, card->name))) { @@ -173,15 +172,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); } @@ -194,8 +193,9 @@ static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new if ((e = read_entry(u, new_data->name)) && e->profile[0]) { if (!new_data->active_profile) { - pa_card_new_data_set_profile(new_data, e->profile); pa_log_info("Restoring profile for card %s.", new_data->name); + pa_card_new_data_set_profile(new_data, e->profile); + new_data->save_profile = TRUE; } else pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name); @@ -208,10 +208,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); @@ -220,37 +219,23 @@ int pa__init(pa_module*m) { goto fail; } - m->userdata = u = pa_xnew(struct userdata, 1); + m->userdata = u = pa_xnew0(struct userdata, 1); u->core = m->core; u->module = m; - u->save_time_event = NULL; - u->gdbm_file = 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); @@ -286,8 +271,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); } -- cgit