php / php/php-src

rename fails on Windows PHP 8.1 if the target file is being executed

Open
#7,910 18 comments 2 reactions 0 assignees View on GitHub

@cmb69 is already working on this.

Since Aug 16, 2022.

  • #7929 by @cmb69 — closed without merging
  • #9351 by @cmb69 — open
Bug Category: Engine OS: Windows Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied bug.php script on Windows 10 with PHP 8.1, then inspect the rename implementation and the ongoing work in pull request #9351. Done means the self-rename returns bool(true) without an access-denied warning on Windows while preserving the behavior described for other platforms.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.