*
* Connection Manager
*
- * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2013 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 <config.h>
#endif
+#include <errno.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <stdio.h>
+
+#include <connman/storage.h>
+
#include "connman.h"
-static GSList *storage_list = NULL;
+#define SETTINGS "settings"
+#define DEFAULT "default.profile"
+
+#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;
- return storage2->priority - storage1->priority;
+ 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);
+
+ 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;
- storage_list = g_slist_insert_sorted(storage_list, storage,
- compare_priority);
+ data = g_key_file_to_data(keyfile, &length, NULL);
- return 0;
+ if (!g_file_set_contents(pathname, data, length, &error)) {
+ DBG("Failed to store information: %s", error->message);
+ g_error_free(error);
+ ret = -EIO;
+ }
+
+#if defined TIZEN_EXT
+ {
+ FILE *fp = NULL;
+ fp = fopen(pathname, "a+");
+ if(fp){
+ fflush(fp);
+ fsync(fp->_fileno);
+ fclose(fp);
+ DBG("sync the file to disk");
+ }
+ }
+#endif
+
+ g_free(data);
+
+ 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);
}
-int __connman_storage_init_device(void)
+GKeyFile *__connman_storage_load_global(void)
{
- GSList *list;
+ gchar *pathname;
+ GKeyFile *keyfile = NULL;
- DBG("");
+ pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+ if (!pathname)
+ return NULL;
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ keyfile = storage_load(pathname);
- if (storage->device_init) {
- if (storage->device_init() == 0)
- return 0;
- }
- }
+ g_free(pathname);
- return -ENOENT;
+ return keyfile;
}
-int __connman_storage_load_device(struct connman_device *device)
+int __connman_storage_save_global(GKeyFile *keyfile)
{
- GSList *list;
+ gchar *pathname;
+ int ret;
- DBG("device %p", device);
+ pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+ if (!pathname)
+ return -ENOMEM;
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ ret = storage_save(keyfile, pathname);
- if (storage->device_load) {
- if (storage->device_load(device) == 0)
- return 0;
- }
- }
+ g_free(pathname);
- return -ENOENT;
+ return ret;
}
-int __connman_storage_save_device(struct connman_device *device)
+void __connman_storage_delete_global(void)
{
- GSList *list;
+ gchar *pathname;
- DBG("device %p", device);
+ pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+ if (!pathname)
+ return;
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ storage_delete(pathname);
- if (storage->device_save) {
- if (storage->device_save(device) == 0)
- return 0;
- }
- }
+ g_free(pathname);
+}
- return -ENOENT;
+GKeyFile *__connman_storage_load_config(const char *ident)
+{
+ gchar *pathname;
+ GKeyFile *keyfile = NULL;
+
+ pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
+ if (!pathname)
+ return NULL;
+
+ keyfile = storage_load(pathname);
+
+ g_free(pathname);
+
+ return keyfile;
}
-int __connman_storage_init_network(struct connman_device *device)
+GKeyFile *__connman_storage_load_provider_config(const char *ident)
{
- GSList *list;
+ gchar *pathname;
+ GKeyFile *keyfile = NULL;
- DBG("device %p", device);
+ pathname = g_strdup_printf("%s/%s.config", VPN_STORAGEDIR, ident);
+ if (!pathname)
+ return NULL;
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ keyfile = storage_load(pathname);
- if (storage->network_init) {
- if (storage->network_init(device) == 0)
- return 0;
- }
- }
+ g_free(pathname);
- return -ENOENT;
+ return keyfile;
}
-int __connman_storage_load_network(struct connman_network *network)
+GKeyFile *__connman_storage_open_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)
+ return NULL;
+
+ keyfile = storage_load(pathname);
+ if (keyfile) {
+ g_free(pathname);
+ return keyfile;
+ }
+
+ g_free(pathname);
- DBG("network %p", network);
+ keyfile = g_key_file_new();
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ return keyfile;
+}
- if (storage->network_load) {
- if (storage->network_load(network) == 0)
- return 0;
+gchar **connman_storage_get_services(void)
+{
+ struct dirent *d;
+ gchar *str;
+ DIR *dir;
+ GString *result;
+ gchar **services = NULL;
+ struct stat buf;
+ int ret;
+
+ dir = opendir(STORAGEDIR);
+ if (!dir)
+ return NULL;
+
+ 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:
+ case DT_UNKNOWN:
+ /*
+ * 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;
}
}
- return -ENOENT;
+ closedir(dir);
+
+ str = g_string_free(result, FALSE);
+ if (str && str[0] != '\0') {
+ /*
+ * Remove the trailing separator so that services doesn't end up
+ * with an empty element.
+ */
+ str[strlen(str) - 1] = '\0';
+ services = g_strsplit(str, "/", -1);
+ }
+ g_free(str);
+
+ return services;
}
-int __connman_storage_save_network(struct connman_network *network)
+GKeyFile *connman_storage_load_service(const char *service_id)
{
- GSList *list;
+ gchar *pathname;
+ GKeyFile *keyfile = NULL;
- DBG("network %p", network);
+ pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
+ if (!pathname)
+ return NULL;
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ keyfile = storage_load(pathname);
+ g_free(pathname);
- if (storage->network_save) {
- if (storage->network_save(network) == 0)
- return 0;
+ return keyfile;
+}
+
+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)
+ 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_init_service(void)
+static bool remove_file(const char *service_id, const char *file)
{
- DBG("");
+ gchar *pathname;
+ bool ret = false;
+
+ pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
+ if (!pathname)
+ return false;
+
+ if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
+ ret = true;
+ } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR)) {
+ unlink(pathname);
+ ret = true;
+ }
- return -ENOENT;
+ g_free(pathname);
+ return ret;
}
-int __connman_storage_load_service(struct connman_service *service)
+static bool remove_dir(const char *service_id)
{
- GSList *list;
+ gchar *pathname;
+ bool ret = false;
+
+ pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
+ if (!pathname)
+ return false;
+
+ if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
+ ret = true;
+ } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR)) {
+ rmdir(pathname);
+ ret = true;
+ }
- DBG("service %p", service);
+ g_free(pathname);
+ return ret;
+}
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+bool __connman_storage_remove_service(const char *service_id)
+{
+ bool removed;
- if (storage->service_load) {
- if (storage->service_load(service) == 0)
- return 0;
- }
- }
+ /* Remove service configuration file */
+ removed = remove_file(service_id, SETTINGS);
+ if (!removed)
+ return false;
+
+ /* Remove the statistics file also */
+ removed = remove_file(service_id, "data");
+ if (!removed)
+ return false;
+
+ removed = remove_dir(service_id);
+ if (!removed)
+ return false;
+
+ DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
- return -ENOENT;
+ return true;
}
-int __connman_storage_save_service(struct connman_service *service)
+GKeyFile *__connman_storage_load_provider(const char *identifier)
{
- GSList *list;
+ gchar *pathname;
+ GKeyFile *keyfile;
- DBG("service %p", service);
+ pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
+ identifier, SETTINGS);
+ if (!pathname)
+ return NULL;
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ keyfile = storage_load(pathname);
+ g_free(pathname);
- if (storage->service_save) {
- if (storage->service_save(service) == 0)
- return 0;
- }
+ return keyfile;
+}
+
+void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
+{
+ gchar *pathname, *dirname;
+
+ dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
+ "provider", identifier);
+ if (!dirname)
+ return;
+
+ if (!g_file_test(dirname, G_FILE_TEST_IS_DIR) &&
+ 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);
+}
+
+static bool remove_all(const char *id)
+{
+ bool removed;
+
+ remove_file(id, SETTINGS);
+ remove_file(id, "data");
+
+ removed = remove_dir(id);
+ if (!removed)
+ return false;
+
+ return true;
}
-int __connman_storage_init(void)
+bool __connman_storage_remove_provider(const char *identifier)
{
- DBG("");
+ bool removed;
+ gchar *id;
+
+ id = g_strdup_printf("%s_%s", "provider", identifier);
+ if (!id)
+ return false;
+
+ if (remove_all(id))
+ DBG("Removed provider dir %s/%s", STORAGEDIR, id);
- return 0;
+ g_free(id);
+
+ id = g_strdup_printf("%s_%s", "vpn", identifier);
+ if (!id)
+ return false;
+
+ if ((removed = remove_all(id)))
+ DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
+
+ g_free(id);
+
+ return removed;
}
-void __connman_storage_cleanup(void)
+gchar **__connman_storage_get_providers(void)
{
- DBG("");
+ 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)
+ return NULL;
+
+ 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;
+ }
+ }
+
+ closedir(dir);
+
+ providers = g_try_new0(char *, num + 1);
+ for (iter = list; iter; iter = g_slist_next(iter)) {
+ if (providers)
+ providers[i] = iter->data;
+ else
+ g_free(iter->data);
+ i += 1;
+ }
+ g_slist_free(list);
+
+ return providers;
}