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 GKeyFile *__connman_storage_load_provider(const char *identifier)
274 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
275 identifier, SETTINGS);
276 if (pathname == NULL)
279 keyfile = storage_load(pathname);
285 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
287 gchar *pathname, *dirname;
289 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
290 "provider", identifier);
294 if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
295 mkdir(dirname, MODE) < 0) {
300 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
303 storage_save(keyfile, pathname);
307 gchar **__connman_storage_get_providers(void)
319 dir = opendir(STORAGEDIR);
323 while ((d = readdir(dir))) {
324 if (strcmp(d->d_name, ".") == 0 ||
325 strcmp(d->d_name, "..") == 0 ||
326 strncmp(d->d_name, "provider_", 9) != 0)
329 if (d->d_type == DT_DIR) {
330 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
332 ret = stat(str, &buf);
336 list = g_slist_prepend(list, g_strdup(d->d_name));
343 providers = g_try_new0(char *, num + 1);
344 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
345 if (providers != NULL)
346 providers[i] = iter->data;
357 * This function migrates keys from default.profile to settings file.
358 * This can be removed once the migration is over.
360 void __connman_storage_migrate()
363 GKeyFile *keyfile_def = NULL;
364 GKeyFile *keyfile = NULL;
365 GError *error = NULL;
366 connman_bool_t delete_old_config = TRUE;
367 char **services, **keys, *value;
371 pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
372 if (pathname == NULL)
375 /* If setting file exists, migration has been done. */
376 keyfile = __connman_storage_load_global();
378 g_key_file_free(keyfile);
384 /* If default.profile exists, create new settings file */
385 keyfile_def = storage_load(pathname);
386 if (keyfile_def == NULL)
389 services = g_key_file_get_groups(keyfile_def, NULL);
390 for (i = 0; services != NULL && services[i] != NULL; i++) {
391 if (strncmp(services[i], "wifi_", 5) != 0 &&
392 strncmp(services[i], "ethernet_", 9) != 0 &&
393 strncmp(services[i], "cellular_", 9) != 0 &&
394 strncmp(services[i], "bluetooth_", 10) != 0 &&
395 strncmp(services[i], "wimax_", 6) != 0 &&
396 strncmp(services[i], "vpn_", 4) != 0)
399 keyfile = connman_storage_load_service(services[i]);
400 if (keyfile != NULL) {
401 g_key_file_free(keyfile);
402 DBG("already exists %s", services[i]);
406 keyfile = g_key_file_new();
407 if (keyfile == NULL) {
408 connman_warn("Migrating %s failed", services[i]);
409 delete_old_config = FALSE;
413 keys = g_key_file_get_keys(keyfile_def, services[i],
416 for (k = 0; keys != NULL && keys[k] != NULL; k++) {
417 value = g_key_file_get_value(keyfile_def, services[i],
419 g_key_file_set_value(keyfile, services[i],
424 if (keys != NULL && keys[0] != NULL) {
425 err = __connman_storage_save_service(keyfile,
428 DBG("migrated %s", services[i]);
430 connman_warn("Migrating %s failed %s",
431 services[i], strerror(-err));
432 delete_old_config = FALSE;
435 DBG("no keys in %s", services[i]);
438 g_key_file_free(keyfile);
440 g_strfreev(services);
442 /* Copy global settings from default.profile to settings. */
443 keyfile = g_key_file_new();
445 val = g_key_file_get_boolean(keyfile_def, "global",
446 "OfflineMode", &error);
448 g_clear_error(&error);
452 g_key_file_set_boolean(keyfile, "global",
455 /* Migrate Powered/Enable state key/value pairs from legacy
459 val = g_key_file_get_boolean(keyfile_def, "WiFi",
462 g_clear_error(&error);
463 val = g_key_file_get_boolean(keyfile_def, "device_Wireless", "Powered", &error);
465 g_clear_error(&error);
470 g_key_file_set_boolean(keyfile, "WiFi",
473 val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
476 g_clear_error(&error);
477 val = g_key_file_get_boolean(keyfile_def, "device_Bluetooth", "Powered", &error);
479 g_clear_error(&error);
484 g_key_file_set_boolean(keyfile, "Bluetooth",
487 val = g_key_file_get_boolean(keyfile_def, "Wired",
490 g_clear_error(&error);
491 val = g_key_file_get_boolean(keyfile_def, "device_Ethernet", "Powered", &error);
493 g_clear_error(&error);
498 g_key_file_set_boolean(keyfile, "Wired",
501 val = g_key_file_get_boolean(keyfile_def, "Cellular",
504 g_clear_error(&error);
505 val = g_key_file_get_boolean(keyfile_def, "device_Cellular", "Powered", &error);
507 g_clear_error(&error);
512 g_key_file_set_boolean(keyfile, "Cellular",
515 val = g_key_file_get_boolean(keyfile_def, "WiMAX",
518 g_clear_error(&error);
519 val = g_key_file_get_boolean(keyfile_def, "device_WiMAX", "Powered", &error);
521 g_clear_error(&error);
526 g_key_file_set_boolean(keyfile, "WiMAX",
529 if (__connman_storage_save_global(keyfile) < 0) {
530 connman_warn("Migrating global config failed");
531 delete_old_config = FALSE;
534 g_key_file_free(keyfile);
536 g_key_file_free(keyfile_def);
538 if (delete_old_config == TRUE) {
539 DBG("migration done for %s", pathname);