From 6317a23d1f99befddfd5d5a7cd3f89f508d6f922 Mon Sep 17 00:00:00 2001 From: chanderlud Date: Wed, 3 Jan 2024 09:32:41 -0800 Subject: [PATCH] working on term signal --- src/main.rs | 29 ++++++++++++++++++++++++++--- src/receiver/mod.rs | 2 +- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 61bb7d6..765f82e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![feature(int_roundings)] +use std::io::BufRead; use std::net::{IpAddr, SocketAddr}; use std::ops::Not; use std::path::Path; @@ -20,7 +21,7 @@ use indicatif::{ProgressBar, ProgressStyle}; use log::{debug, error, info, warn}; use prost::Message; use rpassword::prompt_password; -use russh::{ChannelMsg, Sig}; +use russh::ChannelMsg; use simple_logging::{log_to_file, log_to_stderr}; use tokio::fs::File; use tokio::io::{AsyncReadExt, BufReader}; @@ -127,6 +128,14 @@ async fn main() -> Result<()> { } }); + std::thread::spawn({ + let cancel_signal = cancel_signal.clone(); + + move || { + wait_for_stop(cancel_signal); + } + }); + match options.mode { Mode::Local => { if let Some(path) = &options.log_file { @@ -529,8 +538,8 @@ async fn command_runner( select! { _ = cancel_signal.notified() => { debug!("cancel signal received"); - channel.signal(Sig::INT).await?; - debug!("sent INT signal"); + channel.data(&b"STOP\n"[..]).await?; + debug!("sent STOP message"); break; } message = channel.wait() => { @@ -622,3 +631,17 @@ async fn hash_file>(path: P) -> io::Result { Ok(hasher.finalize()) } + +fn wait_for_stop(signal: Arc) { + let stdin = std::io::stdin(); + let reader = std::io::BufReader::new(stdin); + let mut lines = reader.lines(); + + while let Some(Ok(line)) = lines.next() { + if line.contains("STOP") { + debug!("received STOP message"); + signal.notify_waiters(); + break; + } + } +} \ No newline at end of file diff --git a/src/receiver/mod.rs b/src/receiver/mod.rs index f4fec35..cac18a8 100644 --- a/src/receiver/mod.rs +++ b/src/receiver/mod.rs @@ -413,7 +413,7 @@ fn free_space(path: &Path) -> Result { debug!("getting free space for {:?}", path); let stat = statvfs(&path)?; - Ok(stat.blocks_available() as u64 * stat.fragment_size()) + Ok(stat.blocks_available() as u64 * stat.fragment_size() as u64) } /// returns the amount of free space in bytes for the given path