aria2-core 0.2.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
//! DownloadContext interaction and URI lifecycle delegation methods.
//!
//! In C++ aria2, `RequestGroup` delegates URI management to `FileEntry`
//! objects via `DownloadContext`. This module implements that delegation:
//!
//! - When `DownloadContext` is set, initial URIs are transferred to the
//!   first `FileEntry`'s `remaining_uris`.
//! - URI lifecycle queries (remaining, spent, results) delegate to
//!   `FileEntry` when `DownloadContext` is available.
//! - When `DownloadContext` is not yet set, queries fall back to
//!   `RequestGroup.uris` (the initial URI list).

use std::collections::VecDeque;
use std::sync::Arc;

use tracing::{debug, trace};

use crate::download::DownloadContext;
use crate::download::file_entry::UriResult;
use crate::util::rwlock_ext::RwLockRecover;

impl super::RequestGroup {
    // ── DownloadContext Accessors ────────────────────────────────────────

    /// Get a shared reference to the `DownloadContext`, if set.
    ///
    /// Returns `None` if the download context has not been initialized yet
    /// (e.g. before torrent metadata is parsed for BT downloads).
    pub fn get_download_context(&self) -> Option<Arc<DownloadContext>> {
        self.download_context.recover().clone()
    }

    /// Mark the owned download context as whole-file checksum verified.
    pub fn set_checksum_verified(&self, verified: bool) {
        if let Some(ctx) = self.download_context.recover().as_ref() {
            ctx.set_checksum_verified(verified);
        }
    }

    /// Set the `DownloadContext` for this download and transfer initial URIs.
    ///
    /// When `DownloadContext` is set for the first time, the URIs from
    /// `RequestGroup.uris` are transferred to the first `FileEntry`'s
    /// `remaining_uris`. This mirrors the C++ flow where `RequestGroup`
    /// creates `DownloadContext` with URIs already in `FileEntry`.
    ///
    /// If the `DownloadContext` already has file entries with URIs (e.g.
    /// for BT downloads), the transfer is skipped to avoid duplication.
    ///
    /// # URI Transfer Logic
    ///
    /// In C++ aria2, URIs are added to `FileEntry` during the construction
    /// of `DownloadContext`. In Rust, `DownloadContext` may be created
    /// independently (e.g. by `BtDownloadCommand` after parsing torrent
    /// metadata), so we transfer URIs lazily at the point of attachment.
    pub fn set_download_context(&self, mut ctx: Arc<DownloadContext>) {
        let mut guard = self.download_context.recover_mut();

        // Transfer initial URIs to the first FileEntry's remaining_uris.
        // This mirrors C++ where RequestGroup constructor adds URIs to FileEntry.
        // We only do this if the first FileEntry has no remaining URIs yet
        // (i.e. this is a fresh context, not one loaded from torrent metadata).
        //
        // We use Arc::get_mut which requires exclusive ownership of the Arc.
        // Since we just received this Arc and it hasn't been stored yet, we
        // should have the only reference. If Arc::get_mut fails (shouldn't
        // happen here), we skip the URI transfer — the context is still
        // stored correctly.
        if let Some(ctx_ref) = Arc::get_mut(&mut ctx) {
            let first_fe = match ctx_ref.get_file_entries_mut().first_mut() {
                Some(fe) => fe,
                None => {
                    debug!(
                        gid = self.gid.value(),
                        "DownloadContext has no file entries, cannot transfer URIs"
                    );
                    *guard = Some(ctx);
                    return;
                }
            };

            // Only transfer if the FileEntry doesn't already have URIs
            // (BT downloads have URIs from torrent metadata, HTTP don't).
            if first_fe.remaining_uris().is_empty() && !self.uris.is_empty() {
                trace!(
                    gid = self.gid.value(),
                    uri_count = self.uris.len(),
                    "Transferring initial URIs to FileEntry"
                );
                for uri in &self.uris {
                    first_fe.add_uri(uri);
                }
            }
        }

        *guard = Some(ctx);
    }

    // ── URI Lifecycle Delegation ────────────────────────────────────────
    // These methods delegate to FileEntry via DownloadContext when available,
    // falling back to the initial URI list when context is not yet set.

    /// Return the remaining (not-yet-dispatched) URIs.
    ///
    /// Delegates to `FileEntry::remaining_uris()` when `DownloadContext`
    /// is set; otherwise returns a snapshot of the initial URI list.
    /// Mirrors C++ `RequestGroup::getRemainingUris()` which iterates
    /// file entries in the download context.
    pub fn get_remaining_uris(&self) -> Vec<String> {
        let guard = self.download_context.recover();
        if let Some(ref ctx) = *guard {
            let entries = ctx.get_file_entries();
            let mut uris = Vec::new();
            for fe in entries {
                if fe.is_requested() {
                    uris.extend(fe.remaining_uris().iter().cloned());
                }
            }
            uris
        } else {
            // Fallback: return initial URIs (none have been dispatched yet)
            self.uris.clone()
        }
    }

