From: Dmitry Stogov Date: Thu, 24 Apr 2008 07:46:10 +0000 (+0000) Subject: Fixed bug #44805 (rename() function is not portable to Windows). (Pierre) X-Git-Tag: RELEASE_2_0_0b1~230 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=937b1b7ab6a7451e846e961b4d71119dc933f275;p=php Fixed bug #44805 (rename() function is not portable to Windows). (Pierre) --- diff --git a/TSRM/tsrm_virtual_cwd.c b/TSRM/tsrm_virtual_cwd.c index 1de5723a71..496fb0f6d4 100644 --- a/TSRM/tsrm_virtual_cwd.c +++ b/TSRM/tsrm_virtual_cwd.c @@ -1042,8 +1042,14 @@ CWD_API int virtual_rename(char *oldname, char *newname TSRMLS_DC) /* {{{ */ return -1; } newname = new_state.cwd; - + + /* rename on windows will fail if newname already exists. + MoveFileEx has to be used */ +#ifdef TSRM_WIN32 + retval = (MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING) == 0) ? -1 : 0; +#else retval = rename(oldname, newname); +#endif CWD_STATE_FREE(&old_state); CWD_STATE_FREE(&new_state); diff --git a/TSRM/tsrm_virtual_cwd.h b/TSRM/tsrm_virtual_cwd.h index 7fb25658ba..b9ecdf537a 100644 --- a/TSRM/tsrm_virtual_cwd.h +++ b/TSRM/tsrm_virtual_cwd.h @@ -268,7 +268,7 @@ CWD_API void realpath_cache_del(const char *path, int path_len TSRMLS_DC); #define VCWD_RENAME(oldname, newname) virtual_rename(oldname, newname TSRMLS_CC) #define VCWD_STAT(path, buff) virtual_stat(path, buff TSRMLS_CC) #if !defined(TSRM_WIN32) -#define VCWD_LSTAT(path, buff) virtual_lstat(path, buff TSRMLS_CC) +# define VCWD_LSTAT(path, buff) virtual_lstat(path, buff TSRMLS_CC) #endif #define VCWD_UNLINK(path) virtual_unlink(path TSRMLS_CC) #define VCWD_MKDIR(pathname, mode) virtual_mkdir(pathname, mode TSRMLS_CC) @@ -294,7 +294,13 @@ CWD_API void realpath_cache_del(const char *path, int path_len TSRMLS_DC); #define VCWD_OPEN(path, flags) open(path, flags) #define VCWD_OPEN_MODE(path, flags, mode) open(path, flags, mode) #define VCWD_CREAT(path, mode) creat(path, mode) -#define VCWD_RENAME(oldname, newname) rename(oldname, newname) +/* rename on windows will fail if newname already exists. + MoveFileEx has to be used */ +#if defined(TSRM_WIN32) +# define VCWD_RENAME(oldname, newname) MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING) +#else +# define VCWD_RENAME(oldname, newname) rename(oldname, newname) +#endif #define VCWD_CHDIR(path) chdir(path) #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, chdir) #define VCWD_GETWD(buf) getwd(buf) diff --git a/ext/standard/tests/file/bug44805.phpt b/ext/standard/tests/file/bug44805.phpt new file mode 100644 index 0000000000..7054b76cfc --- /dev/null +++ b/ext/standard/tests/file/bug44805.phpt @@ -0,0 +1,15 @@ +--TEST-- +Bug#44806 (rename() function is not portable to Windows) +--FILE-- + +--EXPECT-- +reading file 2: this is file 1