storage: Switch to <service_id> directories
authorAlok Barsode <alok.barsode@linux.intel.com>
Thu, 25 Aug 2011 13:52:17 +0000 (16:52 +0300)
committerSamuel Ortiz <sameo@linux.intel.com>
Mon, 12 Sep 2011 09:40:47 +0000 (11:40 +0200)
Service settings would now reside in /var/lib/connman/service_id/settings.
we fallback on /var/lib/connman/default.profile for a smooth transition.

src/connman.h
src/service.c
src/storage.c

index d8c64b4..4a37b70 100644 (file)
@@ -152,6 +152,10 @@ GKeyFile *__connman_storage_load_config(const char *ident);
 void __connman_storage_save_config(GKeyFile *keyfile, const char *ident);
 void __connman_storage_delete_config(const char *ident);
 
+GKeyFile *__connman_storage_open_service(const char *ident);
+GKeyFile *__connman_storage_load_service(const char *ident);
+void __connman_storage_save_service(GKeyFile *keyfile, const char *ident);
+
 int __connman_detect_init(void);
 void __connman_detect_cleanup(void);
 
index eda51fc..7a62f3d 100644 (file)
@@ -299,10 +299,8 @@ static enum connman_service_proxy_method string2proxymethod(const char *method)
 
 static int service_load(struct connman_service *service)
 {
-       const char *ident = "default";
        GKeyFile *keyfile;
        GError *error = NULL;
-       gchar *pathname, *data = NULL;
        gsize length;
        gchar *str;
        connman_bool_t autoconnect;
@@ -311,29 +309,9 @@ static int service_load(struct connman_service *service)
 
        DBG("service %p", service);
 
-       if (ident == NULL)
-               return -EINVAL;
-
-       pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
-       if (pathname == NULL)
-               return -ENOMEM;
-
-       keyfile = g_key_file_new();
-
-       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
-               g_free(pathname);
-               return -ENOENT;
-       }
-
-       g_free(pathname);
-
-       if (g_key_file_load_from_data(keyfile, data, length,
-                                                       0, NULL) == FALSE) {
-               g_free(data);
-               return -EILSEQ;
-       }
-
-       g_free(data);
+       keyfile = __connman_storage_load_service(service->identifier);
+       if (keyfile == NULL)
+               return -EIO;
 
        switch (service->type) {
        case CONNMAN_SERVICE_TYPE_UNKNOWN:
@@ -488,37 +466,17 @@ done:
 
 static int service_save(struct connman_service *service)
 {
-       const char *ident = "default";
        GKeyFile *keyfile;
-       gchar *pathname, *data = NULL;
-       gsize length;
        gchar *str;
        const char *cst_str = NULL;
        int err = 0;
 
        DBG("service %p", service);
 
-       if (ident == NULL)
-               return -EINVAL;
-
-       pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
-       if (pathname == NULL)
-               return -ENOMEM;
-
-       keyfile = g_key_file_new();
-
-       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
-               goto update;
-
-       if (length > 0) {
-               if (g_key_file_load_from_data(keyfile, data, length,
-                                                       0, NULL) == FALSE)
-                       goto done;
-       }
-
-       g_free(data);
+       keyfile = __connman_storage_open_service(service->identifier);
+       if (keyfile == NULL)
+               return -EIO;
 
-update:
        if (service->name != NULL)
                g_key_file_set_string(keyfile, service->identifier,
                                                "Name", service->name);
@@ -660,18 +618,11 @@ update:
                g_key_file_remove_key(keyfile, service->identifier,
                                                        "Proxy.URL", NULL);
 
-       data = g_key_file_to_data(keyfile, &length, NULL);
-
-       if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
-               connman_error("Failed to store service information");
-
 done:
-       g_free(data);
+       __connman_storage_save_service(keyfile, service->identifier);
 
        g_key_file_free(keyfile);
 
-       g_free(pathname);
-
        return err;
 }
 
@@ -3453,7 +3404,6 @@ static void service_free(gpointer user_data)
        __connman_notifier_service_remove(service);
 
        stats_stop(service);
-       service_save(service);
 
        service->path = NULL;
 
@@ -5153,7 +5103,7 @@ void __connman_service_read_ip6config(struct connman_service *service)
        if (service->ipconfig_ipv6 == NULL)
                return;
 
-       keyfile = __connman_storage_load_global();
+       keyfile = __connman_storage_load_service(service->identifier);
        if (keyfile == NULL)
                return;
 
index faf2652..4e30a77 100644 (file)
 
 #include <errno.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #include "connman.h"
 
 #define SETTINGS       "settings"
 #define DEFAULT                "default.profile"
 
+#define MODE           (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
+                       S_IXGRP | S_IROTH | S_IXOTH)
+
 static GKeyFile *storage_load(const char *pathname)
 {
        GKeyFile *keyfile = NULL;
@@ -157,6 +161,83 @@ void __connman_storage_delete_config(const char *ident)
        g_free(pathname);
 }
 
+GKeyFile *__connman_storage_open_service(const char *service_id)
+{
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
+
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
+       if(pathname == NULL)
+               return NULL;
+
+       keyfile =  storage_load(pathname);
+       if (keyfile) {
+               g_free(pathname);
+               return keyfile;
+       }
+
+       g_free(pathname);
+
+       keyfile = g_key_file_new();
+
+       return keyfile;
+}
+
+GKeyFile *__connman_storage_load_service(const char *service_id)
+{
+       gchar *pathname;
+       GKeyFile *keyfile = NULL;
+
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
+       if(pathname == NULL)
+               return NULL;
+
+       keyfile =  storage_load(pathname);
+       if (keyfile) {
+               g_free(pathname);
+               return keyfile;
+       }
+
+       g_free(pathname);
+
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
+       if(pathname == NULL)
+               return NULL;
+
+       keyfile =  storage_load(pathname);
+
+       g_free(pathname);
+
+       return keyfile;
+}
+
+void __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
+{
+       gchar *pathname, *dirname;
+
+       dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
+       if(dirname == NULL)
+               return;
+
+       /* 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;
+                       }
+               }
+       }
+
+       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.