Imported Upstream connman version 1.38
[platform/upstream/connman.git] / src / storage.c
old mode 100644 (file)
new mode 100755 (executable)
index 3c99795..9be60de
@@ -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
@@ -27,6 +27,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <dirent.h>
+#include <stdio.h>
 
 #include <connman/storage.h>
 
@@ -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)) {
@@ -73,6 +75,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;
@@ -92,7 +107,7 @@ GKeyFile *__connman_storage_load_global(void)
        GKeyFile *keyfile = NULL;
 
        pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
-       if(pathname == NULL)
+       if (!pathname)
                return NULL;
 
        keyfile = storage_load(pathname);
@@ -108,7 +123,40 @@ 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);
+
+       g_free(pathname);
+
+       return ret;
+}
+
+#if defined TIZEN_EXT
+GKeyFile *__connman_storage_load_ins(void)
+{
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
+
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, INS_SETTINGS);
+       if (!pathname)
+               return NULL;
+
+       keyfile = storage_load(pathname);
+
+       g_free(pathname);
+
+       return keyfile;
+}
+
+int __connman_storage_save_ins(GKeyFile *keyfile)
+{
+       gchar *pathname;
+       int ret;
+
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, INS_SETTINGS);
+       if (!pathname)
                return -ENOMEM;
 
        ret = storage_save(keyfile, pathname);
@@ -117,13 +165,14 @@ int __connman_storage_save_global(GKeyFile *keyfile)
 
        return ret;
 }
+#endif
 
 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 +186,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,25 +196,19 @@ GKeyFile *__connman_storage_load_config(const char *ident)
        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;
 }
 
@@ -180,7 +223,7 @@ gchar **connman_storage_get_services(void)
        int ret;
 
        dir = opendir(STORAGEDIR);
-       if (dir == NULL)
+       if (!dir)
                return NULL;
 
        result = g_string_new(NULL);
@@ -232,7 +275,7 @@ 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);
@@ -247,12 +290,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;
@@ -271,67 +314,67 @@ int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
        return ret;
 }
 
-static gboolean remove_file(const char *service_id, const char *file)
+static bool remove_file(const char *service_id, const char *file)
 {
        gchar *pathname;
-       gboolean ret = FALSE;
+       bool ret = false;
 
        pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
-       if(pathname == NULL)
-               return FALSE;
+       if (!pathname)
+               return false;
 
-       if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
-               ret = TRUE;
-       } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR) == TRUE) {
+       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;
+               ret = true;
        }
 
        g_free(pathname);
        return ret;
 }
 
-static gboolean remove_dir(const char *service_id)
+static bool remove_dir(const char *service_id)
 {
        gchar *pathname;
-       gboolean ret = FALSE;
+       bool ret = false;
 
        pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
-       if(pathname == NULL)
-               return FALSE;
+       if (!pathname)
+               return false;
 
-       if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
-               ret = TRUE;
-       } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR) == TRUE) {
+       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;
+               ret = true;
        }
 
        g_free(pathname);
        return ret;
 }
 
-gboolean __connman_storage_remove_service(const char *service_id)
+bool __connman_storage_remove_service(const char *service_id)
 {
-       gboolean removed;
+       bool removed;
 
        /* Remove service configuration file */
        removed = remove_file(service_id, SETTINGS);
-       if (removed == FALSE)
-               return FALSE;
+       if (!removed)
+               return false;
 
        /* Remove the statistics file also */
        removed = remove_file(service_id, "data");
-       if (removed == FALSE)
-               return FALSE;
+       if (!removed)
+               return false;
 
        removed = remove_dir(service_id);
-       if (removed == FALSE)
-               return FALSE;
+       if (!removed)
+               return false;
 
        DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
 
-       return TRUE;
+       return true;
 }
 
 GKeyFile *__connman_storage_load_provider(const char *identifier)
@@ -341,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);
@@ -356,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;
@@ -372,39 +415,39 @@ void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
        g_free(pathname);
 }
 
-static gboolean remove_all(const char *id)
+static bool remove_all(const char *id)
 {
-       gboolean removed;
+       bool removed;
 
        remove_file(id, SETTINGS);
        remove_file(id, "data");
 
        removed = remove_dir(id);
-       if (removed == FALSE)
-               return FALSE;
+       if (!removed)
+               return false;
 
-       return TRUE;
+       return true;
 }
 
-gboolean __connman_storage_remove_provider(const char *identifier)
+bool __connman_storage_remove_provider(const char *identifier)
 {
-       gboolean removed;
+       bool removed;
        gchar *id;
 
        id = g_strdup_printf("%s_%s", "provider", identifier);
-       if (id == NULL)
-               return FALSE;
+       if (!id)
+               return false;
 
-       if (remove_all(id) == TRUE)
+       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 == NULL)
-               return FALSE;
+       if (!id)
+               return false;
 
