From a832b672cef5d7c72f0010b1cbfc18e8a776f0f5 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 5 Sep 2024 14:18:51 +0200 Subject: [PATCH] make sure temp files can be cleaned up on Windows --- src/Symfony/Component/Filesystem/Filesystem.php | 4 ++++ .../Filesystem/Tests/FilesystemTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 958ef178db2fb..3efd5f6d53e30 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -696,6 +696,10 @@ public function dumpFile(string $filename, $content) $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/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index eea5fe1a68952..d1722db93e669 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1826,6 +1826,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 testCopyShouldKeepExecutionPermission() { $this->markAsSkippedIfChmodIsMissing();