diff --git a/crates/librqbit/src/chunk_tracker.rs b/crates/librqbit/src/chunk_tracker.rs index b830186..ef8db7c 100644 --- a/crates/librqbit/src/chunk_tracker.rs +++ b/crates/librqbit/src/chunk_tracker.rs @@ -156,9 +156,6 @@ impl ChunkTracker { &self.have } - pub fn get_selected_pieces(&self) -> &BF { - &self.selected - } pub fn reserve_needed_piece(&mut self, index: ValidPieceIndex) { self.queue_pieces.set(index.get() as usize, false) } diff --git a/crates/librqbit/src/opened_file.rs b/crates/librqbit/src/opened_file.rs index 357a960..205b7b7 100644 --- a/crates/librqbit/src/opened_file.rs +++ b/crates/librqbit/src/opened_file.rs @@ -7,7 +7,6 @@ use std::{ use anyhow::Context; use librqbit_core::lengths::Lengths; use parking_lot::Mutex; -use tracing::debug; #[derive(Debug)] pub(crate) struct OpenedFile { @@ -63,22 +62,6 @@ impl OpenedFile { piece_range, } } - pub fn reopen(&self, read_only: bool) -> anyhow::Result<()> { - let log_suffix = if read_only { " read only" } else { "" }; - - let mut open_opts = std::fs::OpenOptions::new(); - open_opts.read(true); - if !read_only { - open_opts.write(true).create(false); - } - - let mut g = self.file.lock(); - *g = open_opts - .open(&self.filename) - .with_context(|| format!("error re-opening {:?}{log_suffix}", self.filename))?; - debug!("reopened {:?}{log_suffix}", self.filename); - Ok(()) - } pub fn take(&self) -> anyhow::Result { let mut f = self.file.lock(); diff --git a/crates/librqbit/src/torrent_state/initializing.rs b/crates/librqbit/src/torrent_state/initializing.rs index d77634e..02f1376 100644 --- a/crates/librqbit/src/torrent_state/initializing.rs +++ b/crates/librqbit/src/torrent_state/initializing.rs @@ -121,8 +121,6 @@ impl TorrentStateInitializing { ); } } - - file.reopen(true)?; } Ok::<_, anyhow::Error>(()) })?; diff --git a/crates/librqbit/src/torrent_state/live/mod.rs b/crates/librqbit/src/torrent_state/live/mod.rs index 38e5399..387e09e 100644 --- a/crates/librqbit/src/torrent_state/live/mod.rs +++ b/crates/librqbit/src/torrent_state/live/mod.rs @@ -176,32 +176,6 @@ pub struct TorrentStateLive { cancellation_token: CancellationToken, } -fn reopen_necessary_files_for_write(ct: &ChunkTracker, files: &OpenedFiles) -> anyhow::Result<()> { - // Reopen files that we don't have, but have selected in write-only mode. - for opened_file in files.iter() { - let prange = opened_file.piece_range_usize(); - if prange.is_empty() { - continue; - } - let selected = ct - .get_selected_pieces() - .get(prange.clone()) - .with_context(|| format!("bug: bad range get_selected_pieces(), {prange:?}"))?; - let have = ct - .get_have_pieces() - .get(prange.clone()) - .with_context(|| format!("bug: bad range get_have_pieces(), {prange:?}"))?; - let need_write = selected - .iter() - .zip(have.iter()) - .any(|(selected, have)| *selected && !*have); - if need_write { - opened_file.reopen(false)?; - } - } - Ok(()) -} - impl TorrentStateLive { pub(crate) fn new( paused: TorrentStatePaused, @@ -216,8 +190,6 @@ impl TorrentStateLive { let have_bytes = paused.chunk_tracker.get_hns().have_bytes; let lengths = *paused.chunk_tracker.get_lengths(); - reopen_necessary_files_for_write(&paused.chunk_tracker, &paused.files)?; - // TODO: make it configurable let file_priorities = { let mut pri = (0..paused.files.len()).collect::>(); @@ -661,9 +633,7 @@ impl TorrentStateLive { .iter() .map(|f| f.take_clone()) .collect::>>()?; - for file in files.iter() { - file.reopen(true)?; - } + let mut chunk_tracker = g .chunks .take() @@ -697,7 +667,6 @@ impl TorrentStateLive { let mut g = self.lock_write("update_only_files"); let ct = g.get_chunks_mut()?; let hns = ct.update_only_files(self.files.iter().map(|f| f.len), only_files)?; - reopen_necessary_files_for_write(ct, &self.files)?; if !hns.finished() { self.reconnect_all_not_needed_peers(); } @@ -721,19 +690,6 @@ impl TorrentStateLive { if bytes == 0 { warn!(file_id=idx, piece_id=id.get(), "bug: update_have_on_piece_completed() returned 0, although this piece is present in the file"); } - - let have_all = self - .lock_read("on_piece_completed_reopen") - .get_chunks()? - .get_have_pieces() - .get(opened_file.piece_range_usize()) - .with_context(|| { - format!("bug: invalid range {:?}", opened_file.piece_range_usize()) - })? - .all(); - if have_all { - opened_file.reopen(true)?; - } } if self.is_finished() {