Skip to content

Commit

Permalink
[Filesystem] Better error handling in remove()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Apr 12, 2016
1 parent 1ca8d1c commit 53c2497
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/Symfony/Component/Filesystem/Filesystem.php
Expand Up @@ -155,24 +155,30 @@ public function touch($files, $time = null, $atime = null)
*/
public function remove($files)
{
$files = iterator_to_array($this->toIterator($files));
if ($files instanceof \Traversable) {
$files = iterator_to_array($files, false);
} elseif (!is_array($files)) {
$files = array($files);
}
$files = array_reverse($files);

foreach ($files as $file) {
if (@(unlink($file) || rmdir($file))) {
continue;
}
if (is_link($file)) {
// See https://bugs.php.net/52176
if (!@(unlink($file) || rmdir($file))) {
// See https://bugs.php.net/52176
continue;
}
$error = error_get_last();
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
} elseif (is_dir($file)) {
$this->remove(new \FilesystemIterator($file));

if (!@rmdir($file)) {
$this->remove(new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS));
}
if (!@rmdir($file)) {
$error = error_get_last();
throw new IOException(sprintf('Failed to remove directory "%s": %s.', $file, $error['message']));
}
} elseif (file_exists($file)) {
} elseif (!@unlink($file)) {
$error = error_get_last();
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
}
Expand Down

0 comments on commit 53c2497

Please sign in to comment.