storage: Return any errors when saving services and global config
[framework/connectivity/connman.git] / src / storage.c
index 1755650..e35a893 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
@@ -58,20 +58,24 @@ static GKeyFile *storage_load(const char *pathname)
        return keyfile;
 }
 
-static void storage_save(GKeyFile *keyfile, char *pathname)
+static int storage_save(GKeyFile *keyfile, char *pathname)
 {
        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_free(error);
+               g_error_free(error);
+               ret = -EIO;
        }
 
        g_free(data);
+
+       return ret;
 }
 
 static void storage_delete(const char *pathname)
@@ -98,17 +102,20 @@ GKeyFile *__connman_storage_load_global()
        return keyfile;
 }
 
-void __connman_storage_save_global(GKeyFile *keyfile)
+int __connman_storage_save_global(GKeyFile *keyfile)
 {
        gchar *pathname;
+       int ret;
 
        pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
        if(pathname == NULL)
-               return;
+               return -ENOMEM;
 
-       storage_save(keyfile, pathname);
+       ret = storage_save(keyfile, pathname);
 
        g_free(pathname);
+
+       return ret;
 }
 
 void __connman_storage_delete_global()
@@ -204,7 +211,8 @@ gchar **connman_storage_get_services()
 
        while ((d = readdir(dir))) {
                if (strcmp(d->d_name, ".") == 0 ||
-                               strcmp(d->d_name, "..") == 0)
+                               strcmp(d->d_name, "..") == 0 ||
+                               strncmp(d->d_name, "provider_", 9) == 0)
                        continue;
 
                switch (d->d_type) {
@@ -237,7 +245,7 @@ gchar **connman_storage_get_services()
        return services;
 }
 
-GKeyFile *__connman_storage_load_service(const char *service_id)
+GKeyFile *connman_storage_load_service(const char *service_id)
 {
        gchar *pathname;
        GKeyFile *keyfile = NULL;
@@ -247,12 +255,9 @@ GKeyFile *__connman_storage_load_service(const char *service_id)
                return NULL;
 
        keyfile =  storage_load(pathname);
-       if (keyfile) {
-               g_free(pathname);
-               return keyfile;
-       }
-
        g_free(pathname);
+       if (keyfile)
+               return keyfile;
 
        pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
        if(pathname == NULL)
@@ -265,20 +270,21 @@ GKeyFile *__connman_storage_load_service(const char *service_id)
        return keyfile;
 }
 
-void __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
+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;
+               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;
+                               return -errno;
                        }
                }
        }
@@ -287,9 +293,98 @@ void __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
 
        g_free(dirname);
 
-       storage_save(keyfile, pathname);
+       ret = storage_save(keyfile, pathname);
 
        g_free(pathname);
+
+       return ret;
+}
+
+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 == NULL)
+               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 == NULL)
+               return;
+
+       if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
+                       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);
+}
+
+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 == NULL)
+               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 != 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 providers;
 }
 
 /*
@@ -315,73 +410,80 @@ void __connman_storage_migrate()
        if(pathname == NULL)
                return;
 
-       /* If default.profile doesn't exists, no need to migrate. */
+       /* If default.profile exists, create new settings file */
        keyfile_def = storage_load(pathname);
-       if (keyfile_def == NULL) {
-               g_free(pathname);
-               return;
-       }
+       if (keyfile_def == NULL)
+               goto done;
 
        /* Copy global settings from default.profile to settings. */
        keyfile = g_key_file_new();
 
-       /* offline mode */
        val = g_key_file_get_boolean(keyfile_def, "global",
                                        "OfflineMode", &error);
-       if (error != NULL)
+       if (error != NULL) {
                g_clear_error(&error);
-       else
-               g_key_file_set_boolean(keyfile, "global",
+               val = FALSE;
+       }
+
+       g_key_file_set_boolean(keyfile, "global",
                                        "OfflineMode", val);
 
-       /* wifi */
        val = g_key_file_get_boolean(keyfile_def, "WiFi",
                                        "Enable", &error);
-       if (error != NULL)
+       if (error != NULL) {
                g_clear_error(&error);
-       else
-               g_key_file_set_boolean(keyfile, "WiFi",
+               val = FALSE;
+       }
+
+       g_key_file_set_boolean(keyfile, "WiFi",
                                        "Enable", val);
 
-       /* bluetooth */
        val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
                                        "Enable", &error);
-       if (error != NULL)
+       if (error != NULL) {
                g_clear_error(&error);
-       else
-               g_key_file_set_boolean(keyfile, "Bluetooth",
+               val = FALSE;
+       }
+
+       g_key_file_set_boolean(keyfile, "Bluetooth",
                                        "Enable", val);
 
-       /* wired */
        val = g_key_file_get_boolean(keyfile_def, "Wired",
                                        "Enable", &error);
-       if (error != NULL)
+       if (error != NULL) {
                g_clear_error(&error);
-       else
-               g_key_file_set_boolean(keyfile, "Wired",
+               val = FALSE;
+       }
+
+       g_key_file_set_boolean(keyfile, "Wired",
                                        "Enable", val);
 
-       /* 3G */
-       val = g_key_file_get_boolean(keyfile_def, "3G",
+       val = g_key_file_get_boolean(keyfile_def, "Cellular",
                                        "Enable", &error);
-       if (error != NULL)
+       if (error != NULL) {
                g_clear_error(&error);
-       else
-               g_key_file_set_boolean(keyfile, "3G",
+               val = FALSE;
+       }
+
+       g_key_file_set_boolean(keyfile, "Cellular",
                                        "Enable", val);
 
-       /* WiMAX */
        val = g_key_file_get_boolean(keyfile_def, "WiMAX",
                                        "Enable", &error);
-       if (error != NULL)
+       if (error != NULL) {
                g_clear_error(&error);
-       else
-               g_key_file_set_boolean(keyfile, "WiMAX",
+               val = FALSE;
+       }
+
+       g_key_file_set_boolean(keyfile, "WiMAX",
                                        "Enable", val);
 
        __connman_storage_save_global(keyfile);
 
        g_key_file_free(keyfile);
+
        g_key_file_free(keyfile_def);
+
+done:
        g_free(pathname);
 }