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_init_profile(void)
190 for (list = storage_list; list; list = list->next) {
191 struct connman_storage *storage = list->data;
193 if (storage->profile_init) {
194 if (storage->profile_init() == 0)
202 int __connman_storage_load_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_load) {
212 if (storage->profile_load(profile) == 0)
220 int __connman_storage_save_profile(struct connman_profile *profile)
224 DBG("profile %p", profile);
226 for (list = storage_list; list; list = list->next) {
227 struct connman_storage *storage = list->data;
229 if (storage->profile_save) {
230 if (storage->profile_save(profile) == 0)
238 int __connman_storage_load_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_load) {
248 if (storage->service_load(service) == 0)
256 int __connman_storage_save_service(struct connman_service *service)
260 DBG("service %p", service);
262 for (list = storage_list; list; list = list->next) {
263 struct connman_storage *storage = list->data;
265 if (storage->service_save) {
266 if (storage->service_save(service) == 0)
274 int __connman_storage_load_technology(struct connman_technology *technology)
278 DBG("technology %p", technology);
280 for (list = storage_list; list; list = list->next) {
281 struct connman_storage *storage = list->data;
283 if (storage->tech_load) {
284 if (storage->tech_load(technology) == 0)
292 int __connman_storage_save_technology(struct connman_technology *technology)
296 DBG("technology %p", technology);
298 for (list = storage_list; list; list = list->next) {
299 struct connman_storage *storage = list->data;
301 if (storage->tech_save) {
302 if (storage->tech_save(technology) == 0)
310 int __connman_storage_init(void)
317 void __connman_storage_cleanup(void)