Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Other fixes found by e2e test #197

Merged
merged 11 commits into from
Aug 19, 2024
Merged
85 changes: 83 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/librqbit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ storage_middleware = ["lru"]
storage_examples = []
tracing-subscriber-utils = ["tracing-subscriber"]
postgres = ["sqlx"]
async-bt = ["async-backtrace"]

[dependencies]
sqlx = { version = "0.7", features = [
Expand Down Expand Up @@ -88,6 +89,7 @@ lru = { version = "0.12.3", optional = true }
mime_guess = { version = "2.0.5", default-features = false }
tokio-socks = "0.5.2"
async-trait = "0.1.81"
async-backtrace = { version = "0.2", optional = true }

[build-dependencies]
anyhow = "1"
Expand Down
19 changes: 0 additions & 19 deletions crates/librqbit/src/chunk_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,25 +222,6 @@ impl ChunkTracker {
self.have[id.get() as usize]
}

// None if wrong chunk
// true if did something
// false if didn't do anything
pub fn mark_chunk_request_cancelled(
&mut self,
index: ValidPieceIndex,
_chunk: u32,
) -> Option<bool> {
if *self.have.get(index.get() as usize)? {
return Some(false);
}
// This will trigger the requesters to re-check each chunk in this piece.
let chunk_range = self.lengths.chunk_range(index);
if !self.chunk_status.get(chunk_range)?.all() {
self.queue_pieces.set(index.get() as usize, true);
}
Some(true)
}

pub fn mark_piece_broken_if_not_have(&mut self, index: ValidPieceIndex) {
if self
.have
Expand Down
13 changes: 13 additions & 0 deletions crates/librqbit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@

#![warn(clippy::cast_possible_truncation)]

macro_rules! aframe {
($e:expr) => {{
#[cfg(feature = "async-bt")]
{
async_backtrace::frame!($e)
}
#[cfg(not(feature = "async-bt"))]
{
$e
}
}};
}

pub mod api;
mod api_error;
mod chunk_tracker;
Expand Down
2 changes: 1 addition & 1 deletion crates/librqbit/src/peer_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
.await
.context("error reading handshake")?;
let h_supports_extended = h.supports_extended();
trace!(
debug!(
"connected: id={:?}",
try_decode_peer_id(Id20::new(h.peer_id))
);
Expand Down
46 changes: 37 additions & 9 deletions crates/librqbit/src/storage/filesystem/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,29 @@ impl TorrentStorage for FilesystemStorage {
#[cfg(target_family = "unix")]
{
use std::os::unix::fs::FileExt;
Ok(of.file.read().read_exact_at(buf, offset)?)
Ok(of
.file
.read()
.as_ref()
.context("file is None")?
.read_exact_at(buf, offset)?)
}
#[cfg(not(target_family = "unix"))]
#[cfg(target_family = "windows")]
{
use std::os::windows::fs::FileExt;
let mut remaining = buf.len();
let g = of.file.read();
let f = g.as_ref().context("file is None")?;
f.seek_read(buf, offset)?;
Ok(())
}
#[cfg(not(any(target_family = "unix", target_family = "windows")))]
{
use std::io::{Read, Seek, SeekFrom};
let mut g = of.file.write();
g.seek(SeekFrom::Start(offset))?;
Ok(g.read_exact(buf)?)
let mut f = g.as_ref().context("file is None")?;
f.seek(SeekFrom::Start(offset))?;
Ok(f.read_exact(buf)?)
}
}

Expand All @@ -70,23 +85,31 @@ impl TorrentStorage for FilesystemStorage {
#[cfg(target_family = "unix")]
{
use std::os::unix::fs::FileExt;
Ok(of.file.read().write_all_at(buf, offset)?)
Ok(of
.file
.read()
.as_ref()
.context("file is None")?
.write_all_at(buf, offset)?)
}
#[cfg(target_family = "windows")]
{
use std::os::windows::fs::FileExt;
let mut remaining = buf.len();
let g = of.file.read();
let f = g.as_ref().context("file is None")?;
while remaining > 0 {
remaining -= of.file.read().seek_write(buf, offset)?;
remaining -= f.seek_write(buf, offset)?;
}
Ok(())
}
#[cfg(not(any(target_family = "unix", target_family = "windows")))]
{
use std::io::{Read, Seek, SeekFrom, Write};
let mut g = of.file.write();
g.seek(SeekFrom::Start(offset))?;
Ok(g.write_all(buf)?)
let mut f = g.as_ref().context("file is None")?;
f.seek(SeekFrom::Start(offset))?;
Ok(f.write_all(buf)?)
}
}

Expand All @@ -95,7 +118,12 @@ impl TorrentStorage for FilesystemStorage {
}

fn ensure_file_length(&self, file_id: usize, len: u64) -> anyhow::Result<()> {
Ok(self.opened_files[file_id].file.write().set_len(len)?)
Ok(self.opened_files[file_id]
.file
.write()
.as_ref()
.context("file is None")?
.set_len(len)?)
}

fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
Expand Down
3 changes: 2 additions & 1 deletion crates/librqbit/src/storage/filesystem/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ impl TorrentStorage for MmapFilesystemStorage {
let mut mmaps = Vec::new();
for (idx, file) in self.fs.opened_files.iter().enumerate() {
let fg = file.file.write();
let fg = fg.as_ref().context("file is None")?;
fg.set_len(meta.file_infos[idx].len)
.context("mmap storage: error setting length")?;
let mmap = unsafe { MmapOptions::new().map_mut(&*fg) }.context("error mapping file")?;
let mmap = unsafe { MmapOptions::new().map_mut(fg) }.context("error mapping file")?;
mmaps.push(RwLock::new(mmap));
}

Expand Down
23 changes: 4 additions & 19 deletions crates/librqbit/src/storage/filesystem/opened_file.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
use std::fs::File;

use anyhow::Context;
use parking_lot::RwLock;

#[derive(Debug)]
pub(crate) struct OpenedFile {
pub file: RwLock<File>,
}

pub(crate) fn dummy_file() -> anyhow::Result<std::fs::File> {
#[cfg(target_os = "windows")]
const DEVNULL: &str = "NUL";
#[cfg(not(target_os = "windows"))]
const DEVNULL: &str = "/dev/null";

std::fs::OpenOptions::new()
.read(true)
.open(DEVNULL)
.with_context(|| format!("error opening {}", DEVNULL))
pub file: RwLock<Option<File>>,
}

impl OpenedFile {
pub fn new(f: File) -> Self {
Self {
file: RwLock::new(f),
file: RwLock::new(Some(f)),
}
}

pub fn take(&self) -> anyhow::Result<File> {
pub fn take(&self) -> anyhow::Result<Option<File>> {
let mut f = self.file.write();
let dummy = dummy_file()?;
let f = std::mem::replace(&mut *f, dummy);
Ok(f)
Ok(f.take())
}

pub fn take_clone(&self) -> anyhow::Result<Self> {
Expand Down
Loading