storage: Migrate services from obsolete default.profile
authorPatrik Flykt <patrik.flykt@linux.intel.com>
Fri, 18 May 2012 08:44:07 +0000 (11:44 +0300)
committerPatrik Flykt <patrik.flykt@linux.intel.com>
Thu, 24 May 2012 13:33:36 +0000 (16:33 +0300)
In order to remove the obsolete default.profile, migrate
services into the new setup with per-service directories.
If the service exists in the new format, it is not migrated.

src/storage.c

index a854073..4c00ce0 100644 (file)
@@ -387,6 +387,8 @@ void __connman_storage_migrate()
        GKeyFile *keyfile_def = NULL;
        GKeyFile *keyfile = NULL;
        GError *error = NULL;
+       char **services, **keys, *value;
+       int i, k, err;
        connman_bool_t val;
 
        /* If setting file exists, migration has been done. */
@@ -405,6 +407,56 @@ void __connman_storage_migrate()
        if (keyfile_def == NULL)
                goto done;
 
+       services = g_key_file_get_groups(keyfile_def, NULL);
+       for (i = 0; services != NULL && services[i] != NULL; i++) {
+               if (strncmp(services[i], "wifi_", 5) != 0 &&
+                               strncmp(services[i], "ethernet_", 9) != 0 &&
+                               strncmp(services[i], "cellular_", 9) != 0 &&
+                               strncmp(services[i], "bluetooth_", 10) != 0 &&
+                               strncmp(services[i], "wimax_", 6) != 0 &&
+                               strncmp(services[i], "vpn_", 4) != 0)
+                       continue;
+
+               keyfile = connman_storage_load_service(services[i]);
+               if (keyfile != NULL) {
+                       g_key_file_free(keyfile);
+                       DBG("already exists %s", services[i]);
+                       continue;
+               }
+
+               keyfile = g_key_file_new();
+               if (keyfile == NULL) {
+                       connman_warn("Migrating %s failed", services[i]);
+                       continue;
+               }
+
+               keys = g_key_file_get_keys(keyfile_def, services[i],
+                               NULL, NULL);
+
+               for (k = 0; keys != NULL && keys[k] != NULL; k++) {
+                       value = g_key_file_get_value(keyfile_def, services[i],
+                                       keys[k], NULL);
+                       g_key_file_set_value(keyfile, services[i],
+                                       keys[k], value);
+                       g_free(value);
+               }
+
+               if (keys != NULL && keys[0] != NULL) {
+                       err = __connman_storage_save_service(keyfile,
+                                       services[i]);
+                       if (err >= 0)
+                               DBG("migrated %s", services[i]);
+                       else
+                               connman_warn("Migrating %s failed %s",
+                                               services[i], strerror(-err));
+               } else
+                       DBG("no keys in %s", services[i]);
+
+               g_strfreev(keys);
+               g_key_file_free(keyfile);
+       }
+       g_strfreev(services);
+
        /* Copy global settings from default.profile to settings. */
        keyfile = g_key_file_new();