summaryrefslogtreecommitdiffstats
path: root/src/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/util.rs b/src/util.rs
index fe11c38..a7c2d5e 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,7 +1,7 @@
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::io::ErrorKind;
-use std::path::{Path, PathBuf};
+use std::path::{Component, Path, PathBuf};
use log::debug;
use sha1_smol::{Digest, Sha1};
use tokio::fs::File;
@@ -110,3 +110,21 @@ pub async fn verify_file(path: impl AsRef<Path>, expect_size: Option<usize>, exp
Ok(())
}
+
+pub fn check_path(name: &str) -> Result<&Path, &'static str> {
+ let entry_path: &Path = Path::new(name);
+
+ let mut depth = 0usize;
+ for component in entry_path.components() {
+ depth = match component {
+ Component::Prefix(_) | Component::RootDir =>
+ return Err("root path component in entry"),
+ Component::ParentDir => depth.checked_sub(1)
+ .map_or_else(|| Err("entry path escapes"), |s| Ok(s))?,
+ Component::Normal(_) => depth + 1,
+ _ => depth
+ }
+ }
+
+ Ok(entry_path)
+}