From: Alok Barsode Date: Wed, 24 Aug 2011 13:53:18 +0000 (+0300) Subject: storage: Switch to settings file X-Git-Tag: 0.78~274 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=39245ddf5d1a220702a03683b78f1b868606c60a;p=platform%2Fupstream%2Fconnman.git storage: Switch to settings file All the global settings would reside in /var/lib/connman/settings. We also migrate global keys from /var/lib/connman/default.profile to /var/lib/connman/settings for a smooth transition. --- diff --git a/src/config.c b/src/config.c index 90a2180..db15228 100644 --- a/src/config.c +++ b/src/config.c @@ -383,7 +383,7 @@ static int load_config(struct connman_config *config) DBG("config %p", config); - keyfile = __connman_storage_open_config(config->ident); + keyfile = __connman_storage_load_config(config->ident); if (keyfile == NULL) return -EIO; @@ -418,7 +418,7 @@ static int load_config(struct connman_config *config) g_strfreev(groups); - __connman_storage_close_config(config->ident, keyfile, FALSE); + g_key_file_free(keyfile); return 0; } @@ -453,7 +453,7 @@ int __connman_config_load_service(GKeyFile *keyfile, const char *group, { struct connman_config *config; const char *service_name; - char *ident, *filename = NULL, *content = NULL; + char *ident, *content = NULL; gsize content_length; int err; @@ -491,26 +491,15 @@ int __connman_config_load_service(GKeyFile *keyfile, const char *group, goto out; } - filename = g_strdup_printf("%s/%s.config", STORAGEDIR, ident); - if (filename == NULL) { - err = -ENOMEM; - goto out; - } - DBG("Saving %zu bytes to %s", content_length, service_name); - if (g_file_set_contents(filename, content, - content_length, NULL) == FALSE) { - err = -EIO; - goto out; - } + __connman_storage_save_config(keyfile, ident); return 0; out: g_free(ident); g_free(content); - g_free(filename); return err; } diff --git a/src/connman.h b/src/connman.h index 179ef4f..d8c64b4 100644 --- a/src/connman.h +++ b/src/connman.h @@ -142,19 +142,14 @@ void __connman_resolver_cleanup(void); int __connman_resolvfile_append(const char *interface, const char *domain, const char *server); int __connman_resolvfile_remove(const char *interface, const char *domain, const char *server); -GKeyFile *__connman_storage_open(const char *ident, const char *suffix); -void __connman_storage_close(const char *ident, const char *suffix, - GKeyFile *keyfile, gboolean save); -void __connman_storage_delete(const char *ident, const char *suffix); - -GKeyFile *__connman_storage_open_profile(const char *ident); -void __connman_storage_close_profile(const char *ident, - GKeyFile *keyfile, gboolean save); -void __connman_storage_delete_profile(const char *ident); - -GKeyFile *__connman_storage_open_config(const char *ident); -void __connman_storage_close_config(const char *ident, - GKeyFile *keyfile, gboolean save); +void __connman_storage_migrate(void); +GKeyFile *__connman_storage_open_global(); +GKeyFile *__connman_storage_load_global(); +void __connman_storage_save_global(GKeyFile *keyfile); +void __connman_storage_delete_global(); + +GKeyFile *__connman_storage_load_config(const char *ident); +void __connman_storage_save_config(GKeyFile *keyfile, const char *ident); void __connman_storage_delete_config(const char *ident); int __connman_detect_init(void); diff --git a/src/main.c b/src/main.c index 959a43f..dd67cb9 100644 --- a/src/main.c +++ b/src/main.c @@ -334,6 +334,7 @@ int main(int argc, char *argv[]) parse_config(config); + __connman_storage_migrate(); __connman_technology_init(); __connman_notifier_init(); __connman_location_init(); diff --git a/src/service.c b/src/service.c index f084362..eda51fc 100644 --- a/src/service.c +++ b/src/service.c @@ -5124,7 +5124,7 @@ void __connman_service_read_ip4config(struct connman_service *service) if (service->ipconfig_ipv4 == NULL) return; - keyfile = __connman_storage_open_profile("default"); + keyfile = __connman_storage_load_global(); if (keyfile == NULL) return; @@ -5153,7 +5153,7 @@ void __connman_service_read_ip6config(struct connman_service *service) if (service->ipconfig_ipv6 == NULL) return; - keyfile = __connman_storage_open_profile("default"); + keyfile = __connman_storage_load_global(); if (keyfile == NULL) return; @@ -5172,6 +5172,7 @@ void __connman_service_create_ip6config(struct connman_service *service, return; setup_ip6config(service, index); + __connman_service_read_ip6config(service); } diff --git a/src/storage.c b/src/storage.c index 271d49c..faf2652 100644 --- a/src/storage.c +++ b/src/storage.c @@ -28,114 +28,225 @@ #include "connman.h" -#define PROFILE_SUFFIX "profile" -#define CONFIG_SUFFIX "config" +#define SETTINGS "settings" +#define DEFAULT "default.profile" -GKeyFile *__connman_storage_open(const char *ident, const char *suffix) +static GKeyFile *storage_load(const char *pathname) { - GKeyFile *keyfile; - gchar *pathname, *data = NULL; - gboolean result; - gsize length; + GKeyFile *keyfile = NULL; + GError *error = NULL; - DBG("ident %s suffix %s", ident, suffix); - - pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix); - if (pathname == NULL) - return NULL; - - result = g_file_get_contents(pathname, &data, &length, NULL); - - g_free(pathname); + DBG("Loading %s", pathname); keyfile = g_key_file_new(); - if (result == FALSE) - goto done; + if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) { + DBG("Unable to load %s: %s", pathname, error->message); + g_clear_error(&error); - if (length > 0) - g_key_file_load_from_data(keyfile, data, length, 0, NULL); - - g_free(data); - -done: - DBG("keyfile %p", keyfile); + g_key_file_free(keyfile); + keyfile = NULL; + } return keyfile; } -void __connman_storage_close(const char *ident, const char *suffix, - GKeyFile *keyfile, gboolean save) +static void storage_save(GKeyFile *keyfile, char *pathname) { - gchar *pathname, *data = NULL; + gchar *data = NULL; gsize length = 0; + GError *error = NULL; - DBG("ident %s suffix %s keyfile %p save %d", - ident, suffix, keyfile, save); + data = g_key_file_to_data(keyfile, &length, NULL); - if (save == FALSE) { - g_key_file_free(keyfile); - return; + if (!g_file_set_contents(pathname, data, length, &error)) { + DBG("Failed to store information: %s", error->message); + g_free(error); } - pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix); - if (pathname == NULL) - return; + g_free(data); +} - data = g_key_file_to_data(keyfile, &length, NULL); +static void storage_delete(const char *pathname) +{ + DBG("file path %s", pathname); - if (g_file_set_contents(pathname, data, length, NULL) == FALSE) - connman_error("Failed to store information"); + if (unlink(pathname) < 0) + connman_error("Failed to remove %s", pathname); +} - g_free(data); +GKeyFile *__connman_storage_load_global() +{ + gchar *pathname; + GKeyFile *keyfile = NULL; + + pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS); + if(pathname == NULL) + return NULL; + + keyfile = storage_load(pathname); g_free(pathname); - g_key_file_free(keyfile); + return keyfile; } -void __connman_storage_delete(const char *ident, const char *suffix) +void __connman_storage_save_global(GKeyFile *keyfile) { gchar *pathname; - DBG("ident %s suffix %s", ident, suffix); - - pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix); - if (pathname == NULL) + pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS); + if(pathname == NULL) return; - if (unlink(pathname) < 0) - connman_error("Failed to remove %s", pathname); -} + storage_save(keyfile, pathname); -GKeyFile *__connman_storage_open_profile(const char *ident) -{ - return __connman_storage_open(ident, PROFILE_SUFFIX); + g_free(pathname); } -void __connman_storage_close_profile(const char *ident, - GKeyFile *keyfile, gboolean save) +void __connman_storage_delete_global() { - __connman_storage_close(ident, PROFILE_SUFFIX, keyfile, save); + gchar *pathname; + + pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS); + if(pathname == NULL) + return; + + storage_delete(pathname); + + g_free(pathname); } -void __connman_storage_delete_profile(const char *ident) +GKeyFile *__connman_storage_load_config(const char *ident) { - __connman_storage_delete(ident, PROFILE_SUFFIX); + gchar *pathname; + GKeyFile *keyfile = NULL; + + pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident); + if(pathname == NULL) + return NULL; + + keyfile = storage_load(pathname); + + g_free(pathname); + + return keyfile; } -GKeyFile *__connman_storage_open_config(const char *ident) +void __connman_storage_save_config(GKeyFile *keyfile, const char *ident) { - return __connman_storage_open(ident, CONFIG_SUFFIX); + gchar *pathname; + + pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident); + if(pathname == NULL) + return; + + storage_save(keyfile, pathname); } -void __connman_storage_close_config(const char *ident, - GKeyFile *keyfile, gboolean save) +void __connman_storage_delete_config(const char *ident) { - __connman_storage_close(ident, CONFIG_SUFFIX, keyfile, save); + gchar *pathname; + + pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident); + if(pathname == NULL) + return; + + storage_delete(pathname); + + g_free(pathname); } -void __connman_storage_delete_config(const char *ident) +/* + * This function migrates keys from default.profile to settings file. + * This can be removed once the migration is over. +*/ +void __connman_storage_migrate() { - __connman_storage_delete(ident, CONFIG_SUFFIX); + gchar *pathname; + GKeyFile *keyfile_def = NULL; + GKeyFile *keyfile = NULL; + GError *error = NULL; + connman_bool_t val; + + /* If setting file exists, migration has been done. */ + keyfile = __connman_storage_load_global(); + if (keyfile) { + g_key_file_free(keyfile); + return; + } + + pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT); + if(pathname == NULL) + return; + + /* If default.profile doesn't exists, no need to migrate. */ + keyfile_def = storage_load(pathname); + if (keyfile_def == NULL) { + g_free(pathname); + return; + } + + /* Copy global settings from default.profile to settings. */ + keyfile = g_key_file_new(); + + /* offline mode */ + val = g_key_file_get_boolean(keyfile_def, "global", + "OfflineMode", &error); + if (error != NULL) + g_clear_error(&error); + else + g_key_file_set_boolean(keyfile, "global", + "OfflineMode", val); + + /* wifi */ + val = g_key_file_get_boolean(keyfile_def, "WiFi", + "Enable", &error); + if (error != NULL) + g_clear_error(&error); + else + g_key_file_set_boolean(keyfile, "WiFi", + "Enable", val); + + /* bluetooth */ + val = g_key_file_get_boolean(keyfile_def, "Bluetooth", + "Enable", &error); + if (error != NULL) + g_clear_error(&error); + else + g_key_file_set_boolean(keyfile, "Bluetooth", + "Enable", val); + + /* wired */ + val = g_key_file_get_boolean(keyfile_def, "Wired", + "Enable", &error); + if (error != NULL) + g_clear_error(&error); + else + g_key_file_set_boolean(keyfile, "Wired", + "Enable", val); + + /* 3G */ + val = g_key_file_get_boolean(keyfile_def, "3G", + "Enable", &error); + if (error != NULL) + g_clear_error(&error); + else + g_key_file_set_boolean(keyfile, "3G", + "Enable", val); + + /* WiMAX */ + val = g_key_file_get_boolean(keyfile_def, "WiMAX", + "Enable", &error); + if (error != NULL) + g_clear_error(&error); + else + g_key_file_set_boolean(keyfile, "WiMAX", + "Enable", val); + + __connman_storage_save_global(keyfile); + + g_key_file_free(keyfile); + g_key_file_free(keyfile_def); + g_free(pathname); } diff --git a/src/technology.c b/src/technology.c index 96d64af..7ae88c6 100644 --- a/src/technology.c +++ b/src/technology.c @@ -302,7 +302,7 @@ static const char *get_name(enum connman_service_type type) return NULL; } -static int load_state(struct connman_technology *technology) +static void load_state(struct connman_technology *technology) { GKeyFile *keyfile; gchar *identifier; @@ -311,9 +311,12 @@ static int load_state(struct connman_technology *technology) DBG("technology %p", technology); - keyfile = __connman_storage_open_profile("default"); - if (keyfile == NULL) - return 0; + keyfile = __connman_storage_load_global(); + /* Fallback on disabling technology if file not found. */ + if (keyfile == NULL) { + technology->enable_persistent = FALSE; + return; + } identifier = g_strdup_printf("%s", get_name(technology->type)); if (identifier == NULL) @@ -322,28 +325,28 @@ static int load_state(struct connman_technology *technology) enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error); if (error == NULL) technology->enable_persistent = enable; - else + else { technology->enable_persistent = FALSE; - - g_clear_error(&error); + g_clear_error(&error); + } done: g_free(identifier); - __connman_storage_close_profile("default", keyfile, FALSE); + g_key_file_free(keyfile); - return 0; + return; } -static int save_state(struct connman_technology *technology) +static void save_state(struct connman_technology *technology) { GKeyFile *keyfile; gchar *identifier; DBG("technology %p", technology); - keyfile = __connman_storage_open_profile("default"); + keyfile = __connman_storage_load_global(); if (keyfile == NULL) - return 0; + keyfile = g_key_file_new(); identifier = g_strdup_printf("%s", get_name(technology->type)); if (identifier == NULL) @@ -355,9 +358,11 @@ static int save_state(struct connman_technology *technology) done: g_free(identifier); - __connman_storage_close_profile("default", keyfile, TRUE); + __connman_storage_save_global(keyfile); - return 0; + g_key_file_free(keyfile); + + return; } connman_bool_t __connman_technology_get_offlinemode(void) @@ -365,20 +370,22 @@ connman_bool_t __connman_technology_get_offlinemode(void) return global_offlinemode; } -static int connman_technology_save_offlinemode() +static void connman_technology_save_offlinemode() { GKeyFile *keyfile; - keyfile = __connman_storage_open_profile("default"); + keyfile = __connman_storage_load_global(); if (keyfile == NULL) - return -EIO; + keyfile = g_key_file_new(); g_key_file_set_boolean(keyfile, "global", "OfflineMode", global_offlinemode); - __connman_storage_close_profile("default", keyfile, TRUE); + __connman_storage_save_global(keyfile); - return 0; + g_key_file_free(keyfile); + + return; } static connman_bool_t connman_technology_load_offlinemode() @@ -388,7 +395,7 @@ static connman_bool_t connman_technology_load_offlinemode() connman_bool_t offlinemode; /* If there is a error, we enable offlinemode */ - keyfile = __connman_storage_open_profile("default"); + keyfile = __connman_storage_load_global(); if (keyfile == NULL) return TRUE; @@ -398,7 +405,8 @@ static connman_bool_t connman_technology_load_offlinemode() offlinemode = TRUE; g_clear_error(&error); } - __connman_storage_close_profile("default", keyfile, FALSE); + + g_key_file_free(keyfile); return offlinemode; }