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
use crate::error::{Aria2Error, Result};
use std::path::{Path, PathBuf};

const CONTROL_MAGIC: &[u8; 4] = b"A2CF";
const CONTROL_VERSION: u16 = 1;
const FLAG_HAS_CHECKSUM: u8 = 0x01;
const FLAG_TORRENT_CHECKPOINT: u8 = 0x02;
const FLAG_TORRENT_INFO_HASH: u8 = 0x04;
const FLAG_TORRENT_PIECE_LENGTH: u8 = 0x08;
const CONTROL_HEADER_LEN: usize = 39;

#[derive(Debug, Clone)]
pub struct ControlFile {
    path: PathBuf,
    total_length: u64,
    completed_length: u64,
    upload_length: u64,
    bitfield: Vec<u8>,
    num_pieces: usize,
    checksum_algo: u8,
    checksum_value: Vec<u8>,
    torrent_checkpoint: bool,
    torrent_info_hash: Option<[u8; 20]>,
    torrent_piece_length: Option<u32>,
}

impl ControlFile {
    pub fn path(&self) -> &Path {
        &self.path
    }
    pub fn total_length(&self) -> u64 {
        self.total_length
    }
    pub fn completed_length(&self) -> u64 {
        self.completed_length
    }
    pub fn bitfield(&self) -> &[u8] {
        &self.bitfield
    }
    pub fn set_checksum(&mut self, algo: u8, value: Vec<u8>) {
        self.checksum_algo = algo;
        self.checksum_value = value;
    }
    pub fn checksum_algo(&self) -> u8 {
        self.checksum_algo
    }

    /// Mark this Rust-owned checkpoint as BitTorrent state.
    ///
    /// The marker prevents a generic HTTP/FTP checkpoint with the same output
    /// path and shape from being mistaken for verified torrent pieces.
    pub fn mark_torrent_checkpoint(&mut self) {
        self.torrent_checkpoint = true;
    }

    pub fn is_torrent_checkpoint(&self) -> bool {
        self.torrent_checkpoint
    }

    /// Store the torrent identity for a Rust-owned BitTorrent checkpoint.
    pub fn set_torrent_info_hash(&mut self, info_hash: [u8; 20]) {
        self.torrent_info_hash = Some(info_hash);
    }

    pub fn torrent_info_hash(&self) -> Option<[u8; 20]> {
        self.torrent_info_hash
    }

    /// Store the piece length used by a Rust-owned BitTorrent checkpoint.
    pub fn set_torrent_piece_length(&mut self, piece_length: u32) {
        self.torrent_piece_length = Some(piece_length);
    }

    pub fn torrent_piece_length(&self) -> Option<u32> {
        self.torrent_piece_length
    }

    /// Replace the persisted piece bitfield with a complete snapshot.
    pub fn set_bitfield(&mut self, bitfield: Vec<u8>) {
        self.bitfield = bitfield;
    }

    pub async fn open_or_create(
        ctrl_path: &Path,
        total_length: u64,
        num_pieces: usize,
    ) -> Result<Self> {
        if ctrl_path.exists() {
            let mut control_file = Self::load(ctrl_path).await?.ok_or_else(|| {
                Aria2Error::FileIo(format!(
                    "Failed to load control file: {}",
                    ctrl_path.display()
                ))
            })?;
            // The serialized A2CF bitfield stores bytes, not the caller's
            // logical piece count. Restore that count at the typed open seam
            // so trailing bits and short final pieces are interpreted using
            // the current download layout.
            control_file.normalize_bitfield(num_pieces);
            Ok(control_file)
        } else {
            let bitfield_len = num_pieces.div_ceil(8);
            Ok(Self {
                path: ctrl_path.to_path_buf(),
                total_length,
                completed_length: 0,
                upload_length: 0,
                bitfield: vec![0u8; bitfield_len],
                num_pieces,
                checksum_algo: 0,
                checksum_value: Vec::new(),
                torrent_checkpoint: false,
                torrent_info_hash: None,
                torrent_piece_length: None,
            })
        }
    }

