Reuse fixed file identity within a live alias scan

This commit is contained in:
gloamxun committed 2026-11-19 22:02:53 +08:00
1 parent 1c02d8af25
commit 24ea6ef7ee
4 files changed
+168 -32

No files matched your search

+58 -31
View File
@@ -317,9 +317,13 @@ impl Files {
|| resource.path == pdf
|| existing_log.as_ref() == Some(&resource.path)
|| existing_output.as_ref() == Some(&resource.path)
|| preparation.same_file(&resource.path, &log_path)
|| preparation.same_file(&resource.path, &pdf)
})
|| preparation.aliases_any(
&log_path,
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)
{
@@ -354,9 +358,8 @@ impl Files {
protected.extend(project.immutable_paths().map(Path::to_path_buf));
protected.sort();
protected.dedup();
if project
.immutable_paths()
.any(|p| preparation.same_file(p, &pdf) || preparation.same_file(p, &log_path))
if preparation.aliases_any(&pdf, project.immutable_paths())
|| preparation.aliases_any(&log_path, project.immutable_paths())
{
return Err("native output would overwrite an atomic project input".into());
}
@@ -365,11 +368,8 @@ impl Files {
preparation.same_file(p, source)
|| preparation.same_file(p, &pdf)
|| preparation.same_file(p, &log_path)
|| world
.trace()
.reads
.iter()
.any(|r| preparation.same_file(p, &r.path))
|| preparation
.aliases_any(p, world.trace().reads.iter().map(|r| r.path.as_path()))
}) {
return Err(
"native artifact metadata must differ from protected inputs and outputs".into(),
@@ -408,10 +408,13 @@ impl Files {
let immutable_claims: Vec<_> = ledger
.paths()
.filter(|path| {
protected
.iter()
.chain(std::iter::once(&log_path))
.any(|p| preparation.same_file(p, path))
preparation.aliases_any(
path,
protected
.iter()
.chain(std::iter::once(&log_path))
.map(PathBuf::as_path),
)
})
.map(Path::to_owned)
.collect();
@@ -423,11 +426,13 @@ impl Files {
.map(|p| p.generated().map(Path::to_path_buf).collect())
.unwrap_or_default();
for path in declared {
if protected
.iter()
.chain(std::iter::once(&log_path))
.any(|p| preparation.same_file(p, &path))
{
if preparation.aliases_any(
&path,
protected
.iter()
.chain(std::iter::once(&log_path))
.map(PathBuf::as_path),
) {
return Err("atomic generated role aliases a protected input or output".into());
}
let observed = file_version(&path);
@@ -848,10 +853,13 @@ impl Files {
Ok(())
}
fn is_protected(&self, path: &Path) -> bool {
self.protected
.iter()
.chain(std::iter::once(&self.log_path))
.any(|p| self.same_file(p, path))
self.runtime_observations.borrow_mut().aliases_any(
path,
self.protected
.iter()
.chain(std::iter::once(&self.log_path))
.map(PathBuf::as_path),
)
}
fn observe_resource(&mut self, kind: ResourceKind, resource: &LoadedResource) {
let path = &resource.version.path;
@@ -1511,10 +1519,7 @@ impl SourceProvider for Files {
Ok(resource)
}
fn file_size(&mut self, name: &str, from: &str) -> Result<Option<u64>, String> {
let Some(resource) = self
.world
.read_tex_input_optional(name, Path::new(from))?
else {
let Some(resource) = self.world.read_tex_input_optional(name, Path::new(from))? else {
return Ok(None);
};
self.observe_resource(ResourceKind::Tex, &resource);
@@ -1526,10 +1531,7 @@ impl SourceProvider for Files {
fn open_in(&mut self, stream: u8, name: &str, from: &str) -> Result<Option<String>, String> {
self.release_input(stream, ReleaseCause::Replaced, false);
let result = (|| -> Result<Option<InputOpened>, String> {
let Some(path) = self
.world
.resolve_tex_input(name, Path::new(from))?
else {
let Some(path) = self.world.resolve_tex_input(name, Path::new(from))? else {
return Ok(None);
};
let (reader, storage): (Box<dyn BufRead + Send>, InputStorage) =
@@ -2628,6 +2630,31 @@ mod generated_file_tests {
assert!(p.ledger.as_ref().unwrap().version.is_none());
fs::remove_dir_all(root).unwrap();
}
#[cfg(unix)]
#[test]
fn scanning_aliases_rechecks_a_changed_target() {
let root = root();
let target = root.join("target");
let replacement = root.join("replacement");
let peer = root.join("main.tex");
let other = root.join("other");
fs::write(&target, b"original").unwrap();
fs::write(&other, b"other").unwrap();
fs::hard_link(&peer, &replacement).unwrap();
let switched = std::cell::Cell::new(false);
let candidates = [other.as_path(), peer.as_path()]
.into_iter()
.inspect(|candidate| {
if *candidate == peer && !switched.replace(true) {
fs::rename(&replacement, &target).unwrap();
}
});
let mut preparation = HostPreparation::default();
assert!(preparation.aliases_any(&target, candidates));
assert_eq!(fs::read(&target).unwrap(), fs::read(&peer).unwrap());
fs::remove_dir_all(root).unwrap();
}
#[cfg(unix)]
#[test]
fn preparation_alias_rejection_records_actual_hardlink_identity() {