Merge remote-tracking branch 'origin/master' into worktree-agent-a80b9c17e273a868d
# Conflicts: # TODO.md # src/i18n/de.ts # src/i18n/en.ts
This commit is contained in:
52 files changed
+895
-130
No files matched your search
@@ -0,0 +1,98 @@
|
||||
// Absturzsicheres Speichern ([116]): erst `<pfad>.tmp` im selben Ordner schreiben und
|
||||
// auf den Datentraeger zwingen (fsync), dann die bisherige Datei als `<pfad>~`
|
||||
// (eine Generation) sichern und die Temp-Datei per Umbenennen an den Zielpfad setzen.
|
||||
// Bricht irgendein Schritt ab, bleibt die urspruengliche Datei unveraendert.
|
||||
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn with_suffix(path: &Path, suffix: &str) -> PathBuf {
|
||||
let mut s = path.as_os_str().to_os_string();
|
||||
s.push(suffix);
|
||||
PathBuf::from(s)
|
||||
}
|
||||
|
||||
/// Schreibt `content` atomar nach `path`; die Vorversion landet in `<path>~`.
|
||||
pub fn save_atomic_impl(path: &Path, content: &[u8]) -> Result<(), String> {
|
||||
let tmp = with_suffix(path, ".tmp");
|
||||
let backup = with_suffix(path, "~");
|
||||
let write_tmp = || -> std::io::Result<()> {
|
||||
let mut f = File::create(&tmp)?;
|
||||
f.write_all(content)?;
|
||||
f.sync_all()
|
||||
};
|
||||
if let Err(e) = write_tmp() {
|
||||
let _ = fs::remove_file(&tmp);
|
||||
return Err(format!("Temp-Datei schreiben: {e}"));
|
||||
}
|
||||
// Vorversion sichern (Kopie statt Umbenennen: der Zielpfad existiert so zu jedem Zeitpunkt).
|
||||
if path.exists() {
|
||||
if let Err(e) = fs::copy(path, &backup) {
|
||||
let _ = fs::remove_file(&tmp);
|
||||
return Err(format!("Sicherung anlegen: {e}"));
|
||||
}
|
||||
}
|
||||
if let Err(e) = fs::rename(&tmp, path) {
|
||||
let _ = fs::remove_file(&tmp);
|
||||
return Err(format!("Umbenennen: {e}"));
|
||||
}
|
||||
// Ordner-Eintrag sichern (unter Windows nicht moeglich, dort best effort).
|
||||
if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty()) {
|
||||
if let Ok(d) = File::open(dir) {
|
||||
let _ = d.sync_all();
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn tmpdir(name: &str) -> PathBuf {
|
||||
let d = std::env::temp_dir().join(format!("dossier-atomic-{name}-{}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&d);
|
||||
fs::create_dir_all(&d).unwrap();
|
||||
d
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn neue_datei_ohne_sicherung() {
|
||||
let d = tmpdir("neu");
|
||||
let p = d.join("a.obp");
|
||||
save_atomic_impl(&p, b"eins").unwrap();
|
||||
assert_eq!(fs::read(&p).unwrap(), b"eins");
|
||||
assert!(!with_suffix(&p, "~").exists());
|
||||
assert!(!with_suffix(&p, ".tmp").exists());
|
||||
fs::remove_dir_all(&d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ueberschreiben_legt_sicherung_an() {
|
||||
let d = tmpdir("ueber");
|
||||
let p = d.join("a.obp");
|
||||
save_atomic_impl(&p, b"eins").unwrap();
|
||||
save_atomic_impl(&p, b"zwei").unwrap();
|
||||
assert_eq!(fs::read(&p).unwrap(), b"zwei");
|
||||
assert_eq!(fs::read(with_suffix(&p, "~")).unwrap(), b"eins");
|
||||
save_atomic_impl(&p, b"drei").unwrap();
|
||||
assert_eq!(fs::read(with_suffix(&p, "~")).unwrap(), b"zwei");
|
||||
assert!(!with_suffix(&p, ".tmp").exists());
|
||||
fs::remove_dir_all(&d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fehler_laesst_original_unberuehrt() {
|
||||
let d = tmpdir("fehler");
|
||||
let p = d.join("a.obp");
|
||||
save_atomic_impl(&p, b"original").unwrap();
|
||||
// Ein Ordner an der Stelle der Temp-Datei laesst das Schreiben scheitern.
|
||||
fs::create_dir(with_suffix(&p, ".tmp")).unwrap();
|
||||
assert!(save_atomic_impl(&p, b"neu").is_err());
|
||||
assert_eq!(fs::read(&p).unwrap(), b"original");
|
||||
// Nicht existierender Zielordner: Fehler, nichts angelegt.
|
||||
assert!(save_atomic_impl(&d.join("nope").join("b.obp"), b"x").is_err());
|
||||
fs::remove_dir_all(&d).unwrap();
|
||||
}
|
||||
}
|
||||
+13
-2
@@ -16,6 +16,9 @@ mod native_embed;
|
||||
// App-Instanzen (OS-Advisory-Lock via `fs4`, siehe lock.rs).
|
||||
mod lock;
|
||||
|
||||
// Absturzsicheres Speichern ([116]): Temp-Datei + fsync + Umbenennen, `<pfad>~` als Sicherung.
|
||||
mod atomic_save;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use tauri::State;
|
||||
|
||||
@@ -31,6 +34,12 @@ fn acquire_project_lock(
|
||||
lock::acquire(&state, &PathBuf::from(path))
|
||||
}
|
||||
|
||||
/// Schreibt eine Projektdatei atomar ([116]); die Vorversion bleibt als `<pfad>~` erhalten.
|
||||
#[tauri::command]
|
||||
fn save_atomic(path: String, content: String) -> Result<(), String> {
|
||||
atomic_save::save_atomic_impl(&PathBuf::from(path), content.as_bytes())
|
||||
}
|
||||
|
||||
/// Gibt einen von dieser Instanz gehaltenen Lock frei. No-op, wenn der Pfad
|
||||
/// nicht gelockt war.
|
||||
#[tauri::command]
|
||||
@@ -280,7 +289,8 @@ pub fn run() {
|
||||
push_native_scene,
|
||||
push_native_walls,
|
||||
acquire_project_lock,
|
||||
release_project_lock
|
||||
release_project_lock,
|
||||
save_atomic
|
||||
]);
|
||||
#[cfg(not(any(feature = "native2d", feature = "native3d")))]
|
||||
let builder = builder.invoke_handler(tauri::generate_handler![
|
||||
@@ -295,7 +305,8 @@ pub fn run() {
|
||||
native2d_frame,
|
||||
native2d_texts,
|
||||
acquire_project_lock,
|
||||
release_project_lock
|
||||
release_project_lock,
|
||||
save_atomic
|
||||
]);
|
||||
|
||||
builder
|
||||
|
||||
@@ -21,7 +21,8 @@
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": null
|
||||
"csp": "default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:; style-src 'self' 'unsafe-inline'; font-src 'self' data:; img-src 'self' data: blob: https://ambientcg.com https://api.ambientcg.com https://geo.admin.ch https://api3.geo.admin.ch https://wms.geo.admin.ch https://data.geo.admin.ch https://overpass-api.de https://nominatim.openstreetmap.org; media-src 'self' blob:; connect-src 'self' ipc: http://ipc.localhost blob: data: https://ambientcg.com https://api.ambientcg.com https://geo.admin.ch https://api3.geo.admin.ch https://wms.geo.admin.ch https://data.geo.admin.ch https://overpass-api.de https://nominatim.openstreetmap.org http://localhost:* http://127.0.0.1:* ws://127.0.0.1:*; object-src 'none'; base-uri 'self'; form-action 'none'",
|
||||
"devCsp": null
|
||||
}
|
||||
},
|
||||
"bundle": {
|
||||
|
||||
Reference in new issue
Block a user