    pub async fn load(path: &Path) -> Result<Option<Self>> {
        if !path.exists() {
            return Ok(None);
        }

        let data = tokio::fs::read(path)
            .await
            .map_err(|e| Aria2Error::FileIo(format!("{}: {e}", path.display())))?;

        if data.len() < CONTROL_HEADER_LEN {
            return Err(Aria2Error::FileIo(format!(
                "Truncated control file header: {} bytes (expected at least {})",
                data.len(),
                CONTROL_HEADER_LEN
            )));
        }

        if &data[0..4] != CONTROL_MAGIC {
            return Err(Aria2Error::FileIo("Invalid control file magic".to_string()));
        }

        let version = u16_from_le(&data[4..6]);
        if version > CONTROL_VERSION {
            return Err(Aria2Error::FileIo(format!(
                "Unsupported version: {}",
                version
            )));
        }

        let flags = data[6];
        let total_length = u64_from_le(&data[7..15]);
        let completed_length = u64_from_le(&data[15..23]);
        let upload_length = u64_from_le(&data[23..31]);
        let bitfield_length = usize::try_from(u64_from_le(&data[31..39])).map_err(|_| {
            Aria2Error::FileIo("Control file bitfield length exceeds platform limits".to_string())
        })?;

        let mut offset = CONTROL_HEADER_LEN;
        let checksum_algo = if flags & FLAG_HAS_CHECKSUM != 0 {
            let algo = *data.get(offset).ok_or_else(|| {
                Aria2Error::FileIo("Truncated control file checksum algorithm".to_string())
            })?;
            offset += 1;
            algo
        } else {
            0
        };

        let checksum_value = if flags & FLAG_HAS_CHECKSUM != 0 && checksum_algo > 0 {
            let len = match checksum_algo {
                1 => 16,
                2 => 20,
                3 => 32,
                4 => 8,
                _ => {
                    return Err(Aria2Error::FileIo(format!(
                        "Unsupported control file checksum algorithm: {}",
                        checksum_algo
                    )));
                }
            };
            let end = offset.checked_add(len).ok_or_else(|| {
                Aria2Error::FileIo("Control file checksum length overflow".to_string())
            })?;
            if end > data.len() {
                return Err(Aria2Error::FileIo(
                    "Truncated control file checksum".to_string(),
                ));
            }
            let val = data[offset..end].to_vec();
            offset = end;
            val
        } else {
            Vec::new()
        };

        let torrent_info_hash = if flags & FLAG_TORRENT_INFO_HASH != 0 {
            let end = offset.checked_add(20).ok_or_else(|| {
                Aria2Error::FileIo("Control file torrent info hash length overflow".to_string())
            })?;
            if end > data.len() {
                return Err(Aria2Error::FileIo(
                    "Truncated control file torrent info hash".to_string(),
                ));
            }
            let mut info_hash = [0u8; 20];
            info_hash.copy_from_slice(&data[offset..end]);
            offset = end;
            Some(info_hash)
        } else {
            None
        };

        let torrent_piece_length = if flags & FLAG_TORRENT_PIECE_LENGTH != 0 {
            let end = offset.checked_add(4).ok_or_else(|| {
                Aria2Error::FileIo("Control file torrent piece length overflow".to_string())
            })?;
            if end > data.len() {
                return Err(Aria2Error::FileIo(
                    "Truncated control file torrent piece length".to_string(),
                ));
            }
            let piece_length = u32_from_le(&data[offset..end]);
            if piece_length == 0 {
                return Err(Aria2Error::FileIo(
                    "Control file torrent piece length must not be 0".to_string(),
                ));
            }
            offset = end;
            Some(piece_length)
        } else {
            None
        };

        let bitfield_end = offset.checked_add(bitfield_length).ok_or_else(|| {
            Aria2Error::FileIo("Control file bitfield length overflow".to_string())
        })?;
        if bitfield_end != data.len() {
            return Err(Aria2Error::FileIo(format!(
                "Control file bitfield length mismatch: declared {}, available {}",
                bitfield_length,
                data.len().saturating_sub(offset)
            )));
        }
        let bitfield = data[offset..bitfield_end].to_vec();
        let num_pieces = bitfield.len() * 8;
        if completed_length > total_length {
            return Err(Aria2Error::FileIo(
                "Control file completed length exceeds total length".to_string(),
            ));
        }

        Ok(Some(Self {
            path: path.to_path_buf(),
            total_length,
            completed_length,
            upload_length,
            bitfield,
            num_pieces,
            checksum_algo,
            checksum_value,
            torrent_checkpoint: flags & FLAG_TORRENT_CHECKPOINT != 0,
            torrent_info_hash,
            torrent_piece_length,
        }))
    }

