Skip to content

Commit bb92ba7

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: [Filesystem] fix cleaning up tmp files when dumpFile() fails [MimeType] Add missing alias for @mime_type
2 parents 4326782 + 17b83e3 commit bb92ba7

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

Filesystem.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -658,13 +658,17 @@ public function dumpFile(string $filename, $content)
658658
// when the filesystem supports chmod.
659659
$tmpFile = $this->tempnam($dir, basename($filename));
660660

661-
if (false === @file_put_contents($tmpFile, $content)) {
662-
throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
663-
}
661+
try {
662+
if (false === @file_put_contents($tmpFile, $content)) {
663+
throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
664+
}
664665

665-
@chmod($tmpFile, file_exists($filename) ? fileperms($filename) : 0666 & ~umask());
666+
@chmod($tmpFile, file_exists($filename) ? fileperms($filename) : 0666 & ~umask());
666667

667-
$this->rename($tmpFile, $filename, true);
668+
$this->rename($tmpFile, $filename, true);
669+
} finally {
670+
@unlink($tmpFile);
671+
}
668672
}
669673

670674
/**

Tests/FilesystemTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,18 @@ public function testAppendToFileCreateTheFileIfNotExists()
17201720
$this->assertStringEqualsFile($filename, 'bar');
17211721
}
17221722

1723+
public function testDumpRemovesTmpFilesOnFailure()
1724+
{
1725+
$expected = scandir(__DIR__, \SCANDIR_SORT_ASCENDING);
1726+
1727+
try {
1728+
$this->filesystem->dumpFile(__DIR__.'/Fixtures', 'bar');
1729+
$this->fail('IOException expected.');
1730+
} catch (IOException $e) {
1731+
$this->assertSame($expected, scandir(__DIR__, \SCANDIR_SORT_ASCENDING));
1732+
}
1733+
}
1734+
17231735
public function testDumpKeepsExistingPermissionsWhenOverwritingAnExistingFile()
17241736
{
17251737
$this->markAsSkippedIfChmodIsMissing();

0 commit comments

Comments
 (0)