1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//! SFTP download execution logic.
//!
//! Implements the `Command` trait for `SftpDownloadCommand`, orchestrating
//! the full download lifecycle: SSH connect, SFTP session init, stat remote
//! file, chunked read/write transfer, and cleanup/finalize.
use std::time::{Duration, Instant};
use async_trait::async_trait;
use tracing::{debug, error, info, warn};
use crate::constants;
use crate::engine::command::{Command, CommandStatus};
use crate::error::{Aria2Error, FatalError, RecoverableError, Result};
use crate::filesystem::disk_writer::{DiskWriter, new_sequential_download_writer};
use crate::rate_limiter::{RateLimiter, RateLimiterConfig, ThrottledWriter};
use crate::request::request_group::GroupId;
use crate::util::rwlock_ext::RwLockRecover;
use aria2_protocol::sftp::connection::SshConnection;
use aria2_protocol::sftp::file_ops::{FileOpError, OpenFlags, SftpFileOps};
use aria2_protocol::sftp::session::SftpSession;
use super::types::SftpDownloadCommand;
#[async_trait]
impl Command for SftpDownloadCommand {
/// Execute the SFTP download.
///
/// This is the main entry point called by the download engine. It orchestrates
/// the entire download lifecycle from connection establishment through data
/// transfer to cleanup.
async fn execute(&mut self) -> Result<()> {
// -----------------------------------------------------------------
// Phase 0: Initialization
// -----------------------------------------------------------------
if !self.started {
self.group.recover_mut().start()?;
self.started = true;
}
let in_memory_download = self.group.recover().is_in_memory_download();
debug!(
"[SFTP-CMD] Starting download: {}@{}:{} -> {}",
self.username,
self.host,
self.port,
self.output_path.display()
);
// Ensure output directory exists
if !in_memory_download
&& let Some(parent) = self.output_path.parent()
&& !parent.as_os_str().is_empty()
&& !parent.exists()
{
std::fs::create_dir_all(parent).map_err(|e| {
Aria2Error::Fatal(FatalError::Config(format!(
"Failed to create output directory: {}",
e
)))
})?;
}
// -----------------------------------------------------------------
// Phase 1: SSH Connection
// -----------------------------------------------------------------
let ssh_options = self.build_ssh_options();
let conn_result = SshConnection::connect(ssh_options.clone()).await;
let mut conn = match conn_result {
Ok(c) => c,
Err(e) => {
return Err(Self::map_ssh_error(
&e,
&self.host,
self.port,
&self.remote_path,
));
}
};
info!(
"[SFTP-CMD] SSH connected: {}@{}:{}",
self.username, self.host, self.port
);
// -----------------------------------------------------------------
// Phase 2: SFTP Session Initialization
// -----------------------------------------------------------------
let session_result = SftpSession::open(&mut conn).await;
let session: SftpSession = match session_result {
Ok(s) => s,
Err(e) => {
// Attempt graceful disconnect before returning error
let _ = conn.disconnect().await;
return Err(Aria2Error::Recoverable(
RecoverableError::TemporaryNetworkFailure {
message: format!("SFTP session init failed: {}", e),
},
));
}
};
debug!(
"[SFTP-CMD] SFTP session established (v{})",
session.server_version()
);
// -----------------------------------------------------------------
// Phase 3: Stat Remote File
// -----------------------------------------------------------------
let ops = SftpFileOps::new(&session);
let file_attrs = match ops.stat(&self.remote_path).await {
Ok(attrs) => attrs,
Err(e) => {
let _ = conn.disconnect().await;
let err = FileOpError::from(e);
return Err(Self::map_file_op_error(&err, &self.host, &self.remote_path));
}
};
if !file_attrs.is_regular_file {
let _ = conn.disconnect().await;
return Err(Aria2Error::Fatal(FatalError::FileNotFound {
path: format!("{} (not a regular file)", self.remote_path),
}));
}
let total_length = file_attrs.size;
info!(
"[SFTP-CMD] Remote file size: {} bytes ({:.2} MB)",
total_length,
total_length as f64 / (1024.0 * 1024.0)
);
// Update RequestGroup with total length and recover a local prefix.
// SFTP reads are positioned, so an existing partial file can be resumed
// without downloading or rewriting its already-present prefix.
let existing_length = if in_memory_download {
0
} else {
tokio::fs::metadata(&self.output_path)
.await
.map(|metadata| metadata.len())
.unwrap_or(0)
};
self.completed_bytes = Self::validate_resume_offset(existing_length, total_length)?;
{
let g = self.group.recover();
g.set_total_length(total_length);
g.update_progress(existing_length);
}
// -----------------------------------------------------------------
// Phase 4: Open Remote File for Reading
// -----------------------------------------------------------------
let mut remote_file = match ops.open(&self.remote_path, OpenFlags::readonly(), 0).await {
Ok(f) => f,
Err(e) => {
let _ = conn.disconnect().await;
let err = FileOpError::from(e);
return Err(Self::map_file_op_error(&err, &self.host, &self.remote_path));
}
};
// -----------------------------------------------------------------
// Phase 5: Prepare Local Disk Writer
// -----------------------------------------------------------------
let raw_writer = new_sequential_download_writer(
&self.output_path,
in_memory_download,
self.completed_bytes,
Some(total_length),
);
// Apply rate limiting if configured
let rate_limit = {
let g = self.group.recover();
g.options().max_download_limit
};
// Global (process-wide) limiter: when present and enabled, the writer
// acquires tokens after the per-download limiter so all concurrent
// downloads share a single bandwidth ceiling.
let global_limited = self
.global_limiter
.as_ref()
.is_some_and(|g| g.is_download_limited());
let mut writer: Box<dyn DiskWriter> = if rate_limit.is_some() || global_limited {
let per_rate = rate_limit.filter(|&r| r > 0);
let limiter = per_rate
.map(|rate| RateLimiter::new(&RateLimiterConfig::new(Some(rate), None)))
.unwrap_or_else(RateLimiter::unlimited);
let mut tw = ThrottledWriter::new(raw_writer, limiter);
if let Some(ref gl) = self.global_limiter {
tw = tw.with_global_limiter(gl.clone());
}
Box::new(tw)
} else {
raw_writer
};
// -----------------------------------------------------------------
// Phase 6: Main Download Loop (Chunked Read + Write)
// -----------------------------------------------------------------
let start_time = Instant::now();
let mut last_speed_update = Instant::now();
let mut last_completed: u64 = 0;
let _buf = vec![0u8; constants::SFTP_DISK_WRITE_CHUNK_SIZE];
info!("[SFTP-CMD] Starting transfer loop: {} bytes", total_length);
loop {
let halted = {
let group = self.group.recover();
group.is_force_halt_requested() || group.is_halt_requested()
};
if halted {
let _ = remote_file.close().await;
let _ = writer.finalize().await;
let _ = conn.disconnect().await;
return Err(Aria2Error::DownloadFailed("SFTP download halted".into()));
}
let remaining = total_length.saturating_sub(self.completed_bytes);
if remaining == 0 {
break; // Download complete
}
// Calculate how much to read this iteration
let to_read = (constants::SFTP_DISK_WRITE_CHUNK_SIZE as u64).min(remaining) as usize;
// Read chunk from remote file at current offset
let data = match remote_file
.read_at(self.completed_bytes, to_read as u32)
.await
{
Ok(data) if data.is_empty() => {
debug!(
"[SFTP-CMD] EOF at offset {} (expected {})",
self.completed_bytes, total_length
);
return Err(Aria2Error::DownloadFailed(format!(
"SFTP remote file ended before the advertised length: {} < {}",
self.completed_bytes, total_length
)));
}
Ok(data) => data,
Err(e) => {
error!(
"[SFTP-CMD] Read error at offset {}: {}",
self.completed_bytes, e
);
let _ = remote_file.close().await;
let _ = conn.disconnect().await;
let err = FileOpError::from(e);
return Err(Self::map_file_op_error(&err, &self.host, &self.remote_path));
}
};
let n = data.len();
// Write chunk to local disk via disk writer
if let Err(e) = writer.write(&data).await {
error!("[SFTP-CMD] Disk write error: {}", e);
let _ = remote_file.close().await;
let _ = conn.disconnect().await;
return Err(Aria2Error::Fatal(FatalError::Config(format!(
"Disk write failed: {}",
e
))));
}
self.completed_bytes += n as u64;
// Update progress in RequestGroup
{
let g = self.group.recover();
g.update_progress(self.completed_bytes);
// Periodic speed calculation (every ~500ms)
let elapsed = last_speed_update.elapsed();
if elapsed.as_millis() >= constants::SFTP_SPEED_UPDATE_INTERVAL_MS as u128 {
let delta = self.completed_bytes - last_completed;
let speed = (delta as f64 / elapsed.as_secs_f64()) as u64;
g.update_speed(speed, 0);
last_speed_update = Instant::now();
last_completed = self.completed_bytes;
}
}
}
// -----------------------------------------------------------------
// Phase 7: Finalize and Cleanup
// -----------------------------------------------------------------
// Close remote file handle
if let Err(e) = remote_file.close().await {
warn!("[SFTP-CMD] Warning closing remote file: {}", e);
}
// Finalize disk writer (flush, sync, etc.). Completion is not valid
// unless the local bytes have been durably finalized.
let finalized_data = writer.finalize().await.map_err(|error| {
Aria2Error::FileIo(format!(
"[SFTP-CMD] Failed to finalize disk writer: {error}"
))
})?;
if in_memory_download {
let group = self.group.recover();
group.set_total_length(self.completed_bytes);
group.set_completed_length(self.completed_bytes);
group.set_in_memory_data(finalized_data);
}
// Disconnect SSH session
if let Err(e) = conn.disconnect().await {
warn!("[SFTP-CMD] Warning during SSH disconnect: {}", e);
}
// Calculate final statistics
let final_speed = {
let elapsed = start_time.elapsed().as_secs_f64();
if elapsed > 0.0 {
(self.completed_bytes as f64 / elapsed) as u64
} else {
0
}
};
// Mark RequestGroup as complete
{
let g = self.group.recover();
g.update_progress(self.completed_bytes);
g.update_speed(final_speed, 0);
drop(g);
let mut g = self.group.recover_mut();
g.complete()?;
}
info!(
"[SFTP-CMD] Download complete: {} ({} bytes, {:.1} KB/s)",
self.output_path.display(),
self.completed_bytes,
final_speed as f64 / 1024.0
);
Ok(())
}
/// Return the current status of this command.
fn status(&self) -> CommandStatus {
if self.completed_bytes > 0 {
CommandStatus::Running
} else {
CommandStatus::Pending
}
}
fn gid(&self) -> GroupId {
self.group.recover().gid()
}
fn request_group(
&self,
) -> Option<std::sync::Arc<std::sync::RwLock<crate::request::request_group::RequestGroup>>>
{
Some(std::sync::Arc::clone(&self.group))
}
/// Return the timeout for this command.
fn timeout(&self) -> Option<Duration> {
Some(Duration::from_secs(constants::SFTP_COMMAND_TIMEOUT_SECS))
}
}