Capture editor file dates and make desktop ZIP import observable
This commit is contained in:
1 parent
0ea71d57a9
commit
fbc339f5c4
11 files changed
+231
-72
No files matched your search
@@ -1,15 +1,31 @@
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
fs,
|
||||
path::PathBuf,
|
||||
io::{self, Read},
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
time::SystemTime,
|
||||
};
|
||||
use texrra_engine::native::CompileFailure;
|
||||
use texrra_tex_core::resources::{FileWorld, ProjectSnapshot, ResourceKind, SearchPath};
|
||||
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>,
|
||||
}
|
||||
|
||||
@@ -18,7 +34,7 @@ impl ResourceCapture {
|
||||
&self,
|
||||
project: &Arc<ProjectSnapshot>,
|
||||
) -> Result<Arc<ProjectSnapshot>, String> {
|
||||
project.with_external_resources(&self.resources)
|
||||
project.with_external_resources_and_dates(&self.resources, &self.dates)
|
||||
}
|
||||
|
||||
pub(super) fn expand(
|
||||
@@ -49,6 +65,7 @@ impl ResourceCapture {
|
||||
};
|
||||
let mut queried = BTreeSet::new();
|
||||
let mut added = BTreeMap::new();
|
||||
let mut dates = BTreeMap::new();
|
||||
for lookup in trace
|
||||
.lookups
|
||||
.iter()
|
||||
@@ -67,16 +84,20 @@ impl ResourceCapture {
|
||||
&& 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) = fs::read(&path)
|
||||
&& 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(&added).ok()?;
|
||||
let expanded = project.with_external_resources_and_dates(&added, &dates).ok()?;
|
||||
self.resources.extend(added);
|
||||
self.dates.extend(dates);
|
||||
Some(expanded)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user