remove/push down some unsafe-fn

This commit is contained in:
holger krekel
2019-10-08 15:28:19 +02:00
parent c23e98ff83
commit 1df6229e99
4 changed files with 80 additions and 83 deletions

View File

@@ -35,93 +35,90 @@ fn cb(_ctx: &Context, event: Event) -> usize {
} }
fn main() { fn main() {
unsafe { let dir = tempdir().unwrap();
let dir = tempdir().unwrap(); let dbfile = dir.path().join("db.sqlite");
let dbfile = dir.path().join("db.sqlite"); println!("creating database {:?}", dbfile);
println!("creating database {:?}", dbfile); let ctx =
let ctx = Context::new(Box::new(cb), "FakeOs".into(), dbfile).expect("Failed to create context");
Context::new(Box::new(cb), "FakeOs".into(), dbfile).expect("Failed to create context"); let running = Arc::new(RwLock::new(true));
let running = Arc::new(RwLock::new(true)); let info = ctx.get_info();
let info = ctx.get_info(); let duration = time::Duration::from_millis(4000);
let duration = time::Duration::from_millis(4000); println!("info: {:#?}", info);
println!("info: {:#?}", info);
let ctx = Arc::new(ctx);
let ctx1 = ctx.clone();
let r1 = running.clone();
let t1 = thread::spawn(move || {
while *r1.read().unwrap() {
perform_imap_jobs(&ctx1);
if *r1.read().unwrap() {
perform_imap_fetch(&ctx1);
let ctx = Arc::new(ctx);
let ctx1 = ctx.clone();
let r1 = running.clone();
let t1 = thread::spawn(move || {
while *r1.read().unwrap() {
perform_imap_jobs(&ctx1);
if *r1.read().unwrap() { if *r1.read().unwrap() {
perform_imap_fetch(&ctx1); perform_imap_idle(&ctx1);
if *r1.read().unwrap() {
perform_imap_idle(&ctx1);
}
} }
} }
});
let ctx1 = ctx.clone();
let r1 = running.clone();
let t2 = thread::spawn(move || {
while *r1.read().unwrap() {
perform_smtp_jobs(&ctx1);
if *r1.read().unwrap() {
perform_smtp_idle(&ctx1);
}
}
});
println!("configuring");
let args = std::env::args().collect::<Vec<String>>();
assert_eq!(args.len(), 2, "missing password");
let pw = args[1].clone();
ctx.set_config(config::Config::Addr, Some("d@testrun.org"))
.unwrap();
ctx.set_config(config::Config::MailPw, Some(&pw)).unwrap();
configure(&ctx);
thread::sleep(duration);
println!("sending a message");
let contact_id =
Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com").unwrap();
let chat_id = chat::create_by_contact_id(&ctx, contact_id).unwrap();
chat::send_text_msg(&ctx, chat_id, "Hi, here is my first message!".into()).unwrap();
println!("fetching chats..");
let chats = Chatlist::try_load(&ctx, 0, None, None).unwrap();
for i in 0..chats.len() {
let summary = chats.get_summary(&ctx, 0, None);
let text1 = summary.get_text1();
let text2 = summary.get_text2();
println!("chat: {} - {:?} - {:?}", i, text1, text2,);
} }
});
thread::sleep(duration); let ctx1 = ctx.clone();
let r1 = running.clone();
let t2 = thread::spawn(move || {
while *r1.read().unwrap() {
perform_smtp_jobs(&ctx1);
if *r1.read().unwrap() {
perform_smtp_idle(&ctx1);
}
}
});
// let msglist = dc_get_chat_msgs(&ctx, chat_id, 0, 0); println!("configuring");
// for i in 0..dc_array_get_cnt(msglist) { let args = std::env::args().collect::<Vec<String>>();
// let msg_id = dc_array_get_id(msglist, i); assert_eq!(args.len(), 2, "missing password");
// let msg = dc_get_msg(context, msg_id); let pw = args[1].clone();
// let text = CStr::from_ptr(dc_msg_get_text(msg)).unwrap(); ctx.set_config(config::Config::Addr, Some("d@testrun.org"))
// println!("Message {}: {}\n", i + 1, text.to_str().unwrap()); .unwrap();
// dc_msg_unref(msg); ctx.set_config(config::Config::MailPw, Some(&pw)).unwrap();
// } configure(&ctx);
// dc_array_unref(msglist);
println!("stopping threads"); thread::sleep(duration);
*running.clone().write().unwrap() = false; println!("sending a message");
deltachat::job::interrupt_imap_idle(&ctx); let contact_id = Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com").unwrap();
deltachat::job::interrupt_smtp_idle(&ctx); let chat_id = chat::create_by_contact_id(&ctx, contact_id).unwrap();
chat::send_text_msg(&ctx, chat_id, "Hi, here is my first message!".into()).unwrap();
println!("joining"); println!("fetching chats..");
t1.join().unwrap(); let chats = Chatlist::try_load(&ctx, 0, None, None).unwrap();
t2.join().unwrap();
println!("closing"); for i in 0..chats.len() {
let summary = chats.get_summary(&ctx, 0, None);
let text1 = summary.get_text1();
let text2 = summary.get_text2();
println!("chat: {} - {:?} - {:?}", i, text1, text2,);
} }
thread::sleep(duration);
// let msglist = dc_get_chat_msgs(&ctx, chat_id, 0, 0);
// for i in 0..dc_array_get_cnt(msglist) {
// let msg_id = dc_array_get_id(msglist, i);
// let msg = dc_get_msg(context, msg_id);
// let text = CStr::from_ptr(dc_msg_get_text(msg)).unwrap();
// println!("Message {}: {}\n", i + 1, text.to_str().unwrap());
// dc_msg_unref(msg);
// }
// dc_array_unref(msglist);
println!("stopping threads");
*running.clone().write().unwrap() = false;
deltachat::job::interrupt_imap_idle(&ctx);
deltachat::job::interrupt_smtp_idle(&ctx);
println!("joining");
t1.join().unwrap();
t2.join().unwrap();
println!("closing");
} }

