[PATCH 3/3] storage: Provider save file migration

patrik.flykt at linux.intel.com patrik.flykt at linux.intel.com
Fri Feb 24 06:16:35 PST 2012


From: Patrik Flykt <patrik.flykt at linux.intel.com>

Migrate provider save files from STORAGEDIR to STORAGEDIR/provider.
The migration is considered done if the top-level provider
directory exists.
---
 src/storage.c |   42 ++++++++++++++++++++++++++++++++++++++----
 1 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/src/storage.c b/src/storage.c
index 978c51c..16a0048 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -27,11 +27,14 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <dirent.h>
+#include <stdio.h>
 
 #include <connman/storage.h>
 
 #include "connman.h"
 
+#define PROVIDER_STORAGEDIR STORAGEDIR "/provider"
+
 #define SETTINGS	"settings"
 #define DEFAULT		"default.profile"
 
@@ -295,8 +298,8 @@ GKeyFile *__connman_storage_load_provider(const char *identifier)
 	gchar *pathname;
 	GKeyFile *keyfile;
 
-	pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
-			identifier, SETTINGS);
+	pathname = g_strdup_printf("%s/%s_%s/%s", PROVIDER_STORAGEDIR,
+			"provider", identifier, SETTINGS);
 	if (pathname == NULL)
 		return NULL;
 
@@ -310,7 +313,7 @@ void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
 {
 	gchar *pathname, *dirname;
 
-	dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
+	dirname = g_strdup_printf("%s/%s_%s", PROVIDER_STORAGEDIR,
 			"provider", identifier);
 	if (dirname == NULL)
 		return;
@@ -379,7 +382,37 @@ static gchar **get_providers_from_dir(const char *dirname)
 
 gchar **__connman_storage_get_providers(void)
 {
-	return get_providers_from_dir(STORAGEDIR);
+	return get_providers_from_dir(PROVIDER_STORAGEDIR);
+}
+
+static void migrate_providers(void)
+{
+	char **providers;
+	int i;
+	char *old_path, *new_path;
+
+	if (g_file_test(PROVIDER_STORAGEDIR, G_FILE_TEST_IS_DIR) == TRUE)
+		return;
+
+	if (mkdir(PROVIDER_STORAGEDIR, MODE) < 0) {
+		DBG("cannot create %s err %d", PROVIDER_STORAGEDIR, -errno);
+		return;
+	}
+
+	providers = get_providers_from_dir(STORAGEDIR);
+	for (i = 0; providers[i] != NULL; i += 1) {
+		old_path = g_strdup_printf("%s/%s", STORAGEDIR, providers[i]);
+		new_path = g_strdup_printf("%s/%s", PROVIDER_STORAGEDIR,
+				providers[i]);
+		if (rename(old_path, new_path) < 0) {
+			DBG("could not migrate %s to %s err %d",
+					old_path, new_path, -errno);
+		} else
+			DBG("migrated %s -> %s", old_path, new_path);
+		g_free(old_path);
+		g_free(new_path);
+	}
+	g_strfreev(providers);
 }
 
 /*
@@ -505,5 +538,6 @@ done:
 
 void __connman_storage_migrate(void)
 {
+	migrate_providers();
 	migrate_services();
 }
-- 
1.7.9




More information about the connman mailing list