element: Remove IPv6
[framework/connectivity/connman.git] / src / storage.c
index d8c32f4..800acfa 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2008  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 <unistd.h>
+
 #include "connman.h"
 
-int __connman_storage_init(void)
-{
-       DBG("");
+#define PROFILE_SUFFIX "profile"
+#define CONFIG_SUFFIX  "config"
 
-       return 0;
-}
+static GSList *storage_list = NULL;
 
-void __connman_storage_cleanup(void)
+static gint compare_priority(gconstpointer a, gconstpointer b)
 {
-       DBG("");
+       const struct connman_storage *storage1 = a;
+       const struct connman_storage *storage2 = b;
+
+       return storage2->priority - storage1->priority;
 }
 
-static int do_load(GKeyFile *keyfile, struct connman_element *element)
+/**
+ * connman_storage_register:
+ * @storage: storage module
+ *
+ * Register a new storage module
+ *
+ * Returns: %0 on success
+ */
+int connman_storage_register(struct connman_storage *storage)
 {
-       const gchar *value;
+       DBG("storage %p name %s", storage, storage->name);
 
-       DBG("element %p name %s", element, element->name);
+       storage_list = g_slist_insert_sorted(storage_list, storage,
+                                                       compare_priority);
 
-       value = g_key_file_get_string(keyfile, element->path,
-                                               "Policy", NULL);
-       if (value != NULL)
-               element->policy = __connman_element_string2policy(value);
-
-       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);
+       return 0;
+}
 
-       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);
+/**
+ * connman_storage_unregister:
+ * @storage: storage module
+ *
+ * Remove a previously registered storage module
+ */
+void connman_storage_unregister(struct connman_storage *storage)
+{
+       DBG("storage %p name %s", storage, storage->name);
 
-       return 0;
+       storage_list = g_slist_remove(storage_list, storage);
 }
 
-int __connman_element_load(struct connman_element *element)
+GKeyFile *__connman_storage_open(const char *ident, const char *suffix)
 {
        GKeyFile *keyfile;
        gchar *pathname, *data = NULL;
+       gboolean result;
        gsize length;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("ident %s suffix %s", ident, suffix);
 
-       pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
+       pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
        if (pathname == NULL)
-               return -ENOMEM;
+               return NULL;
+
+       result = g_file_get_contents(pathname, &data, &length, NULL);
+
+       g_free(pathname);
 
        keyfile = g_key_file_new();
 
-       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
-               g_free(pathname);
-               return -ENOENT;
-       }
+       if (result == FALSE)
+               goto done;
 
