aria2-core 0.3.8

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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
//! FTP download execution logic.
//!
//! Implements the `Command` trait for `FtpDownloadCommand` and the
//! single-attempt download procedure (connect, authenticate, transfer,
//! finalize).

use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};

use async_trait::async_trait;
use tokio::io::AsyncReadExt;
use tracing::{debug, error, info, warn};

use crate::checksum::checksum::Checksum;
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::network::ConnectionContext;
use crate::rate_limiter::{RateLimiter, RateLimiterConfig, ThrottledWriter};
use crate::request::request_group::{ActiveConnectionGuard, GroupId};
use crate::util::rwlock_ext::RwLockRecover;

use super::control::RawFtpControl;
use super::types::FtpDownloadCommand;

pub(super) struct FtpAttemptError {
    pub(super) source: Aria2Error,
    pub(super) failed_control: Option<ConnectionContext>,
}

impl FtpAttemptError {
    pub(super) fn control(source: Aria2Error, context: ConnectionContext) -> Self {
        Self {
            source,
            failed_control: Some(context),
        }
    }
}

impl From<Aria2Error> for FtpAttemptError {
    fn from(source: Aria2Error) -> Self {
        Self {
            source,
            failed_control: None,
        }
    }
}

#[async_trait]
impl Command for FtpDownloadCommand {
    /// Execute the FTP download with full lifecycle management
    async fn execute(&mut self) -> Result<()> {
        if !self.started {
            self.group.recover_mut().start()?;
            self.started = true;
        }

        info!(
            "FTP download starting: {}:{} -> {}",
            self.host,
            self.port,
            self.output_path.display()
        );

        let in_memory_download = self.group.recover().is_in_memory_download();

        // Create output directory if needed
        if !in_memory_download
            && let Some(parent) = self.output_path.parent()
            && !parent.exists()
        {
            std::fs::create_dir_all(parent).map_err(|e| {
                Aria2Error::Fatal(FatalError::Config(format!("mkdir failed: {}", e)))
            })?;
        }

        // Retry loop for transient errors. The policy counts total attempts,
        // matching aria2's `--max-tries` contract.
        let mut attempts = 0u32;
        loop {
            let attempt_index = attempts;
            match self.execute_single_attempt(attempt_index).await {
                Ok(_) => {
                    info!(
                        "FTP download completed successfully: {} ({} bytes)",
                        self.output_path.display(),
                        self.completed_bytes
                    );
                    return Ok(());
                }
                Err(attempt_error) => {
                    self.flush_checkpoint().await;
                    let FtpAttemptError {
                        source: mut e,
                        failed_control,
                    } = attempt_error;
                    if matches!(
                        e,
                        Aria2Error::Recoverable(RecoverableError::ResourceNotFound)
                    ) {
                        e = self.group.recover().file_not_found_error();
                    }
                    let reject_control = matches!(
                        e,
                        Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure { .. })
                            | Aria2Error::Recoverable(RecoverableError::Timeout)
                    );
                    if reject_control && let Some(context) = failed_control.as_ref() {
                        if let Some(cache) = self.dns_cache.as_ref() {
                            cache.lock().await.mark_bad_context(context);
                        }
                        self.resolved_addresses
                            .retain(|address| *address != context.peer_addr);
                        tracing::debug!(
                            host = %context.endpoint.hostname(),
                            peer = %context.peer_addr,
                            "FTP control connection failed; peer was rejected"
                        );
                    }
                    // Check if this is a retry-worthy error
                    let should_retry = match &e {
                        Aria2Error::Recoverable(RecoverableError::ResourceNotFound) => {
                            self.retry_policy
                                .can_retry_after(attempts.saturating_add(1))
                                && self.group.recover().can_retry_file_not_found()
                        }
                        Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                            ..
                        })
                        | Aria2Error::Recoverable(RecoverableError::Timeout) => self
                            .retry_policy
                            .can_retry_after(attempts.saturating_add(1)),
                        _ => false,
                    };

                    if should_retry {
                        attempts = attempts.saturating_add(1);
                        let wait = self.retry_policy.compute_wait(attempts).unwrap_or_default();
                        warn!(
                            "FTP download failed (attempt {}/{}), retrying in {:?}: {}",
                            attempts,
                            self.retry_policy.max_tries(),
                            wait,
                            e
                        );
                        self.wait_for_retry(wait).await?;

                        // Reset state for retry
                        self.completed_bytes = 0;
                        continue;
                    }

                    // Non-retryable error or max retries exceeded
                    error!(
                        "FTP download failed permanently after {} attempts: {}",
                        attempts.saturating_add(1),
                        e
                    );
                    return Err(e);
                }
            }
        }
    }

    async fn shutdown(&mut self) {
        self.flush_checkpoint().await;
    }

    fn status(&self) -> CommandStatus {
        if self.completed_bytes > 0 || self.started {
            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))
    }

    fn timeout(&self) -> Option<Duration> {
        self.group.recover().timeout()
    }
}

