Skip to content

Commit

Permalink
working on term signal
Browse files Browse the repository at this point in the history
  • Loading branch information
chanderlud committed Jan 3, 2024
1 parent 583b54c commit 6317a23
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
29 changes: 26 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(int_roundings)]

use std::io::BufRead;
use std::net::{IpAddr, SocketAddr};
use std::ops::Not;
use std::path::Path;
Expand All @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() => {
Expand Down Expand Up @@ -622,3 +631,17 @@ async fn hash_file<P: AsRef<Path>>(path: P) -> io::Result<Hash> {

Ok(hasher.finalize())
}

fn wait_for_stop(signal: Arc<Notify>) {
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;
}
}
}
2 changes: 1 addition & 1 deletion src/receiver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ fn free_space(path: &Path) -> Result<u64> {
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
Expand Down

0 comments on commit 6317a23

Please sign in to comment.