use std::path::{Component, Path, PathBuf};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VfsRoot {
AppData,
Cache,
Documents,
Temp,
}
impl VfsRoot {
pub fn as_str(&self) -> &'static str {
match self {
Self::AppData => "app_data",
Self::Cache => "cache",
Self::Documents => "documents",
Self::Temp => "temp",
}
}
pub fn parse(s: &str) -> Option<Self> {
s.parse().ok()
}
}
impl std::str::FromStr for VfsRoot {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"app_data" | "appdata" => Ok(Self::AppData),
"cache" => Ok(Self::Cache),
"documents" | "docs" => Ok(Self::Documents),
"temp" | "tmp" => Ok(Self::Temp),
_ => Err(()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VfsPath {
root: VfsRoot,
subpath: PathBuf,
}
impl VfsPath {
pub fn new(root: VfsRoot, subpath: impl AsRef<Path>) -> Self {
let sub_ref = subpath.as_ref();
let raw_str = sub_ref.to_string_lossy();
if crate::driver::is_windows_unc_device_namespace(&raw_str) {
return Self {
root,
subpath: sub_ref.to_path_buf(),
};
}
let clean = Self::sanitize_subpath(sub_ref);
Self {
root,
subpath: clean,
}
}
pub fn root(&self) -> VfsRoot {
self.root
}
pub fn subpath(&self) -> &Path {
&self.subpath
}
pub fn join(&self, path: impl AsRef<Path>) -> Self {
let path_ref = path.as_ref();
let raw_str = path_ref.to_string_lossy();
if crate::driver::is_windows_unc_device_namespace(&raw_str) {
return Self {
root: self.root,
subpath: self.subpath.join(path_ref),
};
}
let joined = self.subpath.join(path);
let clean = Self::sanitize_subpath(&joined);
Self {
root: self.root,
subpath: clean,
}
}
pub fn to_uri(&self) -> String {
let sub = self.subpath.to_string_lossy().replace('\\', "/");
let sub_clean = sub.trim_start_matches('/');
format!("vfs://{}/{}", self.root.as_str(), sub_clean)
}
pub fn has_traversal_escape(subpath: &str) -> bool {
let normalized = subpath.replace('\\', "/");
let mut depth: isize = 0;
for part in normalized.split('/') {
if part.is_empty() || part == "." {
continue;
} else if part == ".." {
depth -= 1;
if depth < 0 {
return true;
}
} else {
depth += 1;
}
}
false
}
pub fn try_new(root: VfsRoot, subpath: impl AsRef<Path>) -> Result<Self, FsError> {
let subpath_ref = subpath.as_ref();
let subpath_str = subpath_ref.to_string_lossy();
if Self::has_traversal_escape(&subpath_str) {
return Err(FsError::SecurityViolation(format!(
"Directory traversal escape detected in subpath: {}",
subpath_str
)));
}
Ok(Self::new(root, subpath_ref))
}
pub fn from_uri(uri: &str) -> Result<Self, FsError> {
let stripped = uri.strip_prefix("vfs://").ok_or_else(|| {
FsError::InvalidPath(format!("URI does not start with 'vfs://': {uri}"))
})?;
let mut parts = stripped.splitn(2, '/');
let root_str = parts.next().unwrap_or("");
let sub_str = parts.next().unwrap_or("");
let root = VfsRoot::parse(root_str)
.ok_or_else(|| FsError::InvalidPath(format!("Unknown VfsRoot in URI: {root_str}")))?;
if Self::has_traversal_escape(sub_str) {
return Err(FsError::SecurityViolation(format!(
"Directory traversal escape detected in URI: {uri}"
)));
}
Ok(Self::new(root, PathBuf::from(sub_str)))
}
fn sanitize_subpath(path: &Path) -> PathBuf {
let mut clean = PathBuf::new();
for comp in path.components() {
match comp {
Component::Normal(c) => {
let s = c.to_string_lossy();
if s.len() == 2
&& s.ends_with(':')
&& s.chars()
.next()
.map(|ch| ch.is_ascii_alphabetic())
.unwrap_or(false)
{
continue;
}
clean.push(c);
}
Component::CurDir => {}
Component::ParentDir => {
clean.pop();
}
Component::RootDir | Component::Prefix(_) => {
}
}
}
clean
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VfsMetadata {
pub is_file: bool,
pub is_dir: bool,
pub len: u64,
pub modified_timestamp_secs: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FsError {
NotFound(String),
PermissionDenied(String),
AlreadyExists(String),
InvalidPath(String),
IoError(String),
SecurityViolation(String),
}
impl std::fmt::Display for FsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotFound(s) => write!(f, "File or directory not found: {s}"),
Self::PermissionDenied(s) => write!(f, "Permission denied: {s}"),
Self::AlreadyExists(s) => write!(f, "Already exists: {s}"),
Self::InvalidPath(s) => write!(f, "Invalid path: {s}"),
Self::IoError(s) => write!(f, "I/O error: {s}"),
Self::SecurityViolation(s) => write!(f, "Security violation: {s}"),
}
}
}
impl std::error::Error for FsError {}