5 * Copyright (C) 2007-2010 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
31 #define PROFILE_SUFFIX "profile"
32 #define CONFIG_SUFFIX "config"
34 static GSList *storage_list = NULL;
36 static gint compare_priority(gconstpointer a, gconstpointer b)
38 const struct connman_storage *storage1 = a;
39 const struct connman_storage *storage2 = b;
41 return storage2->priority - storage1->priority;
45 * connman_storage_register:
46 * @storage: storage module
48 * Register a new storage module
50 * Returns: %0 on success
52 int connman_storage_register(struct connman_storage *storage)
54 DBG("storage %p name %s", storage, storage->name);
56 storage_list = g_slist_insert_sorted(storage_list, storage,
63 * connman_storage_unregister:
64 * @storage: storage module
66 * Remove a previously registered storage module
68 void connman_storage_unregister(struct connman_storage *storage)
70 DBG("storage %p name %s", storage, storage->name);
72 storage_list = g_slist_remove(storage_list, storage);
75 GKeyFile *__connman_storage_open(const char *ident, const char *suffix)
78 gchar *pathname, *data = NULL;
82 DBG("ident %s suffix %s", ident, suffix);
84 pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
88 result = g_file_get_contents(pathname, &data, &length, NULL);
92 keyfile = g_key_file_new();
98 g_key_file_load_from_data(keyfile, data, length, 0, NULL);
103 DBG("keyfile %p", keyfile);
108 void __connman_storage_close(const char *ident, const char *suffix,
109 GKeyFile *keyfile, gboolean save)
111 gchar *pathname, *data = NULL;
114 DBG("ident %s suffix %s keyfile %p save %d",
115 ident, suffix, keyfile, save);
118 g_key_file_free(keyfile);
122 pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
123 if (pathname == NULL)
126 data = g_key_file_to_data(keyfile, &length, NULL);
128 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
129 connman_error("Failed to store information");
135 g_key_file_free(keyfile);
138 void __connman_storage_delete(const char *ident, const char *suffix)
142 DBG("ident %s suffix %s", ident, suffix);
144 pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
145 if (pathname == NULL)
148 if (unlink(pathname) < 0)
149 connman_error("Failed to remove %s", pathname);
152 GKeyFile *__connman_storage_open_profile(const char *ident)
154 return __connman_storage_open(ident, PROFILE_SUFFIX);
157 void __connman_storage_close_profile(const char *ident,
158 GKeyFile *keyfile, gboolean save)
160 __connman_storage_close(ident, PROFILE_SUFFIX, keyfile, save);
163 void __connman_storage_delete_profile(const char *ident)
165 __connman_storage_delete(ident, PROFILE_SUFFIX);
168 GKeyFile *__connman_storage_open_config(const char *ident)
170 return __connman_storage_open(ident, CONFIG_SUFFIX);
173 void __connman_storage_close_config(const char *ident,
174 GKeyFile *keyfile, gboolean save)
176 __connman_storage_close(ident, CONFIG_SUFFIX, keyfile, save);
179 void __connman_storage_delete_config(const char *ident)
181 __connman_storage_delete(ident, CONFIG_SUFFIX);
184 int __connman_storage_load_service(struct connman_service *service)
188 DBG("service %p", service);
190 for (list = storage_list; list; list = list->next) {
191 struct connman_storage *storage = list->data;
193 if (storage->service_load) {
194 if (storage->service_load(service) == 0)
202 int __connman_storage_save_service(struct connman_service *service)
206 DBG("service %p", service);
208 for (list = storage_list; list; list = list->next) {
209 struct connman_storage *storage = list->data;
211 if (storage->service_save) {
212 if (storage->service_save(service) == 0)
220 int __connman_storage_init(void)
227 void __connman_storage_cleanup(void)