]> granicus.if.org Git - php/commitdiff
Fixed bug #44805 (rename() function is not portable to Windows). (Pierre)
authorDmitry Stogov <dmitry@php.net>
Thu, 24 Apr 2008 07:46:10 +0000 (07:46 +0000)
committerDmitry Stogov <dmitry@php.net>
Thu, 24 Apr 2008 07:46:10 +0000 (07:46 +0000)
TSRM/tsrm_virtual_cwd.c
TSRM/tsrm_virtual_cwd.h
ext/standard/tests/file/bug44805.phpt [new file with mode: 0644]

index 1de5723a717aec44e51179757b85e47463da4949..496fb0f6d44ca8b53be318035d947e9b0e22f84f 100644 (file)
@@ -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);
index 7fb25658ba6d08bf8047235bf716b7514e37696b..b9ecdf537aecb5d811e2e23c615fc14b3206fabd 100644 (file)
@@ -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 (file)
index 0000000..7054b76
--- /dev/null
@@ -0,0 +1,15 @@
+--TEST--
+Bug#44806 (rename() function is not portable to Windows)
+--FILE--
+<?php
+
+file_put_contents("file1.txt", "this is file 1");
+file_put_contents("file2.txt", "this is file 2");
+
+rename("file1.txt", "file2.txt");
+
+echo "reading file 2: ";
+readfile("file2.txt");
+?>
+--EXPECT--
+reading file 2: this is file 1