impl FtpDownloadCommand {
    fn check_cancelled(&self) -> Result<()> {
        let group = self.group.recover();
        if group.is_removed() {
            Err(Aria2Error::DownloadFailed(
                "Download cancelled by user".into(),
            ))
        } else if group.is_paused_flag() {
            Err(Aria2Error::DownloadFailed("Download paused".into()))
        } else if group.is_force_halt_requested() || group.is_halt_requested() {
            Err(Aria2Error::DownloadFailed("FTP download halted".into()))
        } else {
            Ok(())
        }
    }

    /// Wait between retry attempts while still honoring RequestGroup controls.
    /// A plain sleep would delay pause/remove handling for the full configured
    /// retry interval, which can be several minutes.
    pub(super) async fn wait_for_retry(&self, wait: Duration) -> Result<()> {
        let notifier = self.group.recover().lifecycle_notifier();
        let notified = notifier.notified();
        tokio::pin!(notified);
        notified.as_mut().enable();
        self.check_cancelled()?;
        tokio::select! {
            _ = tokio::time::sleep(wait) => self.check_cancelled(),
            _ = &mut notified => self.check_cancelled(),
        }
    }

    /// Apply the optional remote timestamp after the output handle has been
    /// finalized. This mirrors the original post-download file-attribute
    /// update while keeping failures non-fatal, as aria2 does.
    pub(super) fn apply_remote_time(
        &self,
        remote_modified_time: Option<SystemTime>,
        in_memory_download: bool,
    ) {
        if in_memory_download {
            return;
        }

        let Some(remote_modified_time) = remote_modified_time else {
            return;
        };

        let result = std::fs::OpenOptions::new()
            .write(true)
            .open(&self.output_path)
            .and_then(|file| file.set_modified(remote_modified_time));
        if let Err(error) = result {
            warn!(
                path = %self.output_path.display(),
                %error,
                "Failed to apply FTP remote modification time"
            );
        }
    }

    pub(super) async fn flush_checkpoint(&mut self) {
        if let Some(checkpoint) = self.checkpoint.as_mut() {
            let _ = self.group.recover().take_save_control_file_request();
            checkpoint.update(self.completed_bytes, true).await;
        }
    }

    pub(super) async fn complete_checkpoint(&mut self) {
        if let Some(checkpoint) = self.checkpoint.take() {
            checkpoint.complete().await;
        }
    }

    pub(super) async fn finalize_partial_writer(&mut self, writer: &mut Box<dyn DiskWriter>) {
        let _ = writer.finalize().await;
        self.flush_checkpoint().await;
    }

