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

Skip to content

Commit 58dd0b9

Browse files
committed
[Filesystem] 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).
1 parent 8f3c06b commit 58dd0b9

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,14 @@ public function remove($files)
172172
}
173173
} else {
174174
if (true !== @unlink($file)) {
175-
throw new IOException(sprintf('Failed to remove file "%s".', $file), 0, null, $file);
175+
// handle broken symlinks on Windows systems
176+
if (is_link($file) && false === @readlink($file)) {
177+
if (true !== @rmdir($file)) {
178+
throw new IOException(sprintf('Failed to remove broken symlink "%s".', $file), 0, null, $file);
179+
}
180+
} else {
181+
throw new IOException(sprintf('Failed to remove file "%s".', $file), 0, null, $file);
182+
}
176183
}
177184
}
178185
}

0 commit comments

Comments
 (0)