5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 static GSList *storage_list = NULL;
32 static gint compare_priority(gconstpointer a, gconstpointer b)
34 const struct connman_storage *storage1 = a;
35 const struct connman_storage *storage2 = b;
37 return storage2->priority - storage1->priority;
41 * connman_storage_register:
42 * @storage: storage module
44 * Register a new storage module
46 * Returns: %0 on success
48 int connman_storage_register(struct connman_storage *storage)
50 DBG("storage %p name %s", storage, storage->name);
52 storage_list = g_slist_insert_sorted(storage_list, storage,
59 * connman_storage_unregister:
60 * @storage: storage module
62 * Remove a previously registered storage module
64 void connman_storage_unregister(struct connman_storage *storage)
66 DBG("storage %p name %s", storage, storage->name);
68 storage_list = g_slist_remove(storage_list, storage);
71 GKeyFile *__connman_storage_open(const char *ident)
74 gchar *pathname, *data = NULL;
78 DBG("ident %s", ident);
80 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
84 result = g_file_get_contents(pathname, &data, &length, NULL);
88 keyfile = g_key_file_new();
94 g_key_file_load_from_data(keyfile, data, length, 0, NULL);
99 DBG("keyfile %p", keyfile);
104 void __connman_storage_close(const char *ident,
105 GKeyFile *keyfile, gboolean save)
107 gchar *pathname, *data = NULL;
110 DBG("ident %s keyfile %p save %d", ident, keyfile, save);
113 g_key_file_free(keyfile);
117 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
118 if (pathname == NULL)
121 data = g_key_file_to_data(keyfile, &length, NULL);
123 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
124 connman_error("Failed to store information");
130 g_key_file_free(keyfile);
133 void __connman_storage_delete(const char *ident)
137 DBG("ident %s", ident);
139 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
140 if (pathname == NULL)
143 if (unlink(pathname) < 0)
144 connman_error("Failed to remove %s", pathname);
147 int __connman_storage_init_profile(void)
153 for (list = storage_list; list; list = list->next) {
154 struct connman_storage *storage = list->data;
156 if (storage->profile_init) {
157 if (storage->profile_init() == 0)
165 int __connman_storage_load_profile(struct connman_profile *profile)
169 DBG("profile %p", profile);
171 for (list = storage_list; list; list = list->next) {
172 struct connman_storage *storage = list->data;
174 if (storage->profile_load) {
175 if (storage->profile_load(profile) == 0)
183 int __connman_storage_save_profile(struct connman_profile *profile)
187 DBG("profile %p", profile);
189 for (list = storage_list; list; list = list->next) {
190 struct connman_storage *storage = list->data;
192 if (storage->profile_save) {
193 if (storage->profile_save(profile) == 0)
201 int __connman_storage_load_service(struct connman_service *service)
205 DBG("service %p", service);
207 for (list = storage_list; list; list = list->next) {
208 struct connman_storage *storage = list->data;
210 if (storage->service_load) {
211 if (storage->service_load(service) == 0)
219 int __connman_storage_save_service(struct connman_service *service)
223 DBG("service %p", service);
225 for (list = storage_list; list; list = list->next) {
226 struct connman_storage *storage = list->data;
228 if (storage->service_save) {
229 if (storage->service_save(service) == 0)
237 int __connman_storage_load_device(struct connman_device *device)
241 DBG("device %p", device);
243 for (list = storage_list; list; list = list->next) {
244 struct connman_storage *storage = list->data;
246 if (storage->device_load) {
247 if (storage->device_load(device) == 0)
255 int __connman_storage_save_device(struct connman_device *device)
259 DBG("device %p", device);
261 for (list = storage_list; list; list = list->next) {
262 struct connman_storage *storage = list->data;
264 if (storage->device_save) {
265 if (storage->device_save(device) == 0)
273 int __connman_storage_init(void)
280 void __connman_storage_cleanup(void)