Build a standalone Windows desktop with opt-in project removal

This commit is contained in:
gloamxun committed 2026-12-02 18:20:38 +08:00
1 parent 2c5b9b5f10
commit 2c99cade7d
24 files changed
+1404 -168

No files matched your search

+14 -3
View File
@@ -58,9 +58,20 @@ pub(crate) struct Worker {
impl Worker {
pub(crate) fn start() -> Result<Self, String> {
// Re-execute the current image, even if an installer has replaced its
// original path since the process opened the project.
let mut child = Command::new("/proc/self/exe")
// Linux can re-execute the loaded image if its path has been replaced.
#[cfg(target_os = "linux")]
let executable = "/proc/self/exe";
#[cfg(not(target_os = "linux"))]
let executable =
std::env::current_exe().map_err(|e| format!("pdfTeX regex worker executable: {e}"))?;
let mut command = Command::new(executable);
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
command.creation_flags(CREATE_NO_WINDOW);
}
let mut child = command
.arg(WORKER_ARGUMENT)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
+65 -25
View File
@@ -129,27 +129,38 @@ impl ProjectStore {
MAX_PROJECT_FILES
));
}
let entries: Vec<_> = entries
.into_iter()
.filter_map(|(entry, data)| entry_path(&entry).map(|path| (path, data)))
.collect();
if entries.is_empty() {
return Err("the archive does not contain any usable files".to_string());
}
let id = self.unique_id(&slug(&name));
let dir = self.root.join(&id);
fs::create_dir_all(&dir).map_err(|error| format!("could not create project: {error}"))?;
let mut names = Vec::new();
for (entry, data) in &entries {
let Some(path) = entry_path(entry) else {
continue;
};
let target = dir.join(&path);
fs::create_dir(&dir).map_err(|error| format!("could not create project: {error}"))?;
let extracted = entries.iter().try_for_each(|(relative, data)| {
let target = dir.join(relative);
if let Some(parent) = target.parent() {
fs::create_dir_all(parent)
.map_err(|error| format!("could not create {}: {error}", parent.display()))?;
}
fs::write(&target, data)
.map_err(|error| format!("could not write {}: {error}", target.display()))?;
names.push(entry.clone());
}
if names.is_empty() {
let written = if cfg!(windows) {
fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&target)
.and_then(|mut file| file.write_all(data))
} else {
fs::write(&target, data)
};
written.map_err(|error| format!("could not write {}: {error}", target.display()))
});
if let Err(error) = extracted {
let _ = fs::remove_dir_all(&dir);
return Err("the archive does not contain any usable files".to_string());
return Err(error);
}
let names: Vec<_> = entries.into_iter().map(|(path, _)| path).collect();
let main_file = detect_main(&dir, &names).unwrap_or_else(|| {
names
.iter()
@@ -331,16 +342,10 @@ impl ProjectStore {
if relative.is_empty() {
return Err("missing file path".to_string());
}
if relative.contains('\\') {
if !valid_relative(relative) {
return Err("invalid file path".to_string());
}
let mut path = dir.clone();
for component in Path::new(relative).components() {
match component {
Component::Normal(name) => path.push(name),
_ => return Err("invalid file path".to_string()),
}
}
let path = dir.join(relative);
if !path.starts_with(&dir) {
return Err("invalid file path".to_string());
}
@@ -372,7 +377,9 @@ impl ProjectStore {
fn unique_id(&self, base: &str) -> String {
let mut candidate = base.to_string();
let mut index = 2;
while self.root.join(&candidate).exists() {
while self.root.join(&candidate).exists()
|| cfg!(windows) && !windows_file_component(&candidate)
{
candidate = format!("{base}-{index}");
index += 1;
}
@@ -501,6 +508,7 @@ fn slug(name: &str) -> String {
fn valid_id(id: &str) -> bool {
!id.is_empty()
&& (!cfg!(windows) || windows_file_component(id))
&& id.len() <= 64
&& id.chars().all(|character| {
character.is_ascii_alphanumeric() || character == '-' || character == '_'
@@ -511,9 +519,41 @@ fn valid_relative(path: &str) -> bool {
!path.is_empty()
&& !path.starts_with('/')
&& !path.contains('\\')
&& Path::new(path)
.components()
.all(|component| matches!(component, Component::Normal(_)))
&& Path::new(path).components().all(|component| {
matches!(component, Component::Normal(name) if
!cfg!(windows) || name.to_str().is_some_and(windows_file_component))
})
}
fn windows_file_component(name: &str) -> bool {
if name.ends_with([' ', '.'])
|| name
.bytes()
.any(|byte| byte < 32 || b"<>:\"|?*".contains(&byte))
{
return false;
}
let stem = name
.split('.')
.next()
.unwrap_or_default()
.trim_end_matches(' ');
let stem = stem.to_ascii_uppercase();
if matches!(
stem.as_str(),
"CON" | "PRN" | "AUX" | "NUL" | "CONIN$" | "CONOUT$"
) {
return false;
}
!stem
.strip_prefix("COM")
.or_else(|| stem.strip_prefix("LPT"))
.is_some_and(|suffix| {
matches!(
suffix,
"1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "¹" | "²" | "³"
)
})
}
fn entry_path(entry: &str) -> Option<String> {