    pub async fn save(&self) -> Result<()> {
        let mut buf = Vec::with_capacity(64 + self.bitfield.len());

        buf.extend_from_slice(CONTROL_MAGIC);
        buf.extend_from_slice(&CONTROL_VERSION.to_le_bytes());
        let mut flags: u8 = 0;
        if self.checksum_algo > 0 && !self.checksum_value.is_empty() {
            flags |= FLAG_HAS_CHECKSUM;
        }
        if self.torrent_checkpoint {
            flags |= FLAG_TORRENT_CHECKPOINT;
        }
        if self.torrent_info_hash.is_some() {
            flags |= FLAG_TORRENT_INFO_HASH;
        }
        if self.torrent_piece_length.is_some() {
            flags |= FLAG_TORRENT_PIECE_LENGTH;
        }
        buf.push(flags);
        buf.extend_from_slice(&self.total_length.to_le_bytes());
        buf.extend_from_slice(&self.completed_length.to_le_bytes());
        buf.extend_from_slice(&self.upload_length.to_le_bytes());
        buf.extend_from_slice(&(self.bitfield.len() as u64).to_le_bytes());

        if flags & FLAG_HAS_CHECKSUM != 0 {
            buf.push(self.checksum_algo);
            buf.extend_from_slice(&self.checksum_value);
        }

        if let Some(info_hash) = self.torrent_info_hash {
            buf.extend_from_slice(&info_hash);
        }

        if let Some(piece_length) = self.torrent_piece_length {
            buf.extend_from_slice(&piece_length.to_le_bytes());
        }

        buf.extend_from_slice(&self.bitfield);

        let tmp_path = self.path.with_extension("aria2.tmp");
        {
            tokio::fs::write(&tmp_path, &buf)
                .await
                .map_err(|e| Aria2Error::Io(e.to_string()))?;
            if let Ok(f) = tokio::fs::File::open(&tmp_path).await {
                let _ = f.sync_all().await;
            }
        }
        tokio::fs::rename(&tmp_path, &self.path)
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        Ok(())
    }

    pub fn mark_piece_done(&mut self, index: usize) {
        let byte_index = index / 8;
        let bit_index = index % 8;
        if index < self.num_pieces && byte_index < self.bitfield.len() {
            self.bitfield[byte_index] |= 1 << (7 - bit_index);
            self.completed_length = self.calculate_completed();
        }
    }

    fn normalize_bitfield(&mut self, num_pieces: usize) {
        self.num_pieces = num_pieces;
        self.bitfield.resize(num_pieces.div_ceil(8), 0);

        if let Some(last_byte) = self.bitfield.last_mut() {
            let valid_bits = num_pieces % 8;
            if valid_bits != 0 {
                *last_byte &= u8::MAX << (8 - valid_bits);
            }
        }

        self.completed_length = self.calculate_completed();
    }

    pub fn is_piece_done(&self, index: usize) -> bool {
        let byte_index = index / 8;
        let bit_index = index % 8;
        if byte_index < self.bitfield.len() {
            (self.bitfield[byte_index] & (1 << (7 - bit_index))) != 0
        } else {
            false
        }
    }

    pub fn completed_pieces(&self) -> usize {
        self.bitfield.iter().map(|b| b.count_ones() as usize).sum()
    }