    /// Return the spent (already-dispatched) URIs.
    ///
    /// Delegates to `FileEntry::spent_uris()` when `DownloadContext`
    /// is set; otherwise returns empty (no URIs have been dispatched).
    /// Mirrors C++ `RequestGroup::getSpentUris()`.
    pub fn get_spent_uris(&self) -> Vec<String> {
        let guard = self.download_context.recover();
        if let Some(ref ctx) = *guard {
            let entries = ctx.get_file_entries();
            let mut uris = Vec::new();
            for fe in entries {
                if fe.is_requested() {
                    uris.extend(fe.spent_uris().iter().cloned());
                }
            }
            uris
        } else {
            Vec::new()
        }
    }

    /// Return all URIs (spent + remaining) across all requested file entries.
    ///
    /// Mirrors C++ `RequestGroup::getUris()` which collects URIs from
    /// all `FileEntry` objects.
    pub fn get_all_uris(&self) -> Vec<String> {
        let guard = self.download_context.recover();
        if let Some(ref ctx) = *guard {
            let entries = ctx.get_file_entries();
            let mut uris = Vec::new();
            for fe in entries {
                if fe.is_requested() {
                    uris.extend(fe.uris());
                }
            }
            uris
        } else {
            self.uris.clone()
        }
    }

    /// Return a snapshot of all URI entries and their current lifecycle state.
    pub fn uri_entries(&self) -> Vec<super::UriEntry> {
        let guard = self.download_context.recover();
        if let Some(ref ctx) = *guard {
            ctx.get_file_entries()
                .iter()
                .filter(|fe| fe.is_requested())
                .flat_map(|fe| {
                    fe.uris().into_iter().map(|uri| {
                        let status = if fe.remaining_uris().iter().any(|value| value == &uri) {
                            "waiting"
                        } else if fe.spent_uris().iter().any(|value| value == &uri) {
                            "used"
                        } else {
                            "spent"
                        };
                        super::UriEntry {
                            uri,
                            status: status.to_string(),
                        }
                    })
                })
                .collect()
        } else {
            self.uris
                .iter()
                .cloned()
                .map(|uri| super::UriEntry {
                    uri,
                    status: "waiting".to_string(),
                })
                .collect()
        }
    }

    /// Remove the requested URIs and add new URIs to the first requested file entry.
    ///
    /// When `position` is present, additions are inserted at that zero-based
    /// position in the same order as the input. This mirrors aria2's optional
    /// `changeUri` position argument; deletion happens before insertion.
    pub fn change_uris(
        &mut self,
        file_index: usize,
        del_uris: &[String],
        add_uris: &[String],
        position: Option<usize>,
    ) -> crate::error::Result<(usize, usize)> {
        let file_index = file_index.checked_sub(1).ok_or_else(|| {
            crate::error::Aria2Error::InvalidArgument("file index must be at least 1".to_string())
        })?;
        let mut guard = self.download_context.recover_mut();
        if let Some(ctx) = guard.as_mut() {
            let ctx_inner = Arc::get_mut(ctx).ok_or_else(|| {
                crate::error::Aria2Error::InvalidArgument(
                    "download context is shared and cannot be changed".to_string(),
                )
            })?;
            let entry = ctx_inner
                .get_file_entries_mut()
                .get_mut(file_index)
                .filter(|fe| fe.is_requested())
                .ok_or_else(|| {
                    crate::error::Aria2Error::InvalidArgument(
                        "download context has no requested file entry".to_string(),
                    )
                })?;
            let mut deleted = 0;
            for uri in del_uris {
                deleted += entry.remove_uri(uri) as usize;
            }
            let added = match position {
                Some(mut position) => {
                    let mut added = 0;
                    for uri in add_uris {
                        if entry.insert_uri(uri, position) {
                            added += 1;
                            position = position.saturating_add(1);
                        }
                    }
                    added
                }
                None => entry.add_uris(add_uris),
            };
            return Ok((deleted, added));
        }

        let mut deleted = 0;
        if file_index != 0 {
            return Err(crate::error::Aria2Error::InvalidArgument(
                "file index is unavailable before download context initialization".to_string(),
            ));
        }
        self.uris.retain(|uri| {
            if del_uris.iter().any(|deleted_uri| deleted_uri == uri) {
                deleted += 1;
                false
            } else {
                true
            }
        });
        let added = match position {
            Some(mut position) => {
                let mut added = 0;
                for uri in add_uris {
                    if url::Url::parse(uri).is_err() {
                        continue;
                    }
                    let insert_position = position.min(self.uris.len());
                    self.uris.insert(insert_position, uri.clone());
                    position = position.saturating_add(1);
                    added += 1;
                }
                added
            }
            None => add_uris
                .iter()
                .filter(|uri| url::Url::parse(uri).is_ok())
                .map(|uri| {
                    self.uris.push(uri.clone());
                    1
                })
                .sum(),
        };
        Ok((deleted, added))
    }

