use crate::cli::Cli;
use crate::protocol::TargetOs;
use crate::sync::watcher::{SYNC_ERROR_BACKOFF, start_watcher};
use crate::sync::{Executor, ExecutorOptions, SyncStats};
use crate::target::RemoteTarget;
use crate::transport::{JumpHost, RemoteClient};
use anyhow::Result;
use std::path::Path;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::mpsc::UnboundedReceiver;
pub(crate) async fn watch_push(
cli: &Cli,
remote: &RemoteTarget,
src_path: &Path,
options: &ExecutorOptions,
client: RemoteClient,
jump: Option<JumpHost>,
) -> Result<()> {
let client = Arc::new(tokio::sync::Mutex::new(client));
let server_args = super::sync::server_args(options).join(" ");
let ensured = super::sync::ensure_and_open(
remote,
cli.remote_path.as_deref(),
cli.binaries_dir.as_deref(),
!cli.no_auto_install,
cli.quiet,
&server_args,
&mut *client.lock().await,
jump.as_ref(),
)
.await?;
let (os, remote_path) = match &ensured {
super::sync::Ensured::Merged {
os,
remote_path,
..
}
| super::sync::Ensured::Classic {
os,
remote_path,
..
} => (os.clone(), remote_path.clone()),
};
let arch = match &ensured {
super::sync::Ensured::Merged { arch, .. } => Some(arch.clone()),
super::sync::Ensured::Classic { .. } => None,
};
let first_session = match ensured {
super::sync::Ensured::Merged { session, .. } => Some(session),
super::sync::Ensured::Classic { .. } => None,
};
let first_session = Arc::new(tokio::sync::Mutex::new(first_session));
let auto_install = !cli.no_auto_install;
let binaries_dir = cli.binaries_dir.clone();
let arch = Arc::new(arch);
let mut options = options.clone();
options.progress = None;
let delay = Duration::from_millis(cli.watch_delay);
let src_path = Arc::new(src_path.to_path_buf());
let remote = Arc::new(remote.clone());
let remote_path = Arc::new(remote_path.clone());
let os = Arc::new(os.clone());
let options = Arc::new(options);
run_bounded(
Box::pin(async move {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<()>();
let _watcher = start_watcher(&src_path, tx)?;
if !cli.quiet {
println!("Initial sync...");
}
let client = Arc::clone(&client);
let initial = initial_sync(|| {
let client = Arc::clone(&client);
let remote = Arc::clone(&remote);
let src_path = Arc::clone(&src_path);
let options = Arc::clone(&options);
let os = Arc::clone(&os);
let remote_path = Arc::clone(&remote_path);
let arch = Arc::clone(&arch);
let first_session = Arc::clone(&first_session);
let server_args = server_args.clone();
let binaries_dir = binaries_dir.clone();
let quiet = cli.quiet;
let jump = jump.clone();
async move {
let mut options = (*options).clone();
options.target_os = TargetOs::from_os_name(&os);
if let Some(session) = first_session.lock().await.take() {
let Some(arch) = arch.as_ref() else {
return Err(anyhow::anyhow!(
"merged session without a resolved architecture"
));
};
return super::sync::run_session_with_deploy(
session,
&server_args,
auto_install,
quiet,
&remote,
&os,
arch,
&remote_path,
binaries_dir.as_deref(),
&mut *client.lock().await,
jump.as_ref(),
false,
|send, recv| {
let src_path = (*src_path).clone();
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;
}
super::sync::push_over_ssh(
&remote,
&src_path,
&options,
&os,
&remote_path,
&mut *client.lock().await,
jump.as_ref(),
)
.await
}
})
.await?;
if matches!(initial, InitialSync::Interrupted) {
return Ok(());
}
if !cli.quiet {
println!(
"Watching {} → {remote} (debounce {}ms). Ctrl-C to stop.",
src_path.display(),
cli.watch_delay
);
}
watch_loop(&mut rx, delay, SYNC_ERROR_BACKOFF, move || {
let os = Arc::clone(&os);
let remote = Arc::clone(&remote);
let remote_path = Arc::clone(&remote_path);
let src_path = Arc::clone(&src_path);
let options = Arc::clone(&options);
let client = Arc::clone(&client);
let jump = jump.clone();
async move {
let start = Instant::now();
let stats = super::sync::push_over_ssh(
&remote,
&src_path,
&options,
&os,
&remote_path,
&mut *client.lock().await,
jump.as_ref(),
)
.await?;
tracing::info!(
"watch sync: {} files, {} bytes in {:?}",
stats.files_sent,
stats.bytes_transferred,
start.elapsed()
);
Ok(stats)
}
})
.await
}),
cli.watch,
cli.quiet,
)
.await
}
enum InitialSync {
Completed,
Interrupted,
}
async fn initial_sync<F, Fut>(mut sync: F) -> Result<InitialSync>
where
F: FnMut() -> Fut,
Fut: std::future::Future<Output = Result<SyncStats>>,
{
loop {
match sync().await {
Ok(_) => return Ok(InitialSync::Completed),
Err(e) => {
tracing::warn!(
"initial sync failed: {e}; retrying in {:?}",
SYNC_ERROR_BACKOFF
);
tokio::select! {
() = tokio::time::sleep(SYNC_ERROR_BACKOFF) => {}
_ = tokio::signal::ctrl_c() => return Ok(InitialSync::Interrupted),
}
}
}
}
}
async fn run_bounded<F>(run: F, max: Option<Duration>, quiet: bool) -> Result<()>
where
F: std::future::Future<Output = Result<()>>,
{
match max {
Some(cap) => match tokio::time::timeout(cap, run).await {
Ok(result) => result,
Err(_elapsed) => {
if !quiet {
println!("Watch duration reached; stopping.");
}
Ok(())
}
},
None => run.await,
}
}
async fn watch_loop<F, Fut>(
changes: &mut UnboundedReceiver<()>,
delay: Duration,
backoff: Duration,
mut sync: F,
) -> Result<()>
where
F: FnMut() -> Fut,
Fut: std::future::Future<Output = Result<SyncStats>>,
{
loop {
tokio::select! {
res = changes.recv() => {
if res.is_none() {
return Ok(()); }
}
_ = tokio::signal::ctrl_c() => return Ok(()),
}
match crate::sync::watcher::wait_debounce(changes, delay, async {
tokio::signal::ctrl_c().await.unwrap_or(());
Ok(())
})
.await?
{
crate::sync::watcher::BurstWait::Aborted => return Ok(()),
crate::sync::watcher::BurstWait::Ready
| crate::sync::watcher::BurstWait::ChannelClosed => {}
}
loop {
match sync().await {
Ok(_) => {
let mut dirty = false;
loop {
match changes.try_recv() {
Ok(()) => dirty = true,
Err(tokio::sync::mpsc::error::TryRecvError::Empty) => break,
Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) => {
return Ok(())
}
}
}
if !dirty {
break;
}
}
Err(e) => {
tracing::warn!("watch sync failed: {e}; retrying in {:?}", backoff);
tokio::select! {
() = tokio::time::sleep(backoff) => {}
_ = tokio::signal::ctrl_c() => return Ok(()),
}
}
}
}
}
}
pub(crate) async fn watch_pull(
cli: &Cli,
remote: &RemoteTarget,
dst_path: &Path,
options: &ExecutorOptions,
mut client: RemoteClient,
jump: Option<JumpHost>,
) -> Result<()> {
let server_args = super::sync::server_args(options).join(" ");
let ensured = super::sync::ensure_and_open(
remote,
cli.remote_path.as_deref(),
cli.binaries_dir.as_deref(),
!cli.no_auto_install,
cli.quiet,
&server_args,
&mut client,
jump.as_ref(),
)
.await?;
let mut options = options.clone();
options.progress = None;
if !cli.quiet {
println!(
"Watching {remote} → {} (server-driven, debounce {}ms). Ctrl-C to stop.",
dst_path.display(),
cli.watch_delay
);
}
let delay = Duration::from_millis(cli.watch_delay);
let remote = remote.clone();
let dst_path = dst_path.to_path_buf();
let auto_install = !cli.no_auto_install;
let binaries_dir = cli.binaries_dir.clone();
match ensured {
super::sync::Ensured::Merged {
os,
arch,
remote_path,
session,
} => run_bounded(
Box::pin(async move {
super::sync::run_session_with_deploy(
session,
&server_args,
auto_install,
cli.quiet,
&remote,
&os,
&arch,
&remote_path,
binaries_dir.as_deref(),
&mut client,
jump.as_ref(),
true,
|send, recv| {
let dst_path = dst_path.clone();
let options = options.clone();
async move {
let mut executor = Executor::new(send, recv);
let result = executor
.pull_watch(&dst_path, &options, delay)
.await
.map_err(anyhow::Error::new);
drop(executor);
result
}
},
)
.await
.map(|_| ())
}),
cli.watch,
cli.quiet,
)
.await,
super::sync::Ensured::Classic { os, remote_path } => {
let remote_path = remote_path.clone();
let os = os.clone();
run_bounded(
Box::pin(async move {
let session = client
.open_session(&remote, &remote_path, &os, &server_args, jump.as_ref())
.await?;
let (send, recv, mut handle) = session.into_parts();
let mut executor = Executor::new(send, recv);
let result = executor
.pull_watch(&dst_path, &options, delay)
.await
.map_err(anyhow::Error::new);
drop(executor);
handle.finish(result.map(|_| ())).await
}),
cli.watch,
cli.quiet,
)
.await
}
}
}
pub(crate) async fn watch_local(
cli: &Cli,
src_path: &Path,
dst_path: &Path,
options: &ExecutorOptions,
) -> Result<()> {
tokio::fs::create_dir_all(dst_path)
.await
.map_err(anyhow::Error::new)?;
let mut options = options.clone();
options.progress = None;
let delay = Duration::from_millis(cli.watch_delay);
let src_path = Arc::new(src_path.to_path_buf());
let dst_path = Arc::new(dst_path.to_path_buf());
let options = Arc::new(options);
run_bounded(
async move {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<()>();
let _watcher = start_watcher(&src_path, tx)?;
if !cli.quiet {
println!("Initial sync...");
}
let initial = initial_sync(|| push_local_over_server(&src_path, &dst_path, &options)).await?;
if matches!(initial, InitialSync::Interrupted) {
return Ok(());
}
if !cli.quiet {
println!(
"Watching {} → {} (debounce {}ms). Ctrl-C to stop.",
src_path.display(),
dst_path.display(),
cli.watch_delay
);
}
watch_loop(&mut rx, delay, SYNC_ERROR_BACKOFF, move || {
let src_path = Arc::clone(&src_path);
let dst_path = Arc::clone(&dst_path);
let options = Arc::clone(&options);
async move { push_local_over_server(&src_path, &dst_path, &options).await }
})
.await
},
cli.watch,
cli.quiet,
)
.await
}
pub(crate) async fn push_local_over_server(
src: &Path,
dst: &Path,
options: &ExecutorOptions,
) -> Result<SyncStats> {
let (mut child, send, recv) = spawn_server_child(dst, options)?;
let mut executor = Executor::new(send, recv);
let result = executor.push(src, options).await.map_err(anyhow::Error::new);
drop(executor);
finish_server_child(&mut child, result).await
}
pub(crate) async fn push_multi_local_over_server(
base: &Path,
roots: &[std::path::PathBuf],
dst: &Path,
options: &ExecutorOptions,
) -> Result<SyncStats> {
let (mut child, send, recv) = spawn_server_child(dst, options)?;
let mut executor = Executor::new(send, recv);
let result = executor
.push_multi(base, roots, options)
.await
.map_err(anyhow::Error::new);
drop(executor);
finish_server_child(&mut child, result).await
}
type ServerChild = (
tokio::process::Child,
Box<dyn tokio::io::AsyncWrite + Unpin + Send>,
Box<dyn tokio::io::AsyncRead + Unpin + Send>,
);
fn spawn_server_child(dst: &Path, options: &ExecutorOptions) -> Result<ServerChild> {
let dst = dst.to_path_buf();
std::fs::create_dir_all(&dst).map_err(anyhow::Error::new)?;
let bin = std::env::current_exe().map_err(anyhow::Error::new)?;
let mut cmd = tokio::process::Command::new(&bin);
cmd.arg("--server");
cmd.args(super::sync::server_args(options));
let mut child = cmd
.current_dir(&dst)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::inherit())
.spawn()
.map_err(anyhow::Error::new)?;
let stdin = child
.stdin
.take()
.ok_or_else(|| anyhow::anyhow!("server child stdin unavailable"))?;
let stdout = child
.stdout
.take()
.ok_or_else(|| anyhow::anyhow!("server child stdout unavailable"))?;
crate::platform::fs::enlarge_pipe(&stdin);
crate::platform::fs::enlarge_pipe(&stdout);
Ok((child, Box::new(stdin), Box::new(stdout)))
}
async fn finish_server_child<T>(
child: &mut tokio::process::Child,
result: anyhow::Result<T>,
) -> anyhow::Result<T> {
let status = child.wait().await.map_err(anyhow::Error::new)?;
if !status.success() {
anyhow::bail!("cp2 --server child exited with {status}");
}
result
}
#[cfg(test)]
fn test_stats() -> SyncStats {
use std::sync::atomic::{AtomicU64, Ordering};
static N: AtomicU64 = AtomicU64::new(0);
SyncStats {
files_sent: 0,
files_received: 0,
bytes_transferred: N.fetch_add(1, Ordering::SeqCst),
duration: Duration::ZERO,
changes: Vec::new(),
skipped: Vec::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};
fn test_delay() -> Duration {
Duration::from_millis(50)
}
#[tokio::test]
async fn burst_coalesces_to_one_sync() {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<()>();
let count = Arc::new(AtomicUsize::new(0));
let sync_count = count.clone();
let task = tokio::spawn(async move {
watch_loop(&mut rx, test_delay(), Duration::from_millis(10), move || {
let sync_count = sync_count.clone();
async move {
sync_count.fetch_add(1, AtomicOrdering::SeqCst);
Ok(test_stats())
}
})
.await
});
for _ in 0..5 {
tx.send(()).unwrap();
}
drop(tx); task.await.unwrap().unwrap();
assert_eq!(count.load(AtomicOrdering::SeqCst), 1);
}
#[tokio::test]
async fn changes_during_sync_trigger_immediate_resync() {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<()>();
let count = Arc::new(AtomicUsize::new(0));
let sync_count = count.clone();
let burst_tx = tx.clone();
let task = tokio::spawn(async move {
watch_loop(&mut rx, test_delay(), Duration::from_millis(10), move || {
let sync_count = sync_count.clone();
let burst_tx = burst_tx.clone();
async move {
let n = sync_count.fetch_add(1, AtomicOrdering::SeqCst);
if n == 0 {
let _ = burst_tx.send(());
}
Ok(test_stats())
}
})
.await
});
tx.send(()).unwrap();
let deadline = tokio::time::Instant::now() + Duration::from_millis(1_500);
while count.load(AtomicOrdering::SeqCst) < 2 && tokio::time::Instant::now() < deadline {
tokio::time::sleep(Duration::from_millis(10)).await;
}
task.abort();
assert_eq!(count.load(AtomicOrdering::SeqCst), 2);
}
#[tokio::test]
async fn burst_during_sync_coalesces_to_one_resync() {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<()>();
let count = Arc::new(AtomicUsize::new(0));
let sync_count = count.clone();
let burst_tx = tx.clone();
let task = tokio::spawn(async move {
watch_loop(&mut rx, test_delay(), Duration::from_millis(10), move || {
let sync_count = sync_count.clone();
let burst_tx = burst_tx.clone();
async move {
let n = sync_count.fetch_add(1, AtomicOrdering::SeqCst);
if n == 0 {
for _ in 0..5 {
let _ = burst_tx.send(());
}
}
Ok(test_stats())
}
})
.await
});
tx.send(()).unwrap();
let deadline = tokio::time::Instant::now() + Duration::from_millis(1_500);
while count.load(AtomicOrdering::SeqCst) < 2 && tokio::time::Instant::now() < deadline {
tokio::time::sleep(Duration::from_millis(10)).await;
}
task.abort();
assert_eq!(
count.load(AtomicOrdering::SeqCst),
2,
"a 5-event burst must drain into one resync"
);
}
#[tokio::test]
async fn failed_sync_retries_same_changes() {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<()>();
let count = Arc::new(AtomicUsize::new(0));
let sync_count = count.clone();
let task = tokio::spawn(async move {
watch_loop(&mut rx, test_delay(), Duration::from_millis(10), move || {
let sync_count = sync_count.clone();
async move {
let n = sync_count.fetch_add(1, AtomicOrdering::SeqCst);
if n == 0 {
return Err(anyhow::anyhow!("transient failure"));
}
Ok(test_stats())
}
})
.await
});
tx.send(()).unwrap();
drop(tx);
task.await.unwrap().unwrap();
assert_eq!(count.load(AtomicOrdering::SeqCst), 2);
}
}