diff --git a/crates/librqbit/src/session_persistence/json.rs b/crates/librqbit/src/session_persistence/json.rs index 52b4804..9b0ac89 100644 --- a/crates/librqbit/src/session_persistence/json.rs +++ b/crates/librqbit/src/session_persistence/json.rs @@ -181,7 +181,11 @@ impl BitVFactory for JsonSessionPersistenceStore { async fn load(&self, id: TorrentIdOrHash) -> anyhow::Result>> { let h = self.to_hash(id).await?; let filename = self.bitv_filename(&h); - let f = match std::fs::OpenOptions::new().write(true).open(&filename) { + let f = match std::fs::OpenOptions::new() + .read(true) + .write(true) + .open(&filename) + { Ok(f) => f, Err(e) => match e.kind() { std::io::ErrorKind::NotFound => return Ok(None), @@ -203,18 +207,23 @@ impl BitVFactory for JsonSessionPersistenceStore { .write(true) .create(true) .truncate(true) - .open(&filename) + .open(&tmp_filename) .await .with_context(|| format!("error opening {filename:?}"))?; tokio::io::copy(&mut b.as_raw_slice(), &mut dst) .await .context("error writing bitslice to {filename:?}")?; - tokio::fs::rename(tmp_filename, &filename).await?; + tokio::fs::rename(&tmp_filename, &filename) + .await + .with_context(|| format!("error renaming {tmp_filename:?} to {filename:?}"))?; let f = std::fs::OpenOptions::new() + .read(true) .write(true) .open(&filename) .with_context(|| format!("error opening {filename:?}"))?; - Ok(MmapBitV::new(f)?.into_dyn()) + Ok(MmapBitV::new(f) + .with_context(|| format!("error constructing MmapBitV from file {filename:?}"))? + .into_dyn()) } } @@ -241,11 +250,15 @@ impl SessionPersistenceStore for JsonSessionPersistenceStore { if let Some(t) = removed { debug!(?id, "deleted from in-memory db, flushing"); self.flush().await?; - let tf = self.torrent_bytes_filename(&t.info_hash); - if let Err(e) = tokio::fs::remove_file(&tf).await { - warn!(error=?e, filename=?tf, "error removing torrent file"); - } else { - debug!(filename=?tf, "removed"); + for tf in [ + self.torrent_bytes_filename(&t.info_hash), + self.bitv_filename(&t.info_hash), + ] { + if let Err(e) = tokio::fs::remove_file(&tf).await { + warn!(error=?e, filename=?tf, "error removing"); + } else { + debug!(filename=?tf, "removed"); + } } } else { bail!("error deleting: didn't find torrent id={id}") diff --git a/crates/librqbit/src/torrent_state/initializing.rs b/crates/librqbit/src/torrent_state/initializing.rs index 44c0941..5f7fdb8 100644 --- a/crates/librqbit/src/torrent_state/initializing.rs +++ b/crates/librqbit/src/torrent_state/initializing.rs @@ -99,7 +99,10 @@ impl TorrentStateInitializing { ) .initial_check(&self.checked_bytes) })?; - bitv_factory.store_initial_check(id, have_pieces).await? + bitv_factory + .store_initial_check(id, have_pieces) + .await + .context("error storing initial check bitfield")? } };