191 lines
8.4 KiB
Rust
191 lines
8.4 KiB
Rust
// Author: Guoliang Chen, a Senior Digital Design Engineer
|
|
|
|
//! What one path a file watcher named is to the index: a file it holds
|
|
//! (perhaps under another spelling), a file the scope provably cannot take,
|
|
//! or neither — which only a rescan can settle
|
|
//! (`docs/features/localized-reindex/README.md`).
|
|
//!
|
|
//! A watcher event for a `.sv` the index does not hold used to be a whole
|
|
//! rescan, progress bar and all, every time: a generated file under a
|
|
//! gitignored build directory, a file outside the folder scope or the
|
|
//! filelist, or a held file reported by its real path under a symlinked root.
|
|
//! Only the last kind of file can change what the index says, and none of
|
|
//! them needs the walk to find that out.
|
|
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use lucisv_core::config::Discovery;
|
|
use lucisv_core::walk::{collect_files_below, dir_id, is_excluded, EXTENSIONS};
|
|
|
|
use super::discover::{oversize, Found};
|
|
use super::Workspace;
|
|
|
|
/// What a watched path is to the index.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum Watched {
|
|
/// A file the index holds, by the spelling it holds it under — or an
|
|
/// open buffer it does not hold yet, which a rebuild would take in and the
|
|
/// one-file path adds (QA F4).
|
|
Held(PathBuf),
|
|
/// A file no scan of the scope in force would index. Nothing to do for
|
|
/// the index.
|
|
Outside,
|
|
/// A file the scope may take and the index does not hold: a new file, or
|
|
/// one the cap kept out until now. Only a rescan can say.
|
|
Unknown,
|
|
}
|
|
|
|
impl Workspace {
|
|
/// What `path` is to the index. Errs towards [`Watched::Unknown`]: a
|
|
/// wasted rescan costs time, a skipped one would cost a stale index.
|
|
pub fn watched(&self, path: &Path) -> Watched {
|
|
if let Some(held) = self.held_spelling(path) {
|
|
return Watched::Held(held);
|
|
}
|
|
if let Some(doc) = self.document_for_path(path).filter(|doc| !doc.large) {
|
|
return Watched::Held(doc.path.clone().unwrap_or_else(|| path.to_path_buf()));
|
|
}
|
|
match self.scope_could_take(path) {
|
|
true => Watched::Unknown,
|
|
false => Watched::Outside,
|
|
}
|
|
}
|
|
|
|
/// The spelling the index holds `path` under: `path` itself, the same
|
|
/// file reached through a scope directory's symlink/case alias, or any
|
|
/// file sharing `(dev, ino)` (on Unix) / canonical path without basename
|
|
/// gating.
|
|
fn held_spelling(&self, path: &Path) -> Option<PathBuf> {
|
|
let holds = |candidate: &Path| self.index.file_id(candidate).is_some();
|
|
if holds(path) {
|
|
return Some(path.to_path_buf());
|
|
}
|
|
if let Some(real) = canonical(path) {
|
|
let mut anchors: Vec<PathBuf> = self.roots.clone();
|
|
anchors.extend(self.scope_dirs());
|
|
if let Some(spelled) = anchors.into_iter().find_map(|anchor| {
|
|
let rest = real.strip_prefix(fs::canonicalize(&anchor).ok()?).ok()?;
|
|
let spelled = anchor.join(rest);
|
|
holds(&spelled).then_some(spelled)
|
|
}) {
|
|
return Some(spelled);
|
|
}
|
|
}
|
|
self.held_path_for(path)
|
|
}
|
|
|
|
/// Whether a scan of the scope in force could index `path` — `false` only
|
|
/// when it provably could not: a filelist that does not list it, or a
|
|
/// walk of the scope's directories that does not find it or finds it over
|
|
/// `lucisv.index.maxFileSize`. The walk is `lucisv_core`'s own, rooted
|
|
/// where the scan's is, so `.gitignore`, `exclude` and the config's
|
|
/// excludes rule it out exactly as they rule it out of a scan.
|
|
pub(super) fn scope_could_take(&self, path: &Path) -> bool {
|
|
if let Some(list) = self.filelist_path() {
|
|
// An unreadable filelist sends the scan to the rungs below, and
|
|
// so it sends this.
|
|
if let Ok(list) = lucisv_core::filelist::read(&list) {
|
|
return list.files.iter().any(|listed| same_file(listed, path))
|
|
&& oversize(&Found::of(path), self.client.max_file_size, &mut 0).is_none();
|
|
}
|
|
}
|
|
let Some(dir) = path.parent() else { return true };
|
|
let dirs = match self.walks_the_workspace() {
|
|
true => self.roots.clone(),
|
|
false => self.scope_dirs(),
|
|
};
|
|
if self.roots.is_empty() && self.client.index_scope_depth == 0 {
|
|
// The flat read of a rootless session: siblings only (and must be
|
|
// an existing readable source file under `maxFileSize`).
|
|
return dirs.iter().any(|scope| scope == dir)
|
|
&& path
|
|
.extension()
|
|
.and_then(|e| e.to_str())
|
|
.is_some_and(|e| EXTENSIONS.contains(&e))
|
|
&& fs::metadata(path).is_ok_and(|meta| meta.is_file())
|
|
&& oversize(&Found::of(path), self.client.max_file_size, &mut 0).is_none();
|
|
}
|
|
dirs.iter()
|
|
.filter(|scope| path.starts_with(scope))
|
|
.any(|scope| self.walk_finds(scope, path))
|
|
}
|
|
|
|
/// Whether a walk of `scope` finds `path` under its cap.
|
|
fn walk_finds(&self, scope: &Path, path: &Path) -> bool {
|
|
if !path.extension().and_then(|e| e.to_str()).is_some_and(|e| EXTENSIONS.contains(&e)) {
|
|
return false;
|
|
}
|
|
let Some(dir) = path.parent() else { return true };
|
|
let excludes = self.walk_excludes(&mut Discovery::new(true), scope);
|
|
let mut has_gitignore = scope.join(".gitignore").is_file();
|
|
let mut cursor = Some(path);
|
|
while let Some(part) = cursor {
|
|
if part == scope {
|
|
break;
|
|
}
|
|
if is_excluded(part, &excludes) {
|
|
return false;
|
|
}
|
|
if part != path && part.join(".gitignore").is_file() {
|
|
has_gitignore = true;
|
|
}
|
|
cursor = part.parent();
|
|
}
|
|
if !has_gitignore {
|
|
let Ok(meta) = fs::metadata(path) else { return false };
|
|
return meta.is_file()
|
|
&& (self.client.max_file_size == 0 || meta.len() <= self.client.max_file_size);
|
|
}
|
|
let mut found = None;
|
|
let take = &mut |file: PathBuf, len| {
|
|
if file == path {
|
|
found = Some(Found { path: file, len });
|
|
}
|
|
};
|
|
if collect_files_below(scope, dir, &excludes, true, take, &mut |_| {}).is_err() {
|
|
// The directory is gone or unreadable: nothing in it can be read
|
|
// either, and the scan would say the same.
|
|
return false;
|
|
}
|
|
found.is_some_and(|found| oversize(&found, self.client.max_file_size, &mut 0).is_none())
|
|
}
|
|
|
|
/// The exclude patterns a walk rooted at `dir` applies: the governing
|
|
/// config's, then the client's `lucisv.exclude`. *Appended*, never
|
|
/// substituted — the CLI appends a config's excludes to `--exclude` the
|
|
/// same way, and a setting that silently dropped a checked-in exclude
|
|
/// would put vendored code back in the index for one user only.
|
|
pub(super) fn walk_excludes(&self, discovery: &mut Discovery, dir: &Path) -> Vec<String> {
|
|
// A broken `lucisv.toml` must not stop the server from indexing: an
|
|
// index that excludes nothing is a far better failure than a server
|
|
// that comes up empty. The error is reported when the user formats a
|
|
// file in this tree (`format::settings_for`).
|
|
let mut exclude = discovery.for_dir(dir).unwrap_or_default().exclude.unwrap_or_default();
|
|
exclude.extend(self.client.exclude.iter().cloned());
|
|
exclude
|
|
}
|
|
}
|
|
|
|
/// Whether two spellings name one file: equal, or sharing `(dev, ino)` (on
|
|
/// Unix) / canonical path (elsewhere), without basename equality gating.
|
|
fn same_file(listed: &Path, path: &Path) -> bool {
|
|
if listed == path {
|
|
return true;
|
|
}
|
|
dir_id(listed).is_some_and(|id| dir_id(path).as_ref() == Some(&id))
|
|
|| (canonical(listed).is_some() && canonical(listed) == canonical(path))
|
|
}
|
|
|
|
/// `path` with every symlink resolved and, on a case-insensitive disk, every
|
|
/// component in the case the disk holds it in. A file that is gone resolves
|
|
/// through its directory, so a deletion can still be matched.
|
|
fn canonical(path: &Path) -> Option<PathBuf> {
|
|
fs::canonicalize(path).ok().or_else(|| {
|
|
let real = fs::canonicalize(path.parent()?).ok()?;
|
|
Some(real.join(path.file_name()?))
|
|
})
|
|
}
|
|
|
|
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com
|