From a0f9f2c53739ef336064e9e3d5ecd0c0d2046418 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 5 Sep 2017 19:39:30 +0200 Subject: [PATCH] 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. --- src/Symfony/Component/Filesystem/Filesystem.php | 10 ++++++++++ .../Filesystem/Tests/FilesystemTest.php | 16 ++++++++++++++++ .../Filesystem/Tests/FilesystemTestCase.php | 1 + 3 files changed, 27 insertions(+) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index bc2e3dcc2d..bbef48b466 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -532,6 +532,16 @@ class Filesystem $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); } diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index b7bdfac415..44eeceabd9 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1102,6 +1102,22 @@ class FilesystemTest extends FilesystemTestCase $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(); diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTestCase.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTestCase.php index 5586a00547..47598b2942 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTestCase.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTestCase.php @@ -61,6 +61,7 @@ class FilesystemTestCase extends TestCase $this->longPathNamesWindows = array(); } + chmod($this->workspace, 0777); $this->filesystem->remove($this->workspace); umask($this->umask); }