device: Combine two if statements with identical outcome
[framework/connectivity/connman.git] / src / storage.c
index 274997b..47bd0cb 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
 
 #include <errno.h>
 #include <unistd.h>
+#include <sys/stat.h>
+#include <dirent.h>
+
+#include <connman/storage.h>
 
 #include "connman.h"
 
-#define PROFILE_SUFFIX "profile"
-#define CONFIG_SUFFIX  "config"
+#define SETTINGS       "settings"
+#define DEFAULT                "default.profile"
 
-static GSList *storage_list = NULL;
+#define MODE           (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
+                       S_IXGRP | S_IROTH | S_IXOTH)
 
-static gint compare_priority(gconstpointer a, gconstpointer b)
+static GKeyFile *storage_load(const char *pathname)
 {
-       const struct connman_storage *storage1 = a;
-       const struct connman_storage *storage2 = b;
+       GKeyFile *keyfile = NULL;
+       GError *error = NULL;
+
+       DBG("Loading %s", pathname);
+
+       keyfile = g_key_file_new();
+
+       if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) {
+               DBG("Unable to load %s: %s", pathname, error->message);
+               g_clear_error(&error);
 
-       return storage2->priority - storage1->priority;
+               g_key_file_free(keyfile);
+               keyfile = NULL;
+       }
+
+       return keyfile;
 }
 
-/**
- * connman_storage_register:
- * @storage: storage module
- *
- * Register a new storage module
- *
- * Returns: %0 on success
- */
-int connman_storage_register(struct connman_storage *storage)
+static int storage_save(GKeyFile *keyfile, char *pathname)
 {
-       DBG("storage %p name %s", storage, storage->name);
+       gchar *data = NULL;
+       gsize length = 0;
+       GError *error = NULL;
+       int ret = 0;
+
+       data = g_key_file_to_data(keyfile, &length, NULL);
 
-       storage_list = g_slist_insert_sorted(storage_list, storage,
-                                                       compare_priority);
+       if (!g_file_set_contents(pathname, data, length, &error)) {
+               DBG("Failed to store information: %s", error->message);
+               g_error_free(error);
+               ret = -EIO;
+       }
+
+       g_free(data);
 
-       return 0;
+       return ret;
 }
 
-/**
- * connman_storage_unregister:
- * @storage: storage module
- *
- * Remove a previously registered storage module
- */
-void connman_storage_unregister(struct connman_storage *storage)
+static void storage_delete(const char *pathname)
 {
-       DBG("storage %p name %s", storage, storage->name);
+       DBG("file path %s", pathname);
 
-       storage_list = g_slist_remove(storage_list, storage);
+       if (unlink(pathname) < 0)
+               connman_error("Failed to remove %s", pathname);
 }
 
-GKeyFile *__connman_storage_open(const char *ident, const char *suffix)
+GKeyFile *__connman_storage_load_global()
 {
-       GKeyFile *keyfile;
-       gchar *pathname, *data = NULL;
-       gboolean result;
-       gsize length;
-
-       DBG("ident %s suffix %s", ident, suffix);
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
-       if (pathname == NULL)
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if(pathname == NULL)
                return NULL;
 
-       result = g_file_get_contents(pathname, &data, &length, NULL);
+       keyfile = storage_load(pathname);
 
        g_free(pathname);
 
-       keyfile = g_key_file_new();
+       return keyfile;
+}
 
-       if (result == FALSE)
-               goto done;
+int __connman_storage_save_global(GKeyFile *keyfile)
+{
+       gchar *pathname;
+       int ret;
 
-       if (length > 0)
-               g_key_file_load_from_data(keyfile, data, length, 0, NULL);
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if(pathname == NULL)
+               return -ENOMEM;
 
-       g_free(data);
+       ret = storage_save(keyfile, pathname);
 
-done:
-       DBG("keyfile %p", keyfile);
+       g_free(pathname);
 
-       return keyfile;
+       return ret;
 }
 