View File

@@ -27,7 +27,7 @@ macro_rules! progress {
} }
// connect // connect
pub unsafe fn configure(context: &Context) { pub fn configure(context: &Context) {
if context.has_ongoing() { if context.has_ongoing() {
warn!(context, "There is already another ongoing process running.",); warn!(context, "There is already another ongoing process running.",);
return; return;

View File

@@ -263,7 +263,7 @@ pub fn try_decrypt(
in_out_message: *mut Mailmime, in_out_message: *mut Mailmime,
) -> Result<(bool, HashSet<String>, HashSet<String>)> { ) -> Result<(bool, HashSet<String>, HashSet<String>)> {
// just a pointer into mailmime structure, must not be freed // just a pointer into mailmime structure, must not be freed
let imffields = unsafe { mailmime_find_mailimf_fields(in_out_message) }; let imffields = mailmime_find_mailimf_fields(in_out_message);
ensure!( ensure!(
!in_out_message.is_null() && !imffields.is_null(), !in_out_message.is_null() && !imffields.is_null(),
"corrupt invalid mime inputs" "corrupt invalid mime inputs"

View File

@@ -175,21 +175,21 @@ pub fn mailimf_find_field(
} }
/*the result is a pointer to mime, must not be freed*/ /*the result is a pointer to mime, must not be freed*/
pub unsafe fn mailmime_find_mailimf_fields(mime: *mut Mailmime) -> *mut mailimf_fields { pub fn mailmime_find_mailimf_fields(mime: *mut Mailmime) -> *mut mailimf_fields {
if mime.is_null() { if mime.is_null() {
return ptr::null_mut(); return ptr::null_mut();
} }
match (*mime).mm_type as _ { match unsafe { (*mime).mm_type as _ } {
MAILMIME_MULTIPLE => { MAILMIME_MULTIPLE => {
for cur_data in (*(*mime).mm_data.mm_multipart.mm_mp_list).into_iter() { for cur_data in unsafe { (*(*mime).mm_data.mm_multipart.mm_mp_list).into_iter() } {
let header = mailmime_find_mailimf_fields(cur_data as *mut _); let header = mailmime_find_mailimf_fields(cur_data as *mut _);
if !header.is_null() { if !header.is_null() {
return header; return header;
} }
} }
} }
MAILMIME_MESSAGE => return (*mime).mm_data.mm_message.mm_fields, MAILMIME_MESSAGE => return unsafe { (*mime).mm_data.mm_message.mm_fields },
_ => {} _ => {}
} }