DB backup: drop data_dir writes, use native_root only (match gps_service_read_points pattern)

This commit is contained in:
4aiman 2026-09-21 13:36:17 +03:00
commit 9e0910a35f

View file

@ -639,8 +639,7 @@ fn db_and_root(app: &tauri::AppHandle) -> Result<(std::path::PathBuf, std::path:
// SAF-save it as `filename`. VACUUM INTO is consistent even while the app holds the DB open.
#[tauri::command]
fn export_db(app: tauri::AppHandle, filename: String) -> Result<String, String> {
let data_dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
let (db_path, root) = db_and_root(&app)?;
let (db_path, root) = db_and_root(&app)?; // root = native_root: the writable app dir Kotlin watches
if !db_path.exists() { return Err("baldav.db не найдена".into()); }
let out = root.join("export_db.tmp");
let _ = std::fs::remove_file(&out);
@ -649,9 +648,7 @@ fn export_db(app: tauri::AppHandle, filename: String) -> Result<String, String>
conn.execute("VACUUM INTO ?1", rusqlite::params![out.to_str().unwrap_or("")]).map_err(|e| format!("vacuum: {e}"))?;
}
let size = std::fs::metadata(&out).map(|m| m.len()).unwrap_or(0);
let cmd = format!("exportdb:{}", filename);
let _ = std::fs::write(data_dir.join("gps_command.txt"), &cmd);
let _ = std::fs::write(root.join("gps_command.txt"), &cmd);
let _ = std::fs::write(root.join("gps_command.txt"), format!("exportdb:{}", filename));
Ok(format!("{{\"size\":{}}}", size))
}
@ -660,10 +657,8 @@ fn export_db(app: tauri::AppHandle, filename: String) -> Result<String, String>
// caller then restarts the app.
#[tauri::command]
fn import_db(app: tauri::AppHandle) -> Result<String, String> {
let (_db, root) = db_and_root(&app)?;
let data_dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
let picked = if root.join("picked_import.db").exists() { root.join("picked_import.db") }
else { data_dir.join("picked_import.db") };
let (_db, root) = db_and_root(&app)?; // root = native_root: Kotlin wrote picked_import.db here
let picked = root.join("picked_import.db");
if !picked.exists() { return Err("файл не выбран".into()); }
{
let conn = rusqlite::Connection::open(&picked).map_err(|e| format!("не открыть файл: {e}"))?;
@ -683,9 +678,7 @@ fn import_db(app: tauri::AppHandle) -> Result<String, String> {
// Ask Kotlin to restart the app process (used after import_db to load the restored DB).
#[tauri::command]
fn restart_app(app: tauri::AppHandle) -> Result<String, String> {
let data_dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
let (_db, root) = db_and_root(&app)?;
let _ = std::fs::write(data_dir.join("gps_command.txt"), "restartapp");
let (_db, root) = db_and_root(&app)?; // root = native_root, the dir Kotlin's FileObserver watches
let _ = std::fs::write(root.join("gps_command.txt"), "restartapp");
Ok("ok".into())
}