-void __connman_storage_close(const char *ident, const char *suffix,
-                                       GKeyFile *keyfile, gboolean save)
+void __connman_storage_delete_global()
 {
-       gchar *pathname, *data = NULL;
-       gsize length = 0;
-
-       DBG("ident %s suffix %s keyfile %p save %d",
-                                       ident, suffix, keyfile, save);
+       gchar *pathname;
 
-       if (save == FALSE) {
-               g_key_file_free(keyfile);
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if(pathname == NULL)
                return;
-       }
 
-       pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
-       if (pathname == NULL)
-               return;
+       storage_delete(pathname);
 
-       data = g_key_file_to_data(keyfile, &length, NULL);
+       g_free(pathname);
+}
 
-       if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
-               connman_error("Failed to store information");
+GKeyFile *__connman_storage_load_config(const char *ident)
+{
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       g_free(data);
+       pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
+       if(pathname == NULL)
+               return NULL;
+
+       keyfile = storage_load(pathname);
 
        g_free(pathname);
 
-       g_key_file_free(keyfile);
+       return keyfile;
 }
 
-void __connman_storage_delete(const char *ident, const char *suffix)
+GKeyFile *__connman_storage_open_service(const char *service_id)
 {
        gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       DBG("ident %s suffix %s", ident, suffix);
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
+       if(pathname == NULL)
+               return NULL;
 
-       pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
-       if (pathname == NULL)
-               return;
+       keyfile =  storage_load(pathname);
+       if (keyfile) {
+               g_free(pathname);
+               return keyfile;
+       }
 
-       if (unlink(pathname) < 0)
-               connman_error("Failed to remove %s", pathname);
-}
+       g_free(pathname);
 
-GKeyFile *__connman_storage_open_profile(const char *ident)
-{
-       return __connman_storage_open(ident, PROFILE_SUFFIX);
-}
+       keyfile = g_key_file_new();
 
-void __connman_storage_close_profile(const char *ident,
-                                       GKeyFile *keyfile, gboolean save)
-{
-       __connman_storage_close(ident, PROFILE_SUFFIX, keyfile, save);
+       return keyfile;
 }
 
-void __connman_storage_delete_profile(const char *ident)
+gchar **connman_storage_get_services()
 {
-       __connman_storage_delete(ident, PROFILE_SUFFIX);
-}
+       struct dirent *d;
+       gchar *str;
+       DIR *dir;
+       GString *result;
+       gchar **services = NULL;
+       struct stat buf;
+       int ret;
+
+       dir = opendir(STORAGEDIR);
+       if (dir == NULL)
+               return NULL;
 
-GKeyFile *__connman_storage_open_config(const char *ident)
-{
-       return __connman_storage_open(ident, CONFIG_SUFFIX);
-}
+       result = g_string_new(NULL);
+
+       while ((d = readdir(dir))) {
+               if (strcmp(d->d_name, ".") == 0 ||
+                               strcmp(d->d_name, "..") == 0 ||
+                               strncmp(d->d_name, "provider_", 9) == 0)
+                       continue;
+
+               switch (d->d_type) {
+               case DT_DIR:
+                       /*
+                        * If the settings file is not found, then
+                        * assume this directory is not a services dir.
+                        */
+                       str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
+                                                               d->d_name);
+                       ret = stat(str, &buf);
+                       g_free(str);
+                       if (ret < 0)
+                               continue;
+
+                       g_string_append_printf(result, "%s/", d->d_name);
+                       break;
+               }
+       }
 
-void __connman_storage_close_config(const char *ident,
-                                       GKeyFile *keyfile, gboolean save)
-{
-       __connman_storage_close(ident, CONFIG_SUFFIX, keyfile, save);
-}
+       closedir(dir);
 
-void __connman_storage_delete_config(const char *ident)
-{
-       __connman_storage_delete(ident, CONFIG_SUFFIX);
+       str = g_string_free(result, FALSE);
+       if (str) {
+               str[strlen(str) - 1] = '\0';
+               services = g_strsplit(str, "/", -1);
+       }
+       g_free(str);
+
+       return services;
 }
 
-int __connman_storage_init_profile(void)
+GKeyFile *connman_storage_load_service(const char *service_id)
 {
-       GSList *list;
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
+
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
+       if(pathname == NULL)
+               return NULL;
 
-       DBG("");
+       keyfile =  storage_load(pathname);
+       g_free(pathname);
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       return keyfile;
+}
 
