Imported Upstream connman version 1.38
[platform/upstream/connman.git] / src / storage.c
old mode 100644 (file)
new mode 100755 (executable)
index 3ed0fc1..9be60de
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2008  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"
 
-int __connman_storage_init(void)
+#define SETTINGS       "settings"
+#define DEFAULT                "default.profile"
+#if defined TIZEN_EXT
+#define INS_SETTINGS   "settings.ins"
+#endif
+
+#define MODE           (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
+                       S_IXGRP | S_IROTH | S_IXOTH)
+
+static GKeyFile *storage_load(const char *pathname)
 {
-       DBG("");
+       GKeyFile *keyfile = NULL;
+       GError *error = NULL;
+
+       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 0;
+               g_key_file_free(keyfile);
+               keyfile = NULL;
+       }
+
+       return keyfile;
 }
 
-void __connman_storage_cleanup(void)
+static int storage_save(GKeyFile *keyfile, char *pathname)
 {
-       DBG("");
+       gchar *data = NULL;
+       gsize length = 0;
+       GError *error = NULL;
+       int ret = 0;
+
+       data = g_key_file_to_data(keyfile, &length, NULL);
+
+       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;
 }
 
-static int do_load(GKeyFile *keyfile, struct connman_element *element)
+static void storage_delete(const char *pathname)
 {
-       const gchar *value;
+       DBG("file path %s", pathname);
 
-       DBG("element %p name %s", element, element->name);
+       if (unlink(pathname) < 0)
+               connman_error("Failed to remove %s", pathname);
+}
 
-       value = g_key_file_get_string(keyfile, element->path,
-                                               "Policy", NULL);
-       if (value != NULL)
-               element->policy = __connman_element_string2policy(value);
+GKeyFile *__connman_storage_load_global(void)
+{
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
-               element->remember = g_key_file_get_boolean(keyfile,
-                                       element->path, "Remember", NULL);
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if (!pathname)
+               return NULL;
 
-       value = g_key_file_get_string(keyfile, element->path,
-                                               "WiFi.Security", NULL);
-       if (value != NULL)
-               connman_element_set_property(element,
-                               CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value);
+       keyfile = storage_load(pathname);
 
-       value = g_key_file_get_string(keyfile, element->path,
-                                               "WiFi.Passphrase", NULL);
-       if (value != NULL)
-               connman_element_set_property(element,
-                               CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value);
+       g_free(pathname);
 
-       return 0;
+       return keyfile;
 }
 
-int __connman_element_load(struct connman_element *element)
+int __connman_storage_save_global(GKeyFile *keyfile)
 {
-       GKeyFile *keyfile;
-       gchar *pathname, *data = NULL;
-       gsize length;
+       gchar *pathname;
+       int ret;
 
-       DBG("element %p name %s", element, element->name);
-
-       pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
-       if (pathname == NULL)
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if (!pathname)
                return -ENOMEM;
 
-       keyfile = g_key_file_new();
+       ret = storage_save(keyfile, pathname);
 
-       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
-               g_free(pathname);
-               return -ENOENT;
-       }
+       g_free(pathname);
+
+       return ret;
+}
+
+#if defined TIZEN_EXT
+GKeyFile *__connman_storage_load_ins(void)
+{
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
+
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, INS_SETTINGS);
+       if (!pathname)
+               return NULL;
+
+       keyfile = storage_load(pathname);
 
        g_free(pathname);
 
-       if (g_key_file_load_from_data(keyfile, data, length,
-                                                       0, NULL) == FALSE) {
-               g_free(data);
-               return -EILSEQ;
-       }
+       return keyfile;
+}
 
-       g_free(data);
+int __connman_storage_save_ins(GKeyFile *keyfile)
+{
+       gchar *pathname;
+       int ret;
+
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, INS_SETTINGS);
+       if (!pathname)
+               return -ENOMEM;
 
-       do_load(keyfile, element);
+       ret = storage_save(keyfile, pathname);
 
-       g_key_file_free(keyfile);
+       g_free(pathname);
 
-       return 0;
+       return ret;
 }
+#endif
 
-static void do_update(GKeyFile *keyfile, struct connman_element *element)
+void __connman_storage_delete_global(void)
 {
-       GSList *list;
-       char *value;
-       const char *str;
+       gchar *pathname;
 
-       DBG("element %p name %s", element, element->name);
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
+       if (!pathname)
+               return;
 
-       g_key_file_set_string(keyfile, element->path, "Name", element->name);
+       storage_delete(pathname);
 
-       str = __connman_element_policy2string(element->policy);
-       if (str != NULL)
-               g_key_file_set_string(keyfile, element->path, "Policy", str);
+       g_free(pathname);
+}
 
