[evolution-data-server] Avoid overwriting files while migrating.



commit c2deb0698168256cd67984959105ea114f864ce1
Author: Matthew Barnes <mbarnes redhat com>
Date:   Thu Jul 29 16:34:31 2010 -0400

    Avoid overwriting files while migrating.

 addressbook/libedata-book/e-data-book-migrate.c |   22 ++++++++++++++++++++--
 calendar/libedata-cal/e-data-cal-migrate.c      |   22 ++++++++++++++++++++--
 2 files changed, 40 insertions(+), 4 deletions(-)
---
diff --git a/addressbook/libedata-book/e-data-book-migrate.c b/addressbook/libedata-book/e-data-book-migrate.c
index 5c295eb..9004f57 100644
--- a/addressbook/libedata-book/e-data-book-migrate.c
+++ b/addressbook/libedata-book/e-data-book-migrate.c
@@ -26,14 +26,32 @@ static gboolean
 data_book_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_IS_DIR)) {
-		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;
diff --git a/calendar/libedata-cal/e-data-cal-migrate.c b/calendar/libedata-cal/e-data-cal-migrate.c
index 4d0229d..7207c9e 100644
--- a/calendar/libedata-cal/e-data-cal-migrate.c
+++ b/calendar/libedata-cal/e-data-cal-migrate.c
@@ -26,14 +26,32 @@ static gboolean
 data_cal_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_IS_DIR)) {
-		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]