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