From 71fefa2ca8efbc211e36967b359eb52c0b30a648 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 --- Filesystem.php | 4 ++++ Tests/FilesystemTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Filesystem.php b/Filesystem.php index 958ef178d..3efd5f6d5 100644 --- a/Filesystem.php +++ b/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/Tests/FilesystemTest.php b/Tests/FilesystemTest.php index eea5fe1a6..d1722db93 100644 --- a/Tests/FilesystemTest.php +++ b/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();