M1.2: vfs with ignore rules, polling watcher and safe file IO
- Project::open canonicalises the root (no \?\ prefix on Windows where safe); typed errors for missing and non-directory paths. - Lazy children(dir): dirs first, case-insensitive; kind, ignored flag and role (vendor/ library, node_modules/ excluded, .git hidden). - Ignore rules via the ignore crate: .gitignore, .ignore, .git/info/exclude, global git excludes, with git precedence. - Watcher: std-only polling (100 ms, watched dirs only) with create/modify/remove/rename events and a Failed event on root loss. notify is CC0-1.0 and fails cargo deny; owner decision pending. - read_text with a 32 MiB cap, UTF-8 + BOM, typed InvalidUtf8 and UnsupportedEncoding; write_atomic via temp file + rename, keeping permissions and refusing read-only files. - 28 tests on temp dirs (30 on Unix: permission and symlink write tests). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
faadb05eb3
commit
5d49b57b9c
16 changed files with 2058 additions and 4 deletions
97
crates/vfs/src/path.rs
Normal file
97
crates/vfs/src/path.rs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
//! Path helpers: project-relative path validation and Windows prefix cleanup.
|
||||
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
use crate::VfsError;
|
||||
|
||||
/// Validates a project-relative path and returns it normalised.
|
||||
///
|
||||
/// `.` components are dropped; an empty result means the project root.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`VfsError::OutsideProject`] if the path is absolute, has a drive or UNC
|
||||
/// prefix, or contains `..`.
|
||||
pub(crate) fn check_relative(rel: &Path) -> Result<PathBuf, VfsError> {
|
||||
let mut out = PathBuf::new();
|
||||
for component in rel.components() {
|
||||
match component {
|
||||
Component::Normal(part) => out.push(part),
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
|
||||
return Err(VfsError::OutsideProject {
|
||||
path: rel.to_path_buf(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Turns a canonical Windows path like `\\?\C:\dir` back into `C:\dir` so it
|
||||
/// reads naturally and compares equal to paths users type.
|
||||
///
|
||||
/// The verbatim form is kept when the plain form could mean something else:
|
||||
/// paths of 260 characters or more, and components ending in a dot or a
|
||||
/// space. On other platforms the path is returned unchanged.
|
||||
pub(crate) fn simplify(path: PathBuf) -> PathBuf {
|
||||
if !cfg!(windows) {
|
||||
return path;
|
||||
}
|
||||
let Some(text) = path.to_str() else {
|
||||
return path;
|
||||
};
|
||||
let Some(rest) = text.strip_prefix(r"\\?\") else {
|
||||
return path;
|
||||
};
|
||||
let bytes = rest.as_bytes();
|
||||
let is_disk = bytes.first().is_some_and(u8::is_ascii_alphabetic)
|
||||
&& bytes.get(1) == Some(&b':')
|
||||
&& bytes.get(2) == Some(&b'\\');
|
||||
let safe_parts = rest
|
||||
.split('\\')
|
||||
.all(|part| !part.ends_with('.') && !part.ends_with(' '));
|
||||
if is_disk && rest.len() < 260 && safe_parts {
|
||||
PathBuf::from(rest)
|
||||
} else {
|
||||
path
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn relative_paths_are_normalised() {
|
||||
assert_eq!(
|
||||
check_relative(Path::new("./src/./a.php")).unwrap(),
|
||||
Path::new("src").join("a.php")
|
||||
);
|
||||
assert_eq!(check_relative(Path::new("")).unwrap(), PathBuf::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escaping_paths_are_rejected() {
|
||||
assert!(check_relative(Path::new("../x")).is_err());
|
||||
assert!(check_relative(Path::new("a/../../x")).is_err());
|
||||
let absolute = std::env::temp_dir();
|
||||
assert!(check_relative(&absolute).is_err());
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn verbatim_disk_prefix_is_stripped() {
|
||||
assert_eq!(
|
||||
simplify(PathBuf::from(r"\\?\C:\work\demo")),
|
||||
PathBuf::from(r"C:\work\demo")
|
||||
);
|
||||
assert_eq!(
|
||||
simplify(PathBuf::from(r"\\?\C:\work\odd.")),
|
||||
PathBuf::from(r"\\?\C:\work\odd.")
|
||||
);
|
||||
assert_eq!(
|
||||
simplify(PathBuf::from(r"\\?\UNC\server\share")),
|
||||
PathBuf::from(r"\\?\UNC\server\share")
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue