diff --git a/src/context.rs b/src/context.rs index 04133679b..e66931fc2 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,5 +1,5 @@ use std::ffi::OsString; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::ptr; use std::sync::{Arc, Condvar, Mutex, RwLock}; @@ -38,8 +38,8 @@ use crate::x::*; pub type ContextCallback = dyn Fn(&Context, Event, uintptr_t, uintptr_t) -> uintptr_t + Send + Sync; pub struct Context { - pub dbfile: Arc>, - pub blobdir: Arc>, + pub(crate) dbfile: PathBuf, + pub(crate) blobdir: PathBuf, pub sql: Sql, pub inbox: Arc>, pub perform_inbox_jobs_needed: Arc>, @@ -89,8 +89,8 @@ impl Context { blobdir.display() ); let ctx = Context { - blobdir: Arc::new(RwLock::new(blobdir)), - dbfile: Arc::new(RwLock::new(dbfile)), + blobdir, + dbfile, inbox: Arc::new(RwLock::new({ Imap::new( cb_get_config, @@ -133,18 +133,21 @@ impl Context { perform_inbox_jobs_needed: Arc::new(RwLock::new(false)), generating_key_mutex: Mutex::new(()), }; - if !ctx.sql.open(&ctx, &ctx.dbfile.read().unwrap(), 0) { - return Err(format_err!("Failed opening sqlite database")); - } + + ensure!( + ctx.sql.open(&ctx, &ctx.dbfile, 0), + "Failed opening sqlite database" + ); + Ok(ctx) } - pub fn get_dbfile(&self) -> PathBuf { - self.dbfile.clone().read().unwrap().clone() + pub fn get_dbfile(&self) -> &Path { + self.dbfile.as_path() } - pub fn get_blobdir(&self) -> PathBuf { - self.blobdir.clone().read().unwrap().clone() + pub fn get_blobdir(&self) -> &Path { + self.blobdir.as_path() } pub fn call_cb(&self, event: Event, data1: uintptr_t, data2: uintptr_t) -> uintptr_t { diff --git a/tests/stress.rs b/tests/stress.rs index e3b2e84db..2b7ee1b13 100644 --- a/tests/stress.rs +++ b/tests/stress.rs @@ -53,7 +53,11 @@ unsafe fn stress_functions(context: &Context) { 7i32 as libc::c_ulonglong ); - let abs_path = format!("{}/foobar", context.get_blobdir().to_string_lossy()); + let abs_path = context + .get_blobdir() + .join("foobar") + .to_string_lossy() + .to_string(); assert!(dc_is_blobdir_path(context, &abs_path)); assert!(dc_is_blobdir_path(context, "$BLOBDIR/fofo",));