Skip to content

[Filesystem] check permissions if dump target dir is missing #24105

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

Merged
merged 1 commit into from
Sep 7, 2017
Merged
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: 10 additions & 0 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,16 @@ public function dumpFile($filename, $content, $mode = 0666)
$dir = dirname($filename);

if (!is_dir($dir)) {
$oldCwd = getcwd();

if (!@chdir(dirname($dir))) {
// When the parent directory misses the executable permission bit, we are unable to enter it and thus
// cannot check if the target directory exists.
throw new IOException(sprintf('Unable to detect if the target directory "%s" exists.', $dir));
}

chdir($oldCwd);

$this->mkdir($dir);
}

Expand Down
16 changes: 16 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,22 @@ public function testDumpKeepsExistingPermissionsWhenOverwritingAnExistingFile()
$this->assertFilePermissions(745, $filename);
}

/**
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
* @expectedExceptionMessageRegExp /^Unable to detect if the target directory ".*" exists\.$/
*/
public function testDumpFailsWithExceptionIfExecutablePermissionsForTheParentDirectoryAreMissing()
{
$this->markAsSkippedIfChmodIsMissing();

$target = $this->workspace.DIRECTORY_SEPARATOR.'foo';
$file = $target.DIRECTORY_SEPARATOR.'foobar';
mkdir($target);
chmod($this->workspace, 0666);

$this->filesystem->dumpFile($file, 'baz');
}

public function testCopyShouldKeepExecutionPermission()
{
$this->markAsSkippedIfChmodIsMissing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected function tearDown()
$this->longPathNamesWindows = array();
}

chmod($this->workspace, 0777);
$this->filesystem->remove($this->workspace);
umask($this->umask);
}
Expand Down