-       g_free(pathname);
+       if (length > 0)
+               g_key_file_load_from_data(keyfile, data, length, 0, NULL);
+
+       g_free(data);
+
+done:
+       DBG("keyfile %p", keyfile);
+
+       return keyfile;
+}
+
+void __connman_storage_close(const char *ident, const char *suffix,
+                                       GKeyFile *keyfile, gboolean save)
+{
+       gchar *pathname, *data = NULL;
+       gsize length = 0;
 
-       if (g_key_file_load_from_data(keyfile, data, length,
-                                                       0, NULL) == FALSE) {
-               g_free(data);
-               return -EILSEQ;
+       DBG("ident %s suffix %s keyfile %p save %d",
+                                       ident, suffix, keyfile, save);
+
+       if (save == FALSE) {
+               g_key_file_free(keyfile);
+               return;
        }
 
+       pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
+       if (pathname == NULL)
+               return;
+
+       data = g_key_file_to_data(keyfile, &length, NULL);
+
+       if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
+               connman_error("Failed to store information");
+
        g_free(data);
 
-       do_load(keyfile, element);
+       g_free(pathname);
 
        g_key_file_free(keyfile);
+}
 
-       return 0;
+void __connman_storage_delete(const char *ident, const char *suffix)
+{
+       gchar *pathname;
+
+       DBG("ident %s suffix %s", ident, suffix);
+
+       pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
+       if (pathname == NULL)
+               return;
+
+       if (unlink(pathname) < 0)
+               connman_error("Failed to remove %s", pathname);
+}
+
+GKeyFile *__connman_storage_open_profile(const char *ident)
+{
+       return __connman_storage_open(ident, PROFILE_SUFFIX);
+}
+
+void __connman_storage_close_profile(const char *ident,
+                                       GKeyFile *keyfile, gboolean save)
+{
+       __connman_storage_close(ident, PROFILE_SUFFIX, keyfile, save);
 }
 
-static void do_update(GKeyFile *keyfile, struct connman_element *element)
+void __connman_storage_delete_profile(const char *ident)
+{
+       __connman_storage_delete(ident, PROFILE_SUFFIX);
+}
+
+GKeyFile *__connman_storage_open_config(const char *ident)
+{
+       return __connman_storage_open(ident, CONFIG_SUFFIX);
+}
+
+void __connman_storage_close_config(const char *ident,
+                                       GKeyFile *keyfile, gboolean save)
+{
+       __connman_storage_close(ident, CONFIG_SUFFIX, keyfile, save);
+}
+
+void __connman_storage_delete_config(const char *ident)
+{
+       __connman_storage_delete(ident, CONFIG_SUFFIX);
+}
+
+int __connman_storage_init_profile(void)
 {
        GSList *list;
-       char *value;
-       const char *str;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("");
 
-       g_key_file_set_string(keyfile, element->path, "Name", element->name);
+       for (list = storage_list; list; list = list->next) {
+               struct connman_storage *storage = list->data;
 
-       str = __connman_element_policy2string(element->policy);
-       if (str != NULL)
-               g_key_file_set_string(keyfile, element->path, "Policy", str);
+               if (storage->profile_init) {
+                       if (storage->profile_init() == 0)
+                               return 0;
+               }
+       }
 
-       g_key_file_set_boolean(keyfile, element->path, "Enabled",
-                                                       element->enabled);
+       return -ENOENT;
+}
 
-       __connman_element_lock(element);
+int __connman_storage_load_profile(struct connman_profile *profile)
+{
+       GSList *list;
 
-       for (list = element->properties; list; list = list->next) {
-               struct connman_property *property = list->data;
+       DBG("profile %p", profile);
 
-               if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
-                       continue;
+       for (list = storage_list; list; list = list->next) {
+               struct connman_storage *storage = list->data;
 
-               if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
-                       continue;
+               if (storage->profile_load) {
+                       if (storage->profile_load(profile) == 0)
+                               return 0;
+               }
+       }
+
+       return -ENOENT;
+}
 
-               if (property->type == DBUS_TYPE_STRING)
-                       g_key_file_set_string(keyfile, element->path,
-                                       property->name, property->value);
+int __connman_storage_save_profile(struct connman_profile *profile)
+{
+       GSList *list;
+
+       DBG("profile %p", profile);
+
+       for (list = storage_list; list; list = list->next) {
+               struct connman_storage *storage = list->data;
+
+               if (storage->profile_save) {
+                       if (storage->profile_save(profile) == 0)
+                               return 0;
+               }
        }
 
-       __connman_element_unlock(element);
+       return -ENOENT;
+}
+
+int __connman_storage_load_service(struct connman_service *service)
+{
+       GSList *list;
 
-       if (connman_element_get_value(element,
-                       CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
-               g_key_file_set_string(keyfile, element->path,
-                                               "WiFi.Security", value);
+       DBG("service %p", service);
 
-       if (connman_element_get_value(element,
-                       CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
-               g_key_file_set_string(keyfile, element->path,
-                                               "WiFi.Passphrase", value);
+       for (list = storage_list; list; list = list->next) {
+               struct connman_storage *storage = list->data;
+
+               if (storage->service_load) {
+                       if (storage->service_load(service) == 0)
+                               return 0;
+               }
+       }
+
+       return -ENOENT;
 }
 
-int __connman_element_store(struct connman_element *element)
+int __connman_storage_save_service(struct connman_service *service)
 {
-       GKeyFile *keyfile;
-       gchar *pathname, *data = NULL;
-       gsize length;
+       GSList *list;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("service %p", service);
 
-       if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
-                               element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
-               return -EINVAL;
+       for (list = storage_list; list; list = list->next) {
+               struct connman_storage *storage = list->data;
 
-       if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE ||
-                       element->subtype == CONNMAN_ELEMENT_SUBTYPE_NETWORK)
-               return -EINVAL;
+               if (storage->service_save) {
+                       if (storage->service_save(service) == 0)
+                               return 0;
+               }
+       }
 
-       pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
-       if (pathname == NULL)
-               return -ENOMEM;
+       return -ENOENT;
+}
 
-       keyfile = g_key_file_new();
+int __connman_storage_load_device(struct connman_device *device)
+{
+       GSList *list;
+
+       DBG("device %p", device);
 
-       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
-               goto update;
+       for (list = storage_list; list; list = list->next) {
+               struct connman_storage *storage = list->data;
 
-       if (length > 0) {
-               if (g_key_file_load_from_data(keyfile, data, length,
-                               G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
-                       goto done;
+               if (storage->device_load) {
+                       if (storage->device_load(device) == 0)
+                               return 0;
+               }
        }
 
-       g_free(data);
+       return -ENOENT;
+}
 
-update:
-       do_update(keyfile, element);
+int __connman_storage_save_device(struct connman_device *device)
+{
+       GSList *list;
 
-       data = g_key_file_to_data(keyfile, &length, NULL);
+       DBG("device %p", device);
 
-       g_file_set_contents(pathname, data, length, NULL);
+       for (list = storage_list; list; list = list->next) {
+               struct connman_storage *storage = list->data;
 
-done:
-       g_free(data);
+               if (storage->device_save) {
+                       if (storage->device_save(device) == 0)
+                               return 0;
+               }
+       }
 
-       g_key_file_free(keyfile);
+       return -ENOENT;
+}
 
-       g_free(pathname);
+int __connman_storage_init(void)
+{
+       DBG("");
 
        return 0;
 }
+
+void __connman_storage_cleanup(void)
+{
+       DBG("");
+}