-       //g_key_file_set_boolean(keyfile, element->path, "Enabled",
-       //                                              element->enabled);
+GKeyFile *__connman_storage_load_config(const char *ident)
+{
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
-               g_key_file_set_boolean(keyfile, element->path, "Remember",
-                                                       element->remember);
+       pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
+       if (!pathname)
+               return NULL;
 
-       __connman_element_lock(element);
+       keyfile = storage_load(pathname);
 
-       for (list = element->properties; list; list = list->next) {
-               struct connman_property *property = list->data;
+       g_free(pathname);
 
-               if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
-                       continue;
+       return keyfile;
+}
 
-               if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
+GKeyFile *__connman_storage_load_provider_config(const char *ident)
+{
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
+
+       pathname = g_strdup_printf("%s/%s.config", VPN_STORAGEDIR, ident);
+       if (!pathname)
+               return NULL;
+
+       keyfile = storage_load(pathname);
+
+       g_free(pathname);
+
+       return keyfile;
+}
+
+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;
 
-               if (property->type == DBUS_TYPE_STRING)
-                       g_key_file_set_string(keyfile, element->path,
-                                       property->name, property->value);
+               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;
+               }
        }
 
-       __connman_element_unlock(element);
+       closedir(dir);
 
-       if (connman_element_get_value(element,
-                       CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
-               g_key_file_set_string(keyfile, element->path,
-                                               "WiFi.Security", value);
+       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);
 
-       if (connman_element_get_value(element,
-                       CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
-               g_key_file_set_string(keyfile, element->path,
-                                               "WiFi.Passphrase", value);
+       return services;
 }
 
-int __connman_element_store(struct connman_element *element)
+GKeyFile *connman_storage_load_service(const char *service_id)
 {
-       GKeyFile *keyfile;
-       gchar *pathname, *data = NULL;
-       gsize length;
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
 
-       DBG("element %p name %s", element, element->name);
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
+       if (!pathname)
+               return NULL;
 
-       if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
-                               element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
-               return -EINVAL;
+       keyfile =  storage_load(pathname);
+       g_free(pathname);
 
-       if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE)
-               return -EINVAL;
+       return keyfile;
+}
+
+int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
+{
+       int ret = 0;
+       gchar *pathname, *dirname;
 
-       pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
-       if (pathname == NULL)
+       dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
+       if (!dirname)
                return -ENOMEM;
 
-       keyfile = g_key_file_new();
+       /* 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;
+                       }
+               }
+       }
+
+       pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
+
+       g_free(dirname);
 
-       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
-               goto update;
+       ret = storage_save(keyfile, pathname);
+
+       g_free(pathname);
+
+       return ret;
+}
 
-       if (length > 0) {
-               if (g_key_file_load_from_data(keyfile, data, length,
-                                                       0, NULL) == FALSE)
-                       goto done;
+static bool remove_file(const char *service_id, const char *file)
+{
+       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;
        }
 
-       g_free(data);
+       g_free(pathname);
+       return ret;
+}
 
-update:
-       do_update(keyfile, element);
+static bool remove_dir(const char *service_id)
+{
+       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;
+       }
 
-       data = g_key_file_to_data(keyfile, &length, NULL);
+       g_free(pathname);
+       return ret;
+}
+
+bool __connman_storage_remove_service(const char *service_id)
+{
+       bool removed;
 
-       g_file_set_contents(pathname, data, length, NULL);
+       /* Remove service configuration file */
+       removed = remove_file(service_id, SETTINGS);
+       if (!removed)
+               return false;
 
-done:
-       g_free(data);
+       /* 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 true;
+}
+
+GKeyFile *__connman_storage_load_provider(const char *identifier)
+{
+       gchar *pathname;
+       GKeyFile *keyfile;
+
+       pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
+                       identifier, SETTINGS);
+       if (!pathname)
+               return NULL;
+
+       keyfile = storage_load(pathname);
+       g_free(pathname);
+
+       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;
 
-       g_key_file_free(keyfile);
+       if (!g_file_test(dirname, G_FILE_TEST_IS_DIR) &&
+                       mkdir(dirname, MODE) < 0) {
+               g_free(dirname);
+               return;
+       }
+
+       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;
+}
+
+bool __connman_storage_remove_provider(const char *identifier)
+{
+       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);
+
+       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;
+}
+
+gchar **__connman_storage_get_providers(void)
+{
+       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 0;
+       return providers;
 }