storage: Export service loading function
[platform/upstream/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 #include <sys/stat.h>
29 #include <dirent.h>
30
31 #include <connman/storage.h>
32
33 #include "connman.h"
34
35 #define SETTINGS        "settings"
36 #define DEFAULT         "default.profile"
37
38 #define MODE            (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
39                         S_IXGRP | S_IROTH | S_IXOTH)
40
41 static GKeyFile *storage_load(const char *pathname)
42 {
43         GKeyFile *keyfile = NULL;
44         GError *error = NULL;
45
46         DBG("Loading %s", pathname);
47
48         keyfile = g_key_file_new();
49
50         if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) {
51                 DBG("Unable to load %s: %s", pathname, error->message);
52                 g_clear_error(&error);
53
54                 g_key_file_free(keyfile);
55                 keyfile = NULL;
56         }
57
58         return keyfile;
59 }
60
61 static void storage_save(GKeyFile *keyfile, char *pathname)
62 {
63         gchar *data = NULL;
64         gsize length = 0;
65         GError *error = NULL;
66
67         data = g_key_file_to_data(keyfile, &length, NULL);
68
69         if (!g_file_set_contents(pathname, data, length, &error)) {
70                 DBG("Failed to store information: %s", error->message);
71                 g_free(error);
72         }
73
74         g_free(data);
75 }
76
77 static void storage_delete(const char *pathname)
78 {
79         DBG("file path %s", pathname);
80
81         if (unlink(pathname) < 0)
82                 connman_error("Failed to remove %s", pathname);
83 }
84
85 GKeyFile *__connman_storage_load_global()
86 {
87         gchar *pathname;
88         GKeyFile *keyfile = NULL;
89
90         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
91         if(pathname == NULL)
92                 return NULL;
93
94         keyfile = storage_load(pathname);
95
96         g_free(pathname);
97
98         return keyfile;
99 }
100
101 void __connman_storage_save_global(GKeyFile *keyfile)
102 {
103         gchar *pathname;
104
105         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
106         if(pathname == NULL)
107                 return;
108
109         storage_save(keyfile, pathname);
110
111         g_free(pathname);
112 }
113
114 void __connman_storage_delete_global()
115 {
116         gchar *pathname;
117
118         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
119         if(pathname == NULL)
120                 return;
121
122         storage_delete(pathname);
123
124         g_free(pathname);
125 }
126
127 GKeyFile *__connman_storage_load_config(const char *ident)
128 {
129         gchar *pathname;
130         GKeyFile *keyfile = NULL;
131
132         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
133         if(pathname == NULL)
134                 return NULL;
135
136         keyfile = storage_load(pathname);
137
138         g_free(pathname);
139
140         return keyfile;
141 }
142
143 void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
144 {
145         gchar *pathname;
146
147         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
148         if(pathname == NULL)
149                 return;
150
151         storage_save(keyfile, pathname);
152 }
153
154 void __connman_storage_delete_config(const char *ident)
155 {
156         gchar *pathname;
157
158         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
159         if(pathname == NULL)
160                 return;
161
162         storage_delete(pathname);
163
164         g_free(pathname);
165 }
166
167 GKeyFile *__connman_storage_open_service(const char *service_id)
168 {
169         gchar *pathname;
170         GKeyFile *keyfile = NULL;
171
172         pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
173         if(pathname == NULL)
174                 return NULL;
175
176         keyfile =  storage_load(pathname);
177         if (keyfile) {
178                 g_free(pathname);
179                 return keyfile;
180         }
181
182         g_free(pathname);
183
184         keyfile = g_key_file_new();
185
186         return keyfile;
187 }
188
189 gchar **connman_storage_get_services()
190 {
191         struct dirent *d;
192         gchar *str;
193         DIR *dir;
194         GString *result;
195         gchar **services = NULL;
196         struct stat buf;
197         int ret;
198
199         dir = opendir(STORAGEDIR);
200         if (dir == NULL)
201                 return NULL;
202
203         result = g_string_new(NULL);
204
205         while ((d = readdir(dir))) {
206                 if (strcmp(d->d_name, ".") == 0 ||
207                                 strcmp(d->d_name, "..") == 0)
208                         continue;
209
210                 switch (d->d_type) {
211                 case DT_DIR:
212                         /*
213                          * If the settings file is not found, then
214                          * assume this directory is not a services dir.
215                          */
216                         str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
217                                                                 d->d_name);
218                         ret = stat(str, &buf);
219                         g_free(str);
220                         if (ret < 0)
221                                 continue;
222
223                         g_string_append_printf(result, "%s/", d->d_name);
224                         break;
225                 }
226         }
227
228         closedir(dir);
229
230         str = g_string_free(result, FALSE);
231         if (str) {
232                 str[strlen(str) - 1] = '\0';
233                 services = g_strsplit(str, "/", -1);
234         }
235         g_free(str);
236
237         return services;
238 }
239
240 GKeyFile *connman_storage_load_service(const char *service_id)
241 {
242         gchar *pathname;
243         GKeyFile *keyfile = NULL;
244
245         pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
246         if(pathname == NULL)
247                 return NULL;
248
249         keyfile =  storage_load(pathname);
250         if (keyfile) {
251                 g_free(pathname);
252                 return keyfile;
253         }
254
255         g_free(pathname);
256
257         pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
258         if(pathname == NULL)
259                 return NULL;
260
261         keyfile =  storage_load(pathname);
262
263         g_free(pathname);
264
265         return keyfile;
266 }
267
268 void __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
269 {
270         gchar *pathname, *dirname;
271
272         dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
273         if(dirname == NULL)
274                 return;
275
276         /* If the dir doesn't exist, create it */
277         if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
278                 if(mkdir(dirname, MODE) < 0) {
279                         if (errno != EEXIST) {
280                                 g_free(dirname);
281                                 return;
282                         }
283                 }
284         }
285
286         pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
287
288         g_free(dirname);
289
290         storage_save(keyfile, pathname);
291
292         g_free(pathname);
293 }
294
295 /*
296  * This function migrates keys from default.profile to settings file.
297  * This can be removed once the migration is over.
298 */
299 void __connman_storage_migrate()
300 {
301         gchar *pathname;
302         GKeyFile *keyfile_def = NULL;
303         GKeyFile *keyfile = NULL;
304         GError *error = NULL;
305         connman_bool_t val;
306
307         /* If setting file exists, migration has been done. */
308         keyfile = __connman_storage_load_global();
309         if (keyfile) {
310                 g_key_file_free(keyfile);
311                 return;
312         }
313
314         pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
315         if(pathname == NULL)
316                 return;
317
318         /* If default.profile doesn't exists, no need to migrate. */
319         keyfile_def = storage_load(pathname);
320         if (keyfile_def == NULL) {
321                 g_free(pathname);
322                 return;
323         }
324
325         /* Copy global settings from default.profile to settings. */
326         keyfile = g_key_file_new();
327
328         /* offline mode */
329         val = g_key_file_get_boolean(keyfile_def, "global",
330                                         "OfflineMode", &error);
331         if (error != NULL)
332                 g_clear_error(&error);
333         else
334                 g_key_file_set_boolean(keyfile, "global",
335                                         "OfflineMode", val);
336
337         /* wifi */
338         val = g_key_file_get_boolean(keyfile_def, "WiFi",
339                                         "Enable", &error);
340         if (error != NULL)
341                 g_clear_error(&error);
342         else
343                 g_key_file_set_boolean(keyfile, "WiFi",
344                                         "Enable", val);
345
346         /* bluetooth */
347         val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
348                                         "Enable", &error);
349         if (error != NULL)
350                 g_clear_error(&error);
351         else
352                 g_key_file_set_boolean(keyfile, "Bluetooth",
353                                         "Enable", val);
354
355         /* wired */
356         val = g_key_file_get_boolean(keyfile_def, "Wired",
357                                         "Enable", &error);
358         if (error != NULL)
359                 g_clear_error(&error);
360         else
361                 g_key_file_set_boolean(keyfile, "Wired",
362                                         "Enable", val);
363
364         /* 3G */
365         val = g_key_file_get_boolean(keyfile_def, "3G",
366                                         "Enable", &error);
367         if (error != NULL)
368                 g_clear_error(&error);
369         else
370                 g_key_file_set_boolean(keyfile, "3G",
371                                         "Enable", val);
372
373         /* WiMAX */
374         val = g_key_file_get_boolean(keyfile_def, "WiMAX",
375                                         "Enable", &error);
376         if (error != NULL)
377                 g_clear_error(&error);
378         else
379                 g_key_file_set_boolean(keyfile, "WiMAX",
380                                         "Enable", val);
381
382         __connman_storage_save_global(keyfile);
383
384         g_key_file_free(keyfile);
385         g_key_file_free(keyfile_def);
386         g_free(pathname);
387 }