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()
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()
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()
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)
197 * If the settings file is not found, then
198 * assume this directory is not a services dir.
200 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
202 ret = stat(str, &buf);
207 g_string_append_printf(result, "%s/", d->d_name);
214 str = g_string_free(result, FALSE);
216 str[strlen(str) - 1] = '\0';
217 services = g_strsplit(str, "/", -1);
224 GKeyFile *connman_storage_load_service(const char *service_id)
227 GKeyFile *keyfile = NULL;
229 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
233 keyfile = storage_load(pathname);
239 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
242 gchar *pathname, *dirname;
244 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
248 /* If the dir doesn't exist, create it */
249 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
250 if(mkdir(dirname, MODE) < 0) {
251 if (errno != EEXIST) {
258 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
262 ret = storage_save(keyfile, pathname);
269 static gboolean remove_file(const char *service_id, const char *file)
272 gboolean ret = FALSE;
274 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
278 if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
280 } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR) == TRUE) {
289 static gboolean remove_dir(const char *service_id)
292 gboolean ret = FALSE;
294 pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
298 if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
300 } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR) == TRUE) {
309 gboolean __connman_storage_remove_service(const char *service_id)
313 /* Remove service configuration file */
314 removed = remove_file(service_id, SETTINGS);
315 if (removed == FALSE)
318 /* Remove the statistics file also */
319 removed = remove_file(service_id, "data");
320 if (removed == FALSE)
323 removed = remove_dir(service_id);
324 if (removed == FALSE)
327 DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
332 GKeyFile *__connman_storage_load_provider(const char *identifier)
337 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
338 identifier, SETTINGS);
339 if (pathname == NULL)
342 keyfile = storage_load(pathname);
348 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
350 gchar *pathname, *dirname;
352 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
353 "provider", identifier);
357 if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
358 mkdir(dirname, MODE) < 0) {
363 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
366 storage_save(keyfile, pathname);
370 gchar **__connman_storage_get_providers(void)
382 dir = opendir(STORAGEDIR);
386 while ((d = readdir(dir))) {
387 if (strcmp(d->d_name, ".") == 0 ||
388 strcmp(d->d_name, "..") == 0 ||
389 strncmp(d->d_name, "provider_", 9) != 0)
392 if (d->d_type == DT_DIR) {
393 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
395 ret = stat(str, &buf);
399 list = g_slist_prepend(list, g_strdup(d->d_name));
406 providers = g_try_new0(char *, num + 1);
407 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
408 if (providers != NULL)
409 providers[i] = iter->data;
420 * This function migrates keys from default.profile to settings file.
421 * This can be removed once the migration is over.
423 void __connman_storage_migrate()
426 GKeyFile *keyfile_def = NULL;
427 GKeyFile *keyfile = NULL;
428 GError *error = NULL;
429 connman_bool_t delete_old_config = TRUE;
430 char **services, **keys, *value;
434 pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
435 if (pathname == NULL)
438 /* If setting file exists, migration has been done. */
439 keyfile = __connman_storage_load_global();
441 g_key_file_free(keyfile);
447 /* If default.profile exists, create new settings file */
448 keyfile_def = storage_load(pathname);
449 if (keyfile_def == NULL)
452 services = g_key_file_get_groups(keyfile_def, NULL);
453 for (i = 0; services != NULL && services[i] != NULL; i++) {
454 if (strncmp(services[i], "wifi_", 5) != 0 &&
455 strncmp(services[i], "ethernet_", 9) != 0 &&
456 strncmp(services[i], "cellular_", 9) != 0 &&
457 strncmp(services[i], "bluetooth_", 10) != 0 &&
458 strncmp(services[i], "wimax_", 6) != 0 &&
459 strncmp(services[i], "vpn_", 4) != 0)
462 keyfile = connman_storage_load_service(services[i]);
463 if (keyfile != NULL) {
464 g_key_file_free(keyfile);
465 DBG("already exists %s", services[i]);
469 keyfile = g_key_file_new();
470 if (keyfile == NULL) {
471 connman_warn("Migrating %s failed", services[i]);
472 delete_old_config = FALSE;
476 keys = g_key_file_get_keys(keyfile_def, services[i],
479 for (k = 0; keys != NULL && keys[k] != NULL; k++) {
480 value = g_key_file_get_value(keyfile_def, services[i],
482 g_key_file_set_value(keyfile, services[i],
487 if (keys != NULL && keys[0] != NULL) {
488 err = __connman_storage_save_service(keyfile,
491 DBG("migrated %s", services[i]);
493 connman_warn("Migrating %s failed %s",
494 services[i], strerror(-err));
495 delete_old_config = FALSE;
498 DBG("no keys in %s", services[i]);
501 g_key_file_free(keyfile);
503 g_strfreev(services);
505 /* Copy global settings from default.profile to settings. */
506 keyfile = g_key_file_new();
508 val = g_key_file_get_boolean(keyfile_def, "global",
509 "OfflineMode", &error);
511 g_clear_error(&error);
515 g_key_file_set_boolean(keyfile, "global",
518 /* Migrate Powered/Enable state key/value pairs from legacy
522 val = g_key_file_get_boolean(keyfile_def, "WiFi",
525 g_clear_error(&error);
526 val = g_key_file_get_boolean(keyfile_def, "device_Wireless", "Powered", &error);
528 g_clear_error(&error);
533 g_key_file_set_boolean(keyfile, "WiFi",
536 val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
539 g_clear_error(&error);
540 val = g_key_file_get_boolean(keyfile_def, "device_Bluetooth", "Powered", &error);
542 g_clear_error(&error);
547 g_key_file_set_boolean(keyfile, "Bluetooth",
550 val = g_key_file_get_boolean(keyfile_def, "Wired",
553 g_clear_error(&error);
554 val = g_key_file_get_boolean(keyfile_def, "device_Ethernet", "Powered", &error);
556 g_clear_error(&error);
561 g_key_file_set_boolean(keyfile, "Wired",
564 val = g_key_file_get_boolean(keyfile_def, "Cellular",
567 g_clear_error(&error);
568 val = g_key_file_get_boolean(keyfile_def, "device_Cellular", "Powered", &error);
570 g_clear_error(&error);
575 g_key_file_set_boolean(keyfile, "Cellular",
578 val = g_key_file_get_boolean(keyfile_def, "WiMAX",
581 g_clear_error(&error);
582 val = g_key_file_get_boolean(keyfile_def, "device_WiMAX", "Powered", &error);
584 g_clear_error(&error);
589 g_key_file_set_boolean(keyfile, "WiMAX",
592 if (__connman_storage_save_global(keyfile) < 0) {
593 connman_warn("Migrating global config failed");
594 delete_old_config = FALSE;
597 g_key_file_free(keyfile);
599 g_key_file_free(keyfile_def);
601 if (delete_old_config == TRUE) {
602 DBG("migration done for %s", pathname);