Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64b390a

Browse files
committed
check permissions if dump target dir is missing
`is_dir()` returns `false` if the parent directory misses the executable bit even when the directory itself is present.
1 parent 26ef6d2 commit 64b390a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,16 @@ public function dumpFile($filename, $content, $mode = 0666)
532532
$dir = dirname($filename);
533533

534534
if (!is_dir($dir)) {
535+
$oldCwd = getcwd();
536+
537+
if (!@chdir(dirname($dir))) {
538+
// The target directory may exists, but we are unable to make a decision as we lack proper permissions
539+
// to enter its parent directory.
540+
throw new IOException(sprintf('Unable to detect if the target directory "%s" exists.', $dir));
541+
}
542+
543+
chdir($oldCwd);
544+
535545
$this->mkdir($dir);
536546
}
537547

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,22 @@ public function testDumpKeepsExistingPermissionsWhenOverwritingAnExistingFile()
11021102
$this->assertFilePermissions(745, $filename);
11031103
}
11041104

1105+
/**
1106+
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
1107+
* @expectedExceptionMessageRegExp /^Unable to detect if the target directory ".*" exists\.$/
1108+
*/
1109+
public function testDumpFailsWithExceptionIfExecutablePermissionsForTheParentDirectoryAreMissing()
1110+
{
1111+
$this->markAsSkippedIfChmodIsMissing();
1112+
1113+
$target = $this->workspace.DIRECTORY_SEPARATOR.'foo';
1114+
$file = $target.DIRECTORY_SEPARATOR.'foobar';
1115+
mkdir($target);
1116+
chmod($this->workspace, 0666);
1117+
1118+
$this->filesystem->dumpFile($file, 'baz');
1119+
}
1120+
11051121
public function testCopyShouldKeepExecutionPermission()
11061122
{
11071123
$this->markAsSkippedIfChmodIsMissing();

src/Symfony/Component/Filesystem/Tests/FilesystemTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ protected function tearDown()
6161
$this->longPathNamesWindows = array();
6262
}
6363

64+
chmod($this->workspace, 0777);
6465
$this->filesystem->remove($this->workspace);
6566
umask($this->umask);
6667
}

0 commit comments

Comments
 (0)