diff options
Diffstat (limited to 'crates/fparkan-vfs/src')
| -rw-r--r-- | crates/fparkan-vfs/src/lib.rs | 63 |
1 files changed, 54 insertions, 9 deletions
diff --git a/crates/fparkan-vfs/src/lib.rs b/crates/fparkan-vfs/src/lib.rs index 3c92fa17..a2cc3c82 100644 --- a/crates/fparkan-vfs/src/lib.rs +++ b/crates/fparkan-vfs/src/lib.rs @@ -81,6 +81,19 @@ pub trait Vfs: Send + Sync { /// Returns [`VfsError`] when the prefix is invalid, missing, or cannot be /// traversed by the backing store. fn list(&self, prefix: &NormalizedPath) -> Result<Vec<VfsEntry>, VfsError>; + /// Lists regular files immediately inside a directory. + fn list_files(&self, directory: &NormalizedPath) -> Result<Vec<NormalizedPath>, VfsError> { + Ok(self + .list(directory)? + .into_iter() + .map(|entry| entry.path) + .filter(|path| { + path.as_str() + .rsplit_once('/') + .is_some_and(|(parent, _)| parent.eq_ignore_ascii_case(directory.as_str())) + }) + .collect()) + } } /// Host directory VFS. @@ -151,6 +164,18 @@ impl Vfs for DirectoryVfs { entries.sort_by(|a, b| a.path.as_bytes().cmp(b.path.as_bytes())); Ok(entries) } + + fn list_files(&self, directory: &NormalizedPath) -> Result<Vec<NormalizedPath>, VfsError> { + let mut files = Vec::new(); + for entry in fs::read_dir(self.host_path(directory)?).map_err(VfsError::Io)? { + let entry = entry.map_err(VfsError::Io)?; + if entry.file_type().map_err(VfsError::Io)?.is_file() { + files.push(normalize_host_path(&self.root, &entry.path())?); + } + } + files.sort_by(|a, b| a.as_bytes().cmp(b.as_bytes())); + Ok(files) + } } fn resolve_casefolded(root: &Path, normalized: &NormalizedPath) -> Result<PathBuf, VfsError> { @@ -258,22 +283,24 @@ fn list_recursive(root: &Path, dir: &Path, out: &mut Vec<VfsEntry>) -> Result<() if !metadata.is_file() { continue; } - let rel = child.strip_prefix(root).map_err(|_| VfsError::Path)?; - #[cfg(unix)] - let rel_bytes = rel.as_os_str().as_bytes(); - #[cfg(not(unix))] - let rel_bytes = rel.to_str().ok_or(VfsError::Path)?.as_bytes(); - let path = - fparkan_path::normalize_relative(rel_bytes, fparkan_path::PathPolicy::HostCompatible) - .map_err(|_| VfsError::Path)?; out.push(VfsEntry { - path, + path: normalize_host_path(root, &child)?, metadata: metadata_from_host_file(&child, &metadata)?, }); } Ok(()) } +fn normalize_host_path(root: &Path, child: &Path) -> Result<NormalizedPath, VfsError> { + let relative = child.strip_prefix(root).map_err(|_| VfsError::Path)?; + #[cfg(unix)] + let bytes = relative.as_os_str().as_bytes(); + #[cfg(not(unix))] + let bytes = relative.to_str().ok_or(VfsError::Path)?.as_bytes(); + fparkan_path::normalize_relative(bytes, fparkan_path::PathPolicy::HostCompatible) + .map_err(|_| VfsError::Path) +} + fn metadata_from_host_file(path: &Path, metadata: &fs::Metadata) -> Result<VfsMetadata, VfsError> { if !metadata.is_file() { return Err(VfsError::Path); @@ -486,6 +513,24 @@ impl Vfs for OverlayVfs { entries.sort_by(|a, b| a.path.as_bytes().cmp(b.path.as_bytes())); Ok(entries) } + + fn list_files(&self, directory: &NormalizedPath) -> Result<Vec<NormalizedPath>, VfsError> { + let mut names = BTreeMap::new(); + for layer in &self.layers { + match layer.list_files(directory) { + Ok(files) => { + for path in files { + names + .entry(ascii_lookup_key(path.as_bytes()).0) + .or_insert(path); + } + } + Err(VfsError::NotFound(_)) => {} + Err(error) => return Err(error), + } + } + Ok(names.into_values().collect()) + } } #[cfg(test)] |
