Skip to content

Commit 00dfe70

Browse files
[Filesystem] Better error handling in remove()
1 parent 1ca8d1c commit 00dfe70

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,24 +155,28 @@ public function touch($files, $time = null, $atime = null)
155155
*/
156156
public function remove($files)
157157
{
158-
$files = iterator_to_array($this->toIterator($files));
158+
if ($files instanceof \Traversable) {
159+
$files = iterator_to_array($files, false);
160+
} elseif (!is_array($files)) {
161+
$files = array($files);
162+
}
159163
$files = array_reverse($files);
160164
foreach ($files as $file) {
161-
if (@(unlink($file) || rmdir($file))) {
162-
continue;
163-
}
164165
if (is_link($file)) {
165166
// See https://bugs.php.net/52176
166-
$error = error_get_last();
167-
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
167+
if (!@(unlink($file) || '\\' !== DIRECTORY_SEPARATOR || rmdir($file)) && file_exists($file)) {
168+
$error = error_get_last();
169+
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
170+
}
168171
} elseif (is_dir($file)) {
169-
$this->remove(new \FilesystemIterator($file));
170-
171172
if (!@rmdir($file)) {
173+
$this->remove(new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS));
174+
}
175+
if (!@rmdir($file) && file_exists($file)) {
172176
$error = error_get_last();
173177
throw new IOException(sprintf('Failed to remove directory "%s": %s.', $file, $error['message']));
174178
}
175-
} elseif (file_exists($file)) {
179+
} elseif (!@unlink($file) && file_exists($file)) {
176180
$error = error_get_last();
177181
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
178182
}

0 commit comments

Comments
 (0)