Skip to content

Commit

Permalink
Force set file length on stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatson committed Apr 29, 2024
1 parent feb103e commit f3c12cd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- [x] use some concurrent hashmap e.g. flurry or dashmap
- [x] tracing instead of logging. Debugging peers: RUST_LOG=[{peer=.*}]=debug
test-log for tests
- [x] reopen read only is bugged
- [x] (reopen) read only is bugged
- [x] initializing/checking
- [x] blocks the whole process. Need to break it up. On slower devices (rpi) just hangs for a good while
- [x] checking torrents should be visible right away
Expand Down Expand Up @@ -98,3 +98,4 @@ Other:

- [ ] keepalive is useless, the tieout is 120s, and read timeout is 10s. Need to send keepalive only if nothing was done recently.
- [x] url should have the filename
- [ ] reopening files: get rid of it!!! Even on Windows it should be alright - no need to reopen them.
2 changes: 1 addition & 1 deletion crates/librqbit/src/torrent_state/initializing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
fs::{File, OpenOptions},
fs::OpenOptions,
sync::{atomic::AtomicU64, Arc},
time::Instant,
};
Expand Down
31 changes: 20 additions & 11 deletions crates/librqbit/src/torrent_state/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use librqbit_core::lengths::{Lengths, ValidPieceIndex};
use tokio::io::{AsyncRead, AsyncSeek};
use tracing::{debug, trace};

use crate::{opened_file::OpenedFile, ManagedTorrent};
use crate::{opened_file::OpenedFile, type_aliases::OpenedFiles, ManagedTorrent};

use super::ManagedTorrentHandle;

Expand Down Expand Up @@ -292,21 +292,30 @@ impl Drop for FileStream {
}

impl ManagedTorrent {
fn with_opened_file<F, R>(&self, file_id: usize, f: F) -> anyhow::Result<R>
fn with_opened_files<F, R>(&self, f: F) -> anyhow::Result<R>
where
F: FnOnce(&OpenedFile) -> R,
F: FnOnce(&OpenedFiles) -> R,
{
self.with_state(|s| {
let files = match s {
crate::ManagedTorrentState::Paused(p) => &p.files,
crate::ManagedTorrentState::Live(l) => &l.files,
s => anyhow::bail!("with_opened_file: invalid state {}", s.name()),
};
let fd = files.get(file_id).context("invalid file id")?;
Ok(f(fd))
Ok(f(files))
})
}

fn with_opened_file<F, R>(&self, file_id: usize, f: F) -> anyhow::Result<R>
where
F: FnOnce(&OpenedFile) -> R,
{
self.with_opened_files(|opened_files| {
let fd = opened_files.get(file_id).context("invalid file id")?;
Ok(f(fd))
})?
}

fn streams(&self) -> anyhow::Result<Arc<TorrentStreams>> {
self.with_state(|s| match s {
crate::ManagedTorrentState::Paused(p) => Ok(p.streams.clone()),
Expand Down Expand Up @@ -343,12 +352,12 @@ impl ManagedTorrent {
torrent: self,
};
if s.torrent.maybe_reconnect_needed_peers_for_file(file_id) {
s.torrent.with_opened_file(file_id, |fd| {
fd.reopen(false)?;
fd.file
.lock()
.set_len(fd.len)
.context("error setting file length")
// TODO: get rid of reopening files, it's such a source of bugs and complexity
s.torrent.with_opened_files(|files| {
for file in files {
file.reopen(false)?;
}
Ok::<_, anyhow::Error>(())
})??;
}
streams.streams.insert(
Expand Down

0 comments on commit f3c12cd

Please sign in to comment.