diff options
| author | 2025-01-18 23:47:48 -0600 | |
|---|---|---|
| committer | 2025-01-18 23:47:48 -0600 | |
| commit | cd8bf1667494070c3a22ab5d63b559a9742b8a1a (patch) | |
| tree | 6f93f0c0fbdccfa18733499845a8bc7c298c402f /src/util.rs | |
| parent | building classpath (diff) | |
more stuff
Diffstat (limited to 'src/util.rs')
| -rw-r--r-- | src/util.rs | 20 |
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) +} |
