element: Remove IPv6
[framework/connectivity/connman.git] / src / storage.c
index 88443f9..800acfa 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 <unistd.h>
+
 #include "connman.h"
 
+#define PROFILE_SUFFIX "profile"
+#define CONFIG_SUFFIX  "config"
+
 static GSList *storage_list = NULL;
 
 static gint compare_priority(gconstpointer a, gconstpointer b)
@@ -66,17 +71,16 @@ void connman_storage_unregister(struct connman_storage *storage)
        storage_list = g_slist_remove(storage_list, storage);
 }
 
-GKeyFile *__connman_storage_open(void)
+GKeyFile *__connman_storage_open(const char *ident, const char *suffix)
 {
        GKeyFile *keyfile;
        gchar *pathname, *data = NULL;
        gboolean result;
        gsize length;
 
-       DBG("");
+       DBG("ident %s suffix %s", ident, suffix);
 
-       pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
-                                       __connman_profile_active_ident());
+       pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
        if (pathname == NULL)
                return NULL;
 
@@ -84,39 +88,37 @@ GKeyFile *__connman_storage_open(void)
 
        g_free(pathname);
 
-       if (result == FALSE)
-               return NULL;
-
        keyfile = g_key_file_new();
 
-       if (length > 0) {
-               if (g_key_file_load_from_data(keyfile, data, length,
-                                                       0, NULL) == FALSE)
-                       goto done;
-       }
+       if (result == FALSE)
+               goto done;
+
+       if (length > 0)
+               g_key_file_load_from_data(keyfile, data, length, 0, NULL);
 
-done:
        g_free(data);
 
+done:
        DBG("keyfile %p", keyfile);
 
        return keyfile;
 }
 
-void __connman_storage_close(GKeyFile *keyfile, gboolean save)
+void __connman_storage_close(const char *ident, const char *suffix,
+                                       GKeyFile *keyfile, gboolean save)
 {
        gchar *pathname, *data = NULL;
        gsize length = 0;
 
-       DBG("keyfile %p save %d", keyfile, save);
+       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.conf", STORAGEDIR,
-                                       __connman_profile_active_ident());
+       pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
        if (pathname == NULL)
                return;
 
@@ -132,71 +134,63 @@ void __connman_storage_close(GKeyFile *keyfile, gboolean save)
        g_key_file_free(keyfile);
 }
 
-int __connman_storage_load_global(void)
+void __connman_storage_delete(const char *ident, const char *suffix)
 {
-       GSList *list;
+       gchar *pathname;
 
-       DBG("");
-
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       DBG("ident %s suffix %s", ident, suffix);
 
-               if (storage->global_load) {
-                       if (storage->global_load() == 0)
-                               return 0;
-               }
-       }
+       pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
+       if (pathname == NULL)
+               return;
 
-       return -ENOENT;
+       if (unlink(pathname) < 0)
+               connman_error("Failed to remove %s", pathname);
 }
 
-int __connman_storage_save_global(void)
+GKeyFile *__connman_storage_open_profile(const char *ident)
 {
-       GSList *list;
-
-       DBG("");
-
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
-
-               if (storage->global_save) {
-                       if (storage->global_save() == 0)
-                               return 0;
-               }
-       }
-
-       return -ENOENT;
+       return __connman_storage_open(ident, PROFILE_SUFFIX);
 }
 
-int __connman_storage_init_device(void)
+void __connman_storage_close_profile(const char *ident,
+                                       GKeyFile *keyfile, gboolean save)
 {
-       GSList *list;
+       __connman_storage_close(ident, PROFILE_SUFFIX, keyfile, save);
+}
 
-       DBG("");
+void __connman_storage_delete_profile(const char *ident)
+{
+       __connman_storage_delete(ident, PROFILE_SUFFIX);
+}
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+GKeyFile *__connman_storage_open_config(const char *ident)
+{
+       return __connman_storage_open(ident, CONFIG_SUFFIX);
+}
 
-               if (storage->device_init) {
-                       if (storage->device_init() == 0)
-                               return 0;
-               }
-       }
+void __connman_storage_close_config(const char *ident,
+                                       GKeyFile *keyfile, gboolean save)
+{
+       __connman_storage_close(ident, CONFIG_SUFFIX, keyfile, save);
+}
 
-       return -ENOENT;
+void __connman_storage_delete_config(const char *ident)
+{
+       __connman_storage_delete(ident, CONFIG_SUFFIX);
 }
 