    /// Return URI attempt results across all requested file entries.
    ///
    /// Delegates to `FileEntry::uri_results()` when `DownloadContext`
    /// is set. Mirrors C++ `RequestGroup::getUriResults()`.
    pub fn get_uri_results(&self) -> VecDeque<UriResult> {
        let guard = self.download_context.recover();
        if let Some(ref ctx) = *guard {
            let entries = ctx.get_file_entries();
            let mut results = VecDeque::new();
            for fe in entries {
                if fe.is_requested() {
                    results.extend(fe.uri_results().iter().cloned());
                }
            }
            results
        } else {
            VecDeque::new()
        }
    }

    /// Add a URI result record for the first requested file entry.
    ///
    /// Mirrors C++ `FileEntry::addURIResult()`. Called by download commands
    /// when a URI attempt completes (success or failure).
    /// Publish a real protocol connection for timeout/error handling.
    pub fn set_connection_context(&self, context: crate::network::ConnectionContext) {
        if let Ok(mut contexts) = self.connection_contexts.write()
            && !contexts.iter().any(|existing| existing == &context)
        {
            contexts.push(context);
        }
    }

    /// Clear connection snapshots before a new command generation starts.
    pub fn clear_connection_contexts(&self) {
        if let Ok(mut contexts) = self.connection_contexts.write() {
            contexts.clear();
        }
    }

    /// Read all real protocol connections observed by this group.
    pub fn connection_contexts(&self) -> Vec<crate::network::ConnectionContext> {
        self.connection_contexts
            .read()
            .map(|contexts| contexts.clone())
            .unwrap_or_default()
    }

    pub fn add_uri_result(&self, uri: String, result_code: u16) {
        let mut guard = self.download_context.recover_mut();
        if let Some(ref mut ctx) = *guard
            && let Some(ctx_inner) = Arc::get_mut(ctx)
            && let Some(fe) = ctx_inner
                .get_file_entries_mut()
                .iter_mut()
                .find(|fe| fe.is_requested())
        {
            fe.add_uri_result(uri, result_code);
        }
        // If no DownloadContext yet, the result is lost. This is acceptable
        // because URI results are only meaningful after a download attempt.
    }

    /// Reuse spent URIs that have not produced errors.
    ///
    /// Delegates to `FileEntry::reuse_uri()` for the first requested file
    /// entry. Called when all remaining URIs have been exhausted.
    /// Mirrors C++ `FileEntry::reuseUri()`.
    pub fn reuse_uri(&self, ignore_hosts: &[String]) {
        let mut guard = self.download_context.recover_mut();
        if let Some(ref mut ctx) = *guard
            && let Some(ctx_inner) = Arc::get_mut(ctx)
            && let Some(fe) = ctx_inner
                .get_file_entries_mut()
                .iter_mut()
                .find(|fe| fe.is_requested())
        {
            fe.reuse_uri(ignore_hosts);
        }
    }

    /// Check whether any requested file entry has remaining URIs.
    ///
    /// Mirrors C++ `isUriSuppliedForRequestedFileEntry()`. Returns `true`
    /// if at least one requested file entry has URIs that haven't been tried.
    pub fn has_remaining_uris(&self) -> bool {
        let guard = self.download_context.recover();
        if let Some(ref ctx) = *guard {
            ctx.get_file_entries()
                .iter()
                .any(|fe| fe.is_requested() && !fe.remaining_uris().is_empty())
        } else {
            !self.uris.is_empty()
        }
    }

    /// Add a redirect URI to the first requested file entry's remaining URIs.
    ///
    /// Matches C++ aria2 behavior where `HttpSkipResponseCommand` adds
    /// redirect target URIs to the `FileEntry`'s URI pool so they can be
    /// used for future download attempts (e.g. when the original URI fails
    /// but the redirect target succeeded before).
    ///
    /// If no `DownloadContext` is set yet, the URI is appended to the
    /// group's initial URI list instead.
    pub fn add_redirect_uri(&mut self, uri: &str) {
        let mut guard = self.download_context.recover_mut();
        if let Some(ref mut ctx) = *guard {
            // Try to get exclusive mutable access to the inner DownloadContext.
            // Arc::get_mut succeeds only when we hold the sole Arc reference,
            // which is typical during download execution.
            if let Some(ctx_inner) = Arc::get_mut(ctx)
                && let Some(fe) = ctx_inner
                    .get_file_entries_mut()
                    .iter_mut()
                    .find(|fe| fe.is_requested())
            {
                if fe.add_uri(uri) {
                    trace!("Added redirect URI to FileEntry: {}", uri);
                }
                return;
            }
            // If Arc is shared (rare during download), fall back to the
            // initial URI list. The URI will be available for the next
            // download attempt via reuse_uri().
            drop(guard);
            self.uris.push(uri.to_string());
            trace!(
                "Added redirect URI to initial URI list (shared Arc): {}",
                uri
            );
        } else {
            drop(guard);
            self.uris.push(uri.to_string());
            trace!(
                "Added redirect URI to initial URI list (no context): {}",
                uri
            );
        }
    }
}