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