-       if ((removed = remove_all(id)) == TRUE)
+       if ((removed = remove_all(id)))
                DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
 
        g_free(id);
@@ -425,7 +468,7 @@ gchar **__connman_storage_get_providers(void)
        GSList *iter;
 
        dir = opendir(STORAGEDIR);
-       if (dir == NULL)
+       if (!dir)
                return NULL;
 
        while ((d = readdir(dir))) {
@@ -449,8 +492,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);
@@ -460,178 +503,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(void)
-{
-       gchar *pathname;
-       GKeyFile *keyfile_def = NULL;
-       GKeyFile *keyfile = NULL;
-       GError *error = NULL;
-       connman_bool_t delete_old_config = TRUE;
-       char **services, **keys, *value;
-       int i, k, err;
-       connman_bool_t val;
-
-       pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
-       if (pathname == NULL)
-               return;
-
-       /* If setting file exists, migration has been done. */
-       keyfile = __connman_storage_load_global();
-       if (keyfile) {
-               g_key_file_free(keyfile);
-               unlink(pathname);
-               g_free(pathname);
-               return;
-       }
-
-       /* If default.profile exists, create new settings file */
-       keyfile_def = storage_load(pathname);
-       if (keyfile_def == NULL)
-               goto done;
-
-       services = g_key_file_get_groups(keyfile_def, NULL);
-       for (i = 0; services != NULL && services[i] != NULL; i++) {
-               if (strncmp(services[i], "wifi_", 5) != 0 &&
-                               strncmp(services[i], "ethernet_", 9) != 0 &&
-                               strncmp(services[i], "cellular_", 9) != 0 &&
-                               strncmp(services[i], "bluetooth_", 10) != 0 &&
-                               strncmp(services[i], "vpn_", 4) != 0)
-                       continue;
-
-               keyfile = connman_storage_load_service(services[i]);
-               if (keyfile != NULL) {
-                       g_key_file_free(keyfile);
-                       DBG("already exists %s", services[i]);
-                       continue;
-               }
-
-               keyfile = g_key_file_new();
-               if (keyfile == NULL) {
-                       connman_warn("Migrating %s failed", services[i]);
-                       delete_old_config = FALSE;
-                       continue;
-               }
-
-               keys = g_key_file_get_keys(keyfile_def, services[i],
-                               NULL, NULL);
-
-               for (k = 0; keys != NULL && keys[k] != NULL; k++) {
-                       value = g_key_file_get_value(keyfile_def, services[i],
-                                       keys[k], NULL);
-                       g_key_file_set_value(keyfile, services[i],
-                                       keys[k], value);
-                       g_free(value);
-               }
-
-               if (keys != NULL && keys[0] != NULL) {
-                       err = __connman_storage_save_service(keyfile,
-                                       services[i]);
-                       if (err >= 0)
-                               DBG("migrated %s", services[i]);
-                       else {
-                               connman_warn("Migrating %s failed %s",
-                                               services[i], strerror(-err));
-                               delete_old_config = FALSE;
-                       }
-               } else
-                       DBG("no keys in %s", services[i]);
-
-               g_strfreev(keys);
-               g_key_file_free(keyfile);
-       }
-       g_strfreev(services);
-
-       /* 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);
-
-       /* Migrate Powered/Enable state key/value pairs from legacy
-        * settings
-        */
-
-       val = g_key_file_get_boolean(keyfile_def, "WiFi",
-                                       "Enable", &error);
-       if (error != NULL) {
-               g_clear_error(&error);
-               val = g_key_file_get_boolean(keyfile_def, "device_Wireless", "Powered", &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 = g_key_file_get_boolean(keyfile_def, "device_Bluetooth", "Powered", &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 = g_key_file_get_boolean(keyfile_def, "device_Ethernet", "Powered", &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 = g_key_file_get_boolean(keyfile_def, "device_Cellular", "Powered", &error);
-               if (error != NULL) {
-                       g_clear_error(&error);
-                       val = FALSE;
-               }
-       }
-
-       g_key_file_set_boolean(keyfile, "Cellular",
-                                       "Enable", val);
-
-       if (__connman_storage_save_global(keyfile) < 0) {
-               connman_warn("Migrating global config failed");
-               delete_old_config = FALSE;
-       }
-
-       g_key_file_free(keyfile);
-
-       g_key_file_free(keyfile_def);
-
-       if (delete_old_config == TRUE) {
-               DBG("migration done for %s", pathname);
-               unlink(pathname);
-       }
-done:
-       g_free(pathname);
-}