Skip to content

Commit

Permalink
bug #17626 Try to delete broken symlinks (IchHabRecht)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicolas-grekas committed Mar 2, 2016
2 parents 81b59b9 + 8442ab1 commit 36cb46a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Symfony/Component/Filesystem/Filesystem.php
Expand Up @@ -161,8 +161,15 @@ public function remove($files)
}
} else {
if (true !== @unlink($file)) {
$error = error_get_last();
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
// handle broken symlinks on Windows systems
if (is_link($file) && false === @readlink($file)) {
if (true !== @rmdir($file)) {
throw new IOException(sprintf('Failed to remove broken symlink "%s".', $file), 0, null, $file);
}
} else {
$error = error_get_last();
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
}
}
}
}
Expand Down

0 comments on commit 36cb46a

Please sign in to comment.