From 3075ba51dfe9811150992de995419cb1f10701a7 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 22 Jul 2009 13:50:17 +0200 Subject: [PATCH] Add common helpers for keyfile storage --- src/connman.h | 3 +++ src/storage.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/connman.h b/src/connman.h index 0b5c091..ce286fc 100644 --- a/src/connman.h +++ b/src/connman.h @@ -128,6 +128,9 @@ int __connman_resolver_selftest(void); int __connman_storage_init(void); void __connman_storage_cleanup(void); +GKeyFile *__connman_storage_open(void); +void __connman_storage_close(GKeyFile *keyfile, gboolean save); + int __connman_storage_load_global(); int __connman_storage_save_global(); diff --git a/src/storage.c b/src/storage.c index 2018cf2..88443f9 100644 --- a/src/storage.c +++ b/src/storage.c @@ -66,6 +66,72 @@ void connman_storage_unregister(struct connman_storage *storage) storage_list = g_slist_remove(storage_list, storage); } +GKeyFile *__connman_storage_open(void) +{ + GKeyFile *keyfile; + gchar *pathname, *data = NULL; + gboolean result; + gsize length; + + DBG(""); + + pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, + __connman_profile_active_ident()); + if (pathname == NULL) + return NULL; + + result = g_file_get_contents(pathname, &data, &length, NULL); + + g_free(pathname); + + if (result == FALSE) + return NULL; + + keyfile = g_key_file_new(); + + if (length > 0) { + if (g_key_file_load_from_data(keyfile, data, length, + 0, NULL) == FALSE) + goto done; + } + +done: + g_free(data); + + DBG("keyfile %p", keyfile); + + return keyfile; +} + +void __connman_storage_close(GKeyFile *keyfile, gboolean save) +{ + gchar *pathname, *data = NULL; + gsize length = 0; + + DBG("keyfile %p save %d", keyfile, save); + + if (save == FALSE) { + g_key_file_free(keyfile); + return; + } + + pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, + __connman_profile_active_ident()); + if (pathname == NULL) + return; + + data = g_key_file_to_data(keyfile, &length, NULL); + + if (g_file_set_contents(pathname, data, length, NULL) == FALSE) + connman_error("Failed to store information"); + + g_free(data); + + g_free(pathname); + + g_key_file_free(keyfile); +} + int __connman_storage_load_global(void) { GSList *list; -- 2.7.4