104 lines
3.6 KiB
Rust
104 lines
3.6 KiB
Rust
use std::{
|
|
collections::{BTreeMap, BTreeSet},
|
|
fs,
|
|
io::{self, Read},
|
|
path::{Path, PathBuf},
|
|
sync::Arc,
|
|
time::SystemTime,
|
|
};
|
|
use texrra_engine::native::CompileFailure;
|
|
use texrra_tex_core::resources::{FileModificationTime, FileWorld, ProjectSnapshot, ResourceKind, SearchPath};
|
|
|
|
// Use the opened file for both observations so a renamed path cannot supply
|
|
// a date from a different file.
|
|
pub(super) fn read_with_date(path: &Path) -> io::Result<(Vec<u8>, Option<SystemTime>)> {
|
|
let mut file = fs::File::open(path)?;
|
|
let modified = file.metadata()?.modified().ok();
|
|
let mut bytes = Vec::new();
|
|
file.read_to_end(&mut bytes)?;
|
|
if file.metadata()?.modified().ok() != modified {
|
|
return Err(io::Error::other(format!("Input changed during capture: {}", path.display())));
|
|
}
|
|
Ok((bytes, modified))
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub(super) struct ResourceCapture {
|
|
resources: BTreeMap<PathBuf, Arc<[u8]>>,
|
|
dates: BTreeMap<PathBuf, FileModificationTime>,
|
|
host: Option<FileWorld>,
|
|
}
|
|
|
|
impl ResourceCapture {
|
|
pub(super) fn apply(
|
|
&self,
|
|
project: &Arc<ProjectSnapshot>,
|
|
) -> Result<Arc<ProjectSnapshot>, String> {
|
|
project.with_external_resources_and_dates(&self.resources, &self.dates)
|
|
}
|
|
|
|
pub(super) fn expand(
|
|
&mut self,
|
|
project: &Arc<ProjectSnapshot>,
|
|
failure: &CompileFailure,
|
|
) -> Option<Arc<ProjectSnapshot>> {
|
|
let trace = failure.trace.as_ref()?;
|
|
let config = project.configuration();
|
|
let search_roots = config
|
|
.texmf_roots
|
|
.iter()
|
|
.cloned()
|
|
.chain(
|
|
config
|
|
.paths
|
|
.values()
|
|
.flatten()
|
|
.filter_map(|path| match path {
|
|
SearchPath::Directory { path, .. } => Some(config.cwd.join(path)),
|
|
SearchPath::Defaults => None,
|
|
}),
|
|
)
|
|
.collect::<Vec<_>>();
|
|
let host = match &mut self.host {
|
|
Some(host) => host,
|
|
slot => slot.insert(FileWorld::new(config.clone()).ok()?),
|
|
};
|
|
let mut queried = BTreeSet::new();
|
|
let mut added = BTreeMap::new();
|
|
let mut dates = BTreeMap::new();
|
|
for lookup in trace
|
|
.lookups
|
|
.iter()
|
|
.filter(|lookup| lookup.resolved.is_none())
|
|
{
|
|
if !queried.insert((lookup.kind, &lookup.name, &lookup.from)) {
|
|
continue;
|
|
}
|
|
let resolved = if lookup.kind == ResourceKind::Tex {
|
|
host.resolve_tex_input(&lookup.name, &lookup.from)
|
|
} else {
|
|
host.resolve(lookup.kind, &lookup.name, &lookup.from)
|
|
};
|
|
if let Ok(Some(path)) = resolved
|
|
&& !path.starts_with(&config.cwd)
|
|
&& search_roots.iter().any(|root| path.starts_with(root))
|
|
&& project.file(&path).is_none()
|
|
&& fs::metadata(&path).is_ok_and(|metadata| metadata.is_file())
|
|
&& let Ok((bytes, modified)) = read_with_date(&path)
|
|
{
|
|
if let Some(date) = modified.and_then(|time| FileModificationTime::from_system_time(time).ok()) {
|
|
dates.insert(path.clone(), date);
|
|
}
|
|
added.insert(path, Arc::from(bytes));
|
|
}
|
|
}
|
|
if added.is_empty() {
|
|
return None;
|
|
}
|
|
let expanded = project.with_external_resources_and_dates(&added, &dates).ok()?;
|
|
self.resources.extend(added);
|
|
self.dates.extend(dates);
|
|
Some(expanded)
|
|
}
|
|
}
|