Updated connman to version 1.35
[platform/upstream/connman.git] / src / storage.c
old mode 100644 (file)
new mode 100755 (executable)
index e35a893..50c8e95
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2012  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
@@ -23,6 +23,7 @@
 #include <config.h>
 #endif
 
+#include <stdio.h>
 #include <errno.h>
 #include <unistd.h>
 #include <sys/stat.h>
@@ -43,8 +44,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)) {
@@ -73,6 +72,19 @@ static int storage_save(GKeyFile *keyfile, char *pathname)
                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;
@@ -86,13 +98,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);
@@ -108,7 +120,7 @@ int __connman_storage_save_global(GKeyFile *keyfile)
        int ret;
 
        pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
-       if(pathname == NULL)
+       if (!pathname)
                return -ENOMEM;
 
        ret = storage_save(keyfile, pathname);
@@ -118,12 +130,12 @@ int __connman_storage_save_global(GKeyFile *keyfile)
        return ret;
 }
 
-void __connman_storage_delete_global()
+void __connman_storage_delete_global(void)
 {
        gchar *pathname;
 
        pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
-       if(pathname == NULL)
+       if (!pathname)
                return;
 
        storage_delete(pathname);
@@ -137,7 +149,7 @@ GKeyFile *__connman_storage_load_config(const char *ident)
        GKeyFile *keyfile = NULL;
 
        pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
-       if(pathname == NULL)
+       if (!pathname)
                return NULL;
 
        keyfile = storage_load(pathname);
@@ -147,28 +159,20 @@ GKeyFile *__connman_storage_load_config(const char *ident)
        return keyfile;
 }
 
-void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
-{
-       gchar *pathname;
-
-       pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
-       if(pathname == NULL)
-               return;
-
-       storage_save(keyfile, pathname);
-}
-
-void __connman_storage_delete_config(const char *ident)
+GKeyFile *__connman_storage_load_provider_config(const char *ident)
 {
        gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
-       if(pathname == NULL)
-               return;
+       pathname = g_strdup_printf("%s/%s.config", VPN_STORAGEDIR, ident);
+       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)
@@ -177,7 +181,7 @@ GKeyFile *__connman_storage_open_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);
@@ -193,7 +197,7 @@ GKeyFile *__connman_storage_open_service(const char *service_id)
        return keyfile;
 }
 
-gchar **connman_storage_get_services()
+gchar **connman_storage_get_services(void)
 {
        struct dirent *d;
        gchar *str;
@@ -204,7 +208,7 @@ gchar **connman_storage_get_services()
        int ret;
 
        dir = opendir(STORAGEDIR);
-       if (dir == NULL)
+       if (!dir)
                return NULL;
 
        result = g_string_new(NULL);
@@ -217,6 +221,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.
@@ -236,7 +241,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);
        }
@@ -251,21 +260,11 @@ 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;
 }
@@ -276,12 +275,12 @@ int __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)
+       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 -errno;
@@ -300,6 +299,69 @@ int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
        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)
 {
        gchar *pathname;
@@ -307,7 +369,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);
@@ -322,10 +384,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;
@@ -338,6 +400,46 @@ void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
        g_free(pathname);
 }
 
+static bool remove_all(const char *id)
+{
+       bool removed;
+
+       remove_file(id, SETTINGS);
+       remove_file(id, "data");
+
+       removed = remove_dir(id);
+       if (!removed)
+               return false;
+
+       return true;
+}
+
+bool __connman_storage_remove_provider(const char *identifier)
+{
+       bool removed;
+       gchar *id;
+
+       id = g_strdup_printf("%s_%s", "provider", identifier);
+       if (!id)
+               return false;
+
+       if (remove_all(id))
+               DBG("Removed provider dir %s/%s", STORAGEDIR, id);
+
+       g_free(id);
+
+       id = g_strdup_printf("%s_%s", "vpn", identifier);
+       if (!id)
+               return false;
+
+       if ((removed = remove_all(id)))
+               DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
+
+       g_free(id);
+
+       return removed;
+}
+
 gchar **__connman_storage_get_providers(void)
 {
        GSList *list = NULL;
@@ -351,7 +453,7 @@ gchar **__connman_storage_get_providers(void)
        GSList *iter;
 
        dir = opendir(STORAGEDIR);
-       if (dir == NULL)
+       if (!dir)
                return NULL;
 
        while ((d = readdir(dir))) {
@@ -375,8 +477,8 @@ gchar **__connman_storage_get_providers(void)
        closedir(dir);
 
        providers = g_try_new0(char *, num + 1);
-       for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
-               if (providers != NULL)
+       for (iter = list; iter; iter = g_slist_next(iter)) {
+               if (providers)
                        providers[i] = iter->data;
                else
                        g_free(iter->data);
@@ -386,104 +488,3 @@ gchar **__connman_storage_get_providers(void)
 
        return providers;
 }
-
-/*
- * This function migrates keys from default.profile to settings file.
- * This can be removed once the migration is over.
-*/
-void __connman_storage_migrate()
-{
-       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 exists, create new settings file */
-       keyfile_def = storage_load(pathname);
-       if (keyfile_def == NULL)
-               goto done;
-
-       /* Copy global settings from default.profile to settings. */
-       keyfile = g_key_file_new();
-
-       val = g_key_file_get_boolean(keyfile_def, "global",
-                                       "OfflineMode", &error);
-       if (error != NULL) {
-               g_clear_error(&error);
-               val = FALSE;
-       }
-
-       g_key_file_set_boolean(keyfile, "global",
-                                       "OfflineMode", val);
-
-       val = g_key_file_get_boolean(keyfile_def, "WiFi",
-                                       "Enable", &error);
-       if (error != NULL) {
-               g_clear_error(&error);
-               val = FALSE;
-       }
-
-       g_key_file_set_boolean(keyfile, "WiFi",
-                                       "Enable", val);
-
-       val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
-                                       "Enable", &error);
-       if (error != NULL) {
-               g_clear_error(&error);
-               val = FALSE;
-       }
-
-       g_key_file_set_boolean(keyfile, "Bluetooth",
-                                       "Enable", val);
-
-       val = g_key_file_get_boolean(keyfile_def, "Wired",
-                                       "Enable", &error);
-       if (error != NULL) {
-               g_clear_error(&error);
-               val = FALSE;
-       }
-
-       g_key_file_set_boolean(keyfile, "Wired",
-                                       "Enable", val);
-
-       val = g_key_file_get_boolean(keyfile_def, "Cellular",
-                                       "Enable", &error);
-       if (error != NULL) {
-               g_clear_error(&error);
-               val = FALSE;
-       }
-
-       g_key_file_set_boolean(keyfile, "Cellular",
-                                       "Enable", val);
-
-       val = g_key_file_get_boolean(keyfile_def, "WiMAX",
-                                       "Enable", &error);
-       if (error != NULL) {
-               g_clear_error(&error);
-               val = FALSE;
-       }
-
-       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);
-
-done:
-       g_free(pathname);
-}