dundee: Watch for signals only on DUNDEE_SERVICE
[framework/connectivity/connman.git] / src / storage.c
index 06337c7..47bd0cb 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2012  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
 #include <config.h>
 #endif
 
+#include <errno.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <dirent.h>
+
+#include <connman/storage.h>
+
 #include "connman.h"
 
-int __connman_storage_init(void)
+#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)
 {
-       DBG("");
+       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)) {
+               DBG("Unable to load %s: %s", pathname, error->message);
+               g_clear_error(&error);
+
+               g_key_file_free(keyfile);
+               keyfile = NULL;
+       }
+
+       return keyfile;
+}
+
+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_error_free(error);
+               ret = -EIO;
+       }
+
+       g_free(data);
 
-       return 0;
+       return ret;
 }
 
-void __connman_storage_cleanup(void)
+static void storage_delete(const char *pathname)
 {
-       DBG("");
+       DBG("file path %s", pathname);
+
+       if (unlink(pathname) < 0)
+               connman_error("Failed to remove %s", pathname);
 }
 
-static int do_load(GKeyFile *keyfile, struct connman_element *element)
+GKeyFile *__connman_storage_load_global()
 {
-       const gchar *value;
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       DBG("element %p name %s", element, element->name);
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if(pathname == NULL)
+               return NULL;
 
-       value = g_key_file_get_string(keyfile, element->path,
-                                               "WiFi.Security", NULL);
-       if (value != NULL)
-               connman_element_set_property(element,
-                               CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value);
+       keyfile = storage_load(pathname);
 
-       value = g_key_file_get_string(keyfile, element->path,
-                                               "WiFi.Passphrase", NULL);
-       if (value != NULL)
-               connman_element_set_property(element,
-                               CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value);
+       g_free(pathname);
 
-       return 0;
+       return keyfile;
 }
 
-int __connman_element_load(struct connman_element *element)
+int __connman_storage_save_global(GKeyFile *keyfile)
 {
-       GKeyFile *keyfile;
-       gchar *pathname, *data = NULL;
-       gsize length;
-
-       DBG("element %p name %s", element, element->name);
+       gchar *pathname;
+       int ret;
 
-       pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
-       if (pathname == NULL)
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if(pathname == NULL)
                return -ENOMEM;
 
-       keyfile = g_key_file_new();
+       ret = storage_save(keyfile, pathname);
+
+       g_free(pathname);
+
+       return ret;
+}
+
+void __connman_storage_delete_global()
+{
+       gchar *pathname;
+
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if(pathname == NULL)
+               return;
+
+       storage_delete(pathname);
 
-       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
+       g_free(pathname);
+}
+
+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 NULL;
+
+       keyfile = storage_load(pathname);
+
+       g_free(pathname);
+
+       return keyfile;
+}
+
+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 -ENOENT;
+               return keyfile;
        }
 
        g_free(pathname);
 
-       if (g_key_file_load_from_data(keyfile, data, length,
-                                                       0, NULL) == FALSE) {
-               g_free(data);
-               return -EILSEQ;
+       keyfile = g_key_file_new();
+
+       return keyfile;
+}
+
+gchar **connman_storage_get_services()
+{
+       struct dirent *d;
+       gchar *str;
+       DIR *dir;
+       GString *result;
+       gchar **services = NULL;
+       struct stat buf;
+       int ret;
+
+       dir = opendir(STORAGEDIR);
+       if (dir == NULL)
+               return NULL;
+
+       result = g_string_new(NULL);
+
+       while ((d = readdir(dir))) {
+               if (strcmp(d->d_name, ".") == 0 ||
+                               strcmp(d->d_name, "..") == 0 ||
+                               strncmp(d->d_name, "provider_", 9) == 0)
+                       continue;
+
+               switch (d->d_type) {
+               case DT_DIR:
+                       /*
+                        * If the settings file is not found, then
+                        * assume this directory is not a services dir.
+                        */
+                       str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
+                                                               d->d_name);
+                       ret = stat(str, &buf);
+                       g_free(str);
+                       if (ret < 0)
+                               continue;
+
+                       g_string_append_printf(result, "%s/", d->d_name);
+                       break;
+               }
        }
 
-       g_free(data);
+       closedir(dir);
 
-       do_load(keyfile, element);
+       str = g_string_free(result, FALSE);
+       if (str) {
+               str[strlen(str) - 1] = '\0';
+               services = g_strsplit(str, "/", -1);
+       }
+       g_free(str);
 
-       g_key_file_free(keyfile);
+       return services;
+}
 
-       return 0;
+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);
+       g_free(pathname);
+
+       return keyfile;
 }
 
