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 09f4096
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/Symfony/Component/Filesystem/Filesystem.php
Expand Up @@ -155,24 +155,27 @@ 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
$error = error_get_last();
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
if (!@(unlink($file) || '\\' !== DIRECTORY_SEPARATOR || rmdir($file)) && file_exists($file)) {
$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));
$this->remove(new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS));

if (!@rmdir($file)) {
if (!@rmdir($file) && file_exists($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) && file_exists($file)) {
$error = error_get_last();
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
}
Expand Down

0 comments on commit 09f4096

Please sign in to comment.