From 0706242c69201f63c098886d1e8d9f30b4cf3ea8 Mon Sep 17 00:00:00 2001 From: Mattia Procopio Date: Fri, 8 Mar 2024 10:39:18 +0100 Subject: [PATCH] Backup PHD2 profile if available --- src/backup/database.rs | 36 ++++++++++++++++++++++++++++++++---- src/lib.rs | 13 +++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/backup/database.rs b/src/backup/database.rs index c5c85e6..5d2080a 100644 --- a/src/backup/database.rs +++ b/src/backup/database.rs @@ -10,6 +10,14 @@ fn check_city_db_exists(paths: &Paths) -> bool { Path::new(&paths.city_db_path).exists() } +fn check_indi_folder_exists(paths: &Paths) -> bool { + Path::new(&paths.indi_conf_path).exists() +} + +fn check_phd2_profile_exists(paths: &Paths) -> bool { + Path::new(&paths.phd2_profile_path).exists() +} + pub fn send_db(paths: &Paths, token: &String) -> Result<(), String> { // Init the zip archive let file = match File::create("/tmp/k_backup.tar") { @@ -19,10 +27,12 @@ pub fn send_db(paths: &Paths, token: &String) -> Result<(), String> { let mut arch = Builder::new(file); - // Add all indi devices xml configs to the archive - match arch.append_dir_all("backup/indi", &paths.indi_conf_path) { - Ok(_) => (), - Err(e) => panic!("Couldn't append indi folder to the archive, reason: {}", e), + if check_indi_folder_exists(paths) { + // Add all indi devices xml configs to the archive + match arch.append_dir_all("backup/indi", &paths.indi_conf_path) { + Ok(_) => (), + Err(e) => panic!("Couldn't append indi folder to the archive, reason: {}", e), + } } // Add kstars database to the archive @@ -49,6 +59,22 @@ pub fn send_db(paths: &Paths, token: &String) -> Result<(), String> { } } + if check_phd2_profile_exists(paths) { + // Add PHD2 profile to the archive + let mut phd2 = match File::open(&paths.phd2_profile_path) { + Ok(f) => f, + Err(e) => panic!("Couldn't open the PHD2 profile, reason: {}", e), + }; + + match arch.append_file(format!("backup/{}", paths.phd2_filename), &mut phd2) { + Ok(_) => (), + Err(e) => panic!( + "Couldn't append the PHD2 profile to the archive, reason: {}", + e + ), + } + } + // Add fov.dat to the archive let mut fov_db = match File::open(&paths.fov_path) { Ok(f) => f, @@ -142,6 +168,8 @@ pub fn retrieve_db(paths: &Paths, token: &String) -> Result<(), String> { full_path = paths.fov_path.to_owned(); } else if path.to_str().unwrap().contains(&"kstarsrc") { full_path = paths.kstars_rc_path.to_owned(); + } else if path.to_str().unwrap().contains(&"PHD") { + full_path = paths.phd2_profile_path.to_owned(); } else { full_path = paths.db_path.to_owned(); }; diff --git a/src/lib.rs b/src/lib.rs index 8e675e7..f70bb41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,8 @@ pub struct Paths { pub indi_conf_path: String, pub fov_path: String, pub kstars_rc_path: String, + pub phd2_profile_path: String, + pub phd2_filename: String, } impl Paths { @@ -50,6 +52,12 @@ impl Paths { .to_string(); let data_path = dirs::data_dir().unwrap().as_path().display().to_string(); + #[cfg(target_os = "linux")] + let phd2_filename = String::from(".PHDGuidingV2"); + + #[cfg(target_os = "macos")] + let phd2_filename = String::from("PHDGuidingV2_Preferences"); + Self { folder_path: String::from(".local/share/astromonitor"), logs_path: String::from("logs"), @@ -60,6 +68,11 @@ impl Paths { fov_path: format!("{}/kstars/fov.dat", &data_path), indi_conf_path: format!("{}/.indi/", &home_path), kstars_rc_path: format!("{}/kstarsrc", &config_path), + phd2_filename: phd2_filename.clone(), + #[cfg(target_os = "linux")] + phd2_profile_path: format!("{}/{}", &home_path, &phd2_filename), + #[cfg(target_os = "macos")] + phd2_profile_path: format!("{}/{}", &config_path, &phd2_filename), } } }