diff --git a/Filesystem.php b/Filesystem.php index 52bcca681..11f03edc5 100644 --- a/Filesystem.php +++ b/Filesystem.php @@ -673,6 +673,10 @@ public function dumpFile(string $filename, $content): void $this->rename($tmpFile, $filename, true); } finally { if (file_exists($tmpFile)) { + if ('\\' === \DIRECTORY_SEPARATOR && !is_writable($tmpFile)) { + self::box('chmod', $tmpFile, self::box('fileperms', $tmpFile) | 0200); + } + self::box('unlink', $tmpFile); } } diff --git a/Tests/FilesystemTest.php b/Tests/FilesystemTest.php index 002e7d4c6..c1ce505b0 100644 --- a/Tests/FilesystemTest.php +++ b/Tests/FilesystemTest.php @@ -1813,6 +1813,22 @@ public function testDumpKeepsExistingPermissionsWhenOverwritingAnExistingFile() $this->assertFilePermissions(745, $filename); } + public function testDumpFileCleansUpAfterFailure() + { + $targetFile = $this->workspace.'/dump-file'; + $this->filesystem->touch($targetFile); + $this->filesystem->chmod($targetFile, 0444); + + try { + $this->filesystem->dumpFile($targetFile, 'any content'); + } catch (IOException $e) { + } finally { + $this->filesystem->chmod($targetFile, 0666); + } + + $this->assertSame([$targetFile], glob($this->workspace.'/*')); + } + public function testReadFile() { $licenseFile = \dirname(__DIR__).'/LICENSE';