fx-torrent 0.10.3

FX Torrent is a feature rich Bittorrent protocol implementation written in rust supporting Linux, MacOS and Windows.
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
use crate::storage::{Result, Storage};
use crate::{PieceBlock, PieceIndex};
use itertools::Itertools;
use log::trace;
use std::collections::HashMap;
use std::io;
use std::time::Instant;

/// The index key of available slots within the cache buffer.
type SlotIndex = usize;

/// The allocation type for a piece within the cache.
#[derive(Debug, Copy, Clone)]
enum Allocation {
    /// The slot index where this piece is stored.
    Slot(SlotIndex),
    /// The piece has been stored in the storage and is no longer available in the cache buffer.
    Storage,
}

#[derive(Debug)]
pub struct PickerCache {
    total_pieces: usize,
    piece_len: usize,
    last_piece_len: usize,
    /// The allocations for piece data.
    pieces: HashMap<PieceIndex, Allocation>,
    /// The slots allocated within the buffer.
    slots: Vec<CacheSlot>,
    /// The in-memory cache buffer of incomplete data.
    buffer: Vec<u8>,
    /// The storage to use for flushing the cache when the limit is exceeded.
    storage: Storage,
}

impl PickerCache {
    /// Create a new cache instance for storing incomplete piece data.
    pub fn new(storage: Storage, limit: usize) -> Self {
        Self {
            total_pieces: 0,
            piece_len: 0,
            last_piece_len: 0,
            slots: Default::default(),
            pieces: Default::default(),
            buffer: vec![0u8; limit],
            storage,
        }
    }

    /// Resize the cache.
    /// This replaces any previous allocations and resets the cache state.
    pub fn resize(&mut self, total_pieces: usize, piece_len: usize, last_piece_len: usize) {
        self.total_pieces = total_pieces;
        self.piece_len = piece_len;
        self.last_piece_len = last_piece_len;

        // make sure the buffer is correctly sized to store X pieces
        let buffer_slots = (self.buffer.len() + 1) / self.piece_len;
        self.buffer.resize(buffer_slots * self.piece_len, 0);
        self.slots = (0..buffer_slots)
            .into_iter()
            .map(|i| CacheSlot {
                offset: i * self.piece_len,
                piece: None,
                last_activity: Instant::now(),
            })
            .collect();
        trace!(
            "Picker cache resized to {} pieces, allocated {} bytes to the memory buffer",
            total_pieces,
            self.buffer.len()
        );
    }

    /// Read the data for the given piece from the cache.
    /// The data is either retrieved from in-memory or the underlying storage.
    pub async fn read(&self, piece: &PieceIndex) -> Result<Vec<u8>> {
        match self.pieces.get(piece) {
            None => Err(io::Error::new(io::ErrorKind::NotFound, "piece not found")),
            Some(Allocation::Slot(_)) => self.read_piece_buffer(piece),
            Some(Allocation::Storage) => self.read_piece_storage(piece).await,
        }
    }

