Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unexpected abort when symlink to non-regular file #1383

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions rye/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,39 @@ pub fn copy_dir<T: AsRef<Path>>(from: T, to: T, options: &CopyDirOptions) -> Res
fs::create_dir_all(&destination)
.path_context(&destination, "failed to create directory")?;
copy_dir(entry.path(), destination, options)?;
} else if entry.file_type()?.is_symlink() {
let target = fs::read_link(&entry_path)
.path_context(&entry_path, "failed to read symlink target")?;
#[cfg(unix)]
{
if target.is_absolute() && target.starts_with(from) {
symlink_file(target.strip_prefix(from).unwrap(), &destination)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the strip_prefix here necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example:
If we have a folder symlink:

/project/folder -> /project/folder2

And we want to copy it to /tmp,
It is apparently we don't want this:

/tmp/folder -> /project/folder2

This one is more reasonable, isn't it?

/tmp/folder -> ./folder2

.path_context(&destination, "failed to create symlink")?;
} else {
symlink_file(target, &destination)
.path_context(&destination, "failed to create symlink")?;
}
}
#[cfg(windows)]
{
if target.is_absolute() && target.starts_with(from) {
if target.is_dir() {
symlink_dir(target.strip_prefix(from).unwrap(), &destination)
.path_context(&destination, "failed to create symlink")?;
} else {
symlink_file(target.strip_prefix(from).unwrap(), &destination)
.path_context(&destination, "failed to create symlink")?;
}
} else {
if target.is_dir() {
symlink_dir(target, &destination)
.path_context(&destination, "failed to create symlink")?;
} else {
symlink_file(target, &destination)
.path_context(&destination, "failed to create symlink")?;
}
}
}
} else {
fs::copy(entry.path(), &destination)
.path_context(entry.path(), "failed to copy file")?;
Expand Down