Validate live checkpoint reuse across atomic project file changes

This commit is contained in:
gloamxun committed 2026-11-20 08:26:44 +08:00
1 parent e77b15d8bd
commit 287d4a7cf0
5 files changed
+377 -39

No files matched your search

+51 -23
View File
@@ -30,7 +30,7 @@ struct Edit {
visible_revision: Option<u64>,
source: Option<String>,
#[serde(default)]
files: BTreeMap<String, String>,
files: BTreeMap<String, Option<String>>,
#[serde(default)]
preview_draft: bool,
}
@@ -214,12 +214,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = project.file(&entry).expect("the editor owns its entry");
let files = editable
.iter()
.map(|(name, path)| {
let file = project
.file(path)
.expect("editable project files are immutable");
Ok((name, std::str::from_utf8(&file.data)?))
})
.filter_map(|(name, path)| project.file(path).map(|file| (name, file)))
.map(|(name, file)| Ok((name, std::str::from_utf8(&file.data)?)))
.collect::<Result<BTreeMap<_, _>, std::str::Utf8Error>>()?;
reply(
request,
@@ -257,18 +253,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let path = editable
.get(&name)
.ok_or_else(|| format!("File is not editable: {name}"))?;
Ok((path.clone(), Arc::from(value.into_bytes())))
if *path == entry && value.is_none() {
return Err("The entry file cannot be removed".into());
}
Ok((path.clone(), value.map(|text| Arc::from(text.into_bytes()))))
})
.collect::<Result<BTreeMap<_, _>, String>>();
let next = updates.and_then(|mut updates| {
if let Some(source) = edit.source
&& updates
.insert(entry.clone(), Arc::from(source.into_bytes()))
.insert(entry.clone(), Some(Arc::from(source.into_bytes())))
.is_some()
{
return Err("The entry file was edited twice".into());
}
project.with_file_edits(edit.sequence, updates)
project.with_file_changes(edit.sequence, updates)
});
project = match next {
Ok(project) => project,
@@ -399,6 +398,8 @@ fn prepare(
let mut editable = BTreeMap::from([(name.to_owned(), entry.clone())]);
let mut files = BTreeMap::<PathBuf, Arc<[u8]>>::from([(entry.clone(), Arc::from(original))]);
let source_root = source.parent().expect("the entry has a parent");
let mut declared = BTreeSet::from([entry.clone()]);
let mut directories = BTreeSet::new();
for relative in additional {
if relative.as_os_str().is_empty()
|| relative
@@ -412,23 +413,50 @@ fn prepare(
.into());
}
let logical = run.join(relative);
let physical = source_root.join(relative).canonicalize()?;
if !physical.starts_with(source_root) || files.contains_key(&logical) {
return Err(format!(
"Project file is outside the entry directory or is listed twice: {}",
relative.display()
)
.into());
if !declared.insert(logical.clone()) {
return Err(format!("Project file is listed twice: {}", relative.display()).into());
}
let data: Arc<[u8]> = Arc::from(fs::read(physical)?);
fs::create_dir_all(logical.parent().expect("project file has a parent"))?;
fs::write(&logical, &data)?;
let physical = source_root.join(relative);
let data = match physical.canonicalize() {
Ok(path) if path.starts_with(source_root) => Some(Arc::<[u8]>::from(fs::read(path)?)),
Ok(_) => {
return Err(format!(
"Project file is outside the entry directory: {}",
relative.display()
)
.into());
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
let parent = physical
.parent()
.expect("project file has a parent")
.canonicalize()?;
if !parent.starts_with(source_root) || physical.symlink_metadata().is_ok() {
return Err(error.into());
}
None
}
Err(error) => return Err(error.into()),
};
let directory = logical.parent().expect("project file has a parent");
fs::create_dir_all(directory)?;
directories.extend(
directory
.ancestors()
.take_while(|path| path.starts_with(run))
.map(Path::to_owned),
);
if let Some(name) = relative.to_str()
&& std::str::from_utf8(&data).is_ok()
&& data
.as_deref()
.is_none_or(|bytes| std::str::from_utf8(bytes).is_ok())
{
editable.insert(name.into(), logical.clone());
}
files.insert(logical, data);
if let Some(data) = data {
fs::write(&logical, &data)?;
files.insert(logical, data);
}
}
let mut request = CompileRequest::new(
world.clone(),
@@ -475,7 +503,7 @@ fn prepare(
revision: 1,
roots: coverage,
files,
directories: BTreeSet::new(),
directories,
aliases: BTreeMap::new(),
generated_roots: BTreeSet::from([run.to_owned()]),
generated,