X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fstorage.c;h=9be60defec27511c472101b11bff085bad991876;hb=c647a4b6f1132684c9d8b8ad71ec38d81147b278;hp=0f9c74df63939f186bbd7f80debb42fd5ec60ae6;hpb=c7fa40e99bf1bc6dd7c7c3ffe41ea7db35e8b6bf;p=platform%2Fupstream%2Fconnman.git diff --git a/src/storage.c b/src/storage.c old mode 100644 new mode 100755 index 0f9c74d..9be60de --- a/src/storage.c +++ b/src/storage.c @@ -2,7 +2,7 @@ * * Connection Manager * - * Copyright (C) 2007-2010 Intel Corporation. All rights reserved. + * Copyright (C) 2007-2013 Intel Corporation. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -34,6 +35,9 @@ #define SETTINGS "settings" #define DEFAULT "default.profile" +#if defined TIZEN_EXT +#define INS_SETTINGS "settings.ins" +#endif #define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \ S_IXGRP | S_IROTH | S_IXOTH) @@ -43,8 +47,6 @@ static GKeyFile *storage_load(const char *pathname) GKeyFile *keyfile = NULL; GError *error = NULL; - DBG("Loading %s", pathname); - keyfile = g_key_file_new(); if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) { @@ -58,20 +60,37 @@ static GKeyFile *storage_load(const char *pathname) return keyfile; } -static void storage_save(GKeyFile *keyfile, char *pathname) +static int storage_save(GKeyFile *keyfile, char *pathname) { gchar *data = NULL; gsize length = 0; GError *error = NULL; + int ret = 0; data = g_key_file_to_data(keyfile, &length, NULL); if (!g_file_set_contents(pathname, data, length, &error)) { DBG("Failed to store information: %s", error->message); - g_free(error); + g_error_free(error); + ret = -EIO; } +#if defined TIZEN_EXT + { + FILE *fp = NULL; + fp = fopen(pathname, "a+"); + if(fp){ + fflush(fp); + fsync(fp->_fileno); + fclose(fp); + DBG("sync the file to disk"); + } + } +#endif + g_free(data); + + return ret; } static void storage_delete(const char *pathname) @@ -82,13 +101,13 @@ static void storage_delete(const char *pathname) connman_error("Failed to remove %s", pathname); } -GKeyFile *__connman_storage_load_global() +GKeyFile *__connman_storage_load_global(void) { gchar *pathname; GKeyFile *keyfile = NULL; pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS); - if(pathname == NULL) + if (!pathname) return NULL; keyfile = storage_load(pathname); @@ -98,95 +117,102 @@ GKeyFile *__connman_storage_load_global() return keyfile; } -void __connman_storage_save_global(GKeyFile *keyfile) +int __connman_storage_save_global(GKeyFile *keyfile) { gchar *pathname; + int ret; pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS); - if(pathname == NULL) - return; + if (!pathname) + return -ENOMEM; - storage_save(keyfile, pathname); + ret = storage_save(keyfile, pathname); g_free(pathname); + + return ret; } -void __connman_storage_delete_global() +#if defined TIZEN_EXT +GKeyFile *__connman_storage_load_ins(void) { gchar *pathname; + GKeyFile *keyfile = NULL; - pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS); - if(pathname == NULL) - return; + pathname = g_strdup_printf("%s/%s", STORAGEDIR, INS_SETTINGS); + if (!pathname) + return NULL; - storage_delete(pathname); + keyfile = storage_load(pathname); g_free(pathname); + + return keyfile; } -GKeyFile *__connman_storage_load_config(const char *ident) +int __connman_storage_save_ins(GKeyFile *keyfile) { gchar *pathname; - GKeyFile *keyfile = NULL; + int ret; - pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident); - if(pathname == NULL) - return NULL; + pathname = g_strdup_printf("%s/%s", STORAGEDIR, INS_SETTINGS); + if (!pathname) + return -ENOMEM; - keyfile = storage_load(pathname); + ret = storage_save(keyfile, pathname); g_free(pathname); - return keyfile; + return ret; } +#endif -void __connman_storage_save_config(GKeyFile *keyfile, const char *ident) +void __connman_storage_delete_global(void) { gchar *pathname; - pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident); - if(pathname == NULL) + pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS); + if (!pathname) return; - storage_save(keyfile, pathname); + storage_delete(pathname); + + g_free(pathname); } -void __connman_storage_delete_config(const char *ident) +GKeyFile *__connman_storage_load_config(const char *ident) { gchar *pathname; + GKeyFile *keyfile = NULL; pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident); - if(pathname == NULL) - return; + if (!pathname) + return NULL; - storage_delete(pathname); + keyfile = storage_load(pathname); g_free(pathname); + + return keyfile; } -GKeyFile *__connman_storage_open_service(const char *service_id) +GKeyFile *__connman_storage_load_provider_config(const char *ident) { gchar *pathname; GKeyFile *keyfile = NULL; - pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS); - if(pathname == NULL) + pathname = g_strdup_printf("%s/%s.config", VPN_STORAGEDIR, ident); + if (!pathname) return NULL; - keyfile = storage_load(pathname); - if (keyfile) { - g_free(pathname); - return keyfile; - } + keyfile = storage_load(pathname); g_free(pathname); - keyfile = g_key_file_new(); - return keyfile; } -gchar **connman_storage_get_services() +gchar **connman_storage_get_services(void) { struct dirent *d; gchar *str; @@ -197,7 +223,7 @@ gchar **connman_storage_get_services() int ret; dir = opendir(STORAGEDIR); - if (dir == NULL) + if (!dir) return NULL; result = g_string_new(NULL); @@ -210,6 +236,7 @@ gchar **connman_storage_get_services() switch (d->d_type) { case DT_DIR: + case DT_UNKNOWN: /* * If the settings file is not found, then * assume this directory is not a services dir. @@ -229,7 +256,11 @@ gchar **connman_storage_get_services() closedir(dir); str = g_string_free(result, FALSE); - if (str) { + if (str && str[0] != '\0') { + /* + * Remove the trailing separator so that services doesn't end up + * with an empty element. + */ str[strlen(str) - 1] = '\0'; services = g_strsplit(str, "/", -1); } @@ -244,39 +275,30 @@ GKeyFile *connman_storage_load_service(const char *service_id) GKeyFile *keyfile = NULL; pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS); - if(pathname == NULL) + if (!pathname) return NULL; keyfile = storage_load(pathname); g_free(pathname); - if (keyfile) - return keyfile; - - 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) +int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id) { + int ret = 0; gchar *pathname, *dirname; dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id); - if(dirname == NULL) - return; + if (!dirname) + return -ENOMEM; /* If the dir doesn't exist, create it */ if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) { - if(mkdir(dirname, MODE) < 0) { + if (mkdir(dirname, MODE) < 0) { if (errno != EEXIST) { g_free(dirname); - return; + return -errno; } } } @@ -285,9 +307,74 @@ void __connman_storage_save_service(GKeyFile *keyfile, const char *service_id) g_free(dirname); - storage_save(keyfile, pathname); + ret = storage_save(keyfile, pathname); + + g_free(pathname); + + return ret; +} + +static bool remove_file(const char *service_id, const char *file) +{ + gchar *pathname; + bool ret = false; + + pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file); + if (!pathname) + return false; + + if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) { + ret = true; + } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR)) { + unlink(pathname); + ret = true; + } g_free(pathname); + return ret; +} + +static bool remove_dir(const char *service_id) +{ + gchar *pathname; + bool ret = false; + + pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id); + if (!pathname) + return false; + + if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) { + ret = true; + } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR)) { + rmdir(pathname); + ret = true; + } + + g_free(pathname); + return ret; +} + +bool __connman_storage_remove_service(const char *service_id) +{ + bool removed; + + /* Remove service configuration file */ + removed = remove_file(service_id, SETTINGS); + if (!removed) + return false; + + /* Remove the statistics file also */ + removed = remove_file(service_id, "data"); + if (!removed) + return false; + + removed = remove_dir(service_id); + if (!removed) + return false; + + DBG("Removed service dir %s/%s", STORAGEDIR, service_id); + + return true; } GKeyFile *__connman_storage_load_provider(const char *identifier) @@ -297,7 +384,7 @@ GKeyFile *__connman_storage_load_provider(const char *identifier) pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider", identifier, SETTINGS); - if (pathname == NULL) + if (!pathname) return NULL; keyfile = storage_load(pathname); @@ -312,10 +399,10 @@ void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier) dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR, "provider", identifier); - if (dirname == NULL) + if (!dirname) return; - if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE && + if (!g_file_test(dirname, G_FILE_TEST_IS_DIR) && mkdir(dirname, MODE) < 0) { g_free(dirname); return; @@ -328,129 +415,91 @@ void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier) g_free(pathname); } -/* - * This function migrates keys from default.profile to settings file. - * This can be removed once the migration is over. -*/ -void __connman_storage_migrate() +static bool remove_all(const char *id) { - 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; + bool removed; - /* Copy global settings from default.profile to settings. */ - keyfile = g_key_file_new(); - - /* If default.profile doesn't exists, create settings with defaults. */ - keyfile_def = storage_load(pathname); - if (keyfile_def == NULL) { - g_key_file_set_boolean(keyfile, "global", - "OfflineMode", FALSE); + remove_file(id, SETTINGS); + remove_file(id, "data"); - g_key_file_set_boolean(keyfile, "WiFi", - "Enable", FALSE); + removed = remove_dir(id); + if (!removed) + return false; - g_key_file_set_boolean(keyfile, "Bluetooth", - "Enable", FALSE); - - g_key_file_set_boolean(keyfile, "Wired", - "Enable", FALSE); + return true; +} - g_key_file_set_boolean(keyfile, "Cellular", - "Enable", FALSE); +bool __connman_storage_remove_provider(const char *identifier) +{ + bool removed; + gchar *id; - g_key_file_set_boolean(keyfile, "WiMAX", - "Enable", FALSE); + id = g_strdup_printf("%s_%s", "provider", identifier); + if (!id) + return false; - goto done; - } + if (remove_all(id)) + DBG("Removed provider dir %s/%s", STORAGEDIR, id); - /* offline mode */ - val = g_key_file_get_boolean(keyfile_def, "global", - "OfflineMode", &error); - if (error != NULL) { - g_clear_error(&error); - val = FALSE; - } + g_free(id); - g_key_file_set_boolean(keyfile, "global", - "OfflineMode", val); + id = g_strdup_printf("%s_%s", "vpn", identifier); + if (!id) + return false; - /* wifi */ - val = g_key_file_get_boolean(keyfile_def, "WiFi", - "Enable", &error); - if (error != NULL) { - g_clear_error(&error); - val = FALSE; - } + if ((removed = remove_all(id))) + DBG("Removed vpn dir %s/%s", STORAGEDIR, id); - g_key_file_set_boolean(keyfile, "WiFi", - "Enable", val); + g_free(id); - /* bluetooth */ - val = g_key_file_get_boolean(keyfile_def, "Bluetooth", - "Enable", &error); - if (error != NULL) { - g_clear_error(&error); - val = FALSE; - } + return removed; +} - g_key_file_set_boolean(keyfile, "Bluetooth", - "Enable", val); +gchar **__connman_storage_get_providers(void) +{ + GSList *list = NULL; + int num = 0, i = 0; + struct dirent *d; + gchar *str; + DIR *dir; + struct stat buf; + int ret; + char **providers; + GSList *iter; - /* wired */ - val = g_key_file_get_boolean(keyfile_def, "Wired", - "Enable", &error); - if (error != NULL) { - g_clear_error(&error); - val = FALSE; - } + dir = opendir(STORAGEDIR); + if (!dir) + return NULL; - g_key_file_set_boolean(keyfile, "Wired", - "Enable", val); + while ((d = readdir(dir))) { + if (strcmp(d->d_name, ".") == 0 || + strcmp(d->d_name, "..") == 0 || + strncmp(d->d_name, "provider_", 9) != 0) + continue; - /* cellular */ - val = g_key_file_get_boolean(keyfile_def, "Cellular", - "Enable", &error); - if (error != NULL) { - g_clear_error(&error); - val = FALSE; + if (d->d_type == DT_DIR) { + str = g_strdup_printf("%s/%s/settings", STORAGEDIR, + d->d_name); + ret = stat(str, &buf); + g_free(str); + if (ret < 0) + continue; + list = g_slist_prepend(list, g_strdup(d->d_name)); + num += 1; + } } - g_key_file_set_boolean(keyfile, "Cellular", - "Enable", val); + closedir(dir); - /* WiMAX */ - val = g_key_file_get_boolean(keyfile_def, "WiMAX", - "Enable", &error); - if (error != NULL) { - g_clear_error(&error); - val = FALSE; + providers = g_try_new0(char *, num + 1); + for (iter = list; iter; iter = g_slist_next(iter)) { + if (providers) + providers[i] = iter->data; + else + g_free(iter->data); + i += 1; } + g_slist_free(list); - g_key_file_set_boolean(keyfile, "WiMAX", - "Enable", val); - -done: - __connman_storage_save_global(keyfile); - - g_key_file_free(keyfile); - - if (keyfile_def) - g_key_file_free(keyfile_def); - - g_free(pathname); + return providers; }