[evolution] Avoid overwriting files while migrating.



commit 69c904178f72e5eba88159a65f642d9b903a233e
Author: Matthew Barnes <mbarnes redhat com>
Date:   Thu Jul 29 16:35:34 2010 -0400

    Avoid overwriting files while migrating.

 shell/e-shell-migrate.c |   22 ++++++++++++++++++++--
 1 files changed, 20 insertions(+), 2 deletions(-)
---
diff --git a/shell/e-shell-migrate.c b/shell/e-shell-migrate.c
index 4e938ef..ed6537c 100644
--- a/shell/e-shell-migrate.c
+++ b/shell/e-shell-migrate.c
@@ -47,14 +47,32 @@ static gboolean
 shell_xdg_migrate_rename (const gchar *old_filename,
                           const gchar *new_filename)
 {
+	gboolean old_filename_is_dir;
+	gboolean old_filename_exists;
+	gboolean new_filename_exists;
 	gboolean success = TRUE;
 
-	if (g_file_test (old_filename, G_FILE_TEST_EXISTS)) {
-		g_print ("  mv %s %s\n", old_filename, new_filename);
+	old_filename_is_dir = g_file_test (old_filename, G_FILE_TEST_IS_DIR);
+	old_filename_exists = g_file_test (old_filename, G_FILE_TEST_EXISTS);
+	new_filename_exists = g_file_test (new_filename, G_FILE_TEST_EXISTS);
+
+	if (!old_filename_exists)
+		return TRUE;
+
+	g_print ("  mv %s %s\n", old_filename, new_filename);
+
+	/* It's safe to go ahead and move directories because rename()
+	 * will fail if the new directory already exists with content.
+	 * With regular files we have to be careful not to overwrite
+	 * new files with old files. */
+	if (old_filename_is_dir || !new_filename_exists) {
 		if (g_rename (old_filename, new_filename) < 0) {
 			g_printerr ("  FAILED: %s\n", g_strerror (errno));
 			success = FALSE;
 		}
+	} else {
+		g_printerr ("  FAILED: Destination file already exists\n");
+		success = FALSE;
 	}
 
 	return success;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]