diff options
author | Colin Guthrie <colin@mageia.org> | 2011-06-08 00:47:19 +0100 |
---|---|---|
committer | Colin Guthrie <colin@mageia.org> | 2011-06-22 23:12:20 +0100 |
commit | 9ffa9382dd8627aa12f1cffd67c96a6dacb4420f (patch) | |
tree | a367bd11d828550964a6e085f423718dfcdefccb /src/modules/module-device-manager.c | |
parent | 35f99c6e31d90439f2bfbefb8b45cff323b40661 (diff) |
database: Support legacy format database entries.
This adds code to specifically support legacy entries.
I kept this code in a separate commit so that it can be (relatively)
easily removed at some point in the future.
Diffstat (limited to 'src/modules/module-device-manager.c')
-rw-r--r-- | src/modules/module-device-manager.c | 188 |
1 files changed, 127 insertions, 61 deletions
diff --git a/src/modules/module-device-manager.c b/src/modules/module-device-manager.c index 6bac7c03..ea92cd0e 100644 --- a/src/modules/module-device-manager.c +++ b/src/modules/module-device-manager.c @@ -130,7 +130,7 @@ struct userdata { role_indexes_t preferred_sources; }; -#define ENTRY_VERSION 2 +#define ENTRY_VERSION 1 struct entry { uint8_t version; @@ -152,6 +152,44 @@ enum { }; +/* Forward declarations */ +#ifdef DUMP_DATABASE +static void dump_database(struct userdata *); +#endif +static void notify_subscribers(struct 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(u); + + pa_assert(e == u->save_time_event); + u->core->mainloop->time_free(u->save_time_event); + u->save_time_event = NULL; + + pa_database_sync(u->database); + pa_log_info("Synced."); + +#ifdef DUMP_DATABASE + dump_database(u); +#endif +} + +static void trigger_save(struct userdata *u) { + + pa_assert(u); + + notify_subscribers(u); + + if (u->save_time_event) + return; + + u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u); +} + static struct entry* entry_new(void) { struct entry *r = pa_xnew0(struct entry, 1); r->version = ENTRY_VERSION; @@ -165,6 +203,81 @@ static void entry_free(struct entry* e) { pa_xfree(e->icon); } +static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e) { + pa_tagstruct *t; + pa_datum key, data; + pa_bool_t r; + + pa_assert(u); + pa_assert(name); + pa_assert(e); + + t = pa_tagstruct_new(NULL, 0); + pa_tagstruct_putu8(t, e->version); + pa_tagstruct_puts(t, e->description); + pa_tagstruct_put_boolean(t, e->user_set_description); + pa_tagstruct_puts(t, e->icon); + for (uint8_t i=0; i<ROLE_MAX; ++i) + pa_tagstruct_putu32(t, e->priority[i]); + + key.data = (char *) name; + key.size = strlen(name); + + data.data = (void*)pa_tagstruct_data(t, &data.size); + + r = (pa_database_set(u->database, &key, &data, TRUE) == 0); + + pa_tagstruct_free(t); + + return r; +} + +#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT + +#define LEGACY_ENTRY_VERSION 1 +static struct entry* legacy_entry_read(struct userdata *u, pa_datum *data) { + struct legacy_entry { + uint8_t version; + char description[PA_NAME_MAX]; + pa_bool_t user_set_description; + char icon[PA_NAME_MAX]; + role_indexes_t priority; + } PA_GCC_PACKED; + struct legacy_entry *le; + struct entry *e; + + pa_assert(u); + pa_assert(data); + + if (data->size != sizeof(struct legacy_entry)) { + pa_log_debug("Size does not match."); + return NULL; + } + + le = (struct legacy_entry*)data->data; + + if (le->version != LEGACY_ENTRY_VERSION) { + pa_log_debug("Version mismatch."); + return NULL; + } + + if (!memchr(le->description, 0, sizeof(le->description))) { + pa_log_warn("Description has missing NUL byte."); + return NULL; + } + + if (!memchr(le->icon, 0, sizeof(le->icon))) { + pa_log_warn("Icon has missing NUL byte."); + return NULL; + } + + e = entry_new(); + e->description = pa_xstrdup(le->description); + e->icon = pa_xstrdup(le->icon); + return e; +} +#endif + static struct entry* entry_read(struct userdata *u, const char *name) { pa_datum key, data; struct entry *e = NULL; @@ -217,37 +330,21 @@ fail: entry_free(e); if (t) pa_tagstruct_free(t); - pa_datum_free(&data); - return NULL; -} - -static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e) { - pa_tagstruct *t; - pa_datum key, data; - pa_bool_t r; - - pa_assert(u); - pa_assert(name); - pa_assert(e); - - t = pa_tagstruct_new(NULL, 0); - pa_tagstruct_putu8(t, e->version); - pa_tagstruct_puts(t, e->description); - pa_tagstruct_put_boolean(t, e->user_set_description); - pa_tagstruct_puts(t, e->icon); - for (uint8_t i=0; i<ROLE_MAX; ++i) - pa_tagstruct_putu32(t, e->priority[i]); - - key.data = (char *) name; - key.size = strlen(name); - - data.data = (void*)pa_tagstruct_data(t, &data.size); - - r = (pa_database_set(u->database, &key, &data, TRUE) == 0); - pa_tagstruct_free(t); +#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT + pa_log_debug("Attempting to load legacy (pre-v1.0) data for key: %s", name); + if ((e = legacy_entry_read(u, &data))) { + pa_log_debug("Success. Saving new format for key: %s", name); + if (entry_write(u, name, e)) + trigger_save(u); + pa_datum_free(&data); + return e; + } else + pa_log_debug("Unable to load legacy (pre-v1.0) data for key: %s. Ignoring.", name); +#endif - return r; + pa_datum_free(&data); + return NULL; } #ifdef DUMP_DATABASE @@ -332,25 +429,6 @@ static void dump_database(struct userdata *u) { } #endif -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(u); - - pa_assert(e == u->save_time_event); - u->core->mainloop->time_free(u->save_time_event); - u->save_time_event = NULL; - - pa_database_sync(u->database); - pa_log_info("Synced."); - -#ifdef DUMP_DATABASE - dump_database(u); -#endif -} - static void notify_subscribers(struct userdata *u) { pa_native_connection *c; @@ -372,18 +450,6 @@ static void notify_subscribers(struct userdata *u) { } } -static void trigger_save(struct userdata *u) { - - pa_assert(u); - - notify_subscribers(u); - - if (u->save_time_event) - return; - - u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u); -} - static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) { pa_assert(a); |