Reduce Windows desktop format, opening and edit latency
This commit is contained in:
1 parent
4655f8a3c9
commit
830292b9b6
15 files changed
+1006
-167
No files matched your search
+137
-1
@@ -186,13 +186,60 @@ impl HostPreparation {
|
||||
}
|
||||
return candidates.any(|candidate| self.same_file(candidate, path));
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
#[cfg(windows)]
|
||||
{
|
||||
self.aliases_any_windows(path, candidates)
|
||||
}
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
{
|
||||
let mut candidates = candidates;
|
||||
candidates.any(|candidate| self.same_file(candidate, path))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(windows, test))]
|
||||
fn aliases_any_windows<'a>(
|
||||
&mut self,
|
||||
path: &Path,
|
||||
mut candidates: impl Iterator<Item = &'a Path> + Clone,
|
||||
) -> bool {
|
||||
self.scan_canonical_aliases(path, candidates.clone())
|
||||
.unwrap_or_else(|| candidates.any(|candidate| self.same_file(candidate, path)))
|
||||
}
|
||||
|
||||
#[cfg(any(windows, test))]
|
||||
fn scan_canonical_aliases<'a>(
|
||||
&mut self,
|
||||
path: &Path,
|
||||
candidates: impl Iterator<Item = &'a Path>,
|
||||
) -> Option<bool> {
|
||||
let mut candidates = candidates.peekable();
|
||||
let Some(first) = candidates.peek().copied() else { return Some(false); };
|
||||
if first == path {
|
||||
return Some(self.record_alias(first, path, true));
|
||||
}
|
||||
let target = self.canonical_alias_target(path).ok()?;
|
||||
for candidate in candidates {
|
||||
let same = candidate == path
|
||||
|| (target.is_some() && self.canonical_alias_target(candidate).ok()? == target);
|
||||
if self.record_alias(candidate, path, same) {
|
||||
return Some(true);
|
||||
}
|
||||
}
|
||||
// Retry with live pairwise checks if the fixed path changed during
|
||||
// a negative scan, including when a previously missing file appeared.
|
||||
(self.canonical_alias_target(path).ok()? == target).then_some(false)
|
||||
}
|
||||
|
||||
#[cfg(any(windows, test))]
|
||||
fn canonical_alias_target(&mut self, path: &Path) -> io::Result<Option<PathBuf>> {
|
||||
match self.canonicalize(path) {
|
||||
Ok(path) => Ok(Some(path)),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(None),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
|
||||
/// Compare two outputs with the same protected paths in one live scan.
|
||||
pub(crate) fn either_aliases_any<'a>(
|
||||
&mut self,
|
||||
@@ -338,3 +385,92 @@ impl HostPreparation {
|
||||
result.map(|()| Some(data))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod canonical_alias_tests {
|
||||
use super::*;
|
||||
|
||||
fn directory(name: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("texrra-canonical-alias-{name}-{}", std::process::id()));
|
||||
fs::create_dir(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
fn canonicalization_paths(host: &HostPreparation) -> Vec<&Path> {
|
||||
host.observations.iter().filter_map(|observation| match observation {
|
||||
HostObservation::Canonicalize { path, .. } => Some(path.as_path()),
|
||||
_ => None,
|
||||
}).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn windows_alias_scan_reuses_the_fixed_target_probe() {
|
||||
let dir = directory("stable");
|
||||
let first = dir.join("first");
|
||||
let second = dir.join("second");
|
||||
let fixed = dir.join("fixed");
|
||||
for path in [&first, &second] {
|
||||
fs::write(path, b"input").unwrap();
|
||||
}
|
||||
|
||||
let mut host = HostPreparation::default();
|
||||
assert!(!host.aliases_any_windows(&fixed, [first.as_path(), second.as_path()].into_iter()));
|
||||
assert_eq!(canonicalization_paths(&host), [&fixed, &fixed]);
|
||||
assert!(matches!(host.observations[1], HostObservation::Alias { same: false, .. }));
|
||||
assert!(matches!(host.observations[2], HostObservation::Alias { same: false, .. }));
|
||||
|
||||
let mut host = HostPreparation::default();
|
||||
assert!(host.aliases_any_windows(&fixed, [fixed.as_path()].into_iter()));
|
||||
assert!(canonicalization_paths(&host).is_empty());
|
||||
|
||||
fs::write(&fixed, b"output").unwrap();
|
||||
let mut host = HostPreparation::default();
|
||||
assert!(!host.aliases_any_windows(&fixed, [first.as_path(), second.as_path()].into_iter()));
|
||||
assert_eq!(canonicalization_paths(&host), [&fixed, &first, &second, &fixed]);
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn windows_alias_scan_falls_back_when_the_fixed_target_appears_or_a_probe_fails() {
|
||||
use std::{cell::Cell, os::unix::fs::symlink};
|
||||
|
||||
let dir = directory("fallback");
|
||||
let first = dir.join("first");
|
||||
let second = dir.join("second");
|
||||
let fixed = dir.join("fixed");
|
||||
let loop_path = dir.join("path-loop");
|
||||
fs::write(&first, b"first").unwrap();
|
||||
fs::write(&second, b"second").unwrap();
|
||||
symlink(&loop_path, &loop_path).unwrap();
|
||||
|
||||
let added = Cell::new(false);
|
||||
let candidates = [first.as_path(), second.as_path()].into_iter().inspect(|&path| {
|
||||
if path == second && !added.replace(true) {
|
||||
symlink(&first, &fixed).unwrap();
|
||||
}
|
||||
});
|
||||
let mut host = HostPreparation::default();
|
||||
assert!(host.aliases_any_windows(&fixed, candidates));
|
||||
assert!(host.observations.iter().any(|observation| matches!(observation,
|
||||
HostObservation::Alias { left, right, same: false } if left == &first && right == &fixed
|
||||
)));
|
||||
assert!(host.observations.iter().any(|observation| matches!(observation,
|
||||
HostObservation::Alias { left, right, same: true } if left == &first && right == &fixed
|
||||
)));
|
||||
|
||||
let mut host = HostPreparation::default();
|
||||
assert!(host.aliases_any_windows(&fixed, [first.as_path()].into_iter()));
|
||||
assert_eq!(canonicalization_paths(&host), [&fixed, &first]);
|
||||
|
||||
let mut host = HostPreparation::default();
|
||||
assert!(host.aliases_any_windows(&fixed, [loop_path.as_path(), first.as_path()].into_iter()));
|
||||
assert!(host.observations.iter().any(|observation| matches!(observation,
|
||||
HostObservation::Canonicalize { path, result: Err(_) } if path == &loop_path
|
||||
)));
|
||||
assert!(host.observations.iter().any(|observation| matches!(observation,
|
||||
HostObservation::Alias { left, right, same: true } if left == &first && right == &fixed
|
||||
)));
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user