Skip to content

Commit

Permalink
fix: unexpected abort when symlink to non-regular file
Browse files Browse the repository at this point in the history
  • Loading branch information
fourdim committed Sep 14, 2024
1 parent 06db9d3 commit 519a3bd
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion rye/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static ENV_VAR_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\$\{([A-Z0-9_]+)\}").
#[cfg(unix)]
pub use std::os::unix::fs::symlink as symlink_file;
#[cfg(windows)]
pub use std::os::windows::fs::symlink_file;
pub use std::os::windows::fs::{symlink_dir, symlink_file};

use crate::config::Config;
use crate::consts::VENV_BIN;
Expand Down 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)
.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_folder() {
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

0 comments on commit 519a3bd

Please sign in to comment.