From d9e1b7d67cb278b5ad78128f15ba36f77b04756b Mon Sep 17 00:00:00 2001 From: Christophe Sadoine Date: Wed, 19 Jun 2013 15:47:20 +0900 Subject: [PATCH] evil: added evil_rename() a wrapper around rename(). Signed-off-by: Cedric Bail --- ChangeLog | 4 ++++ NEWS | 1 + src/lib/evil/evil_stdio.c | 29 ++++++++++++++++++++++++++++- src/lib/evil/evil_stdio.h | 20 +++++++++++++++++++- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9ff23e9402..9d02b31f83 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,10 @@ * Evas: optimized path for when map use the same color for all corner. +2013-06-19 Christophe Sadoine + + * Evil: Added evil_rename function, a wrapper around rename(). + 2013-06-18 Cedric Bail * Evas: Use Eo array of callbacks to reduce edje memory foot print of Evas_Object_Box and Evas_Object_Table. diff --git a/NEWS b/NEWS index cd6dda8868..be1b545a1a 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,7 @@ Additions: * Add --with-api=XXX (both|legacy|eo) * Evil: - Add mkdtemp. + - Add evil_rename() a wrapper for rename(). * eina: - Add DOCTYPE children parsing in eina_simple_xml - Add eina_barrier thread API diff --git a/src/lib/evil/evil_stdio.c b/src/lib/evil/evil_stdio.c index 231e5043d6..dd800041f8 100644 --- a/src/lib/evil/evil_stdio.c +++ b/src/lib/evil/evil_stdio.c @@ -6,7 +6,7 @@ #include "evil_private.h" #undef fopen - +#undef rename #ifdef _WIN32_WCE @@ -199,3 +199,30 @@ int evil_fclose_native(FILE *stream) } #endif /* _WIN32_WCE */ + +int +evil_rename(const char *src, const char* dst) +{ + struct stat st; + + if (stat(dst, &st) < 0) + return rename(src, dst); + + if (stat(src, &st) < 0) + return -1; + + if (S_ISDIR(st.st_mode)) + { + rmdir(dst); + return rename(src, dst); + } + + if (S_ISREG(st.st_mode)) + { + unlink(dst); + return rename(src, dst); + } + + return -1; +} + diff --git a/src/lib/evil/evil_stdio.h b/src/lib/evil/evil_stdio.h index 0baf00f5b8..27c116e9c5 100644 --- a/src/lib/evil/evil_stdio.h +++ b/src/lib/evil/evil_stdio.h @@ -204,8 +204,26 @@ EAPI int evil_fclose_native(FILE *stream); /** - * @} + * @brief Emulate the rename() function on Windows. + * + * @param src The old pathname. + * @param dst The new pathname. + * @return 0 on success, -1 otherwise. + * + * This function emulates the POSIX rename() function on Windows. + * The difference with the POSIX function is that the rename() function + * on windows fails if the destination exists. + * + * @since 1.8 */ +EAPI int evil_rename(const char *src, const char *dst); + +/** + * @def rename(src, dest) + * + * Wrapper around evil_rename(). + */ +# define rename(src, dst) evil_rename(src, dst) #endif /* __EVIL_STDIO_H__ */