use assertion for the logic implementation errors

This commit is contained in:
yggverse 2025-08-08 18:21:38 +03:00
parent c3bb7c8b4c
commit 2a953ec0da

View file

@ -54,22 +54,14 @@ impl Preload {
p.canonicalize()? p.canonicalize()?
}; };
// make sure preload path is referring to the expected location // make sure preload path is referring to the expected location
if !tmp_file.starts_with(&self.root) || tmp_file.is_dir() { assert!(tmp_file.starts_with(&self.root) && !tmp_file.is_dir());
bail!("unexpected canonical path `{}`", tmp_file.to_string_lossy())
}
// build new permanent path /root/info-hash // build new permanent path /root/info-hash
let mut permanent_file = PathBuf::from(&permanent_dir); let mut permanent_file = PathBuf::from(&permanent_dir);
for component in tmp_file.components().skip(components_count) { for component in tmp_file.components().skip(components_count) {
permanent_file.push(component) permanent_file.push(component)
} }
// make sure segments count is same to continue // make sure segments count is same to continue
if tmp_file.components().count() != permanent_file.components().count() { assert!(tmp_file.components().count() == permanent_file.components().count());
bail!(
"unexpected components count: `{}` > `{}`",
tmp_file.to_string_lossy(),
permanent_file.to_string_lossy(),
)
}
// move `persist_files` from temporary to permanent location // move `persist_files` from temporary to permanent location
fs::create_dir_all(permanent_file.parent().unwrap())?; fs::create_dir_all(permanent_file.parent().unwrap())?;
fs::rename(&tmp_file, &permanent_file)?; fs::rename(&tmp_file, &permanent_file)?;
@ -102,9 +94,7 @@ impl Preload {
pub fn tmp_dir(&self, info_hash: &str, is_create: bool) -> Result<PathBuf> { pub fn tmp_dir(&self, info_hash: &str, is_create: bool) -> Result<PathBuf> {
let mut p = PathBuf::from(&self.root); let mut p = PathBuf::from(&self.root);
p.push(tmp_component(info_hash)); p.push(tmp_component(info_hash));
if p.is_file() { assert!(!p.is_file());
bail!("output directory `{}` is file", p.to_string_lossy())
}
if is_create && !p.exists() { if is_create && !p.exists() {
fs::create_dir(&p)?; fs::create_dir(&p)?;
log::debug!("create tmp directory `{}`", p.to_string_lossy()) log::debug!("create tmp directory `{}`", p.to_string_lossy())
@ -117,9 +107,7 @@ impl Preload {
fn permanent_dir(&self, info_hash: &str, is_clear: bool) -> Result<PathBuf> { fn permanent_dir(&self, info_hash: &str, is_clear: bool) -> Result<PathBuf> {
let mut p = PathBuf::from(&self.root); let mut p = PathBuf::from(&self.root);
p.push(info_hash); p.push(info_hash);
if p.is_file() { assert!(!p.is_file());
bail!("permanent directory `{}` is file", p.to_string_lossy())
}
if is_clear && p.exists() { if is_clear && p.exists() {
// clean previous data // clean previous data
fs::remove_dir_all(&p)?; fs::remove_dir_all(&p)?;
@ -144,6 +132,7 @@ impl Preload {
fn torrent(&self, info_hash: &str) -> PathBuf { fn torrent(&self, info_hash: &str) -> PathBuf {
let mut p = PathBuf::from(&self.root); let mut p = PathBuf::from(&self.root);
p.push(format!("{info_hash}.torrent")); p.push(format!("{info_hash}.torrent"));
assert!(!p.is_dir());
p p
} }
} }