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
30 #define PROFILE_SUFFIX "profile"
31 #define CONFIG_SUFFIX "config"
33 static GSList *storage_list = NULL;
35 static gint compare_priority(gconstpointer a, gconstpointer b)
37 const struct connman_storage *storage1 = a;
38 const struct connman_storage *storage2 = b;
40 return storage2->priority - storage1->priority;
44 * connman_storage_register:
45 * @storage: storage module
47 * Register a new storage module
49 * Returns: %0 on success
51 int connman_storage_register(struct connman_storage *storage)
53 DBG("storage %p name %s", storage, storage->name);
55 storage_list = g_slist_insert_sorted(storage_list, storage,
62 * connman_storage_unregister:
63 * @storage: storage module
65 * Remove a previously registered storage module
67 void connman_storage_unregister(struct connman_storage *storage)
69 DBG("storage %p name %s", storage, storage->name);
71 storage_list = g_slist_remove(storage_list, storage);
74 GKeyFile *__connman_storage_open(const char *ident, const char *suffix)
77 gchar *pathname, *data = NULL;
81 DBG("ident %s suffix %s", ident, suffix);
83 pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
87 result = g_file_get_contents(pathname, &data, &length, NULL);
91 keyfile = g_key_file_new();
97 g_key_file_load_from_data(keyfile, data, length, 0, NULL);
102 DBG("keyfile %p", keyfile);
107 void __connman_storage_close(const char *ident, const char *suffix,
108 GKeyFile *keyfile, gboolean save)
110 gchar *pathname, *data = NULL;
113 DBG("ident %s suffix %s keyfile %p save %d",
114 ident, suffix, keyfile, save);
117 g_key_file_free(keyfile);
121 pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
122 if (pathname == NULL)
125 data = g_key_file_to_data(keyfile, &length, NULL);
127 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
128 connman_error("Failed to store information");
134 g_key_file_free(keyfile);
137 void __connman_storage_delete(const char *ident, const char *suffix)
141 DBG("ident %s suffix %s", ident, suffix);
143 pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
144 if (pathname == NULL)
147 if (unlink(pathname) < 0)
148 connman_error("Failed to remove %s", pathname);
151 GKeyFile *__connman_storage_open_profile(const char *ident)
153 return __connman_storage_open(ident, PROFILE_SUFFIX);
156 void __connman_storage_close_profile(const char *ident,
157 GKeyFile *keyfile, gboolean save)
159 __connman_storage_close(ident, PROFILE_SUFFIX, keyfile, save);
162 void __connman_storage_delete_profile(const char *ident)
164 __connman_storage_delete(ident, PROFILE_SUFFIX);
167 GKeyFile *__connman_storage_open_config(const char *ident)
169 return __connman_storage_open(ident, CONFIG_SUFFIX);
172 void __connman_storage_close_config(const char *ident,
173 GKeyFile *keyfile, gboolean save)
175 __connman_storage_close(ident, CONFIG_SUFFIX, keyfile, save);
178 void __connman_storage_delete_config(const char *ident)
180 __connman_storage_delete(ident, CONFIG_SUFFIX);
183 int __connman_storage_init_profile(void)
189 for (list = storage_list; list; list = list->next) {
190 struct connman_storage *storage = list->data;
192 if (storage->profile_init) {
193 if (storage->profile_init() == 0)
201 int __connman_storage_load_profile(struct connman_profile *profile)
205 DBG("profile %p", profile);
207 for (list = storage_list; list; list = list->next) {
208 struct connman_storage *storage = list->data;
210 if (storage->profile_load) {
211 if (storage->profile_load(profile) == 0)
219 int __connman_storage_save_profile(struct connman_profile *profile)
223 DBG("profile %p", profile);
225 for (list = storage_list; list; list = list->next) {
226 struct connman_storage *storage = list->data;
228 if (storage->profile_save) {
229 if (storage->profile_save(profile) == 0)
237 int __connman_storage_load_service(struct connman_service *service)
241 DBG("service %p", service);
243 for (list = storage_list; list; list = list->next) {
244 struct connman_storage *storage = list->data;
246 if (storage->service_load) {
247 if (storage->service_load(service) == 0)
255 int __connman_storage_save_service(struct connman_service *service)
259 DBG("service %p", service);
261 for (list = storage_list; list; list = list->next) {
262 struct connman_storage *storage = list->data;
264 if (storage->service_save) {
265 if (storage->service_save(service) == 0)
273 int __connman_storage_load_device(struct connman_device *device)
277 DBG("device %p", device);
279 for (list = storage_list; list; list = list->next) {
280 struct connman_storage *storage = list->data;
282 if (storage->device_load) {
283 if (storage->device_load(device) == 0)
291 int __connman_storage_save_device(struct connman_device *device)
295 DBG("device %p", device);
297 for (list = storage_list; list; list = list->next) {
298 struct connman_storage *storage = list->data;
300 if (storage->device_save) {
301 if (storage->device_save(device) == 0)
309 int __connman_storage_init(void)
316 void __connman_storage_cleanup(void)