8000 bug #18515 [Filesystem] Better error handling in remove() (nicolas-gr… · symfony/symfony@6401371 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6401371

Browse files
bug #18515 [Filesystem] Better error handling in remove() (nicolas-grekas)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #18515). Discussion ---------- [Filesystem] Better error handling in remove() | Q | A | ------------- | --- | Branch? | master (to be moved on 2.3 when merging) | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18512 | License | MIT | Doc PR | - Commits ------- b848ddb [Filesystem] Better error handling in remove()
2 parents 302e192 + b848ddb commit 6401371

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,27 @@ public function touch($files, $time = null, $atime = null)
140140
*/
141141
public function remove($files)
142142
{
143-
$files = iterator_to_array($this->toIterator($files));
143+
if ($files instanceof \Traversable) {
144+
$files = iterator_to_array($files, false);
145+
} elseif (!is_array($files)) {
146+
$files = array($files);
147+
}
144148
$files = array_reverse($files);
145149
foreach ($files as $file) {
146-
if (@(unlink($file) || rmdir($file))) {
147-
continue;
148-
}
149150
if (is_link($file)) {
150151
// See https://bugs.php.net/52176
151-
$error = error_get_last();
152-
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
152+
if (!@(unlink($file) || '\\' !== DIRECTORY_SEPARATOR || rmdir($file)) && file_exists($file)) {
153+
$error = error_get_last();
154+
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
155+
}
153156
} elseif (is_dir($file)) {
154-
$this->remove(new \FilesystemIterator($file));
157+
$this->remove(new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS));
155158

156-
if (!@rmdir($file)) {
159+
if (!@rmdir($file) && file_exists($file)) {
157160
$error = error_get_last();
158161
throw new IOException(sprintf('Failed to remove directory "%s": %s.', $file, $error['message']));
159162
}
160-
} elseif (file_exists($file)) {
163+
} elseif (!@unlink($file) && file_exists($file)) {
161164
$error = error_get_last();
162165
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
163166
}

0 commit comments

Comments
 (0)
0