diff options
| author | Matthew Barnes <mbarnes@redhat.com> | 2010-07-29 16:34:31 -0400 |
|---|---|---|
| committer | Matthew Barnes <mbarnes@redhat.com> | 2010-07-29 16:34:31 -0400 |
| commit | c2deb0698168256cd67984959105ea114f864ce1 (patch) | |
| tree | f03b6c3ac8ca85836a07c3fd3c16f720ab6a7bc1 | |
| parent | a0a35b08099e20bf054f8486410929e31ebe8280 (diff) | |
| download | evolution-data-server-c2deb0698168256cd67984959105ea114f864ce1.tar.gz | |
Avoid overwriting files while migrating.
| -rw-r--r-- | addressbook/libedata-book/e-data-book-migrate.c | 22 | ||||
| -rw-r--r-- | 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 5c295ebd9..9004f57fb 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 4d0229d09..7207c9eb5 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; |
