Skip to content

Commit

Permalink
Merge pull request yiisoft#4091 from resurtm/4080-file-helper-remove-…
Browse files Browse the repository at this point in the history
…directory-symlinks

Fix possibility of non-removed symlinked directory.
  • Loading branch information
cebe committed Jun 27, 2014
2 parents 06f2775 + 9bc8c33 commit 9cbf4d8
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions framework/helpers/BaseFileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,32 +277,29 @@ public static function copyDirectory($src, $dst, $options = [])
*/
public static function removeDirectory($dir, $options = [])
{
if (!isset($options['traverseSymlinks'])) {
$options['traverseSymlinks'] = false;
}
if (!is_dir($dir) || !($handle = opendir($dir))) {
if (!is_dir($dir)) {
return;
}
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
if (!is_link($dir) || isset($options['traverseSymlinks']) && $options['traverseSymlinks']) {
if (!($handle = opendir($dir))) {
return;
}
$path = $dir . DIRECTORY_SEPARATOR . $file;
if (is_link($path)) {
if ($options['traverseSymlinks'] && is_dir($path)) {
static::removeDirectory($path, $options);
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
unlink($path);
} else {
if (is_file($path)) {
unlink($path);
} else {
$path = $dir . DIRECTORY_SEPARATOR . $file;
if (is_dir($path)) {
static::removeDirectory($path, $options);
} else {
unlink($path);
}
}
closedir($handle);
}
closedir($handle);
if (!is_link($dir)) {
if (is_link($dir)) {
unlink($dir);
} else {
rmdir($dir);
}
}
Expand Down

0 comments on commit 9cbf4d8

Please sign in to comment.