Share native checkpoint command bindings and output probes

This commit is contained in:
gloamxun committed 2026-11-29 01:00:10 +08:00
1 parent 8aa079ec25
commit 373204a8d8
4 files changed
+167 -50

No files matched your search

+39
View File
@@ -193,6 +193,45 @@ impl HostPreparation {
}
}
/// Compare two outputs with the same protected paths in one live scan.
pub(crate) fn either_aliases_any<'a>(
&mut self,
outputs: [&Path; 2],
candidates: impl Iterator<Item = &'a Path> + Clone,
) -> bool {
#[cfg(unix)]
if let [Some(first), Some(second)] = outputs.map(|path| self.path_identity(path)) {
let identities = [first, second];
for candidate in candidates.clone() {
if let Some(output) = outputs.iter().find(|&&output| output == candidate) {
return self.record_alias(candidate, output, true);
}
let observed = if identities == [UnixPathIdentity::Missing; 2] {
None
} else {
self.path_identity(candidate)
};
for (output, identity) in outputs.into_iter().zip(identities) {
let same = if identity == UnixPathIdentity::Missing {
self.record_alias(candidate, output, false)
} else if let Some(observed) = observed {
self.record_alias(candidate, output, identity == observed)
} else {
self.same_file(candidate, output)
};
if same {
return true;
}
}
}
// Fresh pairwise checks if either destination changed during the scan.
if outputs.map(|path| self.path_identity(path)) == identities.map(Some) {
return false;
}
}
outputs.into_iter().any(|path| self.aliases_any(path, candidates.clone()))
}
#[cfg(unix)]
fn path_identity(&mut self, path: &Path) -> Option<UnixPathIdentity> {
use std::os::unix::fs::MetadataExt;
+44 -7
View File
@@ -328,12 +328,10 @@ impl Files {
|| existing_log.as_ref() == Some(&resource.path)
|| existing_output.as_ref() == Some(&resource.path)
})
|| preparation.aliases_any(
&log_path,
|| preparation.either_aliases_any(
[&log_path, &pdf],
world.trace().reads.iter().map(|r| r.path.as_path()),
)
|| preparation
.aliases_any(&pdf, world.trace().reads.iter().map(|r| r.path.as_path()))
|| preparation.same_file(source, &log_path)
|| preparation.same_file(&pdf, &log_path)
{
@@ -368,9 +366,7 @@ impl Files {
protected.extend(project.immutable_paths().map(Path::to_path_buf));
protected.sort();
protected.dedup();
if preparation.aliases_any(&pdf, project.immutable_paths())
|| preparation.aliases_any(&log_path, project.immutable_paths())
{
if preparation.either_aliases_any([&pdf, &log_path], project.immutable_paths()) {
return Err("native output would overwrite an atomic project input".into());
}
}
@@ -2864,6 +2860,47 @@ mod generated_file_tests {
fs::remove_dir_all(root).unwrap();
}
#[cfg(unix)]
#[test]
fn paired_alias_scan_reads_an_input_once_and_rechecks_either_destination() {
use crate::native_host::HostObservation;
use std::cell::Cell;
let root = root();
let pdf = root.join("job.pdf");
let log = root.join("job.log");
let input = root.join("main.tex");
let replacement = root.join("pending-link");
for output in [&pdf, &log] {
fs::write(output, b"independent destination").unwrap();
}
let mut preparation = HostPreparation::default();
assert!(!preparation.either_aliases_any([&pdf, &log], [input.as_path()].into_iter()));
let input_reads = preparation.observations.iter().filter(|observation| {
matches!(observation, HostObservation::Metadata { path, .. } if path == &input)
});
assert_eq!(input_reads.count(), 1);
for output in [&pdf, &log] {
fs::hard_link(&input, &replacement).unwrap();
let switched = Cell::new(false);
let candidates = [input.as_path()].into_iter().inspect(|_| {
if !switched.replace(true) {
fs::rename(&replacement, output).unwrap();
}
});
let mut preparation = HostPreparation::default();
assert!(preparation.either_aliases_any([&pdf, &log], candidates));
assert!(preparation.observations.iter().any(|observation| {
matches!(observation, HostObservation::Alias { right, same: true, .. } if right == output)
}));
fs::remove_file(output).unwrap();
fs::write(output, b"independent destination").unwrap();
}
fs::remove_dir_all(root).unwrap();
}
#[cfg(unix)]
#[test]
fn preparation_alias_rejection_records_actual_hardlink_identity() {