implement temporary data cleanup on commit preload content

This commit is contained in:
yggverse 2025-08-08 00:50:12 +03:00
parent 6f4d2894b3
commit 3e053fa806
3 changed files with 58 additions and 27 deletions

View file

@ -31,35 +31,66 @@ impl Preload {
// Actions
/// Recursively remove all files under the `infohash` location (see rqbit#408)
pub fn cleanup(&self, info_hash: &str, keep_filenames: Option<HashSet<PathBuf>>) -> Result<()> {
for e in walkdir::WalkDir::new(self.output_folder(info_hash)?) {
let e = e?;
let p = e.into_path();
if p.is_file() && keep_filenames.as_ref().is_none_or(|k| !k.contains(&p)) {
fs::remove_file(p)?;
/// Persist torrent bytes and preloaded content, cleanup tmp (see rqbit#408)
pub fn commit(
&self,
info_hash: &str,
torrent_bytes: &[u8],
persist_files: Option<HashSet<PathBuf>>,
) -> Result<()> {
// persist torrent bytes to file
fs::write(self.torrent(info_hash)?, torrent_bytes)?;
// persist preload files
let mut d = PathBuf::from(&self.root);
d.push(info_hash);
if d.exists() {
// clean previous data
fs::remove_dir_all(&d)?
}
if let Some(f) = persist_files {
let r = d.components().count(); // count root offset once
for p in f {
// make sure preload path is referring to the expected location
let o = p.canonicalize()?;
if !o.starts_with(&self.root) || o.is_dir() {
bail!("Unexpected canonical path `{}`", o.to_string_lossy())
}
// build new permanent path /root/info-hash
let mut n = PathBuf::from(&d);
for component in o.components().skip(r) {
n.push(component)
}
// make sure segments count is same to continue
if o.components().count() != n.components().count() {
bail!(
"Unexpected components count: `{}` > `{}`",
o.to_string_lossy(),
n.to_string_lossy(),
)
}
// fs::create_dir_all(n.parent().unwrap())?;
fs::rename(o, n)?
}
} // remove empty directories @TODO
}
// cleanup temporary files
let t = self.tmp(info_hash)?;
if t.exists() {
fs::remove_dir_all(t)?
}
Ok(())
}
pub fn persist_torrent_bytes(&self, info_hash: &str, contents: &[u8]) -> Result<PathBuf> {
let p = self.torrent(info_hash)?;
fs::write(&p, contents)?;
Ok(p)
}
// Getters
/// * creates new directory if not exists
pub fn output_folder(&self, info_hash: &str) -> Result<PathBuf> {
/// * creates new temporary directory if not exists
pub fn tmp(&self, info_hash: &str) -> Result<PathBuf> {
if !is_info_hash(info_hash) {
bail!("Invalid info-hash `{info_hash}`")
}
let mut p = PathBuf::from(&self.root);
p.push(info_hash);
p.push(tmp(info_hash));
if p.is_file() {
bail!("Output directory for info-hash `{info_hash}` is file")
bail!("Output directory `{}` is file", p.to_string_lossy())
}
if !p.exists() {
fs::create_dir(&p)?
@ -88,3 +119,7 @@ impl Preload {
fn is_info_hash(value: &str) -> bool {
value.len() == 40 && value.chars().all(|c| c.is_ascii_hexdigit())
}
fn tmp(info_hash: &str) -> String {
format!(".{info_hash}")
}