From baeb790855daafbb0155637342a122002034c9f5 Mon Sep 17 00:00:00 2001 From: Mattia Procopio Date: Fri, 8 Mar 2024 10:22:44 +0100 Subject: [PATCH] Check mycity DB exists before attempting a backup --- src/backup/database.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/backup/database.rs b/src/backup/database.rs index de48f8c..c5c85e6 100644 --- a/src/backup/database.rs +++ b/src/backup/database.rs @@ -3,8 +3,13 @@ use log::info; use minreq; use std::fs::File; use std::io::{Read, Write}; +use std::path::Path; use tar::{Archive, Builder}; +fn check_city_db_exists(paths: &Paths) -> bool { + Path::new(&paths.city_db_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") { @@ -31,15 +36,17 @@ pub fn send_db(paths: &Paths, token: &String) -> Result<(), String> { Err(e) => panic!("Couldn't append the database to the archive, reason: {}", e), } - // Add city database to the archive - let mut city_db = match File::open(&paths.city_db_path) { - Ok(f) => f, - Err(e) => panic!("Couldn't open the Kstars city database, reason: {}", e), - }; + if check_city_db_exists(paths) { + // Add city database to the archive + let mut city_db = match File::open(&paths.city_db_path) { + Ok(f) => f, + Err(e) => panic!("Couldn't open the Kstars city database, reason: {}", e), + }; - match arch.append_file("backup/kstars/mycitydb.sqlite", &mut city_db) { - Ok(_) => (), - Err(e) => panic!("Couldn't append the database to the archive, reason: {}", e), + match arch.append_file("backup/kstars/mycitydb.sqlite", &mut city_db) { + Ok(_) => (), + Err(e) => panic!("Couldn't append the database to the archive, reason: {}", e), + } } // Add fov.dat to the archive