From d60b5edf7fd2ed4eb779e68a139359b4573385eb Mon Sep 17 00:00:00 2001 From: Alok Barsode Date: Thu, 25 Aug 2011 16:52:17 +0300 Subject: [PATCH] storage: Switch to directories Service settings would now reside in /var/lib/connman/service_id/settings. we fallback on /var/lib/connman/default.profile for a smooth transition. --- src/connman.h | 4 +++ src/service.c | 66 ++++++------------------------------------------ src/storage.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 58 deletions(-) diff --git a/src/connman.h b/src/connman.h index d8c64b4..4a37b70 100644 --- a/src/connman.h +++ b/src/connman.h @@ -152,6 +152,10 @@ 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); +GKeyFile *__connman_storage_open_service(const char *ident); +GKeyFile *__connman_storage_load_service(const char *ident); +void __connman_storage_save_service(GKeyFile *keyfile, const char *ident); + int __connman_detect_init(void); void __connman_detect_cleanup(void); diff --git a/src/service.c b/src/service.c index eda51fc..7a62f3d 100644 --- a/src/service.c +++ b/src/service.c @@ -299,10 +299,8 @@ static enum connman_service_proxy_method string2proxymethod(const char *method) static int service_load(struct connman_service *service) { - const char *ident = "default"; GKeyFile *keyfile; GError *error = NULL; - gchar *pathname, *data = NULL; gsize length; gchar *str; connman_bool_t autoconnect; @@ -311,29 +309,9 @@ static int service_load(struct connman_service *service) DBG("service %p", service); - if (ident == NULL) - return -EINVAL; - - pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident); - if (pathname == NULL) - return -ENOMEM; - - keyfile = g_key_file_new(); - - if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) { - g_free(pathname); - return -ENOENT; - } - - g_free(pathname); - - if (g_key_file_load_from_data(keyfile, data, length, - 0, NULL) == FALSE) { - g_free(data); - return -EILSEQ; - } - - g_free(data); + keyfile = __connman_storage_load_service(service->identifier); + if (keyfile == NULL) + return -EIO; switch (service->type) { case CONNMAN_SERVICE_TYPE_UNKNOWN: @@ -488,37 +466,17 @@ done: static int service_save(struct connman_service *service) { - const char *ident = "default"; GKeyFile *keyfile; - gchar *pathname, *data = NULL; - gsize length; gchar *str; const char *cst_str = NULL; int err = 0; DBG("service %p", service); - if (ident == NULL) - return -EINVAL; - - pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident); - if (pathname == NULL) - return -ENOMEM; - - keyfile = g_key_file_new(); - - if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) - goto update; - - if (length > 0) { - if (g_key_file_load_from_data(keyfile, data, length, - 0, NULL) == FALSE) - goto done; - } - - g_free(data); + keyfile = __connman_storage_open_service(service->identifier); + if (keyfile == NULL) + return -EIO; -update: if (service->name != NULL) g_key_file_set_string(keyfile, service->identifier, "Name", service->name); @@ -660,18 +618,11 @@ update: g_key_file_remove_key(keyfile, service->identifier, "Proxy.URL", NULL); - data = g_key_file_to_data(keyfile, &length, NULL); - - if (g_file_set_contents(pathname, data, length, NULL) == FALSE) - connman_error("Failed to store service information"); - done: - g_free(data); + __connman_storage_save_service(keyfile, service->identifier); g_key_file_free(keyfile); - g_free(pathname); - return err; } @@ -3453,7 +3404,6 @@ static void service_free(gpointer user_data) __connman_notifier_service_remove(service); stats_stop(service); - service_save(service); service->path = NULL; @@ -5153,7 +5103,7 @@ void __connman_service_read_ip6config(struct connman_service *service) if (service->ipconfig_ipv6 == NULL) return; - keyfile = __connman_storage_load_global(); + keyfile = __connman_storage_load_service(service->identifier); if (keyfile == NULL) return; diff --git a/src/storage.c b/src/storage.c index faf2652..4e30a77 100644 --- a/src/storage.c +++ b/src/storage.c @@ -25,12 +25,16 @@ #include #include +#include #include "connman.h" #define SETTINGS "settings" #define DEFAULT "default.profile" +#define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \ + S_IXGRP | S_IROTH | S_IXOTH) + static GKeyFile *storage_load(const char *pathname) { GKeyFile *keyfile = NULL; @@ -157,6 +161,83 @@ void __connman_storage_delete_config(const char *ident) g_free(pathname); } +GKeyFile *__connman_storage_open_service(const char *service_id) +{ + gchar *pathname; + GKeyFile *keyfile = NULL; + + pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS); + if(pathname == NULL) + return NULL; + + keyfile = storage_load(pathname); + if (keyfile) { + g_free(pathname); + return keyfile; + } + + g_free(pathname); + + keyfile = g_key_file_new(); + + return keyfile; +} + +GKeyFile *__connman_storage_load_service(const char *service_id) +{ + gchar *pathname; + GKeyFile *keyfile = NULL; + + pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS); + if(pathname == NULL) + return NULL; + + keyfile = storage_load(pathname); + if (keyfile) { + g_free(pathname); + return keyfile; + } + + g_free(pathname); + + pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT); + if(pathname == NULL) + return NULL; + + keyfile = storage_load(pathname); + + g_free(pathname); + + return keyfile; +} + +void __connman_storage_save_service(GKeyFile *keyfile, const char *service_id) +{ + gchar *pathname, *dirname; + + dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id); + if(dirname == NULL) + return; + + /* If the dir doesn't exist, create it */ + if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) { + if(mkdir(dirname, MODE) < 0) { + if (errno != EEXIST) { + g_free(dirname); + return; + } + } + } + + pathname = g_strdup_printf("%s/%s", dirname, SETTINGS); + + g_free(dirname); + + storage_save(keyfile, pathname); + + g_free(pathname); +} + /* * This function migrates keys from default.profile to settings file. * This can be removed once the migration is over. -- 2.7.4