    /// Execute a single download attempt
    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 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 and would make
            // proxy failures look like origin failures.
            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 connect_timeout = self.connect_timeout;
        let proxy_for_connection = proxy_config.as_ref().map(|(proxy, _)| proxy.clone());
        let connect_result = tokio::time::timeout(connect_timeout, async move {
            if let Some(proxy) = proxy_for_connection.as_ref() {
                RawFtpControl::connect_via_http_proxy(
                    &host,
                    port,
                    proxy,
                    ftps_config.as_ref(),
                    ftps_implicit,
                )
                .await
            } else if let Some(config) = ftps_config.as_ref() {
                if ftps_implicit {
                    RawFtpControl::connect_ftps_implicit_at(&host, port, control_address, config)
                        .await
                } else {
                    RawFtpControl::connect_ftps_explicit_at(&host, port, control_address, config)
                        .await
                }
            } else {
                RawFtpControl::connect_at(&host, port, control_address).await
            }
        })
        .await;
        let mut ctrl = 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,
                    )));
                }
                return Err(FtpAttemptError::control(
                    Aria2Error::Recoverable(RecoverableError::Timeout),
                    context,
                ));
            }
        };
        self.last_connection_context = Some(ctrl.connection_context().clone());
        self.group
            .recover()
            .set_connection_context(ctrl.connection_context().clone());

        // Step 2: Authenticate
        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()?;
            ctrl.quit().await.ok();
            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"
                    );
                    ctrl.quit().await.ok();
                    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(
                Duration::from_secs(constants::FTP_DATA_CONNECTION_TIMEOUT_SECS),
                listener.accept(),
            )
            .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 mut data_stream = ctrl.secure_data_stream(data_stream).await?;
        let _ = data_stream.set_nodelay(true); // Ignore error if not supported

        // Step 11: Select a disk or memory writer, then apply optional rate
        // limiting. The memory writer is the FTP/SFTP equivalent of aria2's
        // MemoryPreDownloadHandler and never opens output_path.
        let raw_writer = new_sequential_download_writer(
            &self.output_path,
            in_memory_download,
            write_offset,
            file_size,
        );
        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| {
                    debug!("Rate limiting enabled: {} bytes/sec", 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
        };

        // Seek to resume offset if resuming
        // Note: DiskWriter trait doesn't support seek, so for resume we rely on
        // the FTP REST command to tell server to start from the offset,
        // and data will be appended to existing file if it exists
        if write_offset > 0 {
            debug!(
                "Resume offset: {} bytes (using FTP REST command)",
                write_offset
            );
        }

        // Step 12: Data receive loop with progress tracking. Existing bytes
        // are part of the logical completed length when resuming.
        self.completed_bytes = write_offset;
        {
            let g = self.group.recover();
            g.update_progress(write_offset);
        }
        let mut buffer = vec![0u8; constants::FTP_BUFFER_SIZE];
        let start_time = Instant::now();
        let mut last_speed_update = Instant::now();
        let mut last_completed = 0u64;

        info!("Starting data reception from FTP server");

        loop {
            let halted = {
                let group = self.group.recover();
                group.is_removed() || group.is_force_halt_requested() || group.is_halt_requested()
            };
            if halted {
                let halt_error = {
                    let group = self.group.recover();
                    if group.is_removed() {
                        "Download cancelled by user"
                    } else if group.is_paused_flag() {
                        "Download paused"
                    } else {
                        "FTP download halted"
                    }
                };
                drop(data_stream);
                let _ = writer.finalize().await;
                self.flush_checkpoint().await;
                ctrl.abort_transfer().await;
                ctrl.quit().await.ok();
                return Err(FtpAttemptError::from(Aria2Error::DownloadFailed(
                    halt_error.into(),
                )));
            }

            let lifecycle_notify = self.group.recover().lifecycle_notifier();
            let lifecycle_changed = lifecycle_notify.notified();
            tokio::pin!(lifecycle_changed);
            lifecycle_changed.as_mut().enable();
            let bytes_read = tokio::select! {
                bytes_read = data_stream.read(&mut buffer) => match bytes_read {
                Ok(bytes_read) => bytes_read,
                Err(error) => {
                    use std::io::ErrorKind;
                    let error = match error.kind() {
                        ErrorKind::Interrupted
                        | ErrorKind::WouldBlock
                        | ErrorKind::ConnectionReset
                        | ErrorKind::ConnectionAborted
                        | ErrorKind::BrokenPipe
                        | ErrorKind::TimedOut => {
                            Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                                message: format!("Data read error (transient): {}", error),
                            })
                        }
                        _ => Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                            message: format!("Data read error: {}", error),
                        }),
                    };
                    drop(data_stream);
                    self.finalize_partial_writer(&mut writer).await;
                    ctrl.abort_transfer().await;
                    ctrl.quit().await.ok();
                    return Err(FtpAttemptError::from(error));
                }
                },
                _ = &mut lifecycle_changed => {
                    let halted = {
                        let group = self.group.recover();
                        group.is_removed()
                            || group.is_force_halt_requested()
                            || group.is_halt_requested()
                    };
                    if !halted {
                        // Save-session and other non-terminal lifecycle
                        // notifications do not cancel an in-flight transfer.
                        continue;
                    }

                    let halt_error = {
                        let group = self.group.recover();
                        if group.is_removed() {
                            "Download cancelled by user"
                        } else if group.is_paused_flag() {
                            "Download paused"
                        } else {
                            "FTP download halted"
                        }
                    };
                    drop(data_stream);
                    self.finalize_partial_writer(&mut writer).await;
                    self.flush_checkpoint().await;
                    // The data connection may still be in a server-side
                    // transfer. Dropping the control connection is the only
                    // bounded cleanup here; waiting for ABOR/QUIT can block
                    // behind the stalled data transfer.
                    return Err(FtpAttemptError::from(Aria2Error::DownloadFailed(
                        halt_error.into(),
                    )));
                }
            };

            if bytes_read == 0 {
                debug!("End of data stream reached");
                break;
            }

            self.group.recover().record_network_activity();

            // Write to disk (with rate limiting if enabled)
            if let Err(error) = writer.write(&buffer[..bytes_read]).await {
                drop(data_stream);
                self.finalize_partial_writer(&mut writer).await;
                ctrl.abort_transfer().await;
                ctrl.quit().await.ok();
                return Err(FtpAttemptError::from(error));
            }
            self.completed_bytes += bytes_read as u64;
            if let Some(checkpoint) = self.checkpoint.as_mut() {
                let save_requested = self.group.recover().take_save_control_file_request();
                checkpoint
                    .update(self.completed_bytes, save_requested)
                    .await;
            }

            // Update progress in request group
            {
                let g = self.group.recover();
                g.update_progress(self.completed_bytes);

                // Update speed calculation every 500ms
                let elapsed = last_speed_update.elapsed();
                if elapsed.as_millis() >= constants::FTP_SPEED_UPDATE_INTERVAL_MS as u128 {
                    let delta = self.completed_bytes - last_completed;
                    let speed = if elapsed.as_secs_f64() > 0.0 {
                        (delta as f64 / elapsed.as_secs_f64()) as u64
                    } else {
                        0
                    };
                    g.update_speed(speed, 0);
                    last_speed_update = Instant::now();
                    last_completed = self.completed_bytes;
                }
            }
        }

        if let Some(expected_size) = file_size
            && self.completed_bytes != expected_size
        {
            drop(data_stream);
            self.finalize_partial_writer(&mut writer).await;
            ctrl.abort_transfer().await;
            ctrl.quit().await.ok();
            return Err(FtpAttemptError::from(Aria2Error::FtpProtocol(format!(
                "FTP transfer length mismatch: expected {}, got {}",
                expected_size, self.completed_bytes
            ))));
        }

        // Step 13: Cleanup and finalize
        drop(data_stream); // Close data connection

        // Finalize disk writer (flush buffers, etc.)
        let mut finalized_data = match writer.finalize().await {
            Ok(data) => data,
            Err(error) => {
                self.flush_checkpoint().await;
                ctrl.abort_transfer().await;
                ctrl.quit().await.ok();
                return Err(FtpAttemptError::from(Aria2Error::Fatal(
                    FatalError::Config(format!("Finalize writer failed: {}", error)),
                )));
            }
        };
        drop(writer);

        let checksum_config = self.group.recover().options().checksum.clone();
        if let Some((algorithm, expected)) = checksum_config {
            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)?;
            let verified = if in_memory_download {
                let (data, verified) = checksum.verify_async(finalized_data).await?;
                finalized_data = data;
                verified
            } else {
                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,
                    self.completed_bytes,
                    checksum,
                )
                .await?
            };
            if !verified {
                return Err(FtpAttemptError::from(Aria2Error::Checksum(format!(
                    "{} checksum mismatch for {}",
                    algorithm,
                    self.output_path.display()
                ))));
            }
            self.group.recover().set_checksum_verified(true);
        }

        self.apply_remote_time(remote_modified_time, in_memory_download);

        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);
        }

        // Read transfer completion response from control channel
        ctrl.read_transfer_complete().await?;

        self.complete_checkpoint().await;

        // Disconnect gracefully
        ctrl.quit().await.ok();

        // 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
            }
        };

        // Update final status in request group
        {
            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()?;
        }

        Ok(())
    }

    async fn refresh_control_addresses(&mut self) -> Result<()> {
        if let Some(cache) = self.dns_cache.as_ref() {
            let mut cache = cache.lock().await;
            let addresses = cache.resolve_with_refresh(&self.host, self.port).await?;
            if addresses.is_empty() {
                return Err(Aria2Error::NameResolve(format!(
                    "No usable address for {}:{}",
                    self.host, self.port
                )));
            }
            self.resolved_addresses = addresses;
            return Ok(());
        }

        self.resolved_addresses = tokio::net::lookup_host((self.host.as_str(), self.port))
            .await
            .map_err(|error| {
                Aria2Error::NameResolve(format!(
                    "DNS resolution failed for {}:{}: {}",
                    self.host, self.port, error
                ))
            })?
            .collect();
        if self.resolved_addresses.is_empty() {
            return Err(Aria2Error::NameResolve(format!(
                "No address resolved for {}:{}",
                self.host, self.port
            )));
        }
        Ok(())
    }
}