-int __connman_storage_load_device(struct connman_device *device)
+int __connman_storage_init_profile(void)
 {
        GSList *list;
 
-       DBG("device %p", device);
+       DBG("");
 
        for (list = storage_list; list; list = list->next) {
                struct connman_storage *storage = list->data;
 
-               if (storage->device_load) {
-                       if (storage->device_load(device) == 0)
+               if (storage->profile_init) {
+                       if (storage->profile_init() == 0)
                                return 0;
                }
        }
@@ -204,17 +198,17 @@ int __connman_storage_load_device(struct connman_device *device)
        return -ENOENT;
 }
 
-int __connman_storage_save_device(struct connman_device *device)
+int __connman_storage_load_profile(struct connman_profile *profile)
 {
        GSList *list;
 
-       DBG("device %p", device);
+       DBG("profile %p", profile);
 
        for (list = storage_list; list; list = list->next) {
                struct connman_storage *storage = list->data;
 
-               if (storage->device_save) {
-                       if (storage->device_save(device) == 0)
+               if (storage->profile_load) {
+                       if (storage->profile_load(profile) == 0)
                                return 0;
                }
        }
@@ -222,17 +216,17 @@ int __connman_storage_save_device(struct connman_device *device)
        return -ENOENT;
 }
 
-int __connman_storage_init_network(struct connman_device *device)
+int __connman_storage_save_profile(struct connman_profile *profile)
 {
        GSList *list;
 
-       DBG("device %p", device);
+       DBG("profile %p", profile);
 
        for (list = storage_list; list; list = list->next) {
                struct connman_storage *storage = list->data;
 
-               if (storage->network_init) {
-                       if (storage->network_init(device) == 0)
+               if (storage->profile_save) {
+                       if (storage->profile_save(profile) == 0)
                                return 0;
                }
        }
@@ -240,17 +234,17 @@ int __connman_storage_init_network(struct connman_device *device)
        return -ENOENT;
 }
 
-int __connman_storage_load_network(struct connman_network *network)
+int __connman_storage_load_service(struct connman_service *service)
 {
        GSList *list;
 
-       DBG("network %p", network);
+       DBG("service %p", service);
 
        for (list = storage_list; list; list = list->next) {
                struct connman_storage *storage = list->data;
 
-               if (storage->network_load) {
-                       if (storage->network_load(network) == 0)
+               if (storage->service_load) {
+                       if (storage->service_load(service) == 0)
                                return 0;
                }
        }
@@ -258,17 +252,17 @@ int __connman_storage_load_network(struct connman_network *network)
        return -ENOENT;
 }
 
-int __connman_storage_save_network(struct connman_network *network)
+int __connman_storage_save_service(struct connman_service *service)
 {
        GSList *list;
 
-       DBG("network %p", network);
+       DBG("service %p", service);
 
        for (list = storage_list; list; list = list->next) {
                struct connman_storage *storage = list->data;
 
-               if (storage->network_save) {
-                       if (storage->network_save(network) == 0)
+               if (storage->service_save) {
+                       if (storage->service_save(service) == 0)
                                return 0;
                }
        }
@@ -276,24 +270,17 @@ int __connman_storage_save_network(struct connman_network *network)
        return -ENOENT;
 }
 
-int __connman_storage_init_service(void)
-{
-       DBG("");
-
-       return -ENOENT;
-}
-
-int __connman_storage_load_service(struct connman_service *service)
+int __connman_storage_load_device(struct connman_device *device)
 {
        GSList *list;
 
-       DBG("service %p", service);
+       DBG("device %p", device);
 
        for (list = storage_list; list; list = list->next) {
                struct connman_storage *storage = list->data;
 
-               if (storage->service_load) {
-                       if (storage->service_load(service) == 0)
+               if (storage->device_load) {
+                       if (storage->device_load(device) == 0)
                                return 0;
                }
        }
@@ -301,17 +288,17 @@ int __connman_storage_load_service(struct connman_service *service)
        return -ENOENT;
 }
 
-int __connman_storage_save_service(struct connman_service *service)
+int __connman_storage_save_device(struct connman_device *device)
 {
        GSList *list;
 
-       DBG("service %p", service);
+       DBG("device %p", device);
 
        for (list = storage_list; list; list = list->next) {
                struct connman_storage *storage = list->data;
 
-               if (storage->service_save) {
-                       if (storage->service_save(service) == 0)
+               if (storage->device_save) {
+                       if (storage->device_save(device) == 0)
                                return 0;
                }
        }