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
31 #include <connman/storage.h>
35 #define SETTINGS "settings"
36 #define DEFAULT "default.profile"
38 #define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
39 S_IXGRP | S_IROTH | S_IXOTH)
41 static GKeyFile *storage_load(const char *pathname)
43 GKeyFile *keyfile = NULL;
46 DBG("Loading %s", pathname);
48 keyfile = g_key_file_new();
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);
54 g_key_file_free(keyfile);
61 static void storage_save(GKeyFile *keyfile, char *pathname)
67 data = g_key_file_to_data(keyfile, &length, NULL);
69 if (!g_file_set_contents(pathname, data, length, &error)) {
70 DBG("Failed to store information: %s", error->message);
77 static void storage_delete(const char *pathname)
79 DBG("file path %s", pathname);
81 if (unlink(pathname) < 0)
82 connman_error("Failed to remove %s", pathname);
85 GKeyFile *__connman_storage_load_global()
88 GKeyFile *keyfile = NULL;
90 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
94 keyfile = storage_load(pathname);
101 void __connman_storage_save_global(GKeyFile *keyfile)
105 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
109 storage_save(keyfile, pathname);
114 void __connman_storage_delete_global()
118 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
122 storage_delete(pathname);
127 GKeyFile *__connman_storage_load_config(const char *ident)
130 GKeyFile *keyfile = NULL;
132 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
136 keyfile = storage_load(pathname);
143 void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
147 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
151 storage_save(keyfile, pathname);
154 void __connman_storage_delete_config(const char *ident)
158 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
162 storage_delete(pathname);
167 GKeyFile *__connman_storage_open_service(const char *service_id)
170 GKeyFile *keyfile = NULL;
172 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
176 keyfile = storage_load(pathname);
184 keyfile = g_key_file_new();
189 gchar **connman_storage_get_services()
195 gchar **services = NULL;
199 dir = opendir(STORAGEDIR);
203 result = g_string_new(NULL);
205 while ((d = readdir(dir))) {
206 if (strcmp(d->d_name, ".") == 0 ||
207 strcmp(d->d_name, "..") == 0)
213 * If the settings file is not found, then
214 * assume this directory is not a services dir.
216 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
218 ret = stat(str, &buf);
223 g_string_append_printf(result, "%s/", d->d_name);
230 str = g_string_free(result, FALSE);
232 str[strlen(str) - 1] = '\0';
233 services = g_strsplit(str, "/", -1);
240 GKeyFile *connman_storage_load_service(const char *service_id)
243 GKeyFile *keyfile = NULL;
245 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
249 keyfile = storage_load(pathname);
257 pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
261 keyfile = storage_load(pathname);
268 void __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
270 gchar *pathname, *dirname;
272 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
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) {
286 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
290 storage_save(keyfile, pathname);
296 * This function migrates keys from default.profile to settings file.
297 * This can be removed once the migration is over.
299 void __connman_storage_migrate()
302 GKeyFile *keyfile_def = NULL;
303 GKeyFile *keyfile = NULL;
304 GError *error = NULL;
307 /* If setting file exists, migration has been done. */
308 keyfile = __connman_storage_load_global();
310 g_key_file_free(keyfile);
314 pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
318 /* If default.profile doesn't exists, no need to migrate. */
319 keyfile_def = storage_load(pathname);
320 if (keyfile_def == NULL) {
325 /* Copy global settings from default.profile to settings. */
326 keyfile = g_key_file_new();
329 val = g_key_file_get_boolean(keyfile_def, "global",
330 "OfflineMode", &error);
332 g_clear_error(&error);
334 g_key_file_set_boolean(keyfile, "global",
338 val = g_key_file_get_boolean(keyfile_def, "WiFi",
341 g_clear_error(&error);
343 g_key_file_set_boolean(keyfile, "WiFi",
347 val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
350 g_clear_error(&error);
352 g_key_file_set_boolean(keyfile, "Bluetooth",
356 val = g_key_file_get_boolean(keyfile_def, "Wired",
359 g_clear_error(&error);
361 g_key_file_set_boolean(keyfile, "Wired",
365 val = g_key_file_get_boolean(keyfile_def, "3G",
368 g_clear_error(&error);
370 g_key_file_set_boolean(keyfile, "3G",
374 val = g_key_file_get_boolean(keyfile_def, "WiMAX",
377 g_clear_error(&error);
379 g_key_file_set_boolean(keyfile, "WiMAX",
382 __connman_storage_save_global(keyfile);
384 g_key_file_free(keyfile);
385 g_key_file_free(keyfile_def);