use super::{exit_if_partial, options_from_cli, print_skipped, watch};
use crate::cli::Cli;
use crate::protocol::{BUILD_FINGERPRINT, TargetOs};
use crate::sync::SyncStats;
use crate::sync::{ItemizeAction, ItemizeEntry};
use crate::sync::{Executor, ExecutorOptions};
use crate::target::{Location, RemoteTarget};
use crate::transport::ssh::{default_remote_path, local_platform, sidecar_candidates, sidecar_path};
use crate::transport::{JumpHost, RemoteClient, Session, SessionHandle, Transport};
use anyhow::Result;
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
use terminal_size::{terminal_size, Width};
use tokio::io::{AsyncRead, AsyncWrite};
use unicode_width::UnicodeWidthStr;
use zeroize::Zeroize;
pub async fn execute(cli: &mut Cli) -> Result<()> {
let (source, destination) = match (&cli.source, &cli.destination, cli.files_from.is_some()) {
(Some(single), None, true) => (String::from("."), single.clone()),
(Some(_), Some(_), true) => {
return Err(anyhow::anyhow!(
"--files-from entries are absolute paths; SRC is not used — pass only the destination"
))
}
(None, _, true) => {
return Err(anyhow::anyhow!(
"Missing DESTINATION. Usage: cp2 --files-from FILE DST"
))
}
(Some(src), Some(dst), false) => (src.clone(), dst.clone()),
(Some(_), None, false) => {
return Err(anyhow::anyhow!(
"Missing DESTINATION. Usage: cp2 [OPTIONS] SRC DST"
))
}
(None, _, false) => {
return Err(anyhow::anyhow!("Missing SOURCE. Usage: cp2 [OPTIONS] SRC DST"))
}
};
let mut src = Location::parse(&source);
let mut dst = Location::parse(&destination);
for target in [&src, &dst] {
if let Location::Remote(remote) = target
&& (remote.user.is_empty() || remote.host.is_empty())
{
anyhow::bail!(
"invalid remote target '{target}': user and host must be non-empty \
(expected user@host[:path])"
);
}
}
let src_is_dir = match &src {
Location::Local(path) => path.is_dir(),
Location::Remote(_) => true,
};
let source_multi: Option<(PathBuf, Vec<PathBuf>)> = match &src {
Location::Local(path) => {
if let Some(list_file) = &cli.files_from {
let content = std::fs::read_to_string(list_file).map_err(|e| {
anyhow::anyhow!("failed to read --files-from list {}: {e}", list_file.display())
})?;
let mut roots = Vec::new();
for entry in parse_file_list(&content) {
if !Path::new(&entry).is_absolute() {
anyhow::bail!(
"--files-from entries must be absolute paths, got '{entry}'"
);
}
let full = PathBuf::from(&entry);
if full.exists() {
roots.push(full);
} else {
eprintln!("warning: {entry}: no such file or directory (--files-from)");
}
}
let base = roots
.first()
.map_or_else(|| PathBuf::from("/"), |r| path_root(r));
if roots.iter().any(|r| !r.starts_with(&base)) {
anyhow::bail!("--files-from entries must be on the same filesystem root");
}
Some((base, roots))
} else if !src_is_dir {
match path.to_str() {
Some(pattern) if has_glob_metachars(pattern) && !path.exists() => {
expand_source(pattern)?
}
_ => None,
}
} else {
match path.to_str() {
Some(pattern) => expand_source(pattern)?,
None => None, }
}
}
Location::Remote(_) => None,
};
if source_multi.is_some() && cli.watch.is_some() {
anyhow::bail!(
"--watch is not supported with glob or --files-from sources; quote a single directory instead"
);
}
if let Some(port) = cli.port {
if let Location::Remote(r) = &mut src {
r.port = port;
}
if let Location::Remote(r) = &mut dst {
r.port = port;
}
}
let mut options = options_from_cli(cli)?;
if !cli.quiet && !cli.no_progress {
install_progress(&mut options);
}
if let Some((base, roots)) = &source_multi
&& cli.files_from.is_some()
{
options.delete_scope = roots
.iter()
.filter_map(|r| r.strip_prefix(base).ok())
.map(|s| s.to_string_lossy().replace('\\', "/"))
.collect();
}
options.include_root_component = match &src {
Location::Local(_) => {
src_is_dir
&& source_multi.is_none()
&& crate::sync::scanner::include_root_component(&source)
}
Location::Remote(_) => false,
};
let transport = Transport::default_transport();
let password = resolve_password(cli)?;
let mut client = RemoteClient::new(
transport,
password,
cli.jump_password.take(),
cli.remote_sudo,
cli.sudo_password.take(),
);
let jump = cli.jump_host.as_deref().map(JumpHost::parse).transpose()?;
match (&src, &dst) {
(Location::Local(_), Location::Remote(r)) | (Location::Remote(r), Location::Local(_)) => {
options.remote_path = r.path.clone();
}
_ => {}
}
if matches!(src, Location::Remote(_))
&& let Some(list_file) = &cli.files_from
{
let content = std::fs::read_to_string(list_file).map_err(|e| {
anyhow::anyhow!(
"failed to read --files-from list {}: {e}",
list_file.display()
)
})?;
let mut paths = Vec::new();
for entry in parse_file_list(&content) {
if !Path::new(&entry).is_absolute() {
anyhow::bail!("remote --files-from entries must be absolute paths, got '{entry}'");
}
paths.push(entry);
}
if paths.is_empty() {
anyhow::bail!("--files-from list is empty");
}
options.remote_paths = paths;
options.delete_scope = options
.remote_paths
.iter()
.map(Path::new)
.filter_map(|p| p.strip_prefix(path_root(p)).ok())
.map(|s| s.to_string_lossy().replace('\\', "/"))
.collect();
}
if cli.list_only {
return match src {
Location::Local(src_path) => {
let manifest = crate::sync::executor::scan_tree(&src_path, &options, true).await?;
if !cli.quiet {
println!("Listing {}:", src_path.display());
for f in &manifest.files {
let kind = if f.is_dir {
'd'
} else if f.link_target.is_some() {
'L'
} else {
'f'
};
println!("{kind} {:>12} {}", f.size, f.relative_path);
}
}
Ok(())
}
Location::Remote(remote) => {
let stats = list_via_ssh(
&remote,
&options.remote_path,
&options,
cli.remote_path.as_deref(),
cli.binaries_dir.as_deref(),
!cli.no_auto_install,
cli.quiet,
&mut client,
jump.as_ref(),
)
.await?;
if !cli.quiet {
println!("Listing {remote}:");
for e in &stats.changes {
println!("{} {:>12} {}", e.kind, e.size, e.path);
}
}
Ok(())
}
};
}
match (src, dst) {
(Location::Local(src_path), Location::Remote(remote)) => {
if cli.dry_run {
if !cli.quiet {
println!("Dry run: would push {} to {remote}", src_path.display());
}
return Ok(());
}
if cli.watch.is_some() {
if !src_path.is_dir() {
anyhow::bail!("--watch requires SRC to be a directory");
}
return Box::pin(watch::watch_push(cli, &remote, &src_path, &options, client, jump.clone()))
.await;
}
let stats = match &source_multi {
Some((base, roots)) => {
push_multi_via_ssh(
&remote,
base,
roots,
&options,
cli.remote_path.as_deref(),
cli.binaries_dir.as_deref(),
!cli.no_auto_install,
cli.quiet,
&mut client,
jump.as_ref(),
)
.await?
}
None => {
push_via_ssh(
&remote,
&src_path,
&options,
cli.remote_path.as_deref(),
cli.binaries_dir.as_deref(),
!cli.no_auto_install,
cli.quiet,
&mut client,
jump.as_ref(),
)
.await?
}
};
if !cli.quiet {
println!(
"Done: {} files, {} bytes transferred",
stats.files_sent, stats.bytes_transferred
);
if cli.stats {
print_stats(&stats, stats.files_sent);
}
if cli.itemize_changes {
print_itemize(&stats);
}
}
exit_if_partial(print_skipped(&stats));
Ok(())
}
(Location::Remote(remote), Location::Local(dst_path)) => {
if cli.dry_run {
if !cli.quiet {
println!("Dry run: would pull {remote} to {}", dst_path.display());
}
return Ok(());
}
let (local_os, _) = local_platform();
options.target_os = TargetOs::from_os_name(&local_os);
if cli.watch.is_some() {
return Box::pin(watch::watch_pull(cli, &remote, &dst_path, &options, client, jump.clone()))
.await;
}
let stats = pull_via_ssh(
&remote,
&dst_path,
&options,
cli.remote_path.as_deref(),
cli.binaries_dir.as_deref(),
!cli.no_auto_install,
cli.quiet,
&mut client,
jump.as_ref(),
)
.await?;
if !cli.quiet {
println!(
"Done: {} files, {} bytes transferred",
stats.files_received, stats.bytes_transferred
);
if cli.stats {
print_stats(&stats, stats.files_received);
}
if cli.itemize_changes {
print_itemize(&stats);
}
}
exit_if_partial(print_skipped(&stats));
Ok(())
}
(Location::Local(src_path), Location::Local(dst_path)) => {
if cli.dry_run {
if !cli.quiet {
println!(
"Dry run: would copy {} to {}",
src_path.display(),
dst_path.display()
);
}
return Ok(());
}
let (local_os, _) = local_platform();
options.target_os = TargetOs::from_os_name(&local_os);
if cli.watch.is_some() {
if !src_path.is_dir() {
anyhow::bail!("--watch requires SRC to be a directory");
}
return watch::watch_local(cli, &src_path, &dst_path, &options).await;
}
let stats = match &source_multi {
Some((base, roots)) => {
watch::push_multi_local_over_server(base, roots, &dst_path, &options).await?
}
None => watch::push_local_over_server(&src_path, &dst_path, &options).await?,
};
if !cli.quiet {
println!(
"Done: {} files, {} bytes transferred",
stats.files_sent, stats.bytes_transferred
);
if cli.stats {
print_stats(&stats, stats.files_sent);
}
if cli.itemize_changes {
print_itemize(&stats);
}
}
exit_if_partial(print_skipped(&stats));
Ok(())
}
(Location::Remote(_), Location::Remote(_)) => {
anyhow::bail!("Remote-to-remote sync is not supported")
}
}
}
fn resolve_password(cli: &mut Cli) -> anyhow::Result<Option<String>> {
match (cli.password.take(), cli.password_file.as_deref()) {
(Some(p), Some(_)) => {
let mut p = p;
p.zeroize();
anyhow::bail!("--password and --password-file are mutually exclusive");
}
(Some(p), None) => Ok(Some(p)),
(None, Some(file)) => {
let mut content = std::fs::read_to_string(file).map_err(|e| {
anyhow::anyhow!("failed to read --password-file {}: {e}", file.display())
})?;
let password = content.lines().next().map(str::to_string);
content.zeroize();
Ok(password)
}
(None, None) => Ok(None),
}
}
fn parse_file_list(content: &str) -> Vec<String> {
content
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.map(str::to_string)
.collect()
}
fn path_root(path: &Path) -> PathBuf {
let root: PathBuf = path
.components()
.take_while(|c| matches!(c, Component::Prefix(_) | Component::RootDir))
.collect();
if root.as_os_str().is_empty() {
PathBuf::from("/")
} else {
root
}
}
fn has_glob_metachars(s: &str) -> bool {
s.contains(['*', '?', '['])
}
fn static_prefix_dir(pattern: &str) -> PathBuf {
let prefix = &pattern[..pattern.find(['*', '?', '[']).unwrap_or(pattern.len())];
let base = match prefix.rfind(['/', '\\']) {
Some(idx) => &prefix[..idx],
None => "",
};
if base.is_empty() {
if prefix.starts_with('/') {
PathBuf::from("/")
} else {
PathBuf::new()
}
} else if base == "." {
PathBuf::new()
} else {
PathBuf::from(base)
}
}
fn expand_source(pattern: &str) -> anyhow::Result<Option<(PathBuf, Vec<PathBuf>)>> {
if !has_glob_metachars(pattern) || Path::new(pattern).exists() {
return Ok(None);
}
let mut matches: Vec<PathBuf> = crate::sync::executor::glob_expand(pattern)
.map_err(|e| anyhow::anyhow!("{e}"))?;
if matches.is_empty() {
anyhow::bail!("no files match source pattern '{pattern}'");
}
matches.sort();
let base = static_prefix_dir(pattern);
let base = base.strip_prefix("./").unwrap_or(&base).to_path_buf();
Ok(Some((base, matches)))
}
pub(crate) fn server_args(options: &ExecutorOptions) -> Vec<String> {
let mut args: Vec<String> = Vec::new();
if let Some(n) = options.jobs {
args.push("--jobs".to_string());
args.push(n.max(1).to_string());
}
if let Some(n) = options.max_delete {
args.push("--max-delete".to_string());
args.push(n.to_string());
}
if options.delete {
args.push("--delete".to_string());
}
if options.backup {
args.push("--backup".to_string());
}
if options.fsync {
args.push("--fsync".to_string());
}
if options.checksum {
args.push("--checksum".to_string());
}
if options.remove_source_files {
args.push("--remove-source-files".to_string());
}
if options.verify {
args.push("--verify".to_string());
}
if options.rollsum {
args.push("--rollsum".to_string());
}
if options.quiet {
args.push("--quiet".to_string());
}
if options.archive {
args.push("--archive".to_string());
} else if options.literal_links {
args.push("--literal-links".to_string());
}
if !options.recursive {
args.push("--no-recursive".to_string());
}
if !options.preserve_links {
args.push("--skip-links".to_string());
}
if options.follow_links {
args.push("--follow-links".to_string());
}
if options.literal_internal_links {
args.push("--literal-internal-links".to_string());
}
if options.literal_external_file_links {
args.push("--literal-external-file-links".to_string());
}
if options.literal_external_dir_links {
args.push("--literal-external-dir-links".to_string());
}
if !options.preserve_perms {
args.push("--no-perms".to_string());
}
if !options.preserve_times {
args.push("--no-times".to_string());
}
if options.sparse {
args.push("--sparse".to_string());
}
if options.xattrs {
args.push("--xattrs".to_string());
}
if options.atimes {
args.push("--atimes".to_string());
}
if options.storage != crate::platform::storage::StoragePreference::Auto {
args.push("--storage".to_string());
args.push(options.storage.to_string());
}
args
}
#[expect(clippy::too_many_arguments)]
async fn push_via_ssh(
remote: &RemoteTarget,
src_path: &Path,
options: &ExecutorOptions,
user_remote_path: Option<&str>,
binaries_dir: Option<&Path>,
auto_install: bool,
quiet: bool,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<SyncStats> {
let server_args = server_args(options).join(" ");
match ensure_and_open(
remote,
user_remote_path,
binaries_dir,
auto_install,
quiet,
&server_args,
client,
jump,
)
.await?
{
Ensured::Merged {
os,
arch,
remote_path,
session,
} => {
let mut options = options.clone();
options.target_os = TargetOs::from_os_name(&os);
run_session_with_deploy(
session,
&server_args,
auto_install,
quiet,
remote,
&os,
&arch,
&remote_path,
binaries_dir,
client,
jump,
false,
|send, recv| {
let src_path = src_path.to_path_buf();
let options = options.clone();
async move {
let mut executor = Executor::new(send, recv);
let result = executor
.push(&src_path, &options)
.await
.map_err(anyhow::Error::new);
drop(executor);
result
}
},
)
.await
}
Ensured::Classic { os, remote_path } => {
push_over_ssh(remote, src_path, options, &os, &remote_path, client, jump).await
}
}
}
pub(crate) async fn push_over_ssh(
remote: &RemoteTarget,
src_path: &Path,
options: &ExecutorOptions,
os: &str,
remote_path: &str,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<SyncStats> {
let session = client.open_session(remote, remote_path, os, &server_args(options).join(" "), jump).await?;
let (send, recv, mut handle) = session.into_parts();
let mut executor = Executor::new(send, recv);
let mut options = options.clone();
options.target_os = TargetOs::from_os_name(os);
let result = executor
.push(src_path, &options)
.await
.map_err(anyhow::Error::new);
drop(executor);
handle.finish(result).await
}
#[expect(clippy::too_many_arguments)]
async fn push_multi_via_ssh(
remote: &RemoteTarget,
base: &Path,
roots: &[PathBuf],
options: &ExecutorOptions,
user_remote_path: Option<&str>,
binaries_dir: Option<&Path>,
auto_install: bool,
quiet: bool,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<SyncStats> {
let server_args = server_args(options).join(" ");
match ensure_and_open(
remote,
user_remote_path,
binaries_dir,
auto_install,
quiet,
&server_args,
client,
jump,
)
.await?
{
Ensured::Merged {
os,
arch,
remote_path,
session,
} => {
let mut options = options.clone();
options.target_os = TargetOs::from_os_name(&os);
run_session_with_deploy(
session,
&server_args,
auto_install,
quiet,
remote,
&os,
&arch,
&remote_path,
binaries_dir,
client,
jump,
false,
|send, recv| {
let base = base.to_path_buf();
let roots = roots.to_vec();
let options = options.clone();
async move {
let mut executor = Executor::new(send, recv);
let result = executor
.push_multi(&base, &roots, &options)
.await
.map_err(anyhow::Error::new);
drop(executor);
result
}
},
)
.await
}
Ensured::Classic { os, remote_path } => {
push_multi_over_ssh(remote, base, roots, options, &os, &remote_path, client, jump)
.await
}
}
}
#[expect(clippy::too_many_arguments)]
pub(crate) async fn push_multi_over_ssh(
remote: &RemoteTarget,
base: &Path,
roots: &[PathBuf],
options: &ExecutorOptions,
os: &str,
remote_path: &str,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<SyncStats> {
let session = client.open_session(remote, remote_path, os, &server_args(options).join(" "), jump).await?;
let (send, recv, mut handle) = session.into_parts();
let mut executor = Executor::new(send, recv);
let mut options = options.clone();
options.target_os = TargetOs::from_os_name(os);
let result = executor
.push_multi(base, roots, &options)
.await
.map_err(anyhow::Error::new);
drop(executor);
handle.finish(result).await
}
#[expect(clippy::too_many_arguments)]
async fn pull_via_ssh(
remote: &RemoteTarget,
dst_path: &Path,
options: &ExecutorOptions,
user_remote_path: Option<&str>,
binaries_dir: Option<&Path>,
auto_install: bool,
quiet: bool,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<SyncStats> {
let server_args = server_args(options).join(" ");
match ensure_and_open(
remote,
user_remote_path,
binaries_dir,
auto_install,
quiet,
&server_args,
client,
jump,
)
.await?
{
Ensured::Merged {
os,
arch,
remote_path,
session,
} => {
run_session_with_deploy(
session,
&server_args,
auto_install,
quiet,
remote,
&os,
&arch,
&remote_path,
binaries_dir,
client,
jump,
false,
|send, recv| {
let dst_path = dst_path.to_path_buf();
let options = options.clone();
async move {
let mut executor = Executor::new(send, recv);
let result = executor
.pull(&dst_path, &options)
.await
.map_err(anyhow::Error::new);
drop(executor);
result
}
},
)
.await
}
Ensured::Classic { os, remote_path } => {
pull_over_ssh(remote, dst_path, options, &os, &remote_path, client, jump).await
}
}
}
pub(crate) async fn pull_over_ssh(
remote: &RemoteTarget,
dst_path: &Path,
options: &ExecutorOptions,
os: &str,
remote_path: &str,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<SyncStats> {
let session = client.open_session(remote, remote_path, os, &server_args(options).join(" "), jump).await?;
let (send, recv, mut handle) = session.into_parts();
let mut executor = Executor::new(send, recv);
let result = executor
.pull(dst_path, options)
.await
.map_err(anyhow::Error::new);
drop(executor);
handle.finish(result).await
}
pub(crate) async fn list_over_ssh(
remote: &RemoteTarget,
remote_path: &str,
options: &ExecutorOptions,
os: &str,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<SyncStats> {
let session = client.open_session(remote, remote_path, os, &server_args(options).join(" "), jump).await?;
let (send, recv, mut handle) = session.into_parts();
let mut executor = Executor::new(send, recv);
let result = executor
.list(remote_path, options)
.await
.map_err(anyhow::Error::new);
drop(executor);
handle.finish(result).await
}
#[expect(clippy::too_many_arguments)]
async fn list_via_ssh(
remote: &RemoteTarget,
remote_path: &str,
options: &ExecutorOptions,
user_remote_path: Option<&str>,
binaries_dir: Option<&Path>,
auto_install: bool,
quiet: bool,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<SyncStats> {
let server_args = server_args(options).join(" ");
match ensure_and_open(
remote,
user_remote_path,
binaries_dir,
auto_install,
quiet,
&server_args,
client,
jump,
)
.await?
{
Ensured::Merged {
os,
arch,
remote_path,
session,
} => {
run_session_with_deploy(
session,
&server_args,
auto_install,
quiet,
remote,
&os,
&arch,
&remote_path,
binaries_dir,
client,
jump,
false,
|send, recv| {
let remote_path = remote_path.clone();
let options = options.clone();
async move {
let mut executor = Executor::new(send, recv);
let result = executor
.list(&remote_path, &options)
.await
.map_err(anyhow::Error::new);
drop(executor);
result
}
},
)
.await
}
Ensured::Classic { os, .. } => {
list_over_ssh(remote, remote_path, options, &os, client, jump).await
}
}
}
pub(crate) async fn resolve_and_ensure(
remote: &RemoteTarget,
user_path: Option<&str>,
binaries_dir: Option<&Path>,
auto_install: bool,
quiet: bool,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<(String, String)> {
let candidate = user_path
.map_or_else(|| "~/.cargo/bin/cp2".to_string(), str::to_string);
let (platform, mut version) = match client.probe_and_version(remote, &candidate, jump).await {
Err(e) => return Err(e.into()),
Ok(v) => v,
};
let (os, arch) = platform.unwrap_or_else(local_platform);
let remote_path = match user_path {
Some(p) => p.to_string(),
None => default_remote_path(&os),
};
if !auto_install {
return Ok((os, remote_path));
}
if os == "windows" && version.is_none() && user_path.is_none() {
version = client.check_version(remote, &remote_path, &os, jump).await?;
}
if let Some((_, Some(fp))) = &version
&& fp == BUILD_FINGERPRINT
{
return Ok((os, remote_path));
}
deploy_remote_binary(remote, &os, &arch, &remote_path, binaries_dir, quiet, client, jump).await?;
Ok((os, remote_path))
}
fn deploy_source(
os: &str,
arch: &str,
binaries_dir: Option<&Path>,
) -> anyhow::Result<PathBuf> {
let (local_os, local_arch) = local_platform();
let candidates = sidecar_candidates(os, arch);
if let Some(path) = find_sidecar(&candidates, binaries_dir) {
return Ok(path);
}
if os == local_os && arch == local_arch {
return std::env::current_exe().map_err(anyhow::Error::new);
}
let triple = candidates.first().copied().unwrap_or("<triple>");
let (bin_name, bin_dir) = if os == "windows" {
("cp2.exe", r"%USERPROFILE%\.cargo\bin".to_string())
} else {
("cp2", "~/.cargo/bin".to_string())
};
anyhow::bail!(
"remote is {os}/{arch}: no matching cp2 to deploy (auto-deploy covers \
same-platform remotes only). Install cp2 on the remote once: \
log in and run `cargo install cp2 --locked`, \
or download the prebuilt release tarball for {os}/{arch} from the GitHub \
releases page, extract it, rename the binary to `{bin_name}`, and copy it \
to `{bin_dir}`, \
or `git clone https://github.com/elemeng/cp2 && cargo build --release` and \
copy `target/release/{bin_name}` to `{bin_dir}`. \
Alternatively, place a prebuilt `cp2-{triple}` sidecar next to this binary \
(or in --binaries-dir): the client will deploy that automatically."
)
}
#[expect(clippy::too_many_arguments, reason = "the resolution needs every resolved remote fact")]
async fn deploy_remote_binary(
remote: &RemoteTarget,
os: &str,
arch: &str,
remote_path: &str,
binaries_dir: Option<&Path>,
quiet: bool,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<()> {
let deploy_source = deploy_source(os, arch, binaries_dir)?;
if !quiet {
println!("Deploying cp2 v{} to {remote} ({remote_path})...", env!("CARGO_PKG_VERSION"));
}
client
.deploy(remote, remote_path, &deploy_source, os, jump)
.await?;
match client.check_version(remote, remote_path, os, jump).await {
Ok(Some((_, Some(fp)))) if fp == BUILD_FINGERPRINT => Ok(()),
Ok(None) => Err(anyhow::anyhow!(
"deployed cp2 to {remote} at {remote_path}, but it does not report a version — \
a truncated push, or the remote's glibc is older than this build's. Install cp2 \
on the remote with `cargo install cp2 --locked` (it builds against the remote's \
own glibc) or copy a static musl build to `~/.cargo/bin`"
)),
Ok(Some((v, fp))) => Err(anyhow::anyhow!(
"deployed cp2 to {remote} at {remote_path}, but it reports v{v} (build {fp:?}); \
expected build {BUILD_FINGERPRINT}"
)),
Err(e) => Err(anyhow::anyhow!(
"failed to verify the deployed cp2 on {remote}: {e}"
)),
}
}
#[allow(clippy::large_enum_variant)]
pub(crate) enum Ensured {
Merged {
os: String,
arch: String,
remote_path: String,
session: Session,
},
Classic { os: String, remote_path: String },
}
#[expect(clippy::too_many_arguments, reason = "the resolution needs every resolved remote fact")]
pub(crate) async fn ensure_and_open(
remote: &RemoteTarget,
user_path: Option<&str>,
binaries_dir: Option<&Path>,
auto_install: bool,
quiet: bool,
server_args: &str,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
) -> anyhow::Result<Ensured> {
let candidate = user_path
.map_or_else(|| "~/.cargo/bin/cp2".to_string(), str::to_string);
match client
.open_preamble_session(remote, &candidate, server_args, jump)
.await
{
Ok(Some(p)) => {
return Ok(Ensured::Merged {
os: p.os,
arch: p.arch,
remote_path: candidate,
session: p.session,
})
}
Ok(None) => {}
Err(e) => return Err(e.into()),
}
let (os, remote_path) =
resolve_and_ensure(remote, user_path, binaries_dir, auto_install, quiet, client, jump)
.await?;
Ok(Ensured::Classic { os, remote_path })
}
async fn run_session_once<F, Fut>(
session: Session,
cancellable: bool,
run: &F,
) -> (bool, anyhow::Result<SyncStats>, SessionHandle)
where
F: Fn(
Box<dyn AsyncWrite + Unpin + Send>,
Box<dyn AsyncRead + Unpin + Send>,
) -> Fut,
Fut: std::future::Future<Output = anyhow::Result<SyncStats>>,
{
let (send, recv, mut handle) = session.into_parts();
let executor_result = run(send, recv).await;
let transfer_failed = executor_result.is_err();
let finished = if cancellable {
handle.finish_after_local_cancel(executor_result).await
} else {
handle.finish(executor_result).await
};
(transfer_failed, finished, handle)
}
#[expect(clippy::too_many_arguments)]
pub(crate) async fn run_session_with_deploy<F, Fut>(
session: Session,
server_args: &str,
auto_install: bool,
quiet: bool,
remote: &RemoteTarget,
os: &str,
arch: &str,
remote_path: &str,
binaries_dir: Option<&Path>,
client: &mut RemoteClient,
jump: Option<&JumpHost>,
cancellable: bool,
run: F,
) -> anyhow::Result<SyncStats>
where
F: Fn(
Box<dyn AsyncWrite + Unpin + Send>,
Box<dyn AsyncRead + Unpin + Send>,
) -> Fut,
Fut: std::future::Future<Output = anyhow::Result<SyncStats>>,
{
let (transfer_failed, result, mut handle) = run_session_once(session, cancellable, &run).await;
let rejected = result.as_ref().is_err_and(|e| {
e.downcast_ref::<crate::Error>()
.is_some_and(|ce| matches!(ce, crate::Error::HandshakeRejected { .. }))
});
let child_died = handle.child_exited_nonzero().await;
if result.is_err() && auto_install && (rejected || (child_died && transfer_failed)) {
let source = deploy_source(os, arch, binaries_dir)?;
if !quiet {
println!(
"Deploying cp2 v{} to {remote} ({remote_path})...",
env!("CARGO_PKG_VERSION")
);
}
let session = client
.deploy_and_open_session(remote, remote_path, server_args, &source, jump)
.await?;
let (_, result, _) = run_session_once(session, cancellable, &run).await;
return result;
}
result
}
fn find_sidecar(candidates: &[&str], binaries_dir: Option<&Path>) -> Option<PathBuf> {
candidates.iter().find_map(|triple| {
let names: Vec<String> = if triple.contains("windows") {
vec![triple.to_string(), format!("{triple}.exe")]
} else {
vec![triple.to_string()]
};
names.into_iter().find_map(|name| {
let near = sidecar_path(&name);
if near.is_file() {
return Some(near);
}
binaries_dir
.is_some_and(|dir| dir.join(format!("cp2-{name}")).is_file())
.then(|| binaries_dir.unwrap().join(format!("cp2-{name}")))
})
})
}
fn print_stats(stats: &SyncStats, transferred_files: usize) {
println!("Statistics:");
println!(" files transferred: {transferred_files}");
println!(" bytes transferred: {}", stats.bytes_transferred);
println!(" files skipped: {}", stats.skipped.len());
let secs = stats.duration.as_secs_f64();
println!(" total time: {secs:.2}s");
}
fn itemize_flag(entry: &ItemizeEntry) -> String {
let k = entry.kind;
match entry.action {
ItemizeAction::Create => format!("c{k}+++++++++"),
ItemizeAction::Update => format!(">{k}.s......."),
ItemizeAction::Delete => "*deleting ".to_string(),
ItemizeAction::Skip => format!(".{k}.........."),
}
}
fn print_itemize(stats: &SyncStats) {
for e in &stats.changes {
println!("{} {}", itemize_flag(e), e.path);
}
}
fn install_progress(options: &mut ExecutorOptions) {
use std::io::IsTerminal;
let interactive = std::io::stdout().is_terminal();
let width = terminal_size().map(|(Width(w), _)| w as usize);
let state = std::sync::Arc::new(std::sync::Mutex::new(ProgressState::default()));
let mp = MultiProgress::with_draw_target(ProgressDrawTarget::stdout_with_hz(10));
options.progress = Some(Arc::new(move |path: &str, done: u64, total: u64, files_total: u64| {
use std::fmt::Write as _;
let mut st = state.lock().unwrap();
let now = std::time::Instant::now();
let (delta, started) = {
let prog = st
.files
.entry(path.to_string())
.or_insert_with(|| ProgressFile::new(now));
let delta = done.saturating_sub(prog.done);
prog.done = done;
(delta, prog.started)
};
st.bytes = st.bytes.saturating_add(delta);
if done >= total {
if st.completed_paths.insert(path.to_string()) {
st.completed += 1;
let completed = st.completed;
let speed = st.windowed_speed(now);
let elapsed = now.saturating_duration_since(started);
let row = fit_row(
width,
&format!("[{completed}/{files_total}] "),
path,
&format!(
" 100% | {} | {} | {}",
human_speed(speed),
human_bytes(total),
fmt_elapsed(elapsed)
),
);
if interactive {
let _ = mp.println(row);
if completed == files_total {
if let Some(bar) = &st.bar {
bar.finish_and_clear();
}
}
} else {
let _ = writeln!(st.pending, "{row}");
if st.pending.len() >= PROGRESS_BATCH_BYTES
|| now.duration_since(st.last_flush) >= PROGRESS_BATCH_AGE
|| st.completed == files_total
{
use std::io::Write;
let _ = write!(std::io::stdout(), "{}", st.pending);
st.pending.clear();
st.last_flush = now;
}
}
}
} else if interactive {
let pct = done
.checked_mul(100)
.and_then(|d| d.checked_div(total))
.unwrap_or(100);
let speed = st.windowed_speed(now);
let remaining = files_total.saturating_sub(st.completed);
let ordinal = format!("[{}/{}] ", st.completed + 1, files_total);
let suffix = format!(
" {}% {} / {} {} ({} left)",
pct,
human_bytes(done),
human_bytes(total),
human_speed(speed),
remaining
);
let msg = match width {
Some(w) => {
let fixed = ordinal.width() + suffix.width();
format!("{}{suffix}", elide_path(path, w.saturating_sub(fixed)))
}
None => format!("{path}{suffix}"),
};
if st.bar.is_none() {
let bar = ProgressBar::with_draw_target(
Some(files_total),
ProgressDrawTarget::hidden(),
);
bar.set_style(ProgressStyle::with_template("[{pos}/{len}] {msg}").expect(
"static template",
));
bar.set_position(st.completed + 1);
bar.set_message(msg.clone());
st.bar = Some(mp.add(bar));
}
if let Some(bar) = st.bar.as_ref() {
bar.set_position(st.completed + 1);
bar.set_message(msg);
}
}
}));
}
struct ProgressState {
start: std::time::Instant,
completed: u64,
bytes: u64,
files: std::collections::HashMap<String, ProgressFile>,
completed_paths: std::collections::HashSet<String>,
bar: Option<ProgressBar>,
last_speed_bytes: u64,
last_speed_at: std::time::Instant,
pending: String,
last_flush: std::time::Instant,
}
struct ProgressFile {
done: u64,
started: std::time::Instant,
}
impl ProgressFile {
fn new(now: std::time::Instant) -> Self {
Self { done: 0, started: now }
}
}
impl ProgressState {
#[expect(clippy::cast_precision_loss)]
fn windowed_speed(&mut self, now: std::time::Instant) -> f64 {
let delta_bytes = self.bytes.saturating_sub(self.last_speed_bytes);
let delta_secs = now.duration_since(self.last_speed_at).as_secs_f64();
self.last_speed_bytes = self.bytes;
self.last_speed_at = now;
if delta_secs > 0.0 {
delta_bytes as f64 / delta_secs
} else {
self.bytes as f64 / self.start.elapsed().as_secs_f64()
}
}
}
const PROGRESS_BATCH_BYTES: usize = 32 * 1024;
const PROGRESS_BATCH_AGE: std::time::Duration = std::time::Duration::from_millis(250);
impl Default for ProgressState {
fn default() -> Self {
let now = std::time::Instant::now();
Self {
start: now,
completed: 0,
bytes: 0,
files: std::collections::HashMap::new(),
completed_paths: std::collections::HashSet::new(),
bar: None,
last_speed_bytes: 0,
last_speed_at: now,
pending: String::new(),
last_flush: now,
}
}
}
fn human_speed(bytes_per_sec: f64) -> String {
const UNITS: [&str; 5] = ["B", "K", "M", "G", "T"];
let mut value = bytes_per_sec;
let mut unit = 0;
while value >= 1024.0 && unit < UNITS.len() - 1 {
value /= 1024.0;
unit += 1;
}
format!("{value:.1}{}/s", UNITS[unit])
}
fn fmt_elapsed(d: std::time::Duration) -> String {
let secs = d.as_secs();
format!("{:02}m{:02}s", secs / 60, secs % 60)
}
fn fit_row(width: Option<usize>, prefix: &str, path: &str, suffix: &str) -> String {
let Some(w) = width else {
return format!("{prefix}{path}{suffix}");
};
let fixed = UnicodeWidthStr::width(prefix) + UnicodeWidthStr::width(suffix);
format!("{prefix}{}{suffix}", elide_path(path, w.saturating_sub(fixed)))
}
fn elide_path(path: &str, available: usize) -> String {
use unicode_width::UnicodeWidthChar;
if UnicodeWidthStr::width(path) <= available || available <= 2 {
return path.to_string();
}
let budget = available - 1;
let head = budget * 2 / 5;
let tail = budget - head;
let mut head_s = String::new();
let mut w = 0;
for c in path.chars() {
w += UnicodeWidthChar::width(c).unwrap_or(0);
if w > head {
break;
}
head_s.push(c);
}
let mut tail_s = String::new();
let mut w = 0;
for c in path.chars().rev() {
w += UnicodeWidthChar::width(c).unwrap_or(0);
if w > tail {
break;
}
tail_s.push(c);
}
format!("{head_s}…{}", tail_s.chars().rev().collect::<String>())
}
fn human_bytes(n: u64) -> String {
const UNITS: [&str; 5] = ["B", "K", "M", "G", "T"];
if n < 1024 {
return format!("{n}B");
}
#[expect(clippy::cast_precision_loss)]
let mut value = n as f64;
let mut unit = 0;
while value >= 1024.0 && unit < UNITS.len() - 1 {
value /= 1024.0;
unit += 1;
}
format!("{value:.1}{}", UNITS[unit])
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn itemize_flags_match_rsync_shape() {
let e = |action, kind| ItemizeEntry::new(action, "p".to_string(), kind);
assert_eq!(itemize_flag(&e(ItemizeAction::Create, 'f')), "cf+++++++++");
assert_eq!(itemize_flag(&e(ItemizeAction::Create, 'd')), "cd+++++++++");
assert_eq!(itemize_flag(&e(ItemizeAction::Create, 'L')), "cL+++++++++");
assert_eq!(itemize_flag(&e(ItemizeAction::Update, 'f')), ">f.s.......");
assert_eq!(itemize_flag(&e(ItemizeAction::Delete, 'd')), "*deleting ");
assert_eq!(itemize_flag(&e(ItemizeAction::Skip, 'f')), ".f..........");
}
#[test]
fn server_args_flags_and_values_are_separate_tokens() {
let options = ExecutorOptions {
jobs: Some(4),
max_delete: Some(10),
storage: crate::platform::storage::StoragePreference::Ssd,
delete: true,
..ExecutorOptions::default()
};
let args = server_args(&options);
assert!(args.windows(2).any(|w| w == ["--jobs", "4"]), "{args:?}");
assert!(args.windows(2).any(|w| w == ["--max-delete", "10"]), "{args:?}");
assert!(args.windows(2).any(|w| w == ["--storage", "ssd"]), "{args:?}");
assert!(args.iter().any(|a| a == "--delete"), "{args:?}");
assert!(args.iter().all(|a| !a.contains(' ')), "{args:?}");
}
#[test]
fn find_sidecar_accepts_release_names() {
let dir = tempfile::tempdir().unwrap();
let exe = dir.path().join("cp2-x86_64-pc-windows-gnu.exe");
fs::write(&exe, b"PE").unwrap();
let found = find_sidecar(&["x86_64-pc-windows-msvc", "x86_64-pc-windows-gnu"], Some(dir.path()))
.expect("the .exe sidecar must be found");
assert_eq!(found, exe);
let plain = dir.path().join("cp2-aarch64-unknown-linux-musl");
fs::write(&plain, b"ELF").unwrap();
let found = find_sidecar(&["aarch64-unknown-linux-musl"], Some(dir.path())).unwrap();
assert_eq!(found, plain);
let empty = tempfile::tempdir().unwrap();
assert!(find_sidecar(&["x86_64-pc-windows-gnu"], Some(empty.path())).is_none());
}
#[test]
fn static_prefix_dir_cases() {
assert_eq!(static_prefix_dir("./*"), PathBuf::new());
assert_eq!(static_prefix_dir("*.rs"), PathBuf::new());
assert_eq!(static_prefix_dir("src/*"), PathBuf::from("src"));
assert_eq!(static_prefix_dir("src/**/x.rs"), PathBuf::from("src"));
assert_eq!(static_prefix_dir("sub/dir/*.rs"), PathBuf::from("sub/dir"));
assert_eq!(static_prefix_dir("src/foo*"), PathBuf::from("src"));
assert_eq!(static_prefix_dir("src/foo[ab].rs"), PathBuf::from("src"));
assert_eq!(static_prefix_dir("/abs/path/x?"), PathBuf::from("/abs/path"));
}
#[test]
fn expand_source_literal_path_wins() {
assert!(expand_source("/nonexistent/plain/path").unwrap().is_none());
#[cfg(unix)]
{
let dir = tempfile::tempdir().unwrap();
fs::write(dir.path().join("a*b"), b"x").unwrap();
let literal = format!("{}/a*b", dir.path().display());
assert!(expand_source(&literal).unwrap().is_none());
}
}
#[test]
fn expand_source_no_matches_errors() {
let dir = tempfile::tempdir().unwrap();
let pattern = format!("{}/no*.rs", dir.path().display());
let err = expand_source(&pattern).unwrap_err().to_string();
assert!(err.contains("no files match"), "{err}");
assert!(err.contains(&pattern), "{err}");
}
#[test]
fn expand_source_returns_base_and_sorted_matches() {
let dir = tempfile::tempdir().unwrap();
fs::write(dir.path().join("b.rs"), b"y").unwrap();
fs::write(dir.path().join("a.rs"), b"x").unwrap();
fs::write(dir.path().join("c.txt"), b"z").unwrap();
let pattern = format!("{}/*.rs", dir.path().display());
let (base, roots) = expand_source(&pattern).unwrap().unwrap();
assert_eq!(base, dir.path());
let names: Vec<&str> = roots
.iter()
.map(|p| p.file_name().unwrap().to_str().unwrap())
.collect();
assert_eq!(names, vec!["a.rs", "b.rs"]);
}
#[test]
fn parse_file_list_is_strictly_newline_delimited() {
let list = "a.txt\n\nsub/b.txt\n";
assert_eq!(parse_file_list(list), vec!["a.txt", "sub/b.txt"]);
let crlf = "a.txt\r\nb.txt\r\n";
assert_eq!(parse_file_list(crlf), vec!["a.txt", "b.txt"]);
assert_eq!(
parse_file_list("a, b.txt\n c.txt \n"),
vec!["a, b.txt", "c.txt"]
);
assert!(parse_file_list("\n\n").is_empty());
}
#[cfg(unix)]
#[test]
fn path_root_is_the_filesystem_root_on_unix() {
assert_eq!(path_root(Path::new("/data/a.txt")), Path::new("/"));
assert_eq!(path_root(Path::new("/")), Path::new("/"));
assert_eq!(path_root(Path::new("/data")), Path::new("/"));
}
#[cfg(windows)]
#[test]
fn path_root_is_the_drive_root_on_windows() {
assert_eq!(path_root(Path::new(r"D:\data\a.txt")), Path::new(r"D:\"));
assert_eq!(path_root(Path::new(r"D:\data")), Path::new(r"D:\"));
assert_eq!(path_root(Path::new(r"C:\")), Path::new(r"C:\"));
}
#[test]
fn expand_source_matches_dotfiles() {
let dir = tempfile::tempdir().unwrap();
fs::write(dir.path().join(".hidden"), b"h").unwrap();
let pattern = format!("{}/*", dir.path().display());
let (_base, roots) = expand_source(&pattern).unwrap().unwrap();
assert!(
roots
.iter()
.any(|p| p.file_name().unwrap().to_str() == Some(".hidden"))
);
}
#[test]
fn elide_path_fits_or_keeps_the_name_tail() {
assert_eq!(elide_path("short.txt", 40), "short.txt");
let long = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb/name.txt";
for budget in [16usize, 24, 32, 48] {
let elided = elide_path(long, budget);
let w = elided.width();
assert!(w <= budget, "width {w} > {budget}: {elided}");
assert!(elided.ends_with("name.txt"), "name tail lost: {elided}");
assert!(elided.contains('…') || long.width() <= budget);
}
let tiny = elide_path("12345678", 4);
assert_eq!(tiny.width(), 4, "{tiny}");
}
#[test]
fn fit_row_budgets_the_fixed_parts() {
let row = fit_row(
Some(40),
"[1/5] ",
"envs/miniforge3/pyarrow/libarrow.so.2400",
" 100% | 9.3M/s | 52.7M | 00m04s",
);
assert!(row.width() <= 40, "{row}");
assert!(row.starts_with("[1/5] "), "{row}");
assert!(row.ends_with("100% | 9.3M/s | 52.7M | 00m04s"), "{row}");
let unbounded = fit_row(
None,
"[1/5] ",
"envs/miniforge3/pyarrow/libarrow.so.2400",
" 100%",
);
assert!(unbounded.contains("envs/miniforge3/pyarrow/libarrow.so.2400"), "{unbounded}");
}
}