storage: Load and save functions for providers
[platform/upstream/connman.git] / src / storage.c
index 6bbe051..1d14e13 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2010  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"
 
-static GSList *storage_list = NULL;
+#define SETTINGS       "settings"
+#define DEFAULT                "default.profile"
+
+#define MODE           (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
+                       S_IXGRP | S_IROTH | S_IXOTH)
 
-static gint compare_priority(gconstpointer a, gconstpointer b)
+static GKeyFile *storage_load(const char *pathname)
 {
-       const struct connman_storage *storage1 = a;
-       const struct connman_storage *storage2 = b;
+       GKeyFile *keyfile = NULL;
+       GError *error = NULL;
+
+       DBG("Loading %s", pathname);
+
+       keyfile = g_key_file_new();
 
-       return storage2->priority - storage1->priority;
+       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;
 }
 
-/**
- * connman_storage_register:
- * @storage: storage module
- *
- * Register a new storage module
- *
- * Returns: %0 on success
- */
-int connman_storage_register(struct connman_storage *storage)
+static void storage_save(GKeyFile *keyfile, char *pathname)
 {
-       DBG("storage %p name %s", storage, storage->name);
+       gchar *data = NULL;
+       gsize length = 0;
+       GError *error = NULL;
 
-       storage_list = g_slist_insert_sorted(storage_list, storage,
-                                                       compare_priority);
+       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);
+       }
 
-       return 0;
+       g_free(data);
 }
 
-/**
- * connman_storage_unregister:
- * @storage: storage module
- *
- * Remove a previously registered storage module
- */
-void connman_storage_unregister(struct connman_storage *storage)
+static void storage_delete(const char *pathname)
 {
-       DBG("storage %p name %s", storage, storage->name);
+       DBG("file path %s", pathname);
 
-       storage_list = g_slist_remove(storage_list, storage);
+       if (unlink(pathname) < 0)
+               connman_error("Failed to remove %s", pathname);
 }
 
-int __connman_storage_load_device(struct connman_device *device)
+GKeyFile *__connman_storage_load_global()
 {
-       GSList *list;
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       DBG("device %p", device);
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if(pathname == NULL)
+               return NULL;
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       keyfile = storage_load(pathname);
 
-               if (storage->device_load) {
-                       if (storage->device_load(device) == 0)
-                               return 0;
-               }
-       }
+       g_free(pathname);
 
-       return -ENOENT;
+       return keyfile;
 }
 
-int __connman_storage_save_device(struct connman_device *device)
+void __connman_storage_save_global(GKeyFile *keyfile)
 {
-       GSList *list;
+       gchar *pathname;
 
-       DBG("device %p", device);
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if(pathname == NULL)
+               return;
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       storage_save(keyfile, pathname);
 
-               if (storage->device_save) {
-                       if (storage->device_save(device) == 0)
-                               return 0;
-               }
-       }
-
-       return -ENOENT;
+       g_free(pathname);
 }
 
-int __connman_storage_load_network(struct connman_network *network)
+void __connman_storage_delete_global()
 {
-       GSList *list;
-
-       DBG("network %p", network);
+       gchar *pathname;
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if(pathname == NULL)
+               return;
 
-               if (storage->network_load) {
-                       if (storage->network_load(network) == 0)
-                               return 0;
-               }
-       }
+       storage_delete(pathname);
 
-       return -ENOENT;
+       g_free(pathname);
 }
 
-int __connman_storage_save_network(struct connman_network *network)
+GKeyFile *__connman_storage_load_config(const char *ident)
 {
-       GSList *list;
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       DBG("network %p", network);
+       pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
+       if(pathname == NULL)
+               return NULL;
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       keyfile = storage_load(pathname);
 
-               if (storage->network_save) {
-                       if (storage->network_save(network) == 0)
-                               return 0;
-               }
-       }
+       g_free(pathname);
 
-       return -ENOENT;
+       return keyfile;
 }
 
