From de0b797b00e5b7b782735b5170537f7f63a27c69 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 8 Sep 2017 09:14:15 +0200 Subject: [PATCH] improved errors for invalid dump targets --- .../Component/Filesystem/Filesystem.php | 10 +++++- .../Filesystem/Tests/FilesystemTest.php | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index bc2e3dcc2d897..6df03741dd3b5 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -525,13 +525,21 @@ public function isAbsolutePath($file) * @param null|int $mode The file mode (octal). If null, file permissions are not modified * Deprecated since version 2.3.12, to be removed in 3.0. * - * @throws IOException If the file cannot be written to. + * @throws IOException if the file cannot be written to */ public function dumpFile($filename, $content, $mode = 0666) { $dir = dirname($filename); if (!is_dir($dir)) { + if (is_file($dir)) { + throw new IOException(sprintf('The parent path of "%s" is a file.', $dir)); + } + + if (is_link($dir)) { + throw new IOException(sprintf('The parent path of "%s" is a symlink to a nonexistent directory.', $dir)); + } + $this->mkdir($dir); } diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index b7bdfac4155e0..294d80c2be9fa 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1102,6 +1102,37 @@ public function testDumpKeepsExistingPermissionsWhenOverwritingAnExistingFile() $this->assertFilePermissions(745, $filename); } + /** + * @expectedException \Symfony\Component\Filesystem\Exception\IOException + * @expectedExceptionMessageRegExp /^The parent path of ".*" is a file\.$/ + */ + public function testDumpFailsIfParentDirectoryIsAnExistingFile() + { + $file = $this->workspace.DIRECTORY_SEPARATOR.'foo'; + $target = $file.DIRECTORY_SEPARATOR.'bar'; + touch($file); + + $this->filesystem->dumpFile($target, 'baz'); + } + + /** + * @expectedException \Symfony\Component\Filesystem\Exception\IOException + * @expectedExceptionMessageRegExp /^The parent path of ".*" is a symlink to a nonexistent directory\.$/ + */ + public function testDumpFailsIfTargetIsSymlinkAndSymlinkTargetDoesNotExist() + { + $this->markAsSkippedIfSymlinkIsMissing(); + + $dir = $this->workspace.DIRECTORY_SEPARATOR.'foo'; + mkdir($dir); + $link = $this->workspace.DIRECTORY_SEPARATOR.'bar'; + symlink($dir, $link); + rmdir($dir); + $target = $link.DIRECTORY_SEPARATOR.'baz'; + + $this->filesystem->dumpFile($target, 'baz'); + } + public function testCopyShouldKeepExecutionPermission() { $this->markAsSkippedIfChmodIsMissing();