From cd8bf1667494070c3a22ab5d63b559a9742b8a1a Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Sat, 18 Jan 2025 23:47:48 -0600 Subject: more stuff --- src/util.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/util.rs') 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, expect_size: Option, 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) +} -- cgit v1.2.3-70-g09d2