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
//! File entry operations: accessors, lookup, filtering, and path management.
use tracing::{debug, trace};
use crate::download::download_context::DownloadContext;
use crate::download::file_entry::FileEntry;
impl DownloadContext {
// -----------------------------------------------------------------------
// Total Length / Knowledge
// -----------------------------------------------------------------------
/// Derive the total length from file entries.
///
/// Returns `file_entries.last().last_offset()`, or 0 if empty.
/// This matches the C++ implementation where total length is not stored
/// independently but computed from the last file entry's offset + length.
pub fn get_total_length(&self) -> u64 {
self.file_entries
.last()
.map(|fe| fe.last_offset())
.unwrap_or(0)
}
/// Whether the total download length is known.
pub fn knows_total_length(&self) -> bool {
self.knows_total_length
}
/// Mark the total length as unknown (e.g. content-length missing).
pub fn mark_total_length_is_unknown(&mut self) {
self.knows_total_length = false;
debug!("Total length marked as unknown");
}
/// Mark the total length as known.
pub fn mark_total_length_is_known(&mut self) {
self.knows_total_length = true;
debug!("Total length marked as known");
}
// -----------------------------------------------------------------------
// File Entries
// -----------------------------------------------------------------------
/// Return a reference to the ordered file entry list.
pub fn get_file_entries(&self) -> &[FileEntry] {
&self.file_entries
}
/// Return a mutable reference to the ordered file entry list.
pub fn get_file_entries_mut(&mut self) -> &mut Vec<FileEntry> {
&mut self.file_entries
}
/// Return a reference to the first file entry.
///
/// # Panics
///
/// Panics if there are no file entries (matches C++ `assert`).
pub fn get_first_file_entry(&self) -> &FileEntry {
self.file_entries
.first()
.expect("get_first_file_entry: no file entries")
}
/// Return a reference to the first file entry whose `is_requested()` is true.
///
/// Returns `None` if no such file entry exists.
pub fn get_first_requested_file_entry(&self) -> Option<&FileEntry> {
self.file_entries.iter().find(|fe| fe.is_requested())
}
/// Count the number of file entries whose `is_requested()` is true.
pub fn count_requested_file_entry(&self) -> usize {
self.file_entries
.iter()
.filter(|fe| fe.is_requested())
.count()
}
/// Replace the file entry list with a new vector.
pub fn set_file_entries(&mut self, entries: Vec<FileEntry>) {
self.file_entries = entries;
trace!(count = self.file_entries.len(), "File entries replaced");
}
/// Find the file entry that contains the given byte offset.
///
/// Uses binary search over the sorted-by-offset file entries.
/// Returns `None` if the offset is out of range or no file entries exist.
///
/// # Algorithm
///
/// Matches C++ `findFileEntryByOffset`:
/// 1. Reject if empty or offset beyond the last file's end.
/// 2. Use `partition_point` to find the insertion point for `offset`.
/// 3. If the entry at the insertion point starts exactly at `offset`, return it.
/// 4. Otherwise return the preceding entry (the file containing `offset`).
pub fn find_file_entry_by_offset(&self, offset: u64) -> Option<&FileEntry> {
if self.file_entries.is_empty() {
return None;
}
let last_entry = self.file_entries.last().expect(
"find_file_entry_by_offset: file_entries is non-empty but last() returned None",
);
let last_offset = last_entry.last_offset();
if offset > 0 && last_offset <= offset {
return None;
}
// partition_point: find first entry whose offset > the target offset
let idx = self
.file_entries
.partition_point(|fe| fe.offset() <= offset);
if idx > 0 {
// The entry at idx-1 has offset <= our target.
// If idx is in bounds and its offset == target, it's an exact match;
// otherwise the preceding entry contains the offset.
if idx < self.file_entries.len() && self.file_entries[idx].offset() == offset {
Some(&self.file_entries[idx])
} else {
Some(&self.file_entries[idx - 1])
}
} else {
// idx == 0 means offset < first entry's offset, which shouldn't
// happen for valid offsets (offset 0 maps to the first entry).
// But if the first entry starts at offset 0, partition_point returns 1.
// If we reach here, the offset is before all entries.
None
}
}
// -----------------------------------------------------------------------
// File Filter
// -----------------------------------------------------------------------
/// Mark file entries as requested / not-requested based on a sorted
/// list of 1-based indices.
///
/// If the index list is empty or there is only one file entry, all
/// entries are marked as requested. Otherwise, entries whose 1-based
/// index appears in `indices` are marked requested; all others are
/// marked not-requested.
///
/// # Arguments
///
/// * `indices` - Sorted, deduplicated, 1-based file indices.
/// Must be >= 1.
pub fn set_file_filter(&mut self, mut indices: Vec<usize>) {
// If no filter or single-file, all entries are requested
if indices.is_empty() || self.file_entries.len() <= 1 {
for fe in &mut self.file_entries {
fe.set_requested(true);
}
return;
}
// Sort and dedup for safety
indices.sort_unstable();
indices.dedup();
let mut filter_iter = indices.iter().peekable();
for (i, fe) in self.file_entries.iter_mut().enumerate() {
// Convert to 1-based index for comparison
let one_based = i + 1;
match filter_iter.peek() {
Some(&idx) if *idx == one_based => {
fe.set_requested(true);
let _ = filter_iter.next();
}
Some(&idx) if *idx > one_based => {
fe.set_requested(false);
}
Some(_) => {
// idx < one_based shouldn't happen with sorted input,
// but mark not-requested as fallback
fe.set_requested(false);
}
None => {
fe.set_requested(false);
}
}
}
debug!(
total = self.file_entries.len(),
requested = self.count_requested_file_entry(),
"File filter applied"
);
}
/// Set the file path for the entry at the given 1-based index.
///
/// # Errors
///
/// Returns an error if `index` is 0 or exceeds the number of file entries.
pub fn set_file_path_with_index(&mut self, index: usize, path: String) -> Result<(), String> {
if index == 0 || index > self.file_entries.len() {
return Err(format!("No such file with index={}", index));
}
// Path is not escaped here — matches C++ behavior
self.file_entries[index - 1].set_path(path);
Ok(())
}
// -----------------------------------------------------------------------
// Path
// -----------------------------------------------------------------------
/// Return the representative path for this context.
///
/// Used as part of the `.aria2` control file name. If `base_path` is set,
/// returns `base_path`. Otherwise returns the first file entry's path.
///
/// # Panics
///
/// Panics if `base_path` is empty and there are no file entries.
pub fn get_base_path(&self) -> &str {
if !self.base_path.is_empty() {
&self.base_path
} else {
self.get_first_file_entry().path()
}
}
/// Set an override path for the `.aria2` control file naming.
pub fn set_base_path(&mut self, path: String) {
self.base_path = path;
}
// -----------------------------------------------------------------------
// Post-download handler support
// -----------------------------------------------------------------------
/// Return the path of the first file entry, if any.
///
/// Used by post-download handlers to determine the downloaded file's
/// location. Mirrors C++ `RequestGroup::getFirstFilePath()`.
pub fn first_file_path(&self) -> Option<&str> {
self.file_entries
.first()
.map(|fe| fe.path())
.filter(|s| !s.is_empty())
}
/// Return a reference to the first file entry, if any.
///
/// Unlike `get_first_file_entry()` which panics if empty, this
/// returns `None`. Used by post-download handlers to safely
/// access file entry metadata.
pub fn first_file_entry(&self) -> Option<&FileEntry> {
self.file_entries.first()
}
// -----------------------------------------------------------------------
// Resource Management
// -----------------------------------------------------------------------
/// Release runtime resources held by all file entries.
///
/// Calls `put_back_request()` and `release_runtime_resource()` on each
/// file entry, clearing in-memory download state while preserving the
/// metadata needed for session persistence.
pub fn release_runtime_resource(&mut self) {
for fe in &mut self.file_entries {
fe.put_back_request();
fe.release_runtime_resource();
}
debug!(
count = self.file_entries.len(),
"Runtime resources released"
);
}
}