storage: Load and save functions for providers
[platform/upstream/connman.git] / src / storage.c
index 4e30a77..1d14e13 100644 (file)
@@ -26,6 +26,9 @@
 #include <errno.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <dirent.h>
+
+#include <connman/storage.h>
 
 #include "connman.h"
 
@@ -183,7 +186,59 @@ GKeyFile *__connman_storage_open_service(const char *service_id)
        return keyfile;
 }
 
-GKeyFile *__connman_storage_load_service(const char *service_id)
+gchar **connman_storage_get_services()
+{
+       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;
+
+       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;
+               }
+       }
+
+       closedir(dir);
+
+       str = g_string_free(result, FALSE);
+       if (str) {
+               str[strlen(str) - 1] = '\0';
+               services = g_strsplit(str, "/", -1);
+       }
+       g_free(str);
+
+       return services;
+}
+
+GKeyFile *connman_storage_load_service(const char *service_id)
 {
        gchar *pathname;
        GKeyFile *keyfile = NULL;
@@ -193,12 +248,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)
@@ -238,6 +290,44 @@ void __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
        g_free(pathname);
 }
 
+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);
+}
+
 /*
  * This function migrates keys from default.profile to settings file.
  * This can be removed once the migration is over.