    fn calculate_completed(&self) -> u64 {
        if self.total_length == 0 || self.num_pieces == 0 {
            return 0;
        }
        let piece_size = self.total_length.div_ceil(self.num_pieces as u64);
        (0..self.num_pieces)
            .filter(|&index| self.is_piece_done(index))
            .map(|index| {
                let offset = index as u64 * piece_size;
                self.total_length.saturating_sub(offset).min(piece_size)
            })
            .sum()
    }

    pub fn update_completed_length(&mut self, length: u64) {
        self.completed_length = length.min(self.total_length);
    }

    pub fn control_path_for(output_path: &Path) -> PathBuf {
        let mut p = output_path.to_path_buf();
        p.set_extension("aria2");
        p
    }
}

fn u16_from_le(b: &[u8]) -> u16 {
    u16::from_le_bytes([b[0], b[1]])
}

fn u32_from_le(b: &[u8]) -> u32 {
    u32::from_le_bytes([b[0], b[1], b[2], b[3]])
}

fn u64_from_le(b: &[u8]) -> u64 {
    u64::from_le_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_control_path_uses_aria2_suffix() {
        assert_eq!(
            ControlFile::control_path_for(Path::new("payload.bin")),
            PathBuf::from("payload.aria2")
        );
    }

    #[tokio::test]
    async fn test_control_file_new_and_save() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.aria2");

        let cf = ControlFile::open_or_create(&path, 10000, 10).await.unwrap();
        assert_eq!(cf.total_length(), 10000);
        assert_eq!(cf.completed_length(), 0);
        assert!(!cf.is_piece_done(0));

        cf.save().await.unwrap();

        assert!(path.exists());
        let data = tokio::fs::read(&path).await.unwrap();
        assert_eq!(&data[0..4], b"A2CF");
    }

    #[tokio::test]
    async fn test_control_file_mark_and_check_pieces() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.aria2");

        let mut cf = ControlFile::open_or_create(&path, 1000, 8).await.unwrap();

        cf.mark_piece_done(0);
        cf.mark_piece_done(3);
        cf.mark_piece_done(7);

        assert!(cf.is_piece_done(0));
        assert!(!cf.is_piece_done(1));
        assert!(cf.is_piece_done(3));
        assert!(!cf.is_piece_done(5));
        assert!(cf.is_piece_done(7));
        assert_eq!(cf.completed_pieces(), 3);

        cf.save().await.unwrap();

        let loaded = ControlFile::load(&path).await.unwrap().unwrap();
        assert_eq!(loaded.completed_pieces(), 3);
        assert!(loaded.is_piece_done(0));
        assert!(loaded.is_piece_done(7));
    }

    #[tokio::test]
    async fn test_control_file_piece_completion_handles_short_final_piece() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("short_final.aria2");

        let mut cf = ControlFile::open_or_create(&path, 10, 3).await.unwrap();
        cf.mark_piece_done(0);
        cf.mark_piece_done(2);

        assert_eq!(cf.completed_length(), 6);
        assert!(!cf.is_piece_done(3));
    }

    #[tokio::test]
    async fn test_control_file_reload_restores_logical_piece_count() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("reload_piece_count.aria2");

        let mut cf = ControlFile::open_or_create(&path, 10, 3).await.unwrap();
        cf.mark_piece_done(0);
        cf.save().await.unwrap();

        let mut loaded = ControlFile::open_or_create(&path, 10, 3).await.unwrap();
        loaded.mark_piece_done(2);

        assert_eq!(loaded.completed_length(), 6);
        assert!(!loaded.is_piece_done(3));
    }

    #[tokio::test]
    async fn test_control_file_reload_normalizes_legacy_piece_count() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("normalize_piece_count.aria2");

        let mut old = ControlFile::open_or_create(&path, 10, 5).await.unwrap();
        old.mark_piece_done(0);
        old.mark_piece_done(1);
        old.mark_piece_done(4);
        old.save().await.unwrap();

        let loaded = ControlFile::open_or_create(&path, 10, 3).await.unwrap();

        assert_eq!(loaded.bitfield(), &[0b1100_0000]);
        assert_eq!(loaded.completed_pieces(), 2);
        assert!(loaded.is_piece_done(0));
        assert!(loaded.is_piece_done(1));
        assert!(!loaded.is_piece_done(2));
        assert!(!loaded.is_piece_done(3));
        assert_eq!(loaded.completed_length(), 8);
    }

    #[tokio::test]
    async fn test_control_file_roundtrip_with_checksum() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test_hash.aria2");

        let mut cf = ControlFile::open_or_create(&path, 5000, 5).await.unwrap();
        cf.checksum_algo = 2;
        cf.checksum_value = vec![0xAB; 20];
        cf.mark_piece_done(0);
        cf.mark_piece_done(2);
        cf.save().await.unwrap();

        let loaded = ControlFile::load(&path).await.unwrap().unwrap();
        assert_eq!(loaded.total_length(), 5000);
        assert_eq!(loaded.checksum_algo, 2);
        assert_eq!(loaded.completed_pieces(), 2);
    }

    #[tokio::test]
    async fn test_control_file_atomic_save() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test_atomic.aria2");

        let mut cf = ControlFile::open_or_create(&path, 999, 4).await.unwrap();
        cf.mark_piece_done(1);
        cf.save().await.unwrap();

        let tmp_path = path.with_extension("aria2.tmp");
        assert!(!tmp_path.exists());
        assert!(path.exists());
    }

    #[tokio::test]
    async fn test_control_file_load_nonexistent() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nonexistent.aria2");
        let result = ControlFile::load(&path).await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_control_file_load_invalid_magic() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("bad.aria2");
        tokio::fs::write(&path, b"NOT_A2CF_DATA").await.unwrap();

        let result = ControlFile::load(&path).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_control_file_load_truncated_header() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("truncated.aria2");
        for length in [0usize, 7, 8, 38] {
            let mut data = vec![0u8; length];
            if length >= 4 {
                data[..4].copy_from_slice(CONTROL_MAGIC);
            }
            tokio::fs::write(&path, &data).await.unwrap();
            assert!(ControlFile::load(&path).await.is_err(), "length={length}");
        }
    }

    #[tokio::test]
    async fn test_control_file_rejects_invalid_checksum_and_bitfield_lengths() {
        let dir = tempfile::tempdir().expect("failed to create temporary directory");
        let path = dir.path().join("malformed.aria2");
        let mut data = vec![0u8; CONTROL_HEADER_LEN];
        data[..4].copy_from_slice(CONTROL_MAGIC);
        data[6] = FLAG_HAS_CHECKSUM;
        data[31..39].copy_from_slice(&0u64.to_le_bytes());
        data.push(2);
        tokio::fs::write(&path, &data).await.unwrap();
        assert!(ControlFile::load(&path).await.is_err());

        let mut data = vec![0u8; CONTROL_HEADER_LEN];
        data[..4].copy_from_slice(CONTROL_MAGIC);
        data[31..39].copy_from_slice(&1u64.to_le_bytes());
        tokio::fs::write(&path, &data).await.unwrap();
        assert!(ControlFile::load(&path).await.is_err());
    }

    #[tokio::test]
    async fn test_control_path_for_output() {
        let out = Path::new("/downloads/file.iso");
        let ctrl = ControlFile::control_path_for(out);
        assert_eq!(ctrl.extension().unwrap().to_str().unwrap(), "aria2");
        assert!(ctrl.to_str().unwrap().ends_with(".aria2"));
    }

    #[tokio::test]
    async fn test_control_file_update_completed_length() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test_len.aria2");

        let mut cf = ControlFile::open_or_create(&path, 8000, 8).await.unwrap();
        cf.update_completed_length(3500);
        assert_eq!(cf.completed_length(), 3500);

        cf.update_completed_length(9000);
        assert_eq!(cf.completed_length(), 8000);
    }
}