From e2a9edf210f40519169a1169801df56fcb4ef0fc Mon Sep 17 00:00:00 2001 From: hiro Date: Fri, 5 Aug 2005 09:15:29 +0000 Subject: added rename_force(). git-svn-id: svn://sylpheed.sraoss.jp/sylpheed/trunk@470 ee746299-78ed-0310-b773-934348b2243d --- src/main.c | 7 ++++--- src/prefs.c | 4 ++-- src/utils.c | 25 ++++++++++++++++++++----- src/utils.h | 2 ++ 4 files changed, 28 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 734c3548..d40e4a21 100644 --- a/src/main.c +++ b/src/main.c @@ -212,7 +212,7 @@ int main(int argc, char *argv[]) /* backup if old rc file exists */ if (is_file_exist(RC_DIR)) { - if (rename(RC_DIR, RC_DIR ".bak") < 0) + if (rename_force(RC_DIR, RC_DIR ".bak") < 0) FILE_OP_ERROR(RC_DIR, "rename"); } @@ -258,8 +258,9 @@ int main(int argc, char *argv[]) remove_all_files(get_mime_tmp_dir()); if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) { - if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log", - RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0) + if (rename_force + (RC_DIR G_DIR_SEPARATOR_S "sylpheed.log", + RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0) FILE_OP_ERROR("sylpheed.log", "rename"); } set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log"); diff --git a/src/prefs.c b/src/prefs.c index 6dfc275f..7fb6f666 100644 --- a/src/prefs.c +++ b/src/prefs.c @@ -328,7 +328,7 @@ gint prefs_file_close(PrefFile *pfile) if (is_file_exist(path)) { bakpath = g_strconcat(path, ".bak", NULL); - if (rename(path, bakpath) < 0) { + if (rename_force(path, bakpath) < 0) { FILE_OP_ERROR(path, "rename"); unlink(tmppath); g_free(path); @@ -338,7 +338,7 @@ gint prefs_file_close(PrefFile *pfile) } } - if (rename(tmppath, path) < 0) { + if (rename_force(tmppath, path) < 0) { FILE_OP_ERROR(tmppath, "rename"); unlink(tmppath); g_free(path); diff --git a/src/utils.c b/src/utils.c index 26a43134..518b0737 100644 --- a/src/utils.c +++ b/src/utils.c @@ -2221,6 +2221,21 @@ leave: return ret; } +gint rename_force(const gchar *oldpath, const gchar *newpath) +{ +#ifndef G_OS_UNIX + if (!is_file_entry_exist(oldpath)) { + errno = ENOENT; + return -1; + } + if (is_file_exist(newpath)) { + if (unlink(newpath) < 0) + FILE_OP_ERROR(newpath, "unlink"); + } +#endif + return rename(oldpath, newpath); +} + gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup) { FILE *src_fp, *dest_fp; @@ -2235,7 +2250,7 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup) } if (is_file_exist(dest)) { dest_bak = g_strconcat(dest, ".bak", NULL); - if (rename(dest, dest_bak) < 0) { + if (rename_force(dest, dest_bak) < 0) { FILE_OP_ERROR(dest, "rename"); fclose(src_fp); g_free(dest_bak); @@ -2247,7 +2262,7 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup) FILE_OP_ERROR(dest, "fopen"); fclose(src_fp); if (dest_bak) { - if (rename(dest_bak, dest) < 0) + if (rename_force(dest_bak, dest) < 0) FILE_OP_ERROR(dest_bak, "rename"); g_free(dest_bak); } @@ -2268,7 +2283,7 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup) fclose(src_fp); unlink(dest); if (dest_bak) { - if (rename(dest_bak, dest) < 0) + if (rename_force(dest_bak, dest) < 0) FILE_OP_ERROR(dest_bak, "rename"); g_free(dest_bak); } @@ -2289,7 +2304,7 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup) if (err) { unlink(dest); if (dest_bak) { - if (rename(dest_bak, dest) < 0) + if (rename_force(dest_bak, dest) < 0) FILE_OP_ERROR(dest_bak, "rename"); g_free(dest_bak); } @@ -2346,7 +2361,7 @@ gint move_file(const gchar *src, const gchar *dest, gboolean overwrite) return -1; } - if (rename(src, dest) == 0) return 0; + if (rename_force(src, dest) == 0) return 0; if (EXDEV != errno) { FILE_OP_ERROR(src, "rename"); diff --git a/src/utils.h b/src/utils.h index bbd26fa2..85f1ec2f 100644 --- a/src/utils.h +++ b/src/utils.h @@ -376,6 +376,8 @@ gint remove_all_numbered_files (const gchar *dir); gint remove_expired_files (const gchar *dir, guint hours); gint remove_dir_recursive (const gchar *dir); +gint rename_force (const gchar *oldpath, + const gchar *newpath); gint copy_file (const gchar *src, const gchar *dest, gboolean keep_backup); -- cgit v1.2.3