feat: cancel a download from the TUI, the CLI, and the daemon
Closes #54. A download can now be cancelled whether it is queued, in progress, or paused. Cancelled is a terminal status: the store refuses to overwrite it, the status channel relays nothing after it, the partial file is deleted, and the transfer thread releases its socket at its next one-second poll instead of the 30s read deadline. A queued download that is cancelled declines the peer's later offer with TransferResponse "Cancelled", and an inbound file connection for it is refused. A fresh download of the same file evicts the dead row so it is not shadowed, and a cancelled transfer is not re-queued after a restart. Surfaces: Client::cancel_download, download.cancel over RPC, `transfer cancel <user> <path>` on the CLI (which also stops an upload by that name), and x in the transfers pane, where d dismisses the row.
This commit is contained in:
@@ -3,6 +3,7 @@ use super::{
|
||||
Receiver, Result, RwLock, RwLockExt, Sender, error, info, mpsc,
|
||||
next_download_token,
|
||||
};
|
||||
use crate::peer::part_path_of;
|
||||
|
||||
impl Client {
|
||||
#[must_use]
|
||||
@@ -35,6 +36,24 @@ impl Client {
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn cancel_download(&self, username: &str, filename: &str) -> bool {
|
||||
let cancelled = match self.context.write_safe() {
|
||||
Ok(mut ctx) => ctx.downloads.cancel_by_file(username, filename),
|
||||
Err(e) => {
|
||||
error!("[client] cancel_download: {}", e);
|
||||
None
|
||||
}
|
||||
};
|
||||
let Some(download) = cancelled else {
|
||||
return false;
|
||||
};
|
||||
if let Some(part) = part_path_of(&download) {
|
||||
let _ = std::fs::remove_file(part);
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn remove_queued_download(
|
||||
&self,
|
||||
|
||||
@@ -6,6 +6,7 @@ use super::{
|
||||
thread, trace, warn,
|
||||
};
|
||||
use crate::message::server::MessageFactory;
|
||||
use crate::peer::DownloadError;
|
||||
|
||||
const CONNECT_SWEEP_INTERVAL: Duration = Duration::from_secs(1);
|
||||
|
||||
@@ -148,6 +149,7 @@ impl Client {
|
||||
download.status,
|
||||
DownloadStatus::InProgress { .. }
|
||||
| DownloadStatus::Completed
|
||||
| DownloadStatus::Cancelled
|
||||
) {
|
||||
debug!(
|
||||
"[client] transfer token {} already claimed; \
|
||||
@@ -210,6 +212,7 @@ impl Client {
|
||||
download.size, filename
|
||||
);
|
||||
}
|
||||
Err(DownloadError::Cancelled) => {}
|
||||
Err(e) => {
|
||||
let reason = Some(e.to_string());
|
||||
let _ = download.sender.send(
|
||||
@@ -363,7 +366,14 @@ impl Client {
|
||||
}
|
||||
});
|
||||
|
||||
if let Some((old_token, download)) = download_to_update
|
||||
let cancelled = download_to_update
|
||||
.as_ref()
|
||||
.is_some_and(|(_, d)| {
|
||||
matches!(d.status, DownloadStatus::Cancelled)
|
||||
});
|
||||
if !cancelled
|
||||
&& let Some((old_token, download)) =
|
||||
download_to_update
|
||||
{
|
||||
trace!(
|
||||
"[client] UpdateDownloadTokens found {old_token}, transfer: {:?}",
|
||||
@@ -386,10 +396,16 @@ impl Client {
|
||||
let registry = context.peer_registry.clone();
|
||||
drop(context);
|
||||
if let Some(registry) = registry {
|
||||
let response =
|
||||
let response = if cancelled {
|
||||
MessageFactory::build_transfer_denial_message(
|
||||
transfer.token,
|
||||
"Cancelled",
|
||||
)
|
||||
} else {
|
||||
MessageFactory::build_transfer_response_message(
|
||||
transfer,
|
||||
);
|
||||
)
|
||||
};
|
||||
let _ = registry.send_to_peer(
|
||||
&username,
|
||||
PeerMessage::SendMessage(response),
|
||||
|
||||
@@ -623,6 +623,71 @@ fn a_replayed_transfer_response_does_not_start_a_second_transfer() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cancelled_download_is_not_started_when_the_peer_allows_it() {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
listener.set_nonblocking(true).unwrap();
|
||||
let port = u32::from(listener.local_addr().unwrap().port());
|
||||
|
||||
let client = Client::new("u", "p");
|
||||
let (sender, _receiver) = mpsc::channel();
|
||||
client.context.write().unwrap().add_download(download(
|
||||
"peer",
|
||||
"f.mp3",
|
||||
9,
|
||||
DownloadStatus::Cancelled,
|
||||
sender,
|
||||
));
|
||||
|
||||
let (ops_tx, ops_rx) = mpsc::channel();
|
||||
Client::listen_to_client_operations(
|
||||
ops_rx,
|
||||
client.context,
|
||||
"u".to_string(),
|
||||
);
|
||||
let peer = Peer::new(
|
||||
"peer".to_string(),
|
||||
ConnectionType::F,
|
||||
"127.0.0.1".to_string(),
|
||||
port,
|
||||
None,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
ops_tx
|
||||
.send(ClientOperation::DownloadFromPeer(9, peer, true))
|
||||
.unwrap();
|
||||
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
assert!(
|
||||
matches!(listener.accept(), Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock),
|
||||
"a cancelled download must not dial the peer"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancel_download_marks_the_store_and_answers_whether_it_matched() {
|
||||
let client = Client::new("u", "p");
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
client.context.write().unwrap().add_download(download(
|
||||
"peer",
|
||||
"f.mp3",
|
||||
9,
|
||||
DownloadStatus::Queued,
|
||||
sender,
|
||||
));
|
||||
|
||||
assert!(client.cancel_download("peer", "f.mp3"));
|
||||
assert!(!client.cancel_download("peer", "f.mp3"));
|
||||
assert!(!client.cancel_download("peer", "other.mp3"));
|
||||
assert!(matches!(receiver.try_recv(), Ok(DownloadStatus::Cancelled)));
|
||||
assert!(matches!(
|
||||
client.get_all_downloads()[0].status,
|
||||
DownloadStatus::Cancelled
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn room_member_stats_are_returned_sorted_and_scoped_to_their_room() {
|
||||
use crate::types::{RoomUserStats, UserStatus};
|
||||
|
||||
Reference in New Issue
Block a user