Commit 62346123 authored by catch's avatar catch
Browse files

Issue #2685957 by neclimdul, mpp, pguillard, fomenkoandrey, alexpott,...

Issue #2685957 by neclimdul, mpp, pguillard, fomenkoandrey, alexpott, larowlan, pingwin4eg: Warning: mkdir(): File exists in Drupal\Component\PhpStorage\FileStorage->createDirectory
parent 7206f5d9
...@@ -138,34 +138,42 @@ protected function ensureDirectory($directory, $mode = 0777) { ...@@ -138,34 +138,42 @@ protected function ensureDirectory($directory, $mode = 0777) {
* The directory path. * The directory path.
* @param int $mode * @param int $mode
* The mode, permissions, the directory should have. * The mode, permissions, the directory should have.
* @param bool $is_backwards_recursive
* Internal use only.
* *
* @return bool * @return bool
* TRUE if the directory exists or has been created, FALSE otherwise. * TRUE if the directory exists or has been created, FALSE otherwise.
*/ */
protected function createDirectory($directory, $mode = 0777, $is_backwards_recursive = FALSE) { protected function createDirectory($directory, $mode = 0777) {
// If the directory exists already, there's nothing to do. // If the directory exists already, there's nothing to do.
if (is_dir($directory)) { if (is_dir($directory)) {
return TRUE; return TRUE;
} }
// Otherwise, try to create the directory and ensure to set its permissions,
// because mkdir() obeys the umask of the current process. // If the parent directory doesn't exist, try to create it.
if (is_dir($parent = dirname($directory))) { $parent_exists = is_dir($parent = dirname($directory));
// If the parent directory exists, then the backwards recursion must end, if (!$parent_exists) {
// regardless of whether the subdirectory could be created. $parent_exists = $this->createDirectory($parent, $mode);
if ($status = mkdir($directory)) { }
// Only try to chmod() if the subdirectory could be created.
$status = chmod($directory, $mode); // If parent exists, try to create the directory and ensure to set its
// permissions, because mkdir() obeys the umask of the current process.
if ($parent_exists) {
// We hide warnings and ignore the return because there may have been a
// race getting here and the directory could already exist.
@mkdir($directory);
// Only try to chmod() if the subdirectory could be created.
if (is_dir($directory)) {
// Avoid writing permissions if possible.
if (fileperms($directory) !== $mode) {
return chmod($directory, $mode);
}
return TRUE;
}
else {
// Something failed and the directory doesn't exist.
trigger_error('mkdir(): Permission Denied', E_USER_WARNING);
} }
return $is_backwards_recursive ? TRUE : $status;
} }
// If the parent directory and the requested directory does not exist and return FALSE;
// could not be created above, walk the requested directory path back up
// until an existing directory is hit, and from there, recursively create
// the sub-directories. Only if that recursion succeeds, create the final,
// originally requested subdirectory.
return $this->createDirectory($parent, $mode, TRUE) && mkdir($directory) && chmod($directory, $mode);
} }
/** /**
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
use Drupal\Component\PhpStorage\FileStorage; use Drupal\Component\PhpStorage\FileStorage;
use Drupal\Component\Utility\Random; use Drupal\Component\Utility\Random;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit_Framework_Error_Warning;
/** /**
* @coversDefaultClass \Drupal\Component\PhpStorage\FileStorage * @coversDefaultClass \Drupal\Component\PhpStorage\FileStorage
...@@ -87,4 +89,18 @@ public function testDeleteAll() { ...@@ -87,4 +89,18 @@ public function testDeleteAll() {
unset($GLOBALS[$random]); unset($GLOBALS[$random]);
} }
/**
* @covers ::createDirectory
*/
public function testCreateDirectoryFailWarning() {
$directory = new vfsStreamDirectory('permissionDenied', 0200);
$storage = new FileStorage([
'directory' => $directory->url(),
'bin' => 'test',
]);
$code = "<?php\n echo 'here';";
$this->setExpectedException(PHPUnit_Framework_Error_Warning::class, 'mkdir(): Permission Denied');
$storage->save('subdirectory/foo.php', $code);
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment