Add common helpers for keyfile storage
[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 "connman.h"
27
28 static GSList *storage_list = NULL;
29
30 static gint compare_priority(gconstpointer a, gconstpointer b)
31 {
32         const struct connman_storage *storage1 = a;
33         const struct connman_storage *storage2 = b;
34
35         return storage2->priority - storage1->priority;
36 }
37
38 /**
39  * connman_storage_register:
40  * @storage: storage module
41  *
42  * Register a new storage module
43  *
44  * Returns: %0 on success
45  */
46 int connman_storage_register(struct connman_storage *storage)
47 {
48         DBG("storage %p name %s", storage, storage->name);
49
50         storage_list = g_slist_insert_sorted(storage_list, storage,
51                                                         compare_priority);
52
53         return 0;
54 }
55
56 /**
57  * connman_storage_unregister:
58  * @storage: storage module
59  *
60  * Remove a previously registered storage module
61  */
62 void connman_storage_unregister(struct connman_storage *storage)
63 {
64         DBG("storage %p name %s", storage, storage->name);
65
66         storage_list = g_slist_remove(storage_list, storage);
67 }
68
69 GKeyFile *__connman_storage_open(void)
70 {
71         GKeyFile *keyfile;
72         gchar *pathname, *data = NULL;
73         gboolean result;
74         gsize length;
75
76         DBG("");
77
78         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
79                                         __connman_profile_active_ident());
80         if (pathname == NULL)
81                 return NULL;
82
83         result = g_file_get_contents(pathname, &data, &length, NULL);
84
85         g_free(pathname);
86
87         if (result == FALSE)
88                 return NULL;
89
90         keyfile = g_key_file_new();
91
92         if (length > 0) {
93                 if (g_key_file_load_from_data(keyfile, data, length,
94                                                         0, NULL) == FALSE)
95                         goto done;
96         }
97
98 done:
99         g_free(data);
100
101         DBG("keyfile %p", keyfile);
102
103         return keyfile;
104 }
105
106 void __connman_storage_close(GKeyFile *keyfile, gboolean save)
107 {
108         gchar *pathname, *data = NULL;
109         gsize length = 0;
110
111         DBG("keyfile %p save %d", keyfile, save);
112
113         if (save == FALSE) {
114                 g_key_file_free(keyfile);
115                 return;
116         }
117
118         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
119                                         __connman_profile_active_ident());
120         if (pathname == NULL)
121                 return;
122
123         data = g_key_file_to_data(keyfile, &length, NULL);
124
125         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
126                 connman_error("Failed to store information");
127
128         g_free(data);
129
130         g_free(pathname);
131
132         g_key_file_free(keyfile);
133 }
134
135 int __connman_storage_load_global(void)
136 {
137         GSList *list;
138
139         DBG("");
140
141         for (list = storage_list; list; list = list->next) {
142                 struct connman_storage *storage = list->data;
143
144                 if (storage->global_load) {
145                         if (storage->global_load() == 0)
146                                 return 0;
147                 }
148         }
149
150         return -ENOENT;
151 }
152
153 int __connman_storage_save_global(void)
154 {
155         GSList *list;
156
157         DBG("");
158
159         for (list = storage_list; list; list = list->next) {
160                 struct connman_storage *storage = list->data;
161
162                 if (storage->global_save) {
163                         if (storage->global_save() == 0)
164                                 return 0;
165                 }
166         }
167
168         return -ENOENT;
169 }
170
171 int __connman_storage_init_device(void)
172 {
173         GSList *list;
174
175         DBG("");
176
177         for (list = storage_list; list; list = list->next) {
178                 struct connman_storage *storage = list->data;
179
180                 if (storage->device_init) {
181                         if (storage->device_init() == 0)
182                                 return 0;
183                 }
184         }
185
186         return -ENOENT;
187 }
188
189 int __connman_storage_load_device(struct connman_device *device)
190 {
191         GSList *list;
192
193         DBG("device %p", device);
194
195         for (list = storage_list; list; list = list->next) {
196                 struct connman_storage *storage = list->data;
197
198                 if (storage->device_load) {
199                         if (storage->device_load(device) == 0)
200                                 return 0;
201                 }
202         }
203
204         return -ENOENT;
205 }
206
207 int __connman_storage_save_device(struct connman_device *device)
208 {
209         GSList *list;
210
211         DBG("device %p", device);
212
213         for (list = storage_list; list; list = list->next) {
214                 struct connman_storage *storage = list->data;
215
216                 if (storage->device_save) {
217                         if (storage->device_save(device) == 0)
218                                 return 0;
219                 }
220         }
221
222         return -ENOENT;
223 }
224
225 int __connman_storage_init_network(struct connman_device *device)
226 {
227         GSList *list;
228
229         DBG("device %p", device);
230
231         for (list = storage_list; list; list = list->next) {
232                 struct connman_storage *storage = list->data;
233
234                 if (storage->network_init) {
235                         if (storage->network_init(device) == 0)
236                                 return 0;
237                 }
238         }
239
240         return -ENOENT;
241 }
242
243 int __connman_storage_load_network(struct connman_network *network)
244 {
245         GSList *list;
246
247         DBG("network %p", network);
248
249         for (list = storage_list; list; list = list->next) {
250                 struct connman_storage *storage = list->data;
251
252                 if (storage->network_load) {
253                         if (storage->network_load(network) == 0)
254                                 return 0;
255                 }
256         }
257
258         return -ENOENT;
259 }
260
261 int __connman_storage_save_network(struct connman_network *network)
262 {
263         GSList *list;
264
265         DBG("network %p", network);
266
267         for (list = storage_list; list; list = list->next) {
268                 struct connman_storage *storage = list->data;
269
270                 if (storage->network_save) {
271                         if (storage->network_save(network) == 0)
272                                 return 0;
273                 }
274         }
275
276         return -ENOENT;
277 }
278
279 int __connman_storage_init_service(void)
280 {
281         DBG("");
282
283         return -ENOENT;
284 }
285
286 int __connman_storage_load_service(struct connman_service *service)
287 {
288         GSList *list;
289
290         DBG("service %p", service);
291
292         for (list = storage_list; list; list = list->next) {
293                 struct connman_storage *storage = list->data;
294
295                 if (storage->service_load) {
296                         if (storage->service_load(service) == 0)
297                                 return 0;
298                 }
299         }
300
301         return -ENOENT;
302 }
303
304 int __connman_storage_save_service(struct connman_service *service)
305 {
306         GSList *list;
307
308         DBG("service %p", service);
309
310         for (list = storage_list; list; list = list->next) {
311                 struct connman_storage *storage = list->data;
312
313                 if (storage->service_save) {
314                         if (storage->service_save(service) == 0)
315                                 return 0;
316                 }
317         }
318
319         return -ENOENT;
320 }
321
322 int __connman_storage_init(void)
323 {
324         DBG("");
325
326         return 0;
327 }
328
329 void __connman_storage_cleanup(void)
330 {
331         DBG("");
332 }