[116] Exporte atomar (Tauri save_export), Electron speichert Projekte atomar im Hauptprozess, Wiederherstellung aus .obp~ (Öffnen-Angebot + Datei-Menü)
This commit is contained in:
22 files changed
+755
-29
No files matched your search
@@ -15,6 +15,44 @@ fn with_suffix(path: &Path, suffix: &str) -> PathBuf {
|
||||
|
||||
/// Schreibt `content` atomar nach `path`; die Vorversion landet in `<path>~`.
|
||||
pub fn save_atomic_impl(path: &Path, content: &[u8]) -> Result<(), String> {
|
||||
write_atomic(path, content, true)
|
||||
}
|
||||
|
||||
/// Export ([116]): atomar wie `save_atomic_impl`, aber ohne `<path>~` (ein Export ersetzt eine Datei bewusst).
|
||||
pub fn export_atomic_impl(path: &Path, content: &[u8]) -> Result<(), String> {
|
||||
write_atomic(path, content, false)
|
||||
}
|
||||
|
||||
/// Gibt es eine Sicherung der Vorversion (`<path>~`)?
|
||||
pub fn backup_exists_impl(path: &Path) -> bool {
|
||||
with_suffix(path, "~").is_file()
|
||||
}
|
||||
|
||||
/// Liest die Sicherung der Vorversion (`<path>~`); `None`, wenn es keine gibt oder sie nicht als UTF-8 lesbar ist.
|
||||
pub fn read_backup_impl(path: &Path) -> Option<String> {
|
||||
fs::read_to_string(with_suffix(path, "~")).ok()
|
||||
}
|
||||
|
||||
/// Dekodiert `%XX` (UTF-8) aus einem Header-Wert; ungültige Folgen bleiben wie sie sind.
|
||||
pub fn percent_decode(s: &str) -> String {
|
||||
let b = s.as_bytes();
|
||||
let mut out = Vec::with_capacity(b.len());
|
||||
let mut i = 0;
|
||||
while i < b.len() {
|
||||
if b[i] == b'%' && i + 2 < b.len() + 0 && i + 2 <= b.len() - 1 {
|
||||
if let Ok(v) = u8::from_str_radix(&s[i + 1..i + 3], 16) {
|
||||
out.push(v);
|
||||
i += 3;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
out.push(b[i]);
|
||||
i += 1;
|
||||
}
|
||||
String::from_utf8_lossy(&out).into_owned()
|
||||
}
|
||||
|
||||
fn write_atomic(path: &Path, content: &[u8], keep_backup: bool) -> Result<(), String> {
|
||||
let tmp = with_suffix(path, ".tmp");
|
||||
let backup = with_suffix(path, "~");
|
||||
let write_tmp = || -> std::io::Result<()> {
|
||||
@@ -27,7 +65,7 @@ pub fn save_atomic_impl(path: &Path, content: &[u8]) -> Result<(), String> {
|
||||
return Err(format!("Temp-Datei schreiben: {e}"));
|
||||
}
|
||||
// Vorversion sichern (Kopie statt Umbenennen: der Zielpfad existiert so zu jedem Zeitpunkt).
|
||||
if path.exists() {
|
||||
if keep_backup && path.exists() {
|
||||
if let Err(e) = fs::copy(path, &backup) {
|
||||
let _ = fs::remove_file(&tmp);
|
||||
return Err(format!("Sicherung anlegen: {e}"));
|
||||
@@ -57,6 +95,60 @@ mod tests {
|
||||
d
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_ueberschreibt_atomar_ohne_sicherung() {
|
||||
let d = tmpdir("export");
|
||||
let p = d.join("plan.pdf");
|
||||
export_atomic_impl(&p, b"%PDF-1").unwrap();
|
||||
export_atomic_impl(&p, &[0u8, 255, 1, 2]).unwrap();
|
||||
assert_eq!(fs::read(&p).unwrap(), vec![0u8, 255, 1, 2]);
|
||||
assert!(!with_suffix(&p, "~").exists());
|
||||
assert!(!with_suffix(&p, ".tmp").exists());
|
||||
fs::remove_dir_all(&d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_fehler_laesst_ziel_heil() {
|
||||
let d = tmpdir("export-fehler");
|
||||
let p = d.join("plan.dxf");
|
||||
export_atomic_impl(&p, b"alt").unwrap();
|
||||
fs::create_dir(with_suffix(&p, ".tmp")).unwrap();
|
||||
assert!(export_atomic_impl(&p, b"neu").is_err());
|
||||
assert_eq!(fs::read(&p).unwrap(), b"alt");
|
||||
assert!(export_atomic_impl(&d.join("nope").join("x.dxf"), b"x").is_err());
|
||||
fs::remove_dir_all(&d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sicherung_wird_erkannt() {
|
||||
let d = tmpdir("exists");
|
||||
let p = d.join("a.obp");
|
||||
save_atomic_impl(&p, b"eins").unwrap();
|
||||
assert!(!backup_exists_impl(&p));
|
||||
save_atomic_impl(&p, b"zwei").unwrap();
|
||||
assert!(backup_exists_impl(&p));
|
||||
fs::remove_dir_all(&d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sicherung_wird_gelesen() {
|
||||
let d = tmpdir("read-backup");
|
||||
let p = d.join("a.obp");
|
||||
assert_eq!(read_backup_impl(&p), None);
|
||||
save_atomic_impl(&p, "eins äö".as_bytes()).unwrap();
|
||||
save_atomic_impl(&p, b"zwei").unwrap();
|
||||
assert_eq!(read_backup_impl(&p).as_deref(), Some("eins äö"));
|
||||
fs::remove_dir_all(&d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prozent_dekodierung_von_pfaden() {
|
||||
assert_eq!(percent_decode("/tmp/Pl%C3%A4ne/a%20b.pdf"), "/tmp/Pläne/a b.pdf");
|
||||
assert_eq!(percent_decode("/x/100%"), "/x/100%");
|
||||
assert_eq!(percent_decode("/x/%zz"), "/x/%zz");
|
||||
assert_eq!(percent_decode("C:%5Cx"), "C:\\x");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn neue_datei_ohne_sicherung() {
|
||||
let d = tmpdir("neu");
|
||||
|
||||
Reference in new issue
Block a user