-               if (storage->profile_init) {
-                       if (storage->profile_init() == 0)
-                               return 0;
+int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
+{
+       int ret = 0;
+       gchar *pathname, *dirname;
+
+       dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
+       if(dirname == NULL)
+               return -ENOMEM;
+
+       /* If the dir doesn't exist, create it */
+       if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
+               if(mkdir(dirname, MODE) < 0) {
+                       if (errno != EEXIST) {
+                               g_free(dirname);
+                               return -errno;
+                       }
                }
        }
 
-       return -ENOENT;
+       pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
+
+       g_free(dirname);
+
+       ret = storage_save(keyfile, pathname);
+
+       g_free(pathname);
+
+       return ret;
 }
 
-int __connman_storage_load_profile(struct connman_profile *profile)
+static gboolean remove_file(const char *service_id, const char *file)
 {
-       GSList *list;
-
-       DBG("profile %p", profile);
+       gchar *pathname;
+       gboolean ret = FALSE;
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
+       if(pathname == NULL)
+               return FALSE;
 
-               if (storage->profile_load) {
-                       if (storage->profile_load(profile) == 0)
-                               return 0;
-               }
+       if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
+               ret = TRUE;
+       } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR) == TRUE) {
+               unlink(pathname);
+               ret = TRUE;
        }
 
-       return -ENOENT;
+       g_free(pathname);
+       return ret;
 }
 
-int __connman_storage_save_profile(struct connman_profile *profile)
+static gboolean remove_dir(const char *service_id)
 {
-       GSList *list;
-
-       DBG("profile %p", profile);
+       gchar *pathname;
+       gboolean ret = FALSE;
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
+       if(pathname == NULL)
+               return FALSE;
 
-               if (storage->profile_save) {
-                       if (storage->profile_save(profile) == 0)
-                               return 0;
-               }
+       if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
+               ret = TRUE;
+       } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR) == TRUE) {
+               rmdir(pathname);
+               ret = TRUE;
        }
 
-       return -ENOENT;
+       g_free(pathname);
+       return ret;
 }
 
-int __connman_storage_load_service(struct connman_service *service)
+gboolean __connman_storage_remove_service(const char *service_id)
 {
-       GSList *list;
+       gboolean removed;
 
-       DBG("service %p", service);
+       /* Remove service configuration file */
+       removed = remove_file(service_id, SETTINGS);
+       if (removed == FALSE)
+               return FALSE;
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       /* Remove the statistics file also */
+       removed = remove_file(service_id, "data");
+       if (removed == FALSE)
+               return FALSE;
 
-               if (storage->service_load) {
-                       if (storage->service_load(service) == 0)
-                               return 0;
-               }
-       }
+       removed = remove_dir(service_id);
+       if (removed == FALSE)
+               return FALSE;
 
-       return -ENOENT;
+       DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
+
+       return TRUE;
 }
 
-int __connman_storage_save_service(struct connman_service *service)
+GKeyFile *__connman_storage_load_provider(const char *identifier)
 {
-       GSList *list;
-
-       DBG("service %p", service);
+       gchar *pathname;
+       GKeyFile *keyfile;
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
+                       identifier, SETTINGS);
+       if (pathname == NULL)
+               return NULL;
 
-               if (storage->service_save) {
-                       if (storage->service_save(service) == 0)
-                               return 0;
-               }
-       }
+       keyfile = storage_load(pathname);
+       g_free(pathname);
 
-       return -ENOENT;
+       return keyfile;
 }
 
-int __connman_storage_load_device(struct connman_device *device)
+void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
 {
-       GSList *list;
+       gchar *pathname, *dirname;
 
-       DBG("device %p", device);
-
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
+                       "provider", identifier);
+       if (dirname == NULL)
+               return;
 
-               if (storage->device_load) {
-                       if (storage->device_load(device) == 0)
-                               return 0;
-               }
+       if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
+                       mkdir(dirname, MODE) < 0) {
+               g_free(dirname);
+               return;
        }
 
