Clear have_bitfield on error

This commit is contained in:
Igor Katson 2024-08-21 18:21:15 +01:00
parent b7ed850918
commit c697809e50
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
8 changed files with 88 additions and 32 deletions

View file

@ -196,6 +196,14 @@ impl BitVFactory for JsonSessionPersistenceStore {
Ok(Some(MmapBitV::new(f)?.into_dyn()))
}
async fn clear(&self, id: TorrentIdOrHash) -> anyhow::Result<()> {
let h = self.to_hash(id).await?;
let filename = self.bitv_filename(&h);
tokio::fs::remove_file(&filename)
.await
.with_context(|| format!("error removing {filename:?}"))
}
async fn store_initial_check(
&self,
id: TorrentIdOrHash,

View file

@ -313,4 +313,31 @@ impl BitVFactory for PostgresSessionStorage {
bf.flush()?;
Ok(bf.into_dyn())
}
async fn clear(&self, id: TorrentIdOrHash) -> anyhow::Result<()> {
macro_rules! exec {
($q:expr, $v:expr) => {
sqlx::query($q)
.bind($v)
.execute(&self.pool)
.await
.context($q)
.context("error executing query")?
};
}
match id {
TorrentIdOrHash::Id(id) => {
let id: i32 = id.try_into()?;
exec!("UPDATE torrents SET have_bitfield = NULL WHERE id = $1", id);
}
TorrentIdOrHash::Hash(h) => {
exec!(
"UPDATE torrents SET have_bitfield = NULL WHERE info_hash = $1",
&h.0[..]
);
}
}
Ok(())
}
}