-int __connman_storage_init(void)
+void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
 {
-       DBG("");
+       gchar *pathname;
 
-       return 0;
-}
+       pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
+       if(pathname == NULL)
+               return;
 
-void __connman_storage_cleanup(void)
-{
-       DBG("");
+       storage_save(keyfile, pathname);
 }
 
-static int do_load(GKeyFile *keyfile, struct connman_element *element)
+void __connman_storage_delete_config(const char *ident)
 {
-       const gchar *value;
+       gchar *pathname;
 
-       DBG("element %p name %s", element, element->name);
+       pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
+       if(pathname == NULL)
+               return;
 
-       value = g_key_file_get_string(keyfile, element->path,
-                                               "Policy", NULL);
-       if (value != NULL)
-               element->policy = __connman_element_string2policy(value);
+       storage_delete(pathname);
 
-       if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
-               element->remember = g_key_file_get_boolean(keyfile,
-                                       element->path, "Remember", 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);
-
-       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);
-
-       return 0;
+       g_free(pathname);
 }
 
-int __connman_element_load(struct connman_element *element)
+GKeyFile *__connman_storage_open_service(const char *service_id)
 {
-       GKeyFile *keyfile;
-       gchar *pathname, *data = NULL;
-       gsize length;
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       DBG("element %p name %s", element, element->name);
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
+       if(pathname == NULL)
+               return NULL;
 
-       pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
-       if (pathname == NULL)
-               return -ENOMEM;
-
-       keyfile = g_key_file_new();
-
-       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
+       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;
-       }
-
-       g_free(data);
-
-       do_load(keyfile, element);
-
-       g_key_file_free(keyfile);
+       keyfile = g_key_file_new();
 
-       return 0;
+       return keyfile;
 }
 
-static void do_update(GKeyFile *keyfile, struct connman_element *element)
+gchar **connman_storage_get_services()
 {
-       GSList *list;
-       char *value;
-       const char *str;
+       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;
 
-       DBG("element %p name %s", element, element->name);
+               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;
+               }
+       }
+
+       closedir(dir);
+
+       str = g_string_free(result, FALSE);
+       if (str) {
+               str[strlen(str) - 1] = '\0';
+               services = g_strsplit(str, "/", -1);
+       }
+       g_free(str);
 
-       g_key_file_set_string(keyfile, element->path, "Name", element->name);
+       return services;
+}
 
-       str = __connman_element_policy2string(element->policy);
-       if (str != NULL)
-               g_key_file_set_string(keyfile, element->path, "Policy", str);
+GKeyFile *connman_storage_load_service(const char *service_id)
+{
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       //g_key_file_set_boolean(keyfile, element->path, "Enabled",
-       //                                              element->enabled);
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
+       if(pathname == NULL)
+               return NULL;
 
-       if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
-               g_key_file_set_boolean(keyfile, element->path, "Remember",
-                                                       element->remember);
+       keyfile =  storage_load(pathname);
+       g_free(pathname);
+       if (keyfile)
+               return keyfile;
 
-       __connman_element_lock(element);
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
+       if(pathname == NULL)
+               return NULL;
 
-       for (list = element->properties; list; list = list->next) {
-               struct connman_property *property = list->data;
+       keyfile =  storage_load(pathname);
 
-               if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
-                       continue;
+       g_free(pathname);
 
-               if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
-                       continue;
+       return keyfile;
+}
 
-               if (property->type == DBUS_TYPE_STRING)
-                       g_key_file_set_string(keyfile, element->path,
-                                       property->name, property->value);
+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;
+                       }
+               }
        }
 
-       __connman_element_unlock(element);
+       pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
 
