cvs: TSRM / tsrm_virtual_cwd.c tsrm_virtual_cwd.h php-src/ext/standard/tests/file bug44805.phpt

[email protected] ("Dmitry Stogov")
Newsgroups php.zend-engine.cvs
Message-ID <cvsdmitry1209023170@cvsserver>
dmitry		Thu Apr 24 07:46:10 2008 UTC

  Modified files:              
    /php-src/ext/standard/tests/file	bug44805.phpt 
    /TSRM	tsrm_virtual_cwd.c tsrm_virtual_cwd.h 
  Log:
  Fixed bug #44805 (rename() function is not portable to Windows). (Pierre)
  
  
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/file/bug44805.phpt?r1=1.1&r2=1.2&diff_format=u
Index: php-src/ext/standard/tests/file/bug44805.phpt
diff -u /dev/null php-src/ext/standard/tests/file/bug44805.phpt:1.2
--- /dev/null	Thu Apr 24 07:46:10 2008
+++ php-src/ext/standard/tests/file/bug44805.phpt	Thu Apr 24 07:46:10 2008
@@ -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
http://cvs.php.net/viewvc.cgi/TSRM/tsrm_virtual_cwd.c?r1=1.120&r2=1.121&diff_format=u
Index: TSRM/tsrm_virtual_cwd.c
diff -u TSRM/tsrm_virtual_cwd.c:1.120 TSRM/tsrm_virtual_cwd.c:1.121
--- TSRM/tsrm_virtual_cwd.c:1.120	Fri Mar 21 12:07:14 2008
+++ TSRM/tsrm_virtual_cwd.c	Thu Apr 24 07:46:10 2008
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: tsrm_virtual_cwd.c,v 1.120 2008/03/21 12:07:14 tony2001 Exp $ */
+/* $Id: tsrm_virtual_cwd.c,v 1.121 2008/04/24 07:46:10 dmitry Exp $ */
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -1042,8 +1042,14 @@
 		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);
http://cvs.php.net/viewvc.cgi/TSRM/tsrm_virtual_cwd.h?r1=1.65&r2=1.66&diff_format=u
Index: TSRM/tsrm_virtual_cwd.h
diff -u TSRM/tsrm_virtual_cwd.h:1.65 TSRM/tsrm_virtual_cwd.h:1.66
--- TSRM/tsrm_virtual_cwd.h:1.65	Wed Jan 30 09:56:21 2008
+++ TSRM/tsrm_virtual_cwd.h	Thu Apr 24 07:46:10 2008
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: tsrm_virtual_cwd.h,v 1.65 2008/01/30 09:56:21 dmitry Exp $ */
+/* $Id: tsrm_virtual_cwd.h,v 1.66 2008/04/24 07:46:10 dmitry Exp $ */
 
 #ifndef VIRTUAL_CWD_H
 #define VIRTUAL_CWD_H
@@ -268,7 +268,7 @@
 #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 @@
 #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)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.