use std::{
io::Read,
path::{Path, PathBuf},
};
use bstr::{BStr, BString, ByteSlice, ByteVec};
use crate::{pattern::Case, search::Pattern};
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Default)]
pub struct List<T: Pattern> {
pub patterns: Vec<Mapping<T::Value>>,
pub source: Option<PathBuf>,
pub base: Option<BString>,
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub struct Mapping<T> {
pub pattern: crate::Pattern,
pub value: T,
pub sequence_number: usize,
}
fn read_in_full_ignore_missing(path: &Path, follow_symlinks: bool, buf: &mut Vec<u8>) -> std::io::Result<bool> {
buf.clear();
let file = if follow_symlinks {
std::fs::File::open(path)
} else {
gix_features::fs::open_options_no_follow().read(true).open(path)
};
Ok(match file {
Ok(mut file) => {
if let Err(err) = file.read_to_end(buf) {
if io_err_is_dir(&err) {
false
} else {
return Err(err);
}
} else {
true
}
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound || io_err_is_dir(&err) => false,
Err(err) => return Err(err),
})
}
fn io_err_is_dir(err: &std::io::Error) -> bool {
let raw = err.raw_os_error();
raw == Some(if cfg!(windows) { 5 } else { 21 })
|| raw == Some(20)
}
impl<T> List<T>
where
T: Pattern,
{
pub fn from_bytes(bytes: &[u8], source_file: PathBuf, root: Option<&Path>, parse: T) -> Self {
let patterns = parse.bytes_to_patterns(bytes, source_file.as_path());
let base = root
.and_then(|root| source_file.parent().expect("file").strip_prefix(root).ok())
.and_then(|base| {
(!base.as_os_str().is_empty()).then(|| {
let mut base: BString =
gix_path::to_unix_separators_on_windows(gix_path::into_bstr(base)).into_owned();
base.push_byte(b'/');
base
})
});
List {
patterns,
source: Some(source_file),
base,
}
}
pub fn from_file(
source: impl Into<PathBuf>,
root: Option<&Path>,
follow_symlinks: bool,
buf: &mut Vec<u8>,
parse: T,
) -> std::io::Result<Option<Self>> {
let source = source.into();
Ok(read_in_full_ignore_missing(&source, follow_symlinks, buf)?
.then(|| Self::from_bytes(buf, source, root, parse)))
}
}
impl<T> List<T>
where
T: Pattern,
{
pub fn strip_base_handle_recompute_basename_pos<'a>(
&self,
relative_path: &'a BStr,
basename_pos: Option<usize>,
case: Case,
) -> Option<(&'a BStr, Option<usize>)> {
match self.base.as_deref() {
Some(base) => strip_base_handle_recompute_basename_pos(base.as_bstr(), relative_path, basename_pos, case)?,
None => (relative_path, basename_pos),
}
.into()
}
}
pub fn strip_base_handle_recompute_basename_pos<'a>(
base: &BStr,
relative_path: &'a BStr,
basename_pos: Option<usize>,
case: Case,
) -> Option<(&'a BStr, Option<usize>)> {
Some((
match case {
Case::Sensitive => relative_path.strip_prefix(base.as_bytes())?.as_bstr(),
Case::Fold => {
let rela_dir = relative_path.get(..base.len())?;
if !rela_dir.eq_ignore_ascii_case(base) {
return None;
}
&relative_path[base.len()..]
}
},
basename_pos.and_then(|pos| {
let pos = pos - base.len();
(pos != 0).then_some(pos)
}),
))
}