From de63975fbe66607f8a1ad0ac8332a6bca15d3a01 Mon Sep 17 00:00:00 2001 From: "C. Morgan Hamill" Date: Fri, 20 Mar 2015 11:28:47 -0400 Subject: Add `Database::compact()` and related methods. Not so simple wrapper around notmuch API's `notmuch_database_compact()` function. Variants with and without a callback parameter are provided. --- src/database.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src') 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( + path: &P, backup_path: Option<&P>, + ) -> Result<()> { + let status: Option = None; + Database::_compact(path, backup_path, status) + } + + pub fn compact_with_status( + path: &P, backup_path: Option<&P>, status: F, + ) -> Result<()> { + Database::_compact(path, backup_path, Some(status)) + } + + fn _compact( + path: &P, backup_path: Option<&P>, status: Option, + ) -> Result<()> { + + extern fn wrapper( + 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::) } else { None }, + status.map_or(ptr::null_mut(), |f| { + &f as *const _ as *mut libc::c_void + }), + ) + }.as_result()); + + Ok(()) + } + } -- cgit v1.2.1