    /// Write the piece block data to the cache.
    pub async fn write(&mut self, block: &PieceBlock, data: Vec<u8>) -> Result<()> {
        // early exit if the data doesn't match the expected block length
        if data.len() != block.length {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "invalid data length",
            ));
        }
        // early exit if the block is out of bounds
        if block.begin + block.length > self.piece_len {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "block is out-of-bounds",
            ));
        }

        match self.pieces.get(&block.piece) {
            Some(Allocation::Slot(_)) => return self.write_block_to_buffer(block, data),
            Some(Allocation::Storage) => return self.write_block_to_storage(block, data).await,
            _ => {}
        };

        let slot_index = match self.slots.iter().position(|slot| slot.piece.is_none()) {
            Some(index) => index as SlotIndex,
            None => self.move_lru_to_storage().await?,
        };

        // reserve the free slot for the piece
        {
            let slot = &mut self.slots[slot_index];

            slot.piece = Some(block.piece);
            slot.last_activity = Instant::now();
            self.pieces
                .insert(block.piece, Allocation::Slot(slot_index));
        }

        // write the block data to the reserved buffer slot
        self.write_block_to_buffer(block, data)
    }

    /// Flush the piece data to the underlying [Storage].
    ///
    /// This persists the data within the storage, if it's currently stored in-memory.
    /// Otherwise, this is a no-op.
    pub async fn flush(&mut self, piece: &PieceIndex) -> Result<()> {
        let (slot_index, data) = match self.pieces.get(piece) {
            None => return Err(io::Error::new(io::ErrorKind::NotFound, "piece not found")),
            Some(Allocation::Storage) => return Ok(()), // data is already in storage, nothing to do
            Some(Allocation::Slot(slot_index)) => (*slot_index, self.read_piece_buffer(piece)?),
        };

        self.storage.write(data.as_slice(), piece, 0).await?;
        self.pieces.insert(*piece, Allocation::Storage);
        self.slots[slot_index].piece = None;

        Ok(())
    }

    /// Discard the piece data from the cache, without flushing it to the storage.
    pub fn discard(&mut self, piece: &PieceIndex) {
        match self.pieces.remove(piece) {
            Some(Allocation::Slot(slot_index)) => {
                self.slots[slot_index].piece = None;
            }
            _ => {}
        }
    }

    /// Try to read the data for the given piece index.
    /// Returns the data from the buffer cache if available, else [None].
    fn read_piece_buffer(&self, piece: &PieceIndex) -> Result<Vec<u8>> {
        let slot_index = match self.pieces.get(piece) {
            Some(Allocation::Slot(slot_index)) => *slot_index,
            _ => {
                return Err(io::Error::new(
                    io::ErrorKind::NotFound,
                    "buffer data not found",
                ))
            }
        };

        let len = self.piece_len(piece);
        self.slots
            .get(slot_index)
            .map(|slot| self.buffer[slot.offset..slot.offset + len].to_vec())
            .ok_or(io::Error::new(
                io::ErrorKind::NotFound,
                "buffer data not found",
            ))
    }

    /// Returns the data for the piece from the storage, if available.
    async fn read_piece_storage(&self, piece: &PieceIndex) -> Result<Vec<u8>> {
        let len = self.piece_len(&piece);
        let mut buffer = vec![0u8; len];

        let read = self.storage.read(&mut buffer, &piece, 0).await?;
        if read != len {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "unexpected end of piece",
            ));
        }

        Ok(buffer)
    }

    /// Write the data of the block to the cache buffer.
    fn write_block_to_buffer(&mut self, block: &PieceBlock, data: Vec<u8>) -> Result<()> {
        let slot_index = match self.pieces.get(&block.piece) {
            Some(Allocation::Slot(index)) => *index,
            _ => return Ok(()),
        };
        let slot = match self.slots.get_mut(slot_index) {
            None => {
                return Err(io::Error::new(
                    io::ErrorKind::NotFound,
                    "buffer slot not found",
                ))
            }
            Some(slot) => slot,
        };

        let start = slot.offset + block.begin;
        let end = start + block.length;
        self.buffer[start..end].copy_from_slice(&data);
        slot.last_activity = Instant::now();
        Ok(())
    }

    /// Write the piece block data to the storage.
    async fn write_block_to_storage(&mut self, block: &PieceBlock, data: Vec<u8>) -> Result<()> {
        let written = self
            .storage
            .write(data.as_slice(), &block.piece, block.begin)
            .await?;
        if written != block.length {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                "failed to write piece to storage",
            ));
        }

        Ok(())
    }

    /// Move the **Least Recently Used** piece to the storage.
    /// Returns the slot index of the slot that has been freed up within the cache buffer.
    async fn move_lru_to_storage(&mut self) -> Result<SlotIndex> {
        if self.slots.is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "no buffer slots allocated",
            ));
        }

        // move the least recently used piece to the storage
        let (slot_index, piece) = match self
            .slots
            .iter()
            .enumerate()
            .sorted_by(|(_, a), (_, b)| b.last_activity.cmp(&a.last_activity))
            .find(|(_, slot)| slot.piece.is_some())
            .and_then(|(index, slot)| slot.piece.map(|piece| (index, piece)))
        {
            Some((index, piece)) => (index, piece),
            None => unreachable!("slot should have a piece"),
        };

        // move the buffer data of the lru piece to the storage
        let data = self.read_piece_buffer(&piece)?;
        self.storage.write(data.as_slice(), &piece, 0).await?;
        self.pieces.insert(piece, Allocation::Storage);
        self.slots[slot_index].piece = None;

        Ok(slot_index)
    }

    /// Returns the length of the piece.
    fn piece_len(&self, piece: &PieceIndex) -> usize {
        if *piece == self.total_pieces - 1 {
            self.last_piece_len
        } else {
            self.piece_len
        }
    }
}

