5 * Copyright (C) 2007-2012 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 int storage_save(GKeyFile *keyfile, char *pathname)
68 data = g_key_file_to_data(keyfile, &length, NULL);
70 if (!g_file_set_contents(pathname, data, length, &error)) {
71 DBG("Failed to store information: %s", error->message);
81 static void storage_delete(const char *pathname)
83 DBG("file path %s", pathname);
85 if (unlink(pathname) < 0)
86 connman_error("Failed to remove %s", pathname);
89 GKeyFile *__connman_storage_load_global(void)
92 GKeyFile *keyfile = NULL;
94 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
98 keyfile = storage_load(pathname);
105 int __connman_storage_save_global(GKeyFile *keyfile)
110 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
114 ret = storage_save(keyfile, pathname);
121 void __connman_storage_delete_global(void)
125 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
129 storage_delete(pathname);
134 GKeyFile *__connman_storage_load_config(const char *ident)
137 GKeyFile *keyfile = NULL;
139 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
143 keyfile = storage_load(pathname);
150 GKeyFile *__connman_storage_open_service(const char *service_id)
153 GKeyFile *keyfile = NULL;
155 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
159 keyfile = storage_load(pathname);
167 keyfile = g_key_file_new();
172 gchar **connman_storage_get_services(void)
178 gchar **services = NULL;
182 dir = opendir(STORAGEDIR);
186 result = g_string_new(NULL);
188 while ((d = readdir(dir))) {
189 if (strcmp(d->d_name, ".") == 0 ||
190 strcmp(d->d_name, "..") == 0 ||
191 strncmp(d->d_name, "provider_", 9) == 0)
198 * If the settings file is not found, then
199 * assume this directory is not a services dir.
201 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
203 ret = stat(str, &buf);
208 g_string_append_printf(result, "%s/", d->d_name);
215 str = g_string_free(result, FALSE);
216 if (str && str[0] != '\0') {
218 * Remove the trailing separator so that services doesn't end up
219 * with an empty element.
221 str[strlen(str) - 1] = '\0';
222 services = g_strsplit(str, "/", -1);
229 GKeyFile *connman_storage_load_service(const char *service_id)
232 GKeyFile *keyfile = NULL;
234 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
238 keyfile = storage_load(pathname);
244 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
247 gchar *pathname, *dirname;
249 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
253 /* If the dir doesn't exist, create it */
254 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
255 if(mkdir(dirname, MODE) < 0) {
256 if (errno != EEXIST) {
263 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
267 ret = storage_save(keyfile, pathname);
274 static gboolean remove_file(const char *service_id, const char *file)
277 gboolean ret = FALSE;
279 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
283 if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
285 } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR) == TRUE) {
294 static gboolean remove_dir(const char *service_id)
297 gboolean ret = FALSE;
299 pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
303 if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
305 } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR) == TRUE) {
314 gboolean __connman_storage_remove_service(const char *service_id)
318 /* Remove service configuration file */
319 removed = remove_file(service_id, SETTINGS);
320 if (removed == FALSE)
323 /* Remove the statistics file also */
324 removed = remove_file(service_id, "data");
325 if (removed == FALSE)
328 removed = remove_dir(service_id);
329 if (removed == FALSE)
332 DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
337 GKeyFile *__connman_storage_load_provider(const char *identifier)
342 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
343 identifier, SETTINGS);
344 if (pathname == NULL)
347 keyfile = storage_load(pathname);
353 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
355 gchar *pathname, *dirname;
357 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
358 "provider", identifier);
362 if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
363 mkdir(dirname, MODE) < 0) {
368 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
371 storage_save(keyfile, pathname);
375 static gboolean remove_all(const char *id)
379 remove_file(id, SETTINGS);
380 remove_file(id, "data");
382 removed = remove_dir(id);
383 if (removed == FALSE)
389 gboolean __connman_storage_remove_provider(const char *identifier)
394 id = g_strdup_printf("%s_%s", "provider", identifier);
398 if (remove_all(id) == TRUE)
399 DBG("Removed provider dir %s/%s", STORAGEDIR, id);
403 id = g_strdup_printf("%s_%s", "vpn", identifier);
407 if ((removed = remove_all(id)) == TRUE)
408 DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
415 gchar **__connman_storage_get_providers(void)
427 dir = opendir(STORAGEDIR);
431 while ((d = readdir(dir))) {
432 if (strcmp(d->d_name, ".") == 0 ||
433 strcmp(d->d_name, "..") == 0 ||
434 strncmp(d->d_name, "provider_", 9) != 0)
437 if (d->d_type == DT_DIR) {
438 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
440 ret = stat(str, &buf);
444 list = g_slist_prepend(list, g_strdup(d->d_name));
451 providers = g_try_new0(char *, num + 1);
452 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
453 if (providers != NULL)
454 providers[i] = iter->data;
465 * This function migrates keys from default.profile to settings file.
466 * This can be removed once the migration is over.
468 void __connman_storage_migrate(void)
471 GKeyFile *keyfile_def = NULL;
472 GKeyFile *keyfile = NULL;
473 GError *error = NULL;
474 connman_bool_t delete_old_config = TRUE;
475 char **services, **keys, *value;
479 pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
480 if (pathname == NULL)
483 /* If setting file exists, migration has been done. */
484 keyfile = __connman_storage_load_global();
486 g_key_file_free(keyfile);
492 /* If default.profile exists, create new settings file */
493 keyfile_def = storage_load(pathname);
494 if (keyfile_def == NULL)
497 services = g_key_file_get_groups(keyfile_def, NULL);
498 for (i = 0; services != NULL && services[i] != NULL; i++) {
499 if (strncmp(services[i], "wifi_", 5) != 0 &&
500 strncmp(services[i], "ethernet_", 9) != 0 &&
501 strncmp(services[i], "cellular_", 9) != 0 &&
502 strncmp(services[i], "bluetooth_", 10) != 0 &&
503 strncmp(services[i], "wimax_", 6) != 0 &&
504 strncmp(services[i], "vpn_", 4) != 0)
507 keyfile = connman_storage_load_service(services[i]);
508 if (keyfile != NULL) {
509 g_key_file_free(keyfile);
510 DBG("already exists %s", services[i]);
514 keyfile = g_key_file_new();
515 if (keyfile == NULL) {
516 connman_warn("Migrating %s failed", services[i]);
517 delete_old_config = FALSE;
521 keys = g_key_file_get_keys(keyfile_def, services[i],
524 for (k = 0; keys != NULL && keys[k] != NULL; k++) {
525 value = g_key_file_get_value(keyfile_def, services[i],
527 g_key_file_set_value(keyfile, services[i],
532 if (keys != NULL && keys[0] != NULL) {
533 err = __connman_storage_save_service(keyfile,
536 DBG("migrated %s", services[i]);
538 connman_warn("Migrating %s failed %s",
539 services[i], strerror(-err));
540 delete_old_config = FALSE;
543 DBG("no keys in %s", services[i]);
546 g_key_file_free(keyfile);
548 g_strfreev(services);
550 /* Copy global settings from default.profile to settings. */
551 keyfile = g_key_file_new();
553 val = g_key_file_get_boolean(keyfile_def, "global",
554 "OfflineMode", &error);
556 g_clear_error(&error);
560 g_key_file_set_boolean(keyfile, "global",
563 /* Migrate Powered/Enable state key/value pairs from legacy
567 val = g_key_file_get_boolean(keyfile_def, "WiFi",
570 g_clear_error(&error);
571 val = g_key_file_get_boolean(keyfile_def, "device_Wireless", "Powered", &error);
573 g_clear_error(&error);
578 g_key_file_set_boolean(keyfile, "WiFi",
581 val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
584 g_clear_error(&error);
585 val = g_key_file_get_boolean(keyfile_def, "device_Bluetooth", "Powered", &error);
587 g_clear_error(&error);
592 g_key_file_set_boolean(keyfile, "Bluetooth",
595 val = g_key_file_get_boolean(keyfile_def, "Wired",
598 g_clear_error(&error);
599 val = g_key_file_get_boolean(keyfile_def, "device_Ethernet", "Powered", &error);
601 g_clear_error(&error);
606 g_key_file_set_boolean(keyfile, "Wired",
609 val = g_key_file_get_boolean(keyfile_def, "Cellular",
612 g_clear_error(&error);
613 val = g_key_file_get_boolean(keyfile_def, "device_Cellular", "Powered", &error);
615 g_clear_error(&error);
620 g_key_file_set_boolean(keyfile, "Cellular",
623 val = g_key_file_get_boolean(keyfile_def, "WiMAX",
626 g_clear_error(&error);
627 val = g_key_file_get_boolean(keyfile_def, "device_WiMAX", "Powered", &error);
629 g_clear_error(&error);
634 g_key_file_set_boolean(keyfile, "WiMAX",
637 if (__connman_storage_save_global(keyfile) < 0) {
638 connman_warn("Migrating global config failed");
639 delete_old_config = FALSE;
642 g_key_file_free(keyfile);
644 g_key_file_free(keyfile_def);
646 if (delete_old_config == TRUE) {
647 DBG("migration done for %s", pathname);