5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
32 #define SETTINGS "settings"
33 #define DEFAULT "default.profile"
35 #define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
36 S_IXGRP | S_IROTH | S_IXOTH)
38 static GKeyFile *storage_load(const char *pathname)
40 GKeyFile *keyfile = NULL;
43 DBG("Loading %s", pathname);
45 keyfile = g_key_file_new();
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);
51 g_key_file_free(keyfile);
58 static void storage_save(GKeyFile *keyfile, char *pathname)
64 data = g_key_file_to_data(keyfile, &length, NULL);
66 if (!g_file_set_contents(pathname, data, length, &error)) {
67 DBG("Failed to store information: %s", error->message);
74 static void storage_delete(const char *pathname)
76 DBG("file path %s", pathname);
78 if (unlink(pathname) < 0)
79 connman_error("Failed to remove %s", pathname);
82 GKeyFile *__connman_storage_load_global()
85 GKeyFile *keyfile = NULL;
87 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
91 keyfile = storage_load(pathname);
98 void __connman_storage_save_global(GKeyFile *keyfile)
102 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
106 storage_save(keyfile, pathname);
111 void __connman_storage_delete_global()
115 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
119 storage_delete(pathname);
124 GKeyFile *__connman_storage_load_config(const char *ident)
127 GKeyFile *keyfile = NULL;
129 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
133 keyfile = storage_load(pathname);
140 void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
144 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
148 storage_save(keyfile, pathname);
151 void __connman_storage_delete_config(const char *ident)
155 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
159 storage_delete(pathname);
164 GKeyFile *__connman_storage_open_service(const char *service_id)
167 GKeyFile *keyfile = NULL;
169 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
173 keyfile = storage_load(pathname);
181 keyfile = g_key_file_new();
186 GKeyFile *__connman_storage_load_service(const char *service_id)
189 GKeyFile *keyfile = NULL;
191 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
195 keyfile = storage_load(pathname);
203 pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
207 keyfile = storage_load(pathname);
214 void __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
216 gchar *pathname, *dirname;
218 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
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) {
232 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
236 storage_save(keyfile, pathname);
242 * This function migrates keys from default.profile to settings file.
243 * This can be removed once the migration is over.
245 void __connman_storage_migrate()
248 GKeyFile *keyfile_def = NULL;
249 GKeyFile *keyfile = NULL;
250 GError *error = NULL;
253 /* If setting file exists, migration has been done. */
254 keyfile = __connman_storage_load_global();
256 g_key_file_free(keyfile);
260 pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
264 /* If default.profile doesn't exists, no need to migrate. */
265 keyfile_def = storage_load(pathname);
266 if (keyfile_def == NULL) {
271 /* Copy global settings from default.profile to settings. */
272 keyfile = g_key_file_new();
275 val = g_key_file_get_boolean(keyfile_def, "global",
276 "OfflineMode", &error);
278 g_clear_error(&error);
280 g_key_file_set_boolean(keyfile, "global",
284 val = g_key_file_get_boolean(keyfile_def, "WiFi",
287 g_clear_error(&error);
289 g_key_file_set_boolean(keyfile, "WiFi",
293 val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
296 g_clear_error(&error);
298 g_key_file_set_boolean(keyfile, "Bluetooth",
302 val = g_key_file_get_boolean(keyfile_def, "Wired",
305 g_clear_error(&error);
307 g_key_file_set_boolean(keyfile, "Wired",
311 val = g_key_file_get_boolean(keyfile_def, "3G",
314 g_clear_error(&error);
316 g_key_file_set_boolean(keyfile, "3G",
320 val = g_key_file_get_boolean(keyfile_def, "WiMAX",
323 g_clear_error(&error);
325 g_key_file_set_boolean(keyfile, "WiMAX",
328 __connman_storage_save_global(keyfile);
330 g_key_file_free(keyfile);
331 g_key_file_free(keyfile_def);