Userland: Fix overly-eager loop detection in cp (#4368)

The bug is that if you try to cp DIR_A to DIR_B where DIR_A and DIR_B
have the same parent directory and DIR_A's name is a prefix of DIR_B
(e.g. foo -> foo2, bar -> barbar), it thinks that it's a subdirectory
(since it checks if DIR_A's realpath is a prefix of DIR_B's realpath).

The easiest solution is to put a path delimiter at the end before the
comparison, since you can't have a / in the middle of a directory name.

For example if DIR_A is /home/anon/foo and DIR_B is /home/anon/foo2,
then DIR_A's realpath is a prefix of DIR_B's realpath even though DIR_B
is not inside DIR_A.
This commit is contained in:
Sahan Fernando 2020-12-10 06:56:50 +11:00 committed by GitHub
parent a3bcff0db8
commit 9453032bf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: sideshowbarker 2024-07-19 00:57:39 +09:00

View File

@ -181,7 +181,9 @@ bool copy_directory(String src_path, String dst_path)
}
String src_rp = Core::File::real_path_for(src_path);
src_rp = String::format("%s/", src_rp.characters());
String dst_rp = Core::File::real_path_for(dst_path);
dst_rp = String::format("%s/", dst_rp.characters());
if (!dst_rp.is_empty() && dst_rp.starts_with(src_rp)) {
fprintf(stderr, "cp: Cannot copy %s into itself (%s)\n",