Add support for creating, modifying and removing profiles
[platform/upstream/connman.git] / src / storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27
28 #include "connman.h"
29
30 static GSList *storage_list = NULL;
31
32 static gint compare_priority(gconstpointer a, gconstpointer b)
33 {
34         const struct connman_storage *storage1 = a;
35         const struct connman_storage *storage2 = b;
36
37         return storage2->priority - storage1->priority;
38 }
39
40 /**
41  * connman_storage_register:
42  * @storage: storage module
43  *
44  * Register a new storage module
45  *
46  * Returns: %0 on success
47  */
48 int connman_storage_register(struct connman_storage *storage)
49 {
50         DBG("storage %p name %s", storage, storage->name);
51
52         storage_list = g_slist_insert_sorted(storage_list, storage,
53                                                         compare_priority);
54
55         return 0;
56 }
57
58 /**
59  * connman_storage_unregister:
60  * @storage: storage module
61  *
62  * Remove a previously registered storage module
63  */
64 void connman_storage_unregister(struct connman_storage *storage)
65 {
66         DBG("storage %p name %s", storage, storage->name);
67
68         storage_list = g_slist_remove(storage_list, storage);
69 }
70
71 GKeyFile *__connman_storage_open(const char *ident)
72 {
73         GKeyFile *keyfile;
74         gchar *pathname, *data = NULL;
75         gboolean result;
76         gsize length;
77
78         DBG("ident %s", ident);
79
80         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
81         if (pathname == NULL)
82                 return NULL;
83
84         result = g_file_get_contents(pathname, &data, &length, NULL);
85
86         g_free(pathname);
87
88         keyfile = g_key_file_new();
89
90         if (result == FALSE)
91                 goto done;
92
93         if (length > 0)
94                 g_key_file_load_from_data(keyfile, data, length, 0, NULL);
95
96         g_free(data);
97
98 done:
99         DBG("keyfile %p", keyfile);
100
101         return keyfile;
102 }
103
104 void __connman_storage_close(const char *ident,
105                                         GKeyFile *keyfile, gboolean save)
106 {
107         gchar *pathname, *data = NULL;
108         gsize length = 0;
109
110         DBG("ident %s keyfile %p save %d", ident, keyfile, save);
111
112         if (save == FALSE) {
113                 g_key_file_free(keyfile);
114                 return;
115         }
116
117         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
118         if (pathname == NULL)
119                 return;
120
121         data = g_key_file_to_data(keyfile, &length, NULL);
122
123         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
124                 connman_error("Failed to store information");
125
126         g_free(data);
127
128         g_free(pathname);
129
130         g_key_file_free(keyfile);
131 }
132
133 void __connman_storage_delete(const char *ident)
134 {
135         gchar *pathname;
136
137         DBG("ident %s", ident);
138
139         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
140         if (pathname == NULL)
141                 return;
142
143         if (unlink(pathname) < 0)
144                 connman_error("Failed to remove %s", pathname);
145 }
146
147 int __connman_storage_init_profile(void)
148 {
149         GSList *list;
150
151         DBG("");
152
153         for (list = storage_list; list; list = list->next) {
154                 struct connman_storage *storage = list->data;
155
156                 if (storage->profile_init) {
157                         if (storage->profile_init() == 0)
158                                 return 0;
159                 }
160         }
161
162         return -ENOENT;
163 }
164
165 int __connman_storage_load_profile(struct connman_profile *profile)
166 {
167         GSList *list;
168
169         DBG("profile %p", profile);
170
171         for (list = storage_list; list; list = list->next) {
172                 struct connman_storage *storage = list->data;
173
174                 if (storage->profile_load) {
175                         if (storage->profile_load(profile) == 0)
176                                 return 0;
177                 }
178         }
179
180         return -ENOENT;
181 }
182
183 int __connman_storage_save_profile(struct connman_profile *profile)
184 {
185         GSList *list;
186
187         DBG("profile %p", profile);
188
189         for (list = storage_list; list; list = list->next) {
190                 struct connman_storage *storage = list->data;
191
192                 if (storage->profile_save) {
193                         if (storage->profile_save(profile) == 0)
194                                 return 0;
195                 }
196         }
197
198         return -ENOENT;
199 }
200
201 int __connman_storage_load_service(struct connman_service *service)
202 {
203         GSList *list;
204
205         DBG("service %p", service);
206
207         for (list = storage_list; list; list = list->next) {
208                 struct connman_storage *storage = list->data;
209
210                 if (storage->service_load) {
211                         if (storage->service_load(service) == 0)
212                                 return 0;
213                 }
214         }
215
216         return -ENOENT;
217 }
218
219 int __connman_storage_save_service(struct connman_service *service)
220 {
221         GSList *list;
222
223         DBG("service %p", service);
224
225         for (list = storage_list; list; list = list->next) {
226                 struct connman_storage *storage = list->data;
227
228                 if (storage->service_save) {
229                         if (storage->service_save(service) == 0)
230                                 return 0;
231                 }
232         }
233
234         return -ENOENT;
235 }
236
237 int __connman_storage_load_device(struct connman_device *device)
238 {
239         GSList *list;
240
241         DBG("device %p", device);
242
243         for (list = storage_list; list; list = list->next) {
244                 struct connman_storage *storage = list->data;
245
246                 if (storage->device_load) {
247                         if (storage->device_load(device) == 0)
248                                 return 0;
249                 }
250         }
251
252         return -ENOENT;
253 }
254
255 int __connman_storage_save_device(struct connman_device *device)
256 {
257         GSList *list;
258
259         DBG("device %p", device);
260
261         for (list = storage_list; list; list = list->next) {
262                 struct connman_storage *storage = list->data;
263
264                 if (storage->device_save) {
265                         if (storage->device_save(device) == 0)
266                                 return 0;
267                 }
268         }
269
270         return -ENOENT;
271 }
272
273 int __connman_storage_init(void)
274 {
275         DBG("");
276
277         return 0;
278 }
279
280 void __connman_storage_cleanup(void)
281 {
282         DBG("");
283 }