#[derive(Debug)]
struct CacheSlot {
    /// The offset within the `buffer` this piece entry starts at.
    offset: usize,
    /// The piece this slot is used by, `None` if not in use.
    piece: Option<PieceIndex>,
    /// The last time when data was written to the cache for this piece.
    last_activity: Instant,
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::{rng, RngExt};
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_resize() {
        init_logger!();
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path().to_str().unwrap();
        let (context, _rx) = torrent_context!(
            "debian.torrent",
            temp_path,
            TorrentFlags::none(),
            TorrentConfig::default(),
            vec![],
            vec![],
            None
        );
        let metadata = context.metadata().info.clone().unwrap();
        let mut cache = PickerCache::new(context.storage().clone(), 32 * 1024 * 1024);

        // resize the cache
        let total_pieces = context.metadata().total_pieces().unwrap();
        cache.resize(total_pieces, metadata.piece_length as usize, 1524);

        assert_eq!(total_pieces, cache.total_pieces);
        assert_eq!(metadata.piece_length as usize, cache.piece_len);
        assert_eq!(1524, cache.last_piece_len);
        assert_eq!(33554432, cache.buffer.len());
        assert_eq!(128, cache.slots.len());
    }

    #[tokio::test]
    async fn test_read() {
        init_logger!();
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path().to_str().unwrap();
        let (context, _rx) = torrent_context!(
            "debian.torrent",
            temp_path,
            TorrentFlags::none(),
            TorrentConfig::default(),
            vec![],
            vec![],
            None
        );
        let mut cache = PickerCache::new(context.storage().clone(), 32 * 1024 * 1024);

        // resize the cache
        let piece_len = 3072;
        let total_pieces = context.metadata().total_pieces().unwrap();
        cache.resize(total_pieces, piece_len, piece_len);

        // generate the piece data
        let mut piece_data = vec![0u8; piece_len];
        rng().fill(&mut piece_data[..]);

        // write all blocks to the cache
        let block_len = piece_len / 3;
        for block in 0..3 {
            let block = PieceBlock {
                piece: 0,
                block,
                begin: block_len * block,
                length: block_len,
            };
            let block_data = piece_data[block.begin..block.begin + block.length].to_vec();
            cache
                .write(&block, block_data)
                .await
                .expect("expected the data to have been written");
        }

        // read the data from the cache
        let result = cache.read(&0).await.unwrap();
        assert_eq!(
            piece_len,
            result.len(),
            "expected the read data to be {} bytes",
            piece_len
        );
        assert_eq!(piece_data, result, "expected the data to match");
    }

    #[tokio::test]
    async fn test_discard() {
        init_logger!();
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path().to_str().unwrap();
        let (context, _rx) = torrent_context!(
            "debian.torrent",
            temp_path,
            TorrentFlags::none(),
            TorrentConfig::default(),
            vec![],
            vec![],
            None
        );
        let block = PieceBlock {
            piece: 0,
            block: 3,
            begin: 3072,
            length: 1024,
        };
        let metadata = context.metadata().info.clone().unwrap();
        let mut cache = PickerCache::new(context.storage().clone(), 32 * 1024 * 1024);

        // resize the cache
        let total_pieces = context.metadata().total_pieces().unwrap();
        cache.resize(
            total_pieces,
            metadata.piece_length as usize,
            metadata.len() % metadata.piece_length as usize,
        );

        // write data to the cache
        let mut buffer = vec![0u8; block.length];
        rng().fill(&mut buffer[..]);
        cache
            .write(&block, buffer)
            .await
            .expect("expected the data to have been written");
        let slot_index = {
            let slot = cache
                .pieces
                .get(&block.piece)
                .expect("expected the piece to have been stored in the cache");

            match slot {
                Allocation::Slot(index) => {
                    assert_eq!(&0, index, "expected the first slot to have been allocated");
                    *index
                }
                _ => {
                    assert!(false, "expected Allocation::Slot, but got {:?}", slot);
                    0
                }
            }
        };

        // discard the piece from the cache
        cache.discard(&block.piece);
        let slot = cache
            .slots
            .get(slot_index)
            .expect("expected the slot to be in the cache");
        assert_eq!(None, slot.piece, "expected the piece to be discarded");
        let result = cache.pieces.contains_key(&block.piece);
        assert_eq!(false, result, "expected the piece to be discarded");
    }

