Skip to content

[Filesystem] improved errors for invalid dump targets #24134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we disabled the fabbot rule triggering this, can you check?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I've fixed everything in the codebase... using php-cs-fixer.

*/
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);
}

Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check why this does not apply on Appveyor or if is_link() works differently on Windows than on Unix. If someone using Windows can help here, that would be really appreciated.


$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();
Expand Down