-       if (connman_element_get_value(element,
-                       CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
-               g_key_file_set_string(keyfile, element->path,
-                                               "WiFi.Security", value);
+       g_free(dirname);
 
-       if (connman_element_get_value(element,
-                       CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
-               g_key_file_set_string(keyfile, element->path,
-                                               "WiFi.Passphrase", value);
+       storage_save(keyfile, pathname);
+
+       g_free(pathname);
 }
 
-int __connman_element_store(struct connman_element *element)
+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);
-
-       if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
-                               element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
-               return -EINVAL;
+       pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
+                       identifier, SETTINGS);
+       if (pathname == NULL)
+               return NULL;
 
-       if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE)
-               return -EINVAL;
+       keyfile = storage_load(pathname);
+       g_free(pathname);
 
-       pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
-       if (pathname == NULL)
-               return -ENOMEM;
+       return keyfile;
+}
 
-       keyfile = g_key_file_new();
+void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
+{
+       gchar *pathname, *dirname;
 
-       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
-               goto update;
+       dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
+                       "provider", identifier);
+       if (dirname == NULL)
+               return;
 
-       if (length > 0) {
-               if (g_key_file_load_from_data(keyfile, data, length,
-                                                       0, NULL) == FALSE)
-                       goto done;
+       if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
+                       mkdir(dirname, MODE) < 0) {
+               g_free(dirname);
+               return;
        }
 
-       g_free(data);
+       pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
+       g_free(dirname);
 
-update:
-       do_update(keyfile, element);
+       storage_save(keyfile, pathname);
+       g_free(pathname);
+}
 
-       data = g_key_file_to_data(keyfile, &length, NULL);
+/*
+ * 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;
+       }
 
-       g_file_set_contents(pathname, data, length, NULL);
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
+       if(pathname == NULL)
+               return;
 
-done:
-       g_free(data);
+       /* If default.profile doesn't exists, no need to migrate. */
+       keyfile_def = storage_load(pathname);
+       if (keyfile_def == NULL) {
+               g_free(pathname);
+               return;
+       }
 
-       g_key_file_free(keyfile);
+       /* Copy global settings from default.profile to settings. */
+       keyfile = g_key_file_new();
 
-       g_free(pathname);
+       /* offline mode */
+       val = g_key_file_get_boolean(keyfile_def, "global",
+                                       "OfflineMode", &error);
+       if (error != NULL)
+               g_clear_error(&error);
+       else
+               g_key_file_set_boolean(keyfile, "global",
+                                       "OfflineMode", val);
+
+       /* wifi */
+       val = g_key_file_get_boolean(keyfile_def, "WiFi",
+                                       "Enable", &error);
+       if (error != NULL)
+               g_clear_error(&error);
+       else
+               g_key_file_set_boolean(keyfile, "WiFi",
+                                       "Enable", val);
+
+       /* bluetooth */
+       val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
+                                       "Enable", &error);
+       if (error != NULL)
+               g_clear_error(&error);
+       else
+               g_key_file_set_boolean(keyfile, "Bluetooth",
+                                       "Enable", val);
+
+       /* wired */
+       val = g_key_file_get_boolean(keyfile_def, "Wired",
+                                       "Enable", &error);
+       if (error != NULL)
+               g_clear_error(&error);
+       else
+               g_key_file_set_boolean(keyfile, "Wired",
+                                       "Enable", val);
+
+       /* 3G */
+       val = g_key_file_get_boolean(keyfile_def, "3G",
+                                       "Enable", &error);
+       if (error != NULL)
+               g_clear_error(&error);
+       else
+               g_key_file_set_boolean(keyfile, "3G",
+                                       "Enable", val);
+
+       /* WiMAX */
+       val = g_key_file_get_boolean(keyfile_def, "WiMAX",
+                                       "Enable", &error);
+       if (error != NULL)
+               g_clear_error(&error);
+       else
+               g_key_file_set_boolean(keyfile, "WiMAX",
+                                       "Enable", val);
+
+       __connman_storage_save_global(keyfile);
 
-       return 0;
+       g_key_file_free(keyfile);
+       g_key_file_free(keyfile_def);
+       g_free(pathname);
 }