Fix: Avoid infinite recursion when source and destinations are same while using cp -R
#3018
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Proposing a solution for #3016
The infinite recursion can be prevented when running `cp -r <source_dir> <destination_dir> by handling these two cases:
1. Source and Destination refer to same canonicalized path, after resolving all symbolic links etc
2. Canonicalized destination has a canonicalized source as prefix:
This can also lead to recursion, imagine there is a directory called
child
under/home/ubuntu/parent
(i.e/home/ubuntu/parent/child
, then the following example leads to recursion:The official GNU coreutil's
cp
handles both of these cases.The above two cases can be solved by simply checking for case 2, i.e don't allow copy if destination has source in it's prefix.
I added a function:
Which checks that and calling it in
copy_directory
function.