-       return -ENOENT;
+       pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
+       g_free(dirname);
+
+       storage_save(keyfile, pathname);
+       g_free(pathname);
 }
 
-int __connman_storage_save_device(struct connman_device *device)
+gchar **__connman_storage_get_providers(void)
 {
-       GSList *list;
+       GSList *list = NULL;
+       int num = 0, i = 0;
+       struct dirent *d;
+       gchar *str;
+       DIR *dir;
+       struct stat buf;
+       int ret;
+       char **providers;
+       GSList *iter;
+
+       dir = opendir(STORAGEDIR);
+       if (dir == NULL)
+               return NULL;
 
-       DBG("device %p", device);
+       while ((d = readdir(dir))) {
+               if (strcmp(d->d_name, ".") == 0 ||
+                               strcmp(d->d_name, "..") == 0 ||
+                               strncmp(d->d_name, "provider_", 9) != 0)
+                       continue;
+
+               if (d->d_type == DT_DIR) {
+                       str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
+                                       d->d_name);
+                       ret = stat(str, &buf);
+                       g_free(str);
+                       if (ret < 0)
+                               continue;
+                       list = g_slist_prepend(list, g_strdup(d->d_name));
+                       num += 1;
+               }
+       }
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       closedir(dir);
 
-               if (storage->device_save) {
-                       if (storage->device_save(device) == 0)
-                               return 0;
-               }
+       providers = g_try_new0(char *, num + 1);
+       for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
+               if (providers != NULL)
+                       providers[i] = iter->data;
+               else
+                       g_free(iter->data);
+               i += 1;
        }
+       g_slist_free(list);
 
-       return -ENOENT;
+       return providers;
 }
 
