[php-src] Issue #7910: rename fails on Windows PHP 8.1 if the target file is being executed
| From: | johnstevenson | Date: | Fri, 07 Jan 2022 21:57:16 +0000 |
| Subject: | [php-src] Issue #7910: rename fails on Windows PHP 8.1 if the target file is being executed | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-238865@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7910
Author: johnstevenson
### Description
The following code:
```php
<?php
// file: bug.php
$content = file_get_contents(__FILE__);
file_put_contents('newfile.php', $content."\n//");
var_dump(rename('newfile.php', __FILE__));
```
Resulted in this output:
```
PHP Warning: rename(newfile.php,C:\Users\John\projects\test\bug.php): Access is denied (code: 5) in
C:\Users\John\projects\test\bug.php on line 6
Warning: rename(newfile.php,C:\Users\John\projects\test\bug.php): Access is denied (code: 5) in
C:\Users\John\projects\test\bug.php on line 6
bool(false)
```
But I expected this output instead:
```
bool(true)
```
This code mimics how Composer self updates
(https://getcomposer.org/doc/03-cli.md#self-update-selfupdate-) and has obviously worked for many
years, but is broken on Windows PHP 8.1: https://github.com/composer/composer/issues/10444
Note that on Composer 2 we now use
copy on Windows (to get around potential permission
issues in UAC protected locations) and this still works on PHP 8.1, but Composer 1 uses
rename (as do all non-Windows platforms on all Composer versions).
Fortunately, this appears to be a Windows only thing, as per this demo: https://github.com/johnstevenson/php-rename-bug/actions/runs/1669358275
### PHP Version
PHP 8.1.0
### Operating System
Windows 10