diff --git a/src/context.rs b/src/context.rs index 942a525f6..aee416167 100644 --- a/src/context.rs +++ b/src/context.rs @@ -21,9 +21,11 @@ use crate::types::*; use crate::x::*; use std::ptr; +use std::path::PathBuf; + pub struct Context { pub userdata: *mut libc::c_void, - pub dbfile: Arc>, + pub dbfile: Arc>>, pub blobdir: Arc>, pub sql: Sql, pub inbox: Arc>, @@ -55,15 +57,17 @@ pub struct RunningState { impl Context { pub fn has_dbfile(&self) -> bool { - !self.get_dbfile().is_null() + self.get_dbfile().is_some() } pub fn has_blobdir(&self) -> bool { !self.get_blobdir().is_null() } - pub fn get_dbfile(&self) -> *const libc::c_char { - *self.dbfile.clone().read().unwrap() + pub fn get_dbfile(&self) -> Option { + (*self.dbfile.clone().read().unwrap()) + .as_ref() + .map(|x| x.clone()) } pub fn get_blobdir(&self) -> *const libc::c_char { @@ -130,7 +134,7 @@ pub fn dc_context_new( ) -> Context { Context { blobdir: Arc::new(RwLock::new(std::ptr::null_mut())), - dbfile: Arc::new(RwLock::new(std::ptr::null_mut())), + dbfile: Arc::new(RwLock::new(None)), inbox: Arc::new(RwLock::new({ Imap::new( cb_get_config, @@ -282,8 +286,7 @@ pub unsafe fn dc_close(context: &Context) { context.sql.close(context); let mut dbfile = context.dbfile.write().unwrap(); - free(*dbfile as *mut libc::c_void); - *dbfile = ptr::null_mut(); + *dbfile = None; let mut blobdir = context.blobdir.write().unwrap(); free(*blobdir as *mut libc::c_void); *blobdir = ptr::null_mut(); @@ -302,7 +305,7 @@ pub unsafe fn dc_open(context: &Context, dbfile: &str, blobdir: Option<&str>) -> if 0 != dc_is_open(context) { return false; } - *context.dbfile.write().unwrap() = dbfile.strdup(); + *context.dbfile.write().unwrap() = Some(PathBuf::from(dbfile)); if blobdir.is_some() && !blobdir.unwrap().is_empty() { let dir = dc_ensure_no_slash_safe(blobdir.unwrap()).strdup(); *context.blobdir.write().unwrap() = dir; @@ -446,11 +449,10 @@ pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char { real_msgs, deaddrop_msgs, contacts, - if context.has_dbfile() { - as_str(context.get_dbfile()) - } else { - unset - }, + context + .get_dbfile() + .as_ref() + .map_or(unset, |p| p.to_str().unwrap()), dbversion, if context.has_blobdir() { as_str(context.get_blobdir()) diff --git a/src/dc_imex.rs b/src/dc_imex.rs index 02f6d4d2a..4f8d80c3d 100644 --- a/src/dc_imex.rs +++ b/src/dc_imex.rs @@ -597,7 +597,10 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char 0, "Import \"{}\" to \"{}\".", as_str(backup_to_import), - as_str(context.get_dbfile()), + context + .get_dbfile() + .as_ref() + .map_or("<>", |p| p.to_str().unwrap()) ); if 0 != dc_is_configured(context) { @@ -605,8 +608,8 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char return 0; } &context.sql.close(&context); - dc_delete_file(context, as_path(context.get_dbfile())); - if dc_file_exist(context, as_path(context.get_dbfile())) { + dc_delete_file(context, context.get_dbfile().unwrap()); + if dc_file_exist(context, context.get_dbfile().unwrap()) { error!( context, 0, "Cannot import backups: Cannot delete the old file.", @@ -617,13 +620,16 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char if !dc_copy_file( context, as_path(backup_to_import), - as_path(context.get_dbfile()), + context.get_dbfile().unwrap(), ) { return 0; } /* error already logged */ /* re-open copied database file */ - if !context.sql.open(&context, as_path(context.get_dbfile()), 0) { + if !context + .sql + .open(&context, &context.get_dbfile().unwrap(), 0) + { return 0; } @@ -742,15 +748,20 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_ context, 0, "Backup \"{}\" to \"{}\".", - as_str(context.get_dbfile()), + context + .get_dbfile() + .as_ref() + .map_or("<>", |p| p.to_str().unwrap()), as_str(dest_pathNfilename), ); if dc_copy_file( context, - as_path(context.get_dbfile()), + context.get_dbfile().unwrap(), as_path(dest_pathNfilename), ) { - context.sql.open(&context, as_path(context.get_dbfile()), 0); + context + .sql + .open(&context, &context.get_dbfile().unwrap(), 0); closed = false; /* add all files as blobs to the database copy (this does not require the source to be locked, neigher the destination as it is used only here) */ /*for logging only*/ @@ -906,7 +917,9 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_ } } if closed { - context.sql.open(&context, as_path(context.get_dbfile()), 0); + context + .sql + .open(&context, &context.get_dbfile().unwrap(), 0); } if 0 != delete_dest_file { dc_delete_file(context, as_path(dest_pathNfilename));