-int __connman_storage_load_technology(struct connman_technology *technology)
+/*
+ * This function migrates keys from default.profile to settings file.
+ * This can be removed once the migration is over.
+*/
+void __connman_storage_migrate()
 {
-       GSList *list;
+       gchar *pathname;
+       GKeyFile *keyfile_def = NULL;
+       GKeyFile *keyfile = NULL;
+       GError *error = NULL;
+       connman_bool_t delete_old_config = TRUE;
+       char **services, **keys, *value;
+       int i, k, err;
+       connman_bool_t val;
+
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
+       if (pathname == NULL)
+               return;
+
+       /* If setting file exists, migration has been done. */
+       keyfile = __connman_storage_load_global();
+       if (keyfile) {
+               g_key_file_free(keyfile);
+               unlink(pathname);
+               g_free(pathname);
+               return;
+       }
+
+       /* If default.profile exists, create new settings file */
+       keyfile_def = storage_load(pathname);
+       if (keyfile_def == NULL)
+               goto done;
 
-       DBG("technology %p", technology);
+       services = g_key_file_get_groups(keyfile_def, NULL);
+       for (i = 0; services != NULL && services[i] != NULL; i++) {
+               if (strncmp(services[i], "wifi_", 5) != 0 &&
+                               strncmp(services[i], "ethernet_", 9) != 0 &&
+                               strncmp(services[i], "cellular_", 9) != 0 &&
+                               strncmp(services[i], "bluetooth_", 10) != 0 &&
+                               strncmp(services[i], "wimax_", 6) != 0 &&
+                               strncmp(services[i], "vpn_", 4) != 0)
+                       continue;
+
+               keyfile = connman_storage_load_service(services[i]);
+               if (keyfile != NULL) {
+                       g_key_file_free(keyfile);
+                       DBG("already exists %s", services[i]);
+                       continue;
+               }
+
+               keyfile = g_key_file_new();
+               if (keyfile == NULL) {
+                       connman_warn("Migrating %s failed", services[i]);
+                       delete_old_config = FALSE;
+                       continue;
+               }
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+               keys = g_key_file_get_keys(keyfile_def, services[i],
+                               NULL, NULL);
 
-               if (storage->tech_load) {
-                       if (storage->tech_load(technology) == 0)
-                               return 0;
+               for (k = 0; keys != NULL && keys[k] != NULL; k++) {
+                       value = g_key_file_get_value(keyfile_def, services[i],
+                                       keys[k], NULL);
+                       g_key_file_set_value(keyfile, services[i],
+                                       keys[k], value);
+                       g_free(value);
                }
+
+               if (keys != NULL && keys[0] != NULL) {
+                       err = __connman_storage_save_service(keyfile,
+                                       services[i]);
+                       if (err >= 0)
+                               DBG("migrated %s", services[i]);
+                       else {
+                               connman_warn("Migrating %s failed %s",
+                                               services[i], strerror(-err));
+                               delete_old_config = FALSE;
+                       }
+               } else
+                       DBG("no keys in %s", services[i]);
+
+               g_strfreev(keys);
+               g_key_file_free(keyfile);
        }
+       g_strfreev(services);
 
-       return -ENOENT;
-}
+       /* Copy global settings from default.profile to settings. */
+       keyfile = g_key_file_new();
 
-int __connman_storage_save_technology(struct connman_technology *technology)
-{
-       GSList *list;
+       val = g_key_file_get_boolean(keyfile_def, "global",
+                                       "OfflineMode", &error);
+       if (error != NULL) {
+               g_clear_error(&error);
+               val = FALSE;
+       }
 
-       DBG("technology %p", technology);
+       g_key_file_set_boolean(keyfile, "global",
+                                       "OfflineMode", val);
+
+       /* Migrate Powered/Enable state key/value pairs from legacy
+        * settings
+        */
+
+       val = g_key_file_get_boolean(keyfile_def, "WiFi",
+                                       "Enable", &error);
+       if (error != NULL) {
+               g_clear_error(&error);
+               val = g_key_file_get_boolean(keyfile_def, "device_Wireless", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
+               }
+       }
 
-       for (list = storage_list; list; list = list->next) {
-               struct connman_storage *storage = list->data;
+       g_key_file_set_boolean(keyfile, "WiFi",
+                                       "Enable", val);
+
+       val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
+                                       "Enable", &error);
+       if (error != NULL) {
+               g_clear_error(&error);
+               val = g_key_file_get_boolean(keyfile_def, "device_Bluetooth", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
+               }
+       }
 
-               if (storage->tech_save) {
-                       if (storage->tech_save(technology) == 0)
-                               return 0;
+       g_key_file_set_boolean(keyfile, "Bluetooth",
+                                       "Enable", val);
+
+       val = g_key_file_get_boolean(keyfile_def, "Wired",
+                                       "Enable", &error);
+       if (error != NULL) {
+               g_clear_error(&error);
+               val = g_key_file_get_boolean(keyfile_def, "device_Ethernet", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
                }
        }
 
-       return -ENOENT;
-}
+       g_key_file_set_boolean(keyfile, "Wired",
+                                       "Enable", val);
+
+       val = g_key_file_get_boolean(keyfile_def, "Cellular",
+                                       "Enable", &error);
+       if (error != NULL) {
+               g_clear_error(&error);
+               val = g_key_file_get_boolean(keyfile_def, "device_Cellular", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
+               }
+       }
 
-int __connman_storage_init(void)
-{
-       DBG("");
+       g_key_file_set_boolean(keyfile, "Cellular",
+                                       "Enable", val);
+
+       val = g_key_file_get_boolean(keyfile_def, "WiMAX",
+                                       "Enable", &error);
+       if (error != NULL) {
+               g_clear_error(&error);
+               val = g_key_file_get_boolean(keyfile_def, "device_WiMAX", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
+               }
+       }
 
-       return 0;
-}
+       g_key_file_set_boolean(keyfile, "WiMAX",
+                                       "Enable", val);
 
-void __connman_storage_cleanup(void)
-{
-       DBG("");
+       if (__connman_storage_save_global(keyfile) < 0) {
+               connman_warn("Migrating global config failed");
+               delete_old_config = FALSE;
+       }
+
+       g_key_file_free(keyfile);
+
+       g_key_file_free(keyfile_def);
+
+       if (delete_old_config == TRUE) {
+               DBG("migration done for %s", pathname);
+               unlink(pathname);
+       }
+done:
+       g_free(pathname);
 }