diff --git a/Cargo.lock b/Cargo.lock index 60536ecb6c936a..4ce435af30c0d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1081,7 +1081,6 @@ dependencies = [ "bincode", "bytes", "cache_control", - "cfg-if", "chrono", "clap", "clap_complete", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 3708104c485cff..8c033f55a84d30 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -86,7 +86,6 @@ base64.workspace = true bincode = "=1.3.3" bytes.workspace = true cache_control.workspace = true -cfg-if = "1.0.0" chrono = { workspace = true, features = ["now"] } clap = { version = "=4.4.17", features = ["env", "string"] } clap_complete = "=4.4.7" diff --git a/cli/util/fs.rs b/cli/util/fs.rs index 532ae9c95ff788..9bdb1d014e554a 100644 --- a/cli/util/fs.rs +++ b/cli/util/fs.rs @@ -493,56 +493,62 @@ pub async fn remove_dir_all_if_exists(path: &Path) -> std::io::Result<()> { } mod clone_dir_imp { - use deno_core::error::AnyError; - - use super::copy_dir_recursive; - use std::path::Path; - - cfg_if::cfg_if! { - if #[cfg(target_vendor = "apple")] { - use std::os::unix::ffi::OsStrExt; - fn clonefile(from: &Path, to: &Path) -> std::io::Result<()> { - let from = std::ffi::CString::new(from.as_os_str().as_bytes())?; - let to = std::ffi::CString::new(to.as_os_str().as_bytes())?; - // SAFETY: `from` and `to` are valid C strings. - let ret = unsafe { libc::clonefile(from.as_ptr(), to.as_ptr(), 0) }; - if ret != 0 { - return Err(std::io::Error::last_os_error()); - } - Ok(()) - } - pub(super) fn clone_dir_recursive( - from: &Path, - to: &Path, - ) -> Result<(), AnyError> { - if let Some(parent) = to.parent() { - std::fs::create_dir_all(parent)?; - } - // Try to clone the whole directory - if let Err(err) = clonefile(from, to) { - if err.kind() != std::io::ErrorKind::AlreadyExists { - log::warn!("Failed to clone dir {:?} to {:?} via clonefile: {}", from, to, err); - } - // clonefile won't overwrite existing files, so if the dir exists - // we need to handle it recursively. - copy_dir_recursive(from, to)?; - } + #[cfg(target_vendor = "apple")] + mod apple { + use super::super::copy_dir_recursive; + use deno_core::error::AnyError; + use std::os::unix::ffi::OsStrExt; + use std::path::Path; + fn clonefile(from: &Path, to: &Path) -> std::io::Result<()> { + let from = std::ffi::CString::new(from.as_os_str().as_bytes())?; + let to = std::ffi::CString::new(to.as_os_str().as_bytes())?; + // SAFETY: `from` and `to` are valid C strings. + let ret = unsafe { libc::clonefile(from.as_ptr(), to.as_ptr(), 0) }; + if ret != 0 { + return Err(std::io::Error::last_os_error()); + } + Ok(()) + } - Ok(()) + pub fn clone_dir_recursive(from: &Path, to: &Path) -> Result<(), AnyError> { + if let Some(parent) = to.parent() { + std::fs::create_dir_all(parent)?; } - } else { - use super::hard_link_dir_recursive; - pub(super) fn clone_dir_recursive(from: &Path, to: &Path) -> Result<(), AnyError> { - if let Err(e) = hard_link_dir_recursive(from, to) { - log::debug!("Failed to hard link dir {:?} to {:?}: {}", from, to, e); - copy_dir_recursive(from, to)?; + // Try to clone the whole directory + if let Err(err) = clonefile(from, to) { + if err.kind() != std::io::ErrorKind::AlreadyExists { + log::warn!( + "Failed to clone dir {:?} to {:?} via clonefile: {}", + from, + to, + err + ); } - - Ok(()) + // clonefile won't overwrite existing files, so if the dir exists + // we need to handle it recursively. + copy_dir_recursive(from, to)?; } + + Ok(()) } } + + #[cfg(target_vendor = "apple")] + pub(super) use apple::clone_dir_recursive; + + #[cfg(not(target_vendor = "apple"))] + pub(super) fn clone_dir_recursive( + from: &std::path::Path, + to: &std::path::Path, + ) -> Result<(), deno_core::error::AnyError> { + if let Err(e) = super::hard_link_dir_recursive(from, to) { + log::debug!("Failed to hard link dir {:?} to {:?}: {}", from, to, e); + super::copy_dir_recursive(from, to)?; + } + + Ok(()) + } } /// Clones a directory to another directory. The exact method