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