mirror of
https://github.com/YGGverse/btracker.git
synced 2026-03-31 17:15:31 +00:00
add PathBuf resolved validation
This commit is contained in:
parent
66c9e35481
commit
422ee62c3d
1 changed files with 19 additions and 15 deletions
|
|
@ -3,7 +3,7 @@ use std::{fs, path::PathBuf};
|
|||
|
||||
/// Temporary file storage for `librqbit` preload data
|
||||
pub struct Preload {
|
||||
directory: PathBuf,
|
||||
root: PathBuf,
|
||||
pub max_filecount: Option<usize>,
|
||||
pub max_filesize: Option<u64>,
|
||||
}
|
||||
|
|
@ -20,21 +20,17 @@ impl Preload {
|
|||
Ok(Self {
|
||||
max_filecount,
|
||||
max_filesize,
|
||||
directory,
|
||||
root: directory.canonicalize()?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn clear_output_folder(&self, info_hash: &str) -> Result<()> {
|
||||
let mut p = PathBuf::from(&self.directory);
|
||||
p.push(info_hash);
|
||||
fs::remove_dir_all(&p)?;
|
||||
Ok(())
|
||||
Ok(fs::remove_dir_all(&self.path(&PathBuf::from(info_hash))?)?)
|
||||
}
|
||||
|
||||
/// * create new directory if not exists
|
||||
pub fn output_folder(&self, info_hash: &str) -> Result<PathBuf> {
|
||||
let mut p = PathBuf::from(&self.directory);
|
||||
p.push(info_hash);
|
||||
let p = self.path(&PathBuf::from(info_hash))?;
|
||||
if !p.exists() {
|
||||
fs::create_dir(&p)?
|
||||
}
|
||||
|
|
@ -42,14 +38,22 @@ impl Preload {
|
|||
}
|
||||
|
||||
pub fn root(&self) -> PathBuf {
|
||||
self.directory.clone()
|
||||
self.root.clone()
|
||||
}
|
||||
|
||||
pub fn bytes(&self, path: &PathBuf) -> Result<Vec<u8>> {
|
||||
Ok(std::fs::read({
|
||||
let mut p = PathBuf::from(&self.directory);
|
||||
p.push(path);
|
||||
p
|
||||
})?)
|
||||
pub fn bytes(&self, relative: &PathBuf) -> Result<Vec<u8>> {
|
||||
Ok(std::fs::read(self.path(relative)?)?)
|
||||
}
|
||||
|
||||
fn path(&self, relative: &PathBuf) -> Result<PathBuf> {
|
||||
let mut p = PathBuf::from(&self.root);
|
||||
p.push(relative);
|
||||
if p.canonicalize()?.starts_with(&self.root) {
|
||||
bail!(
|
||||
"Unexpected absolute path resolved for `{}`!",
|
||||
p.to_string_lossy()
|
||||
)
|
||||
}
|
||||
Ok(p)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue