It fully works!

This commit is contained in:
Igor Katson 2024-08-20 21:22:56 +01:00
parent 2fee0ca8c2
commit 29c46f2579
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 26 additions and 10 deletions

View file

@ -181,7 +181,11 @@ impl BitVFactory for JsonSessionPersistenceStore {
async fn load(&self, id: TorrentIdOrHash) -> anyhow::Result<Option<Box<dyn BitV>>> { async fn load(&self, id: TorrentIdOrHash) -> anyhow::Result<Option<Box<dyn BitV>>> {
let h = self.to_hash(id).await?; let h = self.to_hash(id).await?;
let filename = self.bitv_filename(&h); 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, Ok(f) => f,
Err(e) => match e.kind() { Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => return Ok(None), std::io::ErrorKind::NotFound => return Ok(None),
@ -203,18 +207,23 @@ impl BitVFactory for JsonSessionPersistenceStore {
.write(true) .write(true)
.create(true) .create(true)
.truncate(true) .truncate(true)
.open(&filename) .open(&tmp_filename)
.await .await
.with_context(|| format!("error opening {filename:?}"))?; .with_context(|| format!("error opening {filename:?}"))?;
tokio::io::copy(&mut b.as_raw_slice(), &mut dst) tokio::io::copy(&mut b.as_raw_slice(), &mut dst)
.await .await
.context("error writing bitslice to {filename:?}")?; .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() let f = std::fs::OpenOptions::new()
.read(true)
.write(true) .write(true)
.open(&filename) .open(&filename)
.with_context(|| format!("error opening {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 { if let Some(t) = removed {
debug!(?id, "deleted from in-memory db, flushing"); debug!(?id, "deleted from in-memory db, flushing");
self.flush().await?; self.flush().await?;
let tf = self.torrent_bytes_filename(&t.info_hash); for tf in [
if let Err(e) = tokio::fs::remove_file(&tf).await { self.torrent_bytes_filename(&t.info_hash),
warn!(error=?e, filename=?tf, "error removing torrent file"); self.bitv_filename(&t.info_hash),
} else { ] {
debug!(filename=?tf, "removed"); if let Err(e) = tokio::fs::remove_file(&tf).await {
warn!(error=?e, filename=?tf, "error removing");
} else {
debug!(filename=?tf, "removed");
}
} }
} else { } else {
bail!("error deleting: didn't find torrent id={id}") bail!("error deleting: didn't find torrent id={id}")

View file

@ -99,7 +99,10 @@ impl TorrentStateInitializing {
) )
.initial_check(&self.checked_bytes) .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")?
} }
}; };