Skip to content

Commit

Permalink
remove cfg-if
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit committed May 28, 2024
1 parent 71c569f commit cd35ee0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
93 changes: 50 additions & 43 deletions cli/util/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,56 +493,63 @@ 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> {
use super::hard_link_dir_recursive;
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)?;
}

Ok(())
}
}

/// Clones a directory to another directory. The exact method
Expand Down

0 comments on commit cd35ee0

Please sign in to comment.