-static void do_update(GKeyFile *keyfile, struct connman_element *element)
+int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
 {
-       GSList *list;
-       char *value;
+       int ret = 0;
+       gchar *pathname, *dirname;
 
-       DBG("element %p name %s", element, element->name);
+       dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
+       if(dirname == NULL)
+               return -ENOMEM;
 
-       g_key_file_set_string(keyfile, element->path, "Name", element->name);
+       /* 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 -errno;
+                       }
+               }
+       }
 
-       g_key_file_set_boolean(keyfile, element->path, "Enabled",
-                                                       element->enabled);
+       pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
 
-       connman_element_lock(element);
+       g_free(dirname);
 
-       for (list = element->properties; list; list = list->next) {
-               struct connman_property *property = list->data;
+       ret = storage_save(keyfile, pathname);
 
-               if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
-                       continue;
+       g_free(pathname);
 
-               if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
-                       continue;
+       return ret;
+}
 
-               if (property->type == DBUS_TYPE_STRING)
-                       g_key_file_set_string(keyfile, element->path,
-                                       property->name, property->value);
+static gboolean remove_file(const char *service_id, const char *file)
+{
+       gchar *pathname;
+       gboolean ret = FALSE;
+
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
+       if(pathname == NULL)
+               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) {
+               unlink(pathname);
+               ret = TRUE;
        }
 
-       connman_element_unlock(element);
+       g_free(pathname);
+       return ret;
+}
 
-       if (connman_element_get_value(element,
-                       CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
-               g_key_file_set_string(keyfile, element->path,
-                                               "WiFi.Security", value);
+static gboolean remove_dir(const char *service_id)
+{
+       gchar *pathname;
+       gboolean ret = FALSE;
+
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
+       if(pathname == NULL)
+               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) {
+               rmdir(pathname);
+               ret = TRUE;
+       }
 
-       if (connman_element_get_value(element,
-                       CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
-               g_key_file_set_string(keyfile, element->path,
-                                               "WiFi.Passphrase", value);
+       g_free(pathname);
+       return ret;
 }
 
-int __connman_element_store(struct connman_element *element)
+gboolean __connman_storage_remove_service(const char *service_id)
 {
+       gboolean removed;
+
+       /* Remove service configuration file */
+       removed = remove_file(service_id, SETTINGS);
+       if (removed == FALSE)
+               return FALSE;
+
+       /* Remove the statistics file also */
+       removed = remove_file(service_id, "data");
+       if (removed == FALSE)
+               return FALSE;
+
+       removed = remove_dir(service_id);
+       if (removed == FALSE)
+               return FALSE;
+
+       DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
+
+       return TRUE;
+}
+
+GKeyFile *__connman_storage_load_provider(const char *identifier)
+{
+       gchar *pathname;
        GKeyFile *keyfile;
-       gchar *pathname, *data = NULL;
-       gsize length;
 
-       DBG("element %p name %s", element, element->name);
+       pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
+                       identifier, SETTINGS);
+       if (pathname == NULL)
+               return NULL;
+
+       keyfile = storage_load(pathname);
+       g_free(pathname);
+
+       return keyfile;
+}
+
+void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
+{
+       gchar *pathname, *dirname;
+
+       dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
+                       "provider", identifier);
+       if (dirname == NULL)
+               return;
+
+       if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
+                       mkdir(dirname, MODE) < 0) {
+               g_free(dirname);
+               return;
+       }
 
-       if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
-                               element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
-               return -EINVAL;
+       pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
+       g_free(dirname);
+
+       storage_save(keyfile, pathname);
+       g_free(pathname);
+}
+
+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;
+
+       dir = opendir(STORAGEDIR);
+       if (dir == NULL)
+               return NULL;
+
+       while ((d = readdir(dir))) {
+               if (strcmp(d->d_name, ".") == 0 ||
+                               strcmp(d->d_name, "..") == 0 ||
+                               strncmp(d->d_name, "provider_", 9) != 0)
+                       continue;
+
+               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;
+               }
+       }
 
-       pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
+       closedir(dir);
+
+       providers = g_try_new0(char *, num + 1);
+       for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
+               if (providers != NULL)
+                       providers[i] = iter->data;
+               else
+                       g_free(iter->data);
+               i += 1;
+       }
+       g_slist_free(list);
+
+       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 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 -ENOMEM;
+               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], "wimax_", 6) != 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();
 
-       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
-               goto update;
+       val = g_key_file_get_boolean(keyfile_def, "global",
+                                       "OfflineMode", &error);
+       if (error != NULL) {
+               g_clear_error(&error);
+               val = FALSE;
+       }
 
-       if (length > 0) {
-               if (g_key_file_load_from_data(keyfile, data, length,
-                               G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
-                       goto done;
+       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_free(data);
+       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;
+               }
+       }
 
-update:
-       do_update(keyfile, element);
+       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;
+               }
+       }
 
-       data = g_key_file_to_data(keyfile, &length, NULL);
+       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);
+
+       val = g_key_file_get_boolean(keyfile_def, "WiMAX",
+                                       "Enable", &error);
+       if (error != NULL) {
+               g_clear_error(&error);
+               val = g_key_file_get_boolean(keyfile_def, "device_WiMAX", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
+               }
+       }
 
-       g_file_set_contents(pathname, data, length, NULL);
+       g_key_file_set_boolean(keyfile, "WiMAX",
+                                       "Enable", val);
 
-done:
-       g_free(data);
+       if (__connman_storage_save_global(keyfile) < 0) {
+               connman_warn("Migrating global config failed");
+               delete_old_config = FALSE;
+       }
 
        g_key_file_free(keyfile);
 
-       g_free(pathname);
+       g_key_file_free(keyfile_def);
 
-       return 0;
+       if (delete_old_config == TRUE) {
+               DBG("migration done for %s", pathname);
+               unlink(pathname);
+       }
+done:
+       g_free(pathname);
 }