dnsproxy: Do not generate SIGPIPE
[framework/connectivity/connman.git] / src / storage.c
index 4c00ce0..47bd0cb 100644 (file)
@@ -147,30 +147,6 @@ GKeyFile *__connman_storage_load_config(const char *ident)
        return keyfile;
 }
 
-void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
-{
-       gchar *pathname;
-
-       pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
-       if(pathname == NULL)
-               return;
-
-       storage_save(keyfile, pathname);
-}
-
-void __connman_storage_delete_config(const char *ident)
-{
-       gchar *pathname;
-
-       pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
-       if(pathname == NULL)
-               return;
-
-       storage_delete(pathname);
-
-       g_free(pathname);
-}
-
 GKeyFile *__connman_storage_open_service(const char *service_id)
 {
        gchar *pathname;
@@ -290,6 +266,69 @@ int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
        return ret;
 }
 
+static gboolean remove_file(const char *service_id, const char *file)
+{
+       gchar *pathname;
+       gboolean ret = FALSE;
+
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
+       if(pathname == NULL)
+               return FALSE;
+
+       if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
+               ret = TRUE;
+       } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR) == TRUE) {
+               unlink(pathname);
+               ret = TRUE;
+       }
+
+       g_free(pathname);
+       return ret;
+}
+
+static gboolean remove_dir(const char *service_id)
+{
+       gchar *pathname;
+       gboolean ret = FALSE;
+
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
+       if(pathname == NULL)
+               return FALSE;
+
+       if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
+               ret = TRUE;
+       } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR) == TRUE) {
+               rmdir(pathname);
+               ret = TRUE;
+       }
+
+       g_free(pathname);
+       return ret;
+}
+
+gboolean __connman_storage_remove_service(const char *service_id)
+{
+       gboolean removed;
+
+       /* Remove service configuration file */
+       removed = remove_file(service_id, SETTINGS);
+       if (removed == FALSE)
+               return FALSE;
+
+       /* Remove the statistics file also */
+       removed = remove_file(service_id, "data");
+       if (removed == FALSE)
+               return FALSE;
+
+       removed = remove_dir(service_id);
+       if (removed == FALSE)
+               return FALSE;
+
+       DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
+
+       return TRUE;
+}
+
 GKeyFile *__connman_storage_load_provider(const char *identifier)
 {
        gchar *pathname;
@@ -387,21 +426,24 @@ void __connman_storage_migrate()
        GKeyFile *keyfile_def = NULL;
        GKeyFile *keyfile = NULL;
        GError *error = NULL;
+       connman_bool_t delete_old_config = TRUE;
        char **services, **keys, *value;
        int i, k, err;
        connman_bool_t val;
 
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
+       if (pathname == NULL)
+               return;
+
        /* If setting file exists, migration has been done. */
        keyfile = __connman_storage_load_global();
        if (keyfile) {
                g_key_file_free(keyfile);
+               unlink(pathname);
+               g_free(pathname);
                return;
        }
 
-       pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
-       if(pathname == NULL)
-               return;
-
        /* If default.profile exists, create new settings file */
        keyfile_def = storage_load(pathname);
        if (keyfile_def == NULL)
@@ -427,6 +469,7 @@ void __connman_storage_migrate()
                keyfile = g_key_file_new();
                if (keyfile == NULL) {
                        connman_warn("Migrating %s failed", services[i]);
+                       delete_old_config = FALSE;
                        continue;
                }
 
@@ -446,9 +489,11 @@ void __connman_storage_migrate()
                                        services[i]);
                        if (err >= 0)
                                DBG("migrated %s", services[i]);
-                       else
+                       else {
                                connman_warn("Migrating %s failed %s",
                                                services[i], strerror(-err));
+                               delete_old_config = FALSE;
+                       }
                } else
                        DBG("no keys in %s", services[i]);
 
@@ -470,11 +515,19 @@ void __connman_storage_migrate()
        g_key_file_set_boolean(keyfile, "global",
                                        "OfflineMode", val);
 
+       /* Migrate Powered/Enable state key/value pairs from legacy
+        * settings
+        */
+
        val = g_key_file_get_boolean(keyfile_def, "WiFi",
                                        "Enable", &error);
        if (error != NULL) {
                g_clear_error(&error);
-               val = FALSE;
+               val = g_key_file_get_boolean(keyfile_def, "device_Wireless", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
+               }
        }
 
        g_key_file_set_boolean(keyfile, "WiFi",
@@ -484,7 +537,11 @@ void __connman_storage_migrate()
                                        "Enable", &error);
        if (error != NULL) {
                g_clear_error(&error);
-               val = FALSE;
+               val = g_key_file_get_boolean(keyfile_def, "device_Bluetooth", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
+               }
        }
 
        g_key_file_set_boolean(keyfile, "Bluetooth",
@@ -494,7 +551,11 @@ void __connman_storage_migrate()
                                        "Enable", &error);
        if (error != NULL) {
                g_clear_error(&error);
-               val = FALSE;
+               val = g_key_file_get_boolean(keyfile_def, "device_Ethernet", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
+               }
        }
 
        g_key_file_set_boolean(keyfile, "Wired",
@@ -504,7 +565,11 @@ void __connman_storage_migrate()
                                        "Enable", &error);
        if (error != NULL) {
                g_clear_error(&error);
-               val = FALSE;
+               val = g_key_file_get_boolean(keyfile_def, "device_Cellular", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
+               }
        }
 
        g_key_file_set_boolean(keyfile, "Cellular",
@@ -514,18 +579,29 @@ void __connman_storage_migrate()
                                        "Enable", &error);
        if (error != NULL) {
                g_clear_error(&error);
-               val = FALSE;
+               val = g_key_file_get_boolean(keyfile_def, "device_WiMAX", "Powered", &error);
+               if (error != NULL) {
+                       g_clear_error(&error);
+                       val = FALSE;
+               }
        }
 
        g_key_file_set_boolean(keyfile, "WiMAX",
                                        "Enable", val);
 
-       __connman_storage_save_global(keyfile);
+       if (__connman_storage_save_global(keyfile) < 0) {
+               connman_warn("Migrating global config failed");
+               delete_old_config = FALSE;
+       }
 
        g_key_file_free(keyfile);
 
        g_key_file_free(keyfile_def);
 
+       if (delete_old_config == TRUE) {
+               DBG("migration done for %s", pathname);
+               unlink(pathname);
+       }
 done:
        g_free(pathname);
 }