diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index c8a3ce716..36aab586a 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -1727,13 +1727,15 @@ pub unsafe extern "C" fn dc_imex( let ctx = &*context; - let param1 = to_opt_string_lossy(param1); - - spawn(async move { - imex::imex(&ctx, what, param1) - .await - .log_err(ctx, "IMEX failed") - }); + if let Some(param1) = to_opt_string_lossy(param1) { + spawn(async move { + imex::imex(&ctx, what, ¶m1) + .await + .log_err(ctx, "IMEX failed") + }); + } else { + eprintln!("dc_imex called without a valid directory"); + } } #[no_mangle] diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index c89126957..7d416d976 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -443,20 +443,20 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu } "export-backup" => { let dir = dirs::home_dir().unwrap_or_default(); - imex(&context, ImexMode::ExportBackup, Some(&dir)).await?; + imex(&context, ImexMode::ExportBackup, &dir).await?; println!("Exported to {}.", dir.to_string_lossy()); } "import-backup" => { ensure!(!arg1.is_empty(), "Argument missing."); - imex(&context, ImexMode::ImportBackup, Some(arg1)).await?; + imex(&context, ImexMode::ImportBackup, arg1).await?; } "export-keys" => { let dir = dirs::home_dir().unwrap_or_default(); - imex(&context, ImexMode::ExportSelfKeys, Some(&dir)).await?; + imex(&context, ImexMode::ExportSelfKeys, &dir).await?; println!("Exported to {}.", dir.to_string_lossy()); } "import-keys" => { - imex(&context, ImexMode::ImportSelfKeys, Some(arg1)).await?; + imex(&context, ImexMode::ImportSelfKeys, arg1).await?; } "export-setup" => { let setup_code = create_setup_code(&context); diff --git a/src/accounts.rs b/src/accounts.rs index 1c4ba734e..5426525b6 100644 --- a/src/accounts.rs +++ b/src/accounts.rs @@ -188,7 +188,7 @@ impl Accounts { let id = self.add_account().await?; let ctx = self.get_account(id).await.expect("just added"); - match crate::imex::imex(&ctx, crate::imex::ImexMode::ImportBackup, Some(file)).await { + match crate::imex::imex(&ctx, crate::imex::ImexMode::ImportBackup, &file).await { Ok(_) => Ok(id), Err(err) => { // remove temp account diff --git a/src/imex.rs b/src/imex.rs index 9f78efd3e..ba3bde24e 100644 --- a/src/imex.rs +++ b/src/imex.rs @@ -79,11 +79,7 @@ pub enum ImexMode { /// /// Only one import-/export-progress can run at the same time. /// To cancel an import-/export-progress, drop the future returned by this function. -pub async fn imex( - context: &Context, - what: ImexMode, - param1: Option>, -) -> Result<()> { +pub async fn imex(context: &Context, what: ImexMode, param1: impl AsRef) -> Result<()> { let cancel = context.alloc_ongoing().await?; let res = async { @@ -442,20 +438,10 @@ pub fn normalize_setup_code(s: &str) -> String { out } -async fn imex_inner( - context: &Context, - what: ImexMode, - param: Option>, -) -> Result<()> { - let path = if let Some(ref p) = param { - info!(context, "Import/export dir: {}", p.as_ref().display()); - p - } else { - return Err(format_err!("Imex: Param was None")); - }; - - context.emit_event(EventType::ImexProgress(10)); +async fn imex_inner(context: &Context, what: ImexMode, path: impl AsRef) -> Result<()> { + info!(context, "Import/export dir: {}", path.as_ref().display()); ensure!(context.sql.is_open().await, "Database not opened."); + context.emit_event(EventType::ImexProgress(10)); if what == ImexMode::ExportBackup || what == ImexMode::ExportSelfKeys { // before we export anything, make sure the private key exists @@ -1089,16 +1075,16 @@ mod tests { async fn test_export_and_import_key() { let context = TestContext::new().await; context.configure_alice().await; - let blobdir = "$BLOBDIR"; - assert!(imex(&context.ctx, ImexMode::ExportSelfKeys, Some(blobdir)) - .await - .is_ok()); + let blobdir = context.ctx.get_blobdir().to_str().unwrap(); + if let Err(err) = imex(&context.ctx, ImexMode::ExportSelfKeys, blobdir).await { + panic!("got error on export: {:?}", err); + } let context2 = TestContext::new().await; context2.configure_alice().await; - assert!(imex(&context2.ctx, ImexMode::ImportSelfKeys, Some(blobdir)) - .await - .is_ok()); + if let Err(err) = imex(&context2.ctx, ImexMode::ImportSelfKeys, blobdir).await { + panic!("got error on import: {:?}", err); + } } #[test]