diff --git a/core/lib/Drupal/Component/PhpStorage/FileStorage.php b/core/lib/Drupal/Component/PhpStorage/FileStorage.php index 6853356..f933884 100644 --- a/core/lib/Drupal/Component/PhpStorage/FileStorage.php +++ b/core/lib/Drupal/Component/PhpStorage/FileStorage.php @@ -84,20 +84,7 @@ public function writeable() { */ public function deleteAll() { if (is_dir($this->directory)) { - $iterator = new \RecursiveDirectoryIterator($this->directory); - foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) { - if (!$iterator->isDot()) { - $this->filePreDeleteCallback($file->getPathname()); - if ($file->isDir()) { - rmdir($file->getPathname()); - } - else { - unlink($file->getPathname()); - } - } - } - $this->filePreDeleteCallback($this->directory); - return rmdir($this->directory); + return $this->deleteRecursive($this->directory); } else { // Return TRUE for non-existent folder, but log that nothing was actually @@ -107,6 +94,39 @@ public function deleteAll() { } /** + * Deletes all files and directories in the specified filepath recursively. + * + * If the specified path is a directory then the function will call itself + * recursively to process the contents. Once the contents have been removed + * the directory will also be removed. + * + * Note that this only deletes visible files with write permission. + * + * @param string $path + * A string containing either a file or directory path. + * + * @return boolean + * TRUE for success or if path does not exist, FALSE in the event of an + * error. + */ + protected function deleteRecursive($path) { + $this->filePreDeleteCallback($path); + if (is_dir($path)) { + $dir = dir($path); + while (($entry = $dir->read()) !== FALSE) { + if ($entry == '.' || $entry == '..') { + continue; + } + $entry_path = $path . '/' . $entry; + $this->deleteRecursive($entry_path); + } + $dir->close(); + return drupal_rmdir($path); + } + return unlink($path); + } + + /** * Ensures files and directories are deletable. */ public function filePreDeleteCallback($path) {