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 tokio::io::{AsyncRead, AsyncWrite};
use zeroize::Zeroize;
use std::path::{Path, PathBuf};
use std::sync::Arc;
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);
}
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 !entry.starts_with('/') {
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;
}
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 {
path.ancestors()
.last()
.map_or_else(|| PathBuf::from("/"), Path::to_path_buf)
}
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> = glob::glob(pattern)
.map_err(|e| anyhow::anyhow!("invalid glob pattern '{pattern}': {e}"))?
.collect::<Result<_, _>>()
.map_err(|e| anyhow::anyhow!("glob error for '{pattern}': {e}"))?;
if matches.is_empty() {
anyhow::bail!("no files match source pattern '{pattern}'");
}
matches.sort();
let base = static_prefix_dir(pattern);
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,
|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,
|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,
|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,
|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 names = candidates
.iter()
.map(|t| {
if t.contains("windows") {
format!("`cp2-{t}.exe`")
} else {
format!("`cp2-{t}`")
}
})
.collect::<Vec<_>>()
.join(" or ");
anyhow::bail!(
"remote is {os}/{arch}: download {names} for cp2 v{} from the GitHub releases page and place it next to this binary or in --binaries-dir, or pass --no-auto-install",
env!("CARGO_PKG_VERSION")
)
}
#[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"
)),
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}"
)),
}
}
#[expect(clippy::large_enum_variant, reason = "Merged carries the opened Session by design")]
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,
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 = 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>,
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, &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, &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 state = std::sync::Arc::new(std::sync::Mutex::new(ProgressState::default()));
options.progress = Some(Arc::new(move |path: &str, done: u64, total: u64, files_total: u64| {
use std::fmt::Write as _;
use std::io::Write;
let mut out = std::io::stdout();
let mut st = state.lock().unwrap();
let prev = st.last_done.insert(path.to_string(), done).unwrap_or(0);
st.bytes = st.bytes.saturating_add(done.saturating_sub(prev));
if done >= total {
if st.completed_paths.insert(path.to_string()) {
st.completed += 1;
}
let completed = st.completed;
let _ = writeln!(st.pending, "[{completed}/{files_total}] {path}");
let now = std::time::Instant::now();
if st.pending.len() >= PROGRESS_BATCH_BYTES
|| now.duration_since(st.last_flush) >= PROGRESS_BATCH_AGE
|| st.completed == files_total
{
let _ = write!(out, "{}", st.pending);
st.pending.clear();
st.last_flush = now;
}
} else if interactive {
if !st.pending.is_empty() {
let _ = write!(out, "{}", st.pending);
st.pending.clear();
st.last_flush = std::time::Instant::now();
}
let now = std::time::Instant::now();
if st.last_redraw.is_some_and(|t| now.duration_since(t) < std::time::Duration::from_millis(100))
{
return;
}
st.last_redraw = Some(now);
let pct = done
.checked_mul(100)
.and_then(|d| d.checked_div(total))
.unwrap_or(100);
let delta_bytes = st.bytes.saturating_sub(st.last_speed_bytes);
let delta_secs = now.duration_since(st.last_speed_at).as_secs_f64();
st.last_speed_bytes = st.bytes;
st.last_speed_at = now;
#[expect(clippy::cast_precision_loss)]
let speed = if delta_secs > 0.0 {
delta_bytes as f64 / delta_secs
} else {
st.bytes as f64 / st.start.elapsed().as_secs_f64()
};
let remaining = files_total.saturating_sub(st.completed);
let _ = write!(
out,
"\r[{}/{}] {path} {pct}% {} / {} {} ({} left)",
st.completed + 1,
files_total,
human_bytes(done),
human_bytes(total),
human_speed(speed),
remaining
);
let _ = out.flush();
}
}));
}
struct ProgressState {
start: std::time::Instant,
completed: u64,
bytes: u64,
last_done: std::collections::HashMap<String, u64>,
completed_paths: std::collections::HashSet<String>,
last_redraw: Option<std::time::Instant>,
last_speed_bytes: u64,
last_speed_at: std::time::Instant,
pending: String,
last_flush: std::time::Instant,
}
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,
last_done: std::collections::HashMap::new(),
completed_paths: std::collections::HashSet::new(),
last_redraw: 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 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() {
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());
assert!(expand_source("/nonexistent/plain/path").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"))
);
}
}