use std::{
borrow::Cow,
path::{Path, PathBuf},
};
use bstr::BStr;
use gix_fs::{stack::ToNormalPathComponents, Stack};
use crate::SymlinkCheck;
#[derive(Debug, thiserror::Error)]
#[error("Cannot step through symlink to perform an lstat")]
struct CannotStepThroughSymlink;
pub(crate) fn is_symlink_step_error(err: &std::io::Error) -> bool {
err.get_ref()
.and_then(|source| source.downcast_ref::<CannotStepThroughSymlink>())
.is_some()
}
impl SymlinkCheck {
pub fn new(root: PathBuf) -> Self {
Self {
inner: gix_fs::Stack::new(root),
}
}
pub fn verified_path(&mut self, relative_path: impl ToNormalPathComponents) -> std::io::Result<&Path> {
self.inner.make_relative_path_current(relative_path, &mut Delegate)?;
Ok(self.inner.current())
}
pub fn verified_path_allow_nonexisting(&mut self, relative_path: &BStr) -> std::io::Result<Cow<'_, Path>> {
let rela_path = gix_path::try_from_bstr(relative_path).map_err(std::io::Error::other)?;
if let Err(err) = self.verified_path(rela_path.as_ref()) {
if err.kind() == std::io::ErrorKind::NotFound {
Ok(Cow::Owned(self.inner.root().join(rela_path)))
} else {
Err(err)
}
} else {
Ok(Cow::Borrowed(self.inner.current()))
}
}
}
struct Delegate;
impl gix_fs::stack::Delegate for Delegate {
fn push_directory(&mut self, _stack: &Stack) -> std::io::Result<()> {
Ok(())
}
#[cfg_attr(windows, allow(unused_variables))]
fn push(&mut self, is_last_component: bool, stack: &Stack) -> std::io::Result<()> {
#[cfg(windows)]
{
Ok(())
}
#[cfg(not(windows))]
{
if is_last_component {
return Ok(());
}
if stack.current().symlink_metadata()?.is_symlink() {
return Err(std::io::Error::other(CannotStepThroughSymlink));
}
Ok(())
}
}
fn pop_directory(&mut self) {}
}