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 #define PROFILE_SUFFIX "profile"
32 static GSList *storage_list = NULL;
34 static gint compare_priority(gconstpointer a, gconstpointer b)
36 const struct connman_storage *storage1 = a;
37 const struct connman_storage *storage2 = b;
39 return storage2->priority - storage1->priority;
43 * connman_storage_register:
44 * @storage: storage module
46 * Register a new storage module
48 * Returns: %0 on success
50 int connman_storage_register(struct connman_storage *storage)
52 DBG("storage %p name %s", storage, storage->name);
54 storage_list = g_slist_insert_sorted(storage_list, storage,
61 * connman_storage_unregister:
62 * @storage: storage module
64 * Remove a previously registered storage module
66 void connman_storage_unregister(struct connman_storage *storage)
68 DBG("storage %p name %s", storage, storage->name);
70 storage_list = g_slist_remove(storage_list, storage);
73 GKeyFile *__connman_storage_open(const char *ident, const char *suffix)
76 gchar *pathname, *data = NULL;
80 DBG("ident %s suffix %s", ident, suffix);
82 pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
86 result = g_file_get_contents(pathname, &data, &length, NULL);
90 keyfile = g_key_file_new();
96 g_key_file_load_from_data(keyfile, data, length, 0, NULL);
101 DBG("keyfile %p", keyfile);
106 void __connman_storage_close(const char *ident, const char *suffix,
107 GKeyFile *keyfile, gboolean save)
109 gchar *pathname, *data = NULL;
112 DBG("ident %s suffix %s keyfile %p save %d",
113 ident, suffix, keyfile, save);
116 g_key_file_free(keyfile);
120 pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
121 if (pathname == NULL)
124 data = g_key_file_to_data(keyfile, &length, NULL);
126 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
127 connman_error("Failed to store information");
133 g_key_file_free(keyfile);
136 void __connman_storage_delete(const char *ident, const char *suffix)
140 DBG("ident %s suffix %s", ident, suffix);
142 pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
143 if (pathname == NULL)
146 if (unlink(pathname) < 0)
147 connman_error("Failed to remove %s", pathname);
150 GKeyFile *__connman_storage_open_profile(const char *ident)
152 return __connman_storage_open(ident, PROFILE_SUFFIX);
155 void __connman_storage_close_profile(const char *ident,
156 GKeyFile *keyfile, gboolean save)
158 __connman_storage_close(ident, PROFILE_SUFFIX, keyfile, save);
161 void __connman_storage_delete_profile(const char *ident)
163 __connman_storage_delete(ident, PROFILE_SUFFIX);
166 int __connman_storage_init_profile(void)
172 for (list = storage_list; list; list = list->next) {
173 struct connman_storage *storage = list->data;
175 if (storage->profile_init) {
176 if (storage->profile_init() == 0)
184 int __connman_storage_load_profile(struct connman_profile *profile)
188 DBG("profile %p", profile);
190 for (list = storage_list; list; list = list->next) {
191 struct connman_storage *storage = list->data;
193 if (storage->profile_load) {
194 if (storage->profile_load(profile) == 0)
202 int __connman_storage_save_profile(struct connman_profile *profile)
206 DBG("profile %p", profile);
208 for (list = storage_list; list; list = list->next) {
209 struct connman_storage *storage = list->data;
211 if (storage->profile_save) {
212 if (storage->profile_save(profile) == 0)
220 int __connman_storage_load_service(struct connman_service *service)
224 DBG("service %p", service);
226 for (list = storage_list; list; list = list->next) {
227 struct connman_storage *storage = list->data;
229 if (storage->service_load) {
230 if (storage->service_load(service) == 0)
238 int __connman_storage_save_service(struct connman_service *service)
242 DBG("service %p", service);
244 for (list = storage_list; list; list = list->next) {
245 struct connman_storage *storage = list->data;
247 if (storage->service_save) {
248 if (storage->service_save(service) == 0)
256 int __connman_storage_load_device(struct connman_device *device)
260 DBG("device %p", device);
262 for (list = storage_list; list; list = list->next) {
263 struct connman_storage *storage = list->data;
265 if (storage->device_load) {
266 if (storage->device_load(device) == 0)
274 int __connman_storage_save_device(struct connman_device *device)
278 DBG("device %p", device);
280 for (list = storage_list; list; list = list->next) {
281 struct connman_storage *storage = list->data;
283 if (storage->device_save) {
284 if (storage->device_save(device) == 0)
292 int __connman_storage_init(void)
299 void __connman_storage_cleanup(void)