gix_ref/store/file/loose/
iter.rs1use std::path::{Path, PathBuf};
2
3use gix_features::fs::walkdir::DirEntryIter;
4use gix_object::bstr::ByteSlice;
5use gix_path::RelativePath;
6
7use crate::{file::iter::LooseThenPacked, store_impl::file, BString, FullName};
8
9pub(in crate::store_impl::file) struct SortedLoosePaths {
11 pub(crate) base: PathBuf,
12 prefix: Option<BString>,
14 suffix: Option<BString>,
16 file_walk: Option<DirEntryIter>,
17}
18
19impl SortedLoosePaths {
20 pub fn at(
21 path: &Path,
22 base: PathBuf,
23 prefix: Option<BString>,
24 suffix: Option<BString>,
25 precompose_unicode: bool,
26 ) -> Self {
27 let depth = if suffix.is_some() { 1 } else { usize::MAX };
28 SortedLoosePaths {
29 base,
30 prefix,
31 suffix,
32 file_walk: path.is_dir().then(|| {
33 gix_features::fs::walkdir_sorted_new(
35 path,
36 gix_features::fs::walkdir::Parallelism::Serial,
37 depth,
38 precompose_unicode,
39 )
40 .into_iter()
41 }),
42 }
43 }
44}
45
46impl Iterator for SortedLoosePaths {
47 type Item = std::io::Result<(PathBuf, FullName)>;
48
49 fn next(&mut self) -> Option<Self::Item> {
50 for entry in self.file_walk.as_mut()?.by_ref() {
51 match entry {
52 Ok(entry) => {
53 if !entry.file_type().is_ok_and(|ft| ft.is_file()) {
54 continue;
55 }
56 let full_path = entry.path().into_owned();
57 let full_name = full_path
58 .strip_prefix(&self.base)
59 .expect("prefix-stripping cannot fail as base is within our root");
60 let Ok(full_name) = gix_path::try_into_bstr(full_name)
61 .map(|name| gix_path::to_unix_separators_on_windows(name).into_owned())
62 else {
63 continue;
64 };
65 if let Some(prefix) = &self.prefix {
66 if !full_name.starts_with(prefix) {
67 continue;
68 }
69 }
70 if let Some(suffix) = &self.suffix {
71 if !full_name.ends_with(suffix) {
72 continue;
73 }
74 }
75 if gix_validate::reference::name_partial(full_name.as_bstr()).is_ok() {
76 let name = FullName(full_name);
77 return Some(Ok((full_path, name)));
78 } else {
79 continue;
80 }
81 }
82 Err(err) => return Some(Err(err.into_io_error().expect("no symlink related errors"))),
83 }
84 }
85 None
86 }
87}
88
89impl file::Store {
90 pub fn loose_iter(&self) -> std::io::Result<LooseThenPacked<'_, '_>> {
96 self.iter_packed(None)
97 }
98
99 pub fn loose_iter_prefixed(&self, prefix: &RelativePath) -> std::io::Result<LooseThenPacked<'_, '_>> {
109 self.iter_prefixed_packed(prefix, None)
110 }
111}