Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36cb46a

Browse files
bug #17626 Try to delete broken symlinks (IchHabRecht)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #17626). Discussion ---------- Try to delete broken symlinks If you delete the target of a symlink (at least on Windows systems) you don't get the kind of the target anymore (obviously). Therefore it might happen that a broken symlink to a directory should be removed with unlink() which fails. This patch adds another check for a broken symlink and tries to remove with rmdir() before throwing an exception. It helps to clean up test folders on Windows systems (so already proofed by the existing tests). Commits ------- 8442ab1 [Filesystem] Try to delete broken symlinks
2 parents 81b59b9 + 8442ab1 commit 36cb46a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,15 @@ public function remove($files)
161161
}
162162
} else {
163163
if (true !== @unlink($file)) {
164-
$error = error_get_last();
165-
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
164+
// handle broken symlinks on Windows systems
165+
if (is_link($file) && false === @readlink($file)) {
166+
if (true !== @rmdir($file)) {
167+
throw new IOException(sprintf('Failed to remove broken symlink "%s".', $file), 0, null, $file);
168+
}
169+
} else {
170+
$error = error_get_last();
171+
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
172+
}
166173
}
167174
}
168175
}

0 commit comments

Comments
 (0)