    mod write {
        use super::*;

        #[tokio::test]
        async fn test_invalid_data_length() {
            init_logger!();
            let temp_dir = tempdir().unwrap();
            let temp_path = temp_dir.path().to_str().unwrap();
            let (context, _rx) = torrent_context!(
                "debian.torrent",
                temp_path,
                TorrentFlags::none(),
                TorrentConfig::default(),
                vec![],
                vec![],
                None
            );
            let block = PieceBlock {
                piece: 0,
                block: 0,
                begin: 0,
                length: 1024,
            };
            let mut cache = PickerCache::new(context.storage().clone(), 32 * 1024 * 1024);

            // resize the cache
            cache.resize(128, 1024, 1024);

            // write the block data
            let mut data = vec![0u8; 960];
            rng().fill(&mut data[..]);
            let result = cache.write(&block, data).await;
            match result {
                Err(e) => {
                    assert_eq!("invalid data length", e.to_string());
                }
                _ => assert!(false, "expected Err, but got {:?}", result),
            }
        }

        #[tokio::test]
        async fn test_block_out_of_bounds() {
            init_logger!();
            let temp_dir = tempdir().unwrap();
            let temp_path = temp_dir.path().to_str().unwrap();
            let (context, _rx) = torrent_context!(
                "debian.torrent",
                temp_path,
                TorrentFlags::none(),
                TorrentConfig::default(),
                vec![],
                vec![],
                None
            );
            let block = PieceBlock {
                piece: 0,
                block: 1,
                begin: 512,
                length: 1024,
            };
            let mut cache = PickerCache::new(context.storage().clone(), 32 * 1024 * 1024);

            // resize the cache
            cache.resize(128, 1024, 1024);

            // write the block data
            let mut data = vec![0u8; 1024];
            rng().fill(&mut data[..]);
            let result = cache.write(&block, data).await;
            match result {
                Err(e) => {
                    assert_eq!("block is out-of-bounds", e.to_string());
                }
                _ => assert!(false, "expected Err, but got {:?}", result),
            }
        }

        #[tokio::test]
        async fn test_write_to_storage() {
            init_logger!();
            let temp_dir = tempdir().unwrap();
            let temp_path = temp_dir.path().to_str().unwrap();
            let (context, _rx) = torrent_context!(
                "debian.torrent",
                temp_path,
                TorrentFlags::none(),
                TorrentConfig::default(),
                vec![],
                vec![],
                None
            );
            let block = PieceBlock {
                piece: 0,
                block: 0,
                begin: 0,
                length: 2048,
            };
            let mut cache = PickerCache::new(context.storage().clone(), 2048);

            // resize the cache
            cache.resize(128, 2048, 2048);

            // fill the memory cache
            let mut data = vec![0u8; 2048];
            rng().fill(&mut data[..]);
            cache
                .write(
                    &PieceBlock {
                        piece: 1,
                        block: 0,
                        begin: 0,
                        length: 2048,
                    },
                    data.clone(),
                )
                .await
                .unwrap();

            // write block data to storage
            let mut data = vec![0u8; 2048];
            rng().fill(&mut data[..]);
            let result = cache.write(&block, data.clone()).await;
            match result {
                Ok(_) => {}
                _ => assert!(false, "expected Ok, but got {:?}", result),
            }

            // read the data again from the storage
            let result = cache.read(&0).await;
            match result {
                Ok(result) => assert_eq!(data, result, "expected the stored data to match"),
                _ => assert!(false, "expected Ok, but got {:?}", result),
            }
        }
    }
}