Reuse fixed file identity within a live alias scan
This commit is contained in:
1 parent
1c02d8af25
commit
24ea6ef7ee
4 files changed
+168
-32
No files matched your search
@@ -91,6 +91,13 @@ pub struct HostPreparation {
|
||||
#[serde(default)]
|
||||
pub generated_initial: Option<crate::native_generated::GeneratedInitialState>,
|
||||
}
|
||||
#[cfg(unix)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum UnixPathIdentity {
|
||||
Present(u64, u64),
|
||||
Missing,
|
||||
}
|
||||
|
||||
impl HostPreparation {
|
||||
pub(crate) fn environment(&mut self, name: &str) -> Option<OsString> {
|
||||
let value = std::env::var_os(name);
|
||||
@@ -141,6 +148,64 @@ impl HostPreparation {
|
||||
}
|
||||
pub(crate) fn same_file(&mut self, a: &Path, b: &Path) -> bool {
|
||||
let same = a == b || self.same_file_target(a, b);
|
||||
self.record_alias(a, b, same)
|
||||
}
|
||||
|
||||
pub(crate) fn aliases_any<'a>(
|
||||
&mut self,
|
||||
path: &Path,
|
||||
candidates: impl Iterator<Item = &'a Path> + Clone,
|
||||
) -> bool {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let mut candidates = candidates.peekable();
|
||||
let Some(first) = candidates.peek().copied() else {
|
||||
return false;
|
||||
};
|
||||
if path == first {
|
||||
return self.same_file(first, path);
|
||||
}
|
||||
if let Some(identity) = self.path_identity(path) {
|
||||
let same = candidates.clone().any(|candidate| {
|
||||
let alias = if candidate == path {
|
||||
true
|
||||
} else if identity == UnixPathIdentity::Missing {
|
||||
false
|
||||
} else if let Some(candidate_identity) = self.path_identity(candidate) {
|
||||
identity == candidate_identity
|
||||
} else {
|
||||
return self.same_file(candidate, path);
|
||||
};
|
||||
self.record_alias(candidate, path, alias)
|
||||
});
|
||||
// Keep observations local to this scan. If the fixed path
|
||||
// changed during a negative scan, use fresh pairwise probes.
|
||||
if same || self.path_identity(path) == Some(identity) {
|
||||
return same;
|
||||
}
|
||||
}
|
||||
return candidates.any(|candidate| self.same_file(candidate, path));
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
let mut candidates = candidates;
|
||||
candidates.any(|candidate| self.same_file(candidate, path))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn path_identity(&mut self, path: &Path) -> Option<UnixPathIdentity> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
match self.metadata(path) {
|
||||
Ok(metadata) => Some(UnixPathIdentity::Present(metadata.dev(), metadata.ino())),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => {
|
||||
Some(UnixPathIdentity::Missing)
|
||||
}
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn record_alias(&mut self, a: &Path, b: &Path, same: bool) -> bool {
|
||||
self.observations.push(HostObservation::Alias {
|
||||
left: a.into(),
|
||||
right: b.into(),
|
||||
|
||||
+58
-31
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user