aria2-core 0.3.14

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! FTP control-plane attempt preparation.

use std::sync::Arc;

use crate::checksum::checksum::Checksum;
use tracing::{info, warn};

use crate::error::{Aria2Error, RecoverableError};
use crate::network::ConnectionContext;
use crate::request::request_group::ActiveConnectionGuard;
use crate::util::rwlock_ext::RwLockRecover;
use aria2_protocol::ftp::tls::FtpsClient;

use super::control::RawFtpControl;
use super::execution::FtpAttemptError;
use super::transfer::FtpReceiveTransfer;
use super::types::FtpDownloadCommand;

impl FtpDownloadCommand {
    pub(super) async fn execute_single_attempt(
        &mut self,
        attempt_index: u32,
    ) -> std::result::Result<(), FtpAttemptError> {
        let connection_guard = ActiveConnectionGuard::new(Arc::clone(&self.group));
        connection_guard.set(1);
        let in_memory_download = self.group.recover().is_in_memory_download();
        let proxy_config = self.ftp_proxy_config().map_err(FtpAttemptError::from)?;
        if let Some((proxy, crate::ftp::connection::ProxyMethod::Get)) = proxy_config.as_ref() {
            return self.execute_proxy_get_attempt(proxy).await;
        }

        let reuse_enabled = self.group.recover().options().ftp_reuse_connection;
        let pool = self.connection_pool.clone();
        let pool_key = self.control_session_key(proxy_config.as_ref().map(|(proxy, _)| proxy));
        let pooled = if reuse_enabled {
            if let Some(pool) = &pool {
                pool.try_get_control_session(&pool_key).await
            } else {
                None
            }
        } else {
            None
        };
        let reuse_key = (reuse_enabled && pool.is_some()).then_some(pool_key.clone());
        let reused = pooled.is_some();
        let mut ctrl = if let Some(pooled) = pooled {
            RawFtpControl::from_pooled(pooled)
        } else {
            let control_address = if proxy_config.is_some() {
                // The proxy resolves the FTP origin in tunnel mode. Resolving
                // it locally would reject valid proxy-only DNS names.
                std::net::SocketAddr::from(([0, 0, 0, 0], 0))
            } else {
                if self.resolved_addresses.is_empty() {
                    self.refresh_control_addresses().await?;
                }
                self.resolved_addresses[attempt_index as usize % self.resolved_addresses.len()]
            };
            let context = ConnectionContext::new(&self.host, self.port, control_address);
            let host = self.host.clone();
            let port = self.port;
            let ftps_config = self.ftps_config.clone();
            let ftps_implicit = self.ftps_implicit;
            let outbound_network_policy = Arc::clone(&self.outbound_network_policy);
            let connect_timeout = self.connect_timeout;
            let request_timeout = self.group.recover().timeout();
            let proxy_for_connection = proxy_config.as_ref().map(|(proxy, _)| proxy.clone());
            let connect_result = tokio::time::timeout(connect_timeout, async move {
                let ftps_client = ftps_config
                    .as_ref()
                    .map(FtpsClient::new)
                    .transpose()
                    .map_err(|error| {
                        Aria2Error::Network(format!("FTPS TLS configuration failed: {error}"))
                    })?;
                if let Some(proxy) = proxy_for_connection.as_ref() {
                    RawFtpControl::connect_via_http_proxy(
                        &host,
                        port,
                        proxy,
                        ftps_client.as_ref(),
                        ftps_implicit,
                        Arc::clone(&outbound_network_policy),
                        request_timeout,
                    )
                    .await
                } else if let Some(client) = ftps_client.as_ref() {
                    if ftps_implicit {
                        RawFtpControl::connect_ftps_implicit_at(
                            &host,
                            port,
                            control_address,
                            client,
                            Arc::clone(&outbound_network_policy),
                            request_timeout,
                        )
                        .await
                    } else {
                        RawFtpControl::connect_ftps_explicit_at(
                            &host,
                            port,
                            control_address,
                            client,
                            Arc::clone(&outbound_network_policy),
                            request_timeout,
                        )
                        .await
                    }
                } else {
                    RawFtpControl::connect_at(
                        &host,
                        port,
                        control_address,
                        outbound_network_policy,
                        request_timeout,
                    )
                    .await
                }
            })
            .await;
            match connect_result {
                Ok(Ok(ctrl)) => ctrl,
                Ok(Err(error)) if proxy_config.is_some() => {
                    return Err(FtpAttemptError::from(error));
                }
                Ok(Err(error)) => return Err(FtpAttemptError::control(error, context)),
                Err(_) if proxy_config.is_some() => {
                    return Err(FtpAttemptError::from(Aria2Error::Recoverable(
                        RecoverableError::Timeout,
                    )));
                }
                Err(_) => {
                    return Err(FtpAttemptError::control(
                        Aria2Error::Recoverable(RecoverableError::Timeout),
                        context,
                    ));
                }
            }
        };
        ctrl.set_request_timeout(self.group.recover().timeout());
        self.last_connection_context = Some(ctrl.connection_context().clone());
        self.group
            .recover()
            .set_connection_context(ctrl.connection_context().clone());

        // Step 2: Authenticate
        if !reused {
            ctrl.authenticate(&self.username, &self.password).await?;
        }

        // Step 3: Set the configured transfer representation.
        let ftp_type = self.group.recover().options().ftp_type.clone();
        ctrl.set_transfer_type(&ftp_type).await?;

        // Step 4: Resolve the URI directory and retain only the file name for
        // SIZE/RETR, matching the original FTP command sequence.
        let file_path = ctrl.prepare_remote_path(&self.remote_path).await?;

        // The original queries MDTM after CWD traversal and before SIZE when
        // remote-time is enabled. A missing/unsupported MDTM response does
        // not make an otherwise valid FTP download fail.
        let remote_modified_time = if self.group.recover().options().remote_time {
            ctrl.get_modification_time(&file_path).await?
        } else {
            None
        };

        // Step 5: Probe file size
        let file_size = ctrl.get_file_size(&file_path).await?;

        // aria2_original's dry-run path stops after metadata discovery. It
        // marks the file as found without opening a data connection or
        // issuing REST/RETR, so no local output is created.
        if self.group.recover().options().dry_run {
            let discovered_length = file_size.unwrap_or_default();
            self.completed_bytes = discovered_length;
            {
                let g = self.group.recover();
                g.set_total_length(discovered_length);
                g.update_progress(discovered_length);
                g.set_checksum_verified(true);
            }
            self.group.recover_mut().complete()?;
            self.release_control_connection(ctrl, reuse_key.clone())
                .await;
            return Ok(());
        }

        // Reconcile the local file with SIZE before allocation/REST/RETR.
        // This mirrors FtpNegotiationCommand::onFileSizeDetermined(): a
        // complete local file is terminal, while an oversized file must not
        // be used as a resume prefix.
        let local_size = if in_memory_download {
            0
        } else {
            std::fs::metadata(&self.output_path)
                .map(|metadata| metadata.len())
                .unwrap_or(0)
        };
        let continue_download = self.group.recover().options().continue_download;
        let mut restart_from_zero = false;
        if let Some(actual_size) = file_size {
            let resume_input_length =
                crate::engine::progress_checkpoint::ProgressCheckpoint::resume_input_length(
                    &self.output_path,
                    local_size,
                    continue_download,
                    actual_size,
                )
                .await;
            {
                let g = self.group.recover();
                g.validate_total_length(g.total_length(), actual_size)
                    .map_err(FtpAttemptError::from)?;
                g.set_total_length(actual_size);
            }

            if in_memory_download {
                self.checkpoint = None;
            } else {
                self.checkpoint = Some(
                    crate::engine::progress_checkpoint::ProgressCheckpoint::open(
                        &self.output_path,
                        actual_size,
                        resume_input_length,
                    )
                    .await,
                );
                self.resume_offset = self
                    .checkpoint
                    .as_ref()
                    .map(|checkpoint| checkpoint.resume_offset(resume_input_length))
                    .unwrap_or(resume_input_length);
            }

            if !in_memory_download && local_size == actual_size && self.resume_offset == actual_size
            {
                let checksum_valid = {
                    let checksum_config = self.group.recover().options().checksum.clone();
                    match checksum_config {
                        Some((algorithm, expected)) => {
                            let hash_type =
                                crate::checksum::message_digest::HashType::from_str(&algorithm)
                                    .ok_or_else(|| {
                                        Aria2Error::Parse(format!(
                                            "unknown checksum algorithm: {}",
                                            algorithm
                                        ))
                                    })?;
                            let checksum = Checksum::new(hash_type, &expected)?;
                            crate::checksum::check_integrity::man::enqueue_file_checksum_for_group(
                                &crate::checksum::check_integrity::man::shared(),
                                std::sync::Arc::clone(&self.group),
                                &self.output_path,
                                actual_size,
                                checksum,
                            )
                            .await?
                        }
                        None => true,
                    }
                };

                if checksum_valid {
                    self.resume_offset = actual_size;
                    self.completed_bytes = actual_size;
                    {
                        let g = self.group.recover();
                        g.update_progress(actual_size);
                    }
                    if self.group.recover().options().checksum.is_some() {
                        self.group.recover().set_checksum_verified(true);
                    }
                    self.apply_remote_time(remote_modified_time, in_memory_download);
                    self.group.recover_mut().complete()?;
                    self.complete_checkpoint().await;
                    info!(
                        path = %self.output_path.display(),
                        size = actual_size,
                        "FTP target already matches remote SIZE and checksum"
                    );
                    self.release_control_connection(ctrl, reuse_key.clone())
                        .await;
                    return Ok(());
                }

                warn!(
                    path = %self.output_path.display(),
                    "FTP target checksum mismatch; restarting from byte zero"
                );
                std::fs::OpenOptions::new()
                    .write(true)
                    .open(&self.output_path)
                    .and_then(|file| file.set_len(0))
                    .map_err(|error| {
                        FtpAttemptError::from(Aria2Error::FileIo(format!(
                            "truncate checksum-mismatched FTP target {}: {}",
                            self.output_path.display(),
                            error
                        )))
                    })?;
                self.resume_offset = 0;
                self.flush_checkpoint().await;
                restart_from_zero = true;
            }

            if in_memory_download {
                self.resume_offset = 0;
            } else if local_size > actual_size {
                std::fs::OpenOptions::new()
                    .write(true)
                    .open(&self.output_path)
                    .and_then(|file| file.set_len(0))
                    .map_err(|error| {
                        FtpAttemptError::from(Aria2Error::FileIo(format!(
                            "truncate oversized FTP target {}: {}",
                            self.output_path.display(),
                            error
                        )))
                    })?;
                self.resume_offset = 0;
                self.flush_checkpoint().await;
            } else if !restart_from_zero {
                self.resume_offset = self
                    .checkpoint
                    .as_ref()
                    .map(|checkpoint| checkpoint.resume_offset(resume_input_length))
                    .unwrap_or(resume_input_length);
            }
        } else if !in_memory_download {
            self.resume_offset = if continue_download { local_size } else { 0 };
        }

        // Step 6: Allocate the destination before RETR, matching the C++
        // FileAllocationEntry command chain used by FTP downloads.
        let allocation =
            crate::filesystem::file_allocation::AllocationStrategy::from_str(&self.file_allocation);
        if !in_memory_download
            && allocation != crate::filesystem::file_allocation::AllocationStrategy::None
            && file_size.unwrap_or(0) > 0
        {
            let gid = { self.group.recover().gid().value() };
            crate::filesystem::file_allocation_man::enqueue_path(
                &crate::filesystem::file_allocation_man::shared(),
                &self.output_path,
                file_size.unwrap_or(0),
                allocation,
                self.secure_falloc,
                gid,
            )
            .await
            .map_err(FtpAttemptError::from)?;
        }

        // Step 7: Negotiate the data connection mode before REST/RETR.
        let passive_stream = if self.passive_mode {
            Some(ctrl.enter_passive_mode().await?)
        } else {
            None
        };
        let active_listener = if self.passive_mode {
            None
        } else {
            Some(ctrl.enter_active_mode().await?)
        };

        // Step 8: Set the resume offset after data-channel preparation. The
        // original sends REST 0 as well; only a non-zero rejection restarts
        // the local partial file.
        let resume_accepted = ctrl.set_resume_offset(self.resume_offset).await?;
        let write_offset = if resume_accepted {
            self.resume_offset
        } else {
            0
        };
        if !resume_accepted {
            // REST rejection means RETR will send the complete object. Make
            // the restart explicit so stale bytes cannot survive past EOF.
            self.resume_offset = 0;
            std::fs::OpenOptions::new()
                .write(true)
                .open(&self.output_path)
                .and_then(|file| file.set_len(0))
                .map_err(|error| {
                    FtpAttemptError::from(Aria2Error::FileIo(format!(
                        "truncate FTP target after REST rejection {}: {}",
                        self.output_path.display(),
                        error
                    )))
                })?;
        }

        // Step 9: Initiate file transfer (RETR command).
        ctrl.initiate_retr(&file_path).await?;

        // Step 10: Establish the data connection. In active mode the server
        // connects back after RETR; never attempt a client-side connect.
        let data_stream = if let Some(stream) = passive_stream {
            stream
        } else {
            let listener =
                active_listener.expect("active listener is present when passive mode is disabled");
            tokio::time::timeout(ctrl.data_connection_timeout(), listener.accept_with_peer())
                .await
                .map_err(|_| {
                    Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                        message: "Active FTP data connection timeout".into(),
                    })
                })?
                .map_err(|e| {
                    Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                        message: format!("Active FTP data connection failed: {}", e),
                    })
                })?
                .0
        };

        // Upgrade the data channel after the server accepted RETR. For plain
        // FTP this preserves the TCP stream; FTPS performs the PROT P TLS
        // handshake before any payload bytes are read.
        let data_stream = ctrl.secure_data_stream(data_stream).await?;
        let _ = data_stream.set_nodelay(true); // Ignore error if not supported

        self.receive_data_transfer(FtpReceiveTransfer {
            ctrl,
            data_stream,
            file_size,
            in_memory_download,
            remote_modified_time,
            write_offset,
            reuse_key,
        })
        .await
    }
}