use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FileId(u32);
impl FileId {
pub fn index(self) -> usize {
self.0 as usize
}
pub fn new(number: u32) -> FileId {
FileId(number)
}
}
pub trait FileProvider: Sync {
fn read(&self, path: &Path) -> Option<String>;
fn exists(&self, path: &Path) -> bool;
fn is_case_insensitive(&self) -> bool {
cfg!(windows)
}
fn is_open_buffer(&self, _path: &Path) -> bool {
false
}
}
#[derive(Debug, Clone, Default)]
pub struct DiskFiles;
impl FileProvider for DiskFiles {
fn read(&self, path: &Path) -> Option<String> {
std::fs::read_to_string(path).ok()
}
fn exists(&self, path: &Path) -> bool {
path.is_file()
}
}
#[derive(Debug)]
pub struct CachedFiles<F: FileProvider = DiskFiles> {
inner: F,
contents: std::sync::Mutex<HashMap<String, Option<std::sync::Arc<str>>>>,
reads: std::sync::atomic::AtomicUsize,
paths_read: std::sync::Mutex<std::collections::HashSet<String>>,
}
impl<F: FileProvider> CachedFiles<F> {
pub fn new(inner: F) -> Self {
CachedFiles {
inner,
contents: std::sync::Mutex::new(HashMap::new()),
reads: std::sync::atomic::AtomicUsize::new(0),
paths_read: std::sync::Mutex::new(std::collections::HashSet::new()),
}
}
pub fn inner(&self) -> &F {
&self.inner
}
pub fn text(&self, path: &Path) -> Option<std::sync::Arc<str>> {
let key = normalize_path(path, self.inner.is_case_insensitive());
let mut contents = self
.contents
.lock()
.expect("the content cache is not poisoned");
if let Some(known) = contents.get(&key) {
return known.clone();
}
self.reads
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
self.paths_read
.lock()
.expect("the read log is not poisoned")
.insert(key.clone());
let text = self
.inner
.read(path)
.map(|text| std::sync::Arc::from(text.as_str()));
contents.insert(key, text.clone());
text
}
pub fn reload(&self, path: &Path) {
let key = normalize_path(path, self.inner.is_case_insensitive());
self.contents
.lock()
.expect("the content cache is not poisoned")
.remove(&key);
}
pub fn reads(&self) -> usize {
self.reads.load(std::sync::atomic::Ordering::Relaxed)
}
pub fn paths_read(&self) -> usize {
self.paths_read
.lock()
.expect("the read log is not poisoned")
.len()
}
}
impl<F: FileProvider> FileProvider for CachedFiles<F> {
fn read(&self, path: &Path) -> Option<String> {
self.text(path).map(|text| text.to_string())
}
fn exists(&self, path: &Path) -> bool {
self.inner.exists(path)
}
fn is_case_insensitive(&self) -> bool {
self.inner.is_case_insensitive()
}
fn is_open_buffer(&self, path: &Path) -> bool {
self.inner.is_open_buffer(path)
}
}
#[derive(Debug, Clone, Default)]
pub struct MemoryFiles {
files: HashMap<String, String>,
case_insensitive: bool,
reads: std::sync::Arc<std::sync::Mutex<Vec<String>>>,
probes: std::sync::Arc<std::sync::Mutex<Vec<String>>>,
}
impl MemoryFiles {
pub fn new() -> Self {
Self::default()
}
pub fn with_case_insensitive(mut self, value: bool) -> Self {
self.case_insensitive = value;
self
}
pub fn reads_of(&self, path: impl AsRef<Path>) -> usize {
let wanted = normalize_path(path.as_ref(), self.case_insensitive);
let reads = self.reads.lock().expect("the read log is not poisoned");
reads.iter().filter(|read| **read == wanted).count()
}
pub fn reads(&self) -> Vec<String> {
self.reads
.lock()
.expect("the read log is not poisoned")
.clone()
}
pub fn exists_of(&self, path: impl AsRef<Path>) -> usize {
let wanted = normalize_path(path.as_ref(), self.case_insensitive);
let probes = self.probes.lock().expect("the probe log is not poisoned");
probes.iter().filter(|probe| **probe == wanted).count()
}
pub fn probes(&self) -> Vec<String> {
self.probes
.lock()
.expect("the probe log is not poisoned")
.clone()
}
pub fn insert(&mut self, path: impl AsRef<Path>, contents: impl Into<String>) -> &mut Self {
self.files.insert(
normalize_path(path.as_ref(), self.case_insensitive),
contents.into(),
);
self
}
pub fn with_file(mut self, path: impl AsRef<Path>, contents: impl Into<String>) -> Self {
self.insert(path, contents);
self
}
pub fn from_files<I, P>(files: I, case_insensitive: bool) -> Self
where
I: IntoIterator<Item = (P, &'static str)>,
P: AsRef<Path>,
{
let mut memory = MemoryFiles::new().with_case_insensitive(case_insensitive);
for (path, contents) in files {
memory.insert(path, contents);
}
memory
}
}
impl FileProvider for MemoryFiles {
fn read(&self, path: &Path) -> Option<String> {
let key = normalize_path(path, self.case_insensitive);
self.reads
.lock()
.expect("the read log is not poisoned")
.push(key.clone());
self.files.get(&key).cloned()
}
fn exists(&self, path: &Path) -> bool {
let key = normalize_path(path, self.case_insensitive);
self.probes
.lock()
.expect("the probe log is not poisoned")
.push(key.clone());
self.files.contains_key(&key)
}
fn is_case_insensitive(&self) -> bool {
self.case_insensitive
}
}
#[derive(Debug, Clone)]
pub struct OverlayFiles<F: FileProvider, G: FileProvider> {
pub overlay: F,
pub fallback: G,
}
impl<F: FileProvider, G: FileProvider> OverlayFiles<F, G> {
pub fn new(overlay: F, fallback: G) -> Self {
OverlayFiles { overlay, fallback }
}
}
impl<F: FileProvider, G: FileProvider> FileProvider for OverlayFiles<F, G> {
fn read(&self, path: &Path) -> Option<String> {
self.overlay.read(path).or_else(|| self.fallback.read(path))
}
fn exists(&self, path: &Path) -> bool {
self.overlay.exists(path) || self.fallback.exists(path)
}
fn is_case_insensitive(&self) -> bool {
self.overlay.is_case_insensitive() || self.fallback.is_case_insensitive()
}
fn is_open_buffer(&self, path: &Path) -> bool {
self.overlay.is_open_buffer(path)
}
}
#[derive(Debug, Clone, Default)]
pub struct PathInterner {
by_normalized: HashMap<String, FileId>,
paths: Vec<PathBuf>,
case_insensitive: bool,
}
impl PathInterner {
pub fn new(case_insensitive: bool) -> Self {
PathInterner {
case_insensitive,
..PathInterner::default()
}
}
pub fn case_insensitive(&self) -> bool {
self.case_insensitive
}
pub fn intern(&mut self, path: &Path) -> FileId {
let key = normalize_path(path, self.case_insensitive);
if let Some(existing) = self.by_normalized.get(&key) {
return *existing;
}
let id = FileId(self.paths.len() as u32);
self.paths.push(path.to_path_buf());
self.by_normalized.insert(key, id);
id
}
pub fn get(&self, path: &Path) -> Option<FileId> {
self.by_normalized
.get(&normalize_path(path, self.case_insensitive))
.copied()
}
pub fn path(&self, id: FileId) -> Option<&Path> {
self.paths.get(id.index()).map(PathBuf::as_path)
}
pub fn len(&self) -> usize {
self.paths.len()
}
pub fn is_empty(&self) -> bool {
self.paths.is_empty()
}
}
pub fn normalize_path(path: &Path, case_insensitive: bool) -> String {
let raw = path.to_string_lossy();
let text: String = match raw.strip_prefix(r"\\?\UNC\") {
Some(rest) => format!(r"\\{rest}"),
None => match raw.strip_prefix(r"\\?\") {
Some(rest) => rest.to_string(),
None => raw.into_owned(),
},
};
let text = text.as_str();
let mut parts: Vec<String> = Vec::new();
let mut rest: &str = text;
if let Some(unc) = rest.strip_prefix(r"\\") {
let mut segments = unc.split(['/', '\\']);
let server = segments.next().unwrap_or_default();
let share = segments.next().unwrap_or_default();
if !server.is_empty() {
parts.push(format!(r"\\{server}\{share}"));
rest = unc
.get(server.len() + 1 + share.len()..)
.unwrap_or_default();
}
} else if rest.len() >= 2 && rest.as_bytes()[1] == b':' {
let drive = &rest[..2];
rest = &rest[2..];
let rooted = rest.starts_with('/') || rest.starts_with('\\');
parts.push(if rooted {
format!("{drive}/")
} else {
drive.to_string()
});
} else if rest.starts_with('/') || rest.starts_with('\\') {
parts.push("/".to_string());
}
for segment in rest.split(['/', '\\']) {
match segment {
"" | "." => {}
".." => match parts.last() {
Some(last) if is_a_root(last) => {}
Some(last) if last != ".." => {
parts.pop();
}
_ => parts.push("..".to_string()),
},
name => parts.push(name.to_string()),
}
}
let mut joined = String::new();
for part in &parts {
if !joined.is_empty() && !joined.ends_with('/') {
joined.push('/');
}
joined.push_str(part);
}
if case_insensitive {
joined.to_lowercase()
} else {
joined
}
}
fn is_a_root(part: &str) -> bool {
part == "/"
|| part.ends_with(":/")
|| (part.starts_with(r"\\") && part.matches('\\').count() >= 2)
}
pub fn join_normalized(directory: &Path, relative: &Path, case_insensitive: bool) -> PathBuf {
let joined = directory.join(relative);
PathBuf::from(normalize_path(&joined, case_insensitive))
}
pub fn parent_normalized(path: &Path, case_insensitive: bool) -> PathBuf {
match path.parent() {
Some(parent) => PathBuf::from(normalize_path(parent, case_insensitive)),
None => PathBuf::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalization_folds_the_spellings_of_one_path() {
let cases = [
("./a/b.h", "a/b.h"),
("a/./b.h", "a/b.h"),
("a/c/../b.h", "a/b.h"),
("a\\b.h", "a/b.h"),
("a//b.h", "a/b.h"),
];
for (input, expected) in cases {
assert_eq!(normalize_path(Path::new(input), false), expected, "{input}");
}
}
#[test]
fn normalization_keeps_what_it_cannot_resolve() {
assert_eq!(normalize_path(Path::new("/../a.h"), false), "/a.h");
assert_eq!(normalize_path(Path::new("../a.h"), false), "../a.h");
assert_eq!(normalize_path(Path::new("a/../../b.h"), false), "../b.h");
}
#[test]
fn normalization_can_fold_case() {
assert_eq!(normalize_path(Path::new("A/B.H"), true), "a/b.h");
assert_ne!(
normalize_path(Path::new("A/B.H"), true),
normalize_path(Path::new("A/B.H"), false)
);
}
#[test]
fn a_drive_prefix_is_attached_to_the_root() {
assert_eq!(normalize_path(Path::new("C:\\a\\b.h"), false), "C:/a/b.h");
assert_eq!(normalize_path(Path::new("C:/a/b.h"), false), "C:/a/b.h");
assert_eq!(normalize_path(Path::new("C:\\a\\..\\b.h"), false), "C:/b.h");
assert_eq!(normalize_path(Path::new("C:\\..\\b.h"), false), "C:/b.h");
}
#[test]
fn interning_gives_one_id_per_file() {
let mut interner = PathInterner::new(false);
let first = interner.intern(Path::new("a/b.h"));
let again = interner.intern(Path::new("./a/b.h"));
let other = interner.intern(Path::new("a/c.h"));
assert_eq!(first, again, "two spellings of one path are one file");
assert_ne!(first, other);
assert_eq!(interner.len(), 2);
}
#[test]
fn a_read_and_a_search_are_counted_separately() {
let files = MemoryFiles::new().with_file("/p/a.h", "int x;\n");
assert_eq!(files.exists_of("/p/a.h"), 0);
assert!(files.exists(Path::new("/p/a.h")));
assert!(files.exists(Path::new("/p/./a.h")), "spellings fold");
assert_eq!(files.exists_of("/p/a.h"), 2);
assert_eq!(
files.reads_of("/p/a.h"),
0,
"looking for a file is not reading it"
);
assert_eq!(files.read(Path::new("/p/a.h")).as_deref(), Some("int x;\n"));
assert_eq!(files.reads_of("/p/a.h"), 1);
assert_eq!(files.probes().len(), 2, "and the two logs are separate");
}
#[test]
fn interning_folds_case_when_the_filesystem_does() {
let mut interner = PathInterner::new(true);
assert_eq!(
interner.intern(Path::new("A.H")),
interner.intern(Path::new("a.h"))
);
}
#[test]
fn the_first_spelling_of_a_path_is_the_one_remembered() {
let mut interner = PathInterner::new(true);
let id = interner.intern(Path::new("A.h"));
interner.intern(Path::new("a.h"));
assert_eq!(interner.path(id), Some(Path::new("A.h")));
}
#[test]
fn memory_files_normalize_like_the_interner_does() {
let memory = MemoryFiles::new().with_file("inc/a.h", "contents");
assert!(memory.exists(Path::new("./inc/a.h")));
assert!(memory.exists(Path::new("inc/b/../a.h")));
assert_eq!(
memory.read(Path::new("inc/a.h")).as_deref(),
Some("contents")
);
}
#[test]
fn an_overlay_finds_a_buffer_before_the_disk() {
let memory = MemoryFiles::new().with_file("a.h", "from memory");
let disk = MemoryFiles::new().with_file("a.h", "from disk");
let overlay = OverlayFiles::new(memory, disk);
assert_eq!(
overlay.read(Path::new("a.h")).as_deref(),
Some("from memory")
);
}
#[test]
fn an_overlay_falls_through_to_the_disk() {
let memory = MemoryFiles::new().with_file("a.h", "from memory");
let disk = MemoryFiles::new().with_file("b.h", "from disk");
let overlay = OverlayFiles::new(memory, disk);
assert_eq!(overlay.read(Path::new("b.h")).as_deref(), Some("from disk"));
}
#[test]
fn a_content_cache_reads_each_file_once() {
let inner = MemoryFiles::new()
.with_file("a.h", "contents")
.with_file("b.h", "other");
let files = CachedFiles::new(inner);
let first = files.text(Path::new("a.h")).expect("read");
let second = files.text(Path::new("a.h")).expect("read");
assert_eq!(&*first, "contents");
assert_eq!(first.as_ptr(), second.as_ptr(), "the same text, not a copy");
assert_eq!(files.reads(), 1, "one read for two asks");
assert_eq!(files.paths_read(), 1);
assert_eq!(files.read(Path::new("a.h")).as_deref(), Some("contents"));
assert_eq!(files.reads(), 1);
assert_eq!(files.text(Path::new("missing.h")), None);
assert_eq!(files.text(Path::new("missing.h")), None);
assert_eq!(files.reads(), 2, "a.h once and missing.h once");
assert_eq!(files.text(Path::new("b.h")).as_deref(), Some("other"));
assert_eq!(files.paths_read(), 3);
}
#[test]
fn a_content_cache_forgets_a_file_when_it_is_told_to() {
let inner = MemoryFiles::new().with_file("a.h", "before");
let files = CachedFiles::new(inner);
assert_eq!(files.text(Path::new("a.h")).as_deref(), Some("before"));
files.reload(Path::new("a.h"));
assert_eq!(
files.text(Path::new("a.h")).as_deref(),
Some("before"),
"the text is re-read, and the provider still says what it said"
);
assert_eq!(files.reads(), 2, "the second ask went to the provider");
}
#[test]
fn a_content_cache_does_not_cache_existence() {
let inner = MemoryFiles::new().with_file("a.h", "contents");
let files = CachedFiles::new(inner);
assert!(files.exists(Path::new("a.h")));
assert!(!files.exists(Path::new("b.h")));
assert_eq!(
files.reads(),
0,
"existence is the provider's question, not the cache's"
);
}
}