diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/database.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/database.rs b/src/database.rs index 5a7cec4..7364942 100644 --- a/src/database.rs +++ b/src/database.rs @@ -9,6 +9,7 @@ use error::Result; use utils::{ NotmuchEnum, ToCString, + ToStr, }; use ffi; @@ -46,4 +47,48 @@ impl Database { Ok(Database(db)) } + pub fn compact<P: path::AsPath, F: FnMut(&str)>( + path: &P, backup_path: Option<&P>, + ) -> Result<()> { + let status: Option<F> = None; + Database::_compact(path, backup_path, status) + } + + pub fn compact_with_status<P: path::AsPath, F: FnMut(&str)>( + path: &P, backup_path: Option<&P>, status: F, + ) -> Result<()> { + Database::_compact(path, backup_path, Some(status)) + } + + fn _compact<P: path::AsPath, F: FnMut(&str)>( + path: &P, backup_path: Option<&P>, status: Option<F>, + ) -> Result<()> { + + extern fn wrapper<F: FnMut(&str)>( + message:*const libc::c_char, closure: *mut libc::c_void, + ) { + let closure = closure as *mut F; + unsafe { + (*closure)(message.to_str().unwrap()) + } + } + + let path = path.as_path().to_cstring().unwrap(); + let backup_path = backup_path.map(|p| { + p.as_path().to_cstring().unwrap() + }); + + try!(unsafe { + ffi::notmuch_database_compact( + path.as_ptr(), backup_path.map_or(ptr::null(), |p| p.as_ptr()), + if status.is_some() { Some(wrapper::<F>) } else { None }, + status.map_or(ptr::null_mut(), |f| { + &f as *const _ as *mut libc::c_void + }), + ) + }.as_result()); + + Ok(()) + } + } |
