fix segfaults and some queries

This commit is contained in:
dignifiedquire
2019-07-10 22:41:20 +02:00
parent a0acfca255
commit 45f7eba1f4
7 changed files with 604 additions and 795 deletions

View File

@@ -79,10 +79,12 @@ fn main() {
println!("opening database {:?}", dbfile); println!("opening database {:?}", dbfile);
dc_open(&ctx, dbfile.as_ptr(), std::ptr::null()); assert_eq!(dc_open(&ctx, dbfile.as_ptr(), std::ptr::null()), 1);
println!("configuring"); println!("configuring");
let pw = std::env::args().collect::<Vec<String>>()[1].clone(); let args = std::env::args().collect::<Vec<String>>();
assert_eq!(args.len(), 2, "missing password");
let pw = args[1].clone();
dc_set_config(&ctx, "addr", Some("d@testrun.org")); dc_set_config(&ctx, "addr", Some("d@testrun.org"));
dc_set_config(&ctx, "mail_pw", Some(&pw)); dc_set_config(&ctx, "mail_pw", Some(&pw));
dc_configure(&ctx); dc_configure(&ctx);

View File

@@ -324,7 +324,7 @@ pub fn dc_lookup_real_nchat_by_contact_id(
unsafe { *ret_chat_blocked = row.get(1)? }; unsafe { *ret_chat_blocked = row.get(1)? };
Ok(()) Ok(())
} }
).unwrap(); );
} }
pub unsafe fn dc_get_chat_id_by_contact_id(context: &Context, contact_id: uint32_t) -> uint32_t { pub unsafe fn dc_get_chat_id_by_contact_id(context: &Context, contact_id: uint32_t) -> uint32_t {

View File

@@ -49,23 +49,16 @@ pub struct dc_job_t {
} }
pub unsafe fn dc_perform_imap_jobs(context: &Context) { pub unsafe fn dc_perform_imap_jobs(context: &Context) {
dc_log_info( info!(context, 0, "INBOX-jobs started...",);
context,
0i32,
b"INBOX-jobs started...\x00" as *const u8 as *const libc::c_char,
);
let probe_imap_network = *context.probe_imap_network.clone().read().unwrap(); let probe_imap_network = *context.probe_imap_network.clone().read().unwrap();
*context.probe_imap_network.write().unwrap() = 0; *context.probe_imap_network.write().unwrap() = 0;
*context.perform_inbox_jobs_needed.write().unwrap() = 0; *context.perform_inbox_jobs_needed.write().unwrap() = 0;
dc_job_perform(context, 100, probe_imap_network); dc_job_perform(context, 100, probe_imap_network);
dc_log_info( info!(context, 0, "INBOX-jobs ended.",);
context,
0i32,
b"INBOX-jobs ended.\x00" as *const u8 as *const libc::c_char,
);
} }
unsafe fn dc_job_perform(context: &Context, thread: libc::c_int, probe_network: libc::c_int) { unsafe fn dc_job_perform(context: &Context, thread: libc::c_int, probe_network: libc::c_int) {
let process_row = |row: &rusqlite::Row| { let process_row = |row: &rusqlite::Row| {
let job = dc_job_t { let job = dc_job_t {
@@ -101,103 +94,97 @@ unsafe fn dc_job_perform(context: &Context, thread: libc::c_int, probe_network:
params_probe params_probe
}; };
context context.sql.query_map(query, params, process_row, |jobs| {
.sql for job in jobs {
.query_map(query, params, process_row, |jobs| { let mut job: dc_job_t = job?;
for job in jobs { info!(
let mut job: dc_job_t = job?; context,
0,
"{}-job #{}, action {} started...",
if thread == 100 { "INBOX" } else { "SMTP" },
job.job_id,
job.action,
);
if 900 == job.action || 910 == job.action {
dc_job_kill_action(context, job.action);
dc_jobthread_suspend(context, &context.sentbox_thread.clone().read().unwrap(), 1);
dc_jobthread_suspend(context, &context.mvbox_thread.clone().read().unwrap(), 1);
dc_suspend_smtp_thread(context, 1);
}
let mut tries = 0;
while tries <= 1 {
job.try_again = 0;
match job.action {
5901 => {
dc_job_do_DC_JOB_SEND(context, &mut job);
}
110 => {
dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(context, &mut job);
}
130 => {
dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP(context, &mut job);
}
120 => {
dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP(context, &mut job);
}
200 => {
dc_job_do_DC_JOB_MOVE_MSG(context, &mut job);
}
5011 => {
dc_job_do_DC_JOB_SEND(context, &mut job);
}
900 => {
dc_job_do_DC_JOB_CONFIGURE_IMAP(context, &mut job);
}
910 => {
dc_job_do_DC_JOB_IMEX_IMAP(context, &mut job);
}
5005 => {
dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context, &mut job);
}
5007 => {
dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED(context, &mut job);
}
105 => {
dc_housekeeping(context);
}
_ => {}
}
if job.try_again != -1 {
break;
}
tries += 1
}
if 900 == job.action || 910 == job.action {
dc_jobthread_suspend(
context,
&mut context.sentbox_thread.clone().read().unwrap(),
0,
);
dc_jobthread_suspend(
context,
&mut context.mvbox_thread.clone().read().unwrap(),
0,
);
dc_suspend_smtp_thread(context, 0);
break;
} else if job.try_again == 2 {
info!( info!(
context, context,
0, 0,
"{}-job #{}, action {} started...", "{}-job #{} not yet ready and will be delayed.",
if thread == 100 { "INBOX" } else { "SMTP" }, if thread == 100 { "INBOX" } else { "SMTP" },
job.job_id, job.job_id
job.action,
); );
} else if job.try_again == -1 || job.try_again == 3 {
if 900 == job.action || 910 == job.action { let tries = job.tries + 1;
dc_job_kill_action(context, job.action); if tries < 17 {
dc_jobthread_suspend( job.tries = tries;
context, let time_offset = get_backoff_time_offset(tries);
&context.sentbox_thread.clone().read().unwrap(), job.desired_timestamp = job.added_timestamp + time_offset;
1, dc_job_update(context, &mut job);
);
dc_jobthread_suspend(context, &context.mvbox_thread.clone().read().unwrap(), 1);
dc_suspend_smtp_thread(context, 1);
}
let mut tries = 0;
while tries <= 1 {
job.try_again = 0;
match job.action {
5901 => {
dc_job_do_DC_JOB_SEND(context, &mut job);
}
110 => {
dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(context, &mut job);
}
130 => {
dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP(context, &mut job);
}
120 => {
dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP(context, &mut job);
}
200 => {
dc_job_do_DC_JOB_MOVE_MSG(context, &mut job);
}
5011 => {
dc_job_do_DC_JOB_SEND(context, &mut job);
}
900 => {
dc_job_do_DC_JOB_CONFIGURE_IMAP(context, &mut job);
}
910 => {
dc_job_do_DC_JOB_IMEX_IMAP(context, &mut job);
}
5005 => {
dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context, &mut job);
}
5007 => {
dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED(context, &mut job);
}
105 => {
dc_housekeeping(context);
}
_ => {}
}
if job.try_again != -1 {
break;
}
tries += 1
}
if 900 == job.action || 910 == job.action {
dc_jobthread_suspend(
context,
&mut context.sentbox_thread.clone().read().unwrap(),
0,
);
dc_jobthread_suspend(
context,
&mut context.mvbox_thread.clone().read().unwrap(),
0,
);
dc_suspend_smtp_thread(context, 0);
break;
} else if job.try_again == 2 {
info!( info!(
context,
0,
"{}-job #{} not yet ready and will be delayed.",
if thread == 100 { "INBOX" } else { "SMTP" },
job.job_id
);
} else if job.try_again == -1 || job.try_again == 3 {
let tries = job.tries + 1;
if tries < 17 {
job.tries = tries;
let time_offset = get_backoff_time_offset(tries);
job.desired_timestamp = job.added_timestamp + time_offset;
dc_job_update(context, &mut job);
info!(
context, context,
0, 0,
"{}-job #{} not succeeded on try #{}, retry in ADD_TIME+{} (in {} seconds).", "{}-job #{} not succeeded on try #{}, retry in ADD_TIME+{} (in {} seconds).",
@@ -207,48 +194,45 @@ unsafe fn dc_job_perform(context: &Context, thread: libc::c_int, probe_network:
time_offset, time_offset,
job.added_timestamp + time_offset - time() job.added_timestamp + time_offset - time()
); );
if thread == 5000 && tries < 17 - 1 { if thread == 5000 && tries < 17 - 1 {
context context
.smtp_state .smtp_state
.clone() .clone()
.0 .0
.lock() .lock()
.unwrap() .unwrap()
.perform_jobs_needed = 2; .perform_jobs_needed = 2;
}
} else {
if job.action == 5901 {
dc_set_msg_failed(context, job.foreign_id, job.pending_error);
}
dc_job_delete(context, &mut job);
} }
if 0 == probe_network {
continue;
}
// on dc_maybe_network() we stop trying here;
// these jobs are already tried once.
// otherwise, we just continue with the next job
// to give other jobs a chance being tried at least once.
break;
} else { } else {
if job.action == 5901 {
dc_set_msg_failed(context, job.foreign_id, job.pending_error);
}
dc_job_delete(context, &mut job); dc_job_delete(context, &mut job);
} }
dc_param_unref(job.param); if 0 == probe_network {
free(job.pending_error as *mut libc::c_void); continue;
}
// on dc_maybe_network() we stop trying here;
// these jobs are already tried once.
// otherwise, we just continue with the next job
// to give other jobs a chance being tried at least once.
break;
} else {
dc_job_delete(context, &mut job);
} }
dc_param_unref(job.param);
free(job.pending_error as *mut libc::c_void);
}
Ok(()) Ok(())
}) }); // TODO: better error handling
.unwrap(); // TODO: better error handling
} }
fn dc_job_delete(context: &Context, job: &dc_job_t) -> bool { fn dc_job_delete(context: &Context, job: &dc_job_t) -> bool {
dc_sqlite3_execute( context
context, .sql
&context.sql, .execute("DELETE FROM jobs WHERE id=?;", params![job.job_id as i32])
"DELETE FROM jobs WHERE id=?;", .is_ok()
params![job.job_id as i32],
)
} }
/* ****************************************************************************** /* ******************************************************************************
@@ -1053,28 +1037,16 @@ pub unsafe fn dc_perform_smtp_jobs(context: &Context) {
state.perform_jobs_needed = 0; state.perform_jobs_needed = 0;
if 0 != state.suspended { if 0 != state.suspended {
dc_log_info( info!(context, 0, "SMTP-jobs suspended.",);
context,
0,
b"SMTP-jobs suspended.\x00" as *const u8 as *const libc::c_char,
);
return; return;
} }
state.doing_jobs = 1; state.doing_jobs = 1;
probe_smtp_network probe_smtp_network
}; };
dc_log_info( info!(context, 0, "SMTP-jobs started...",);
context,
0,
b"SMTP-jobs started...\x00" as *const u8 as *const libc::c_char,
);
dc_job_perform(context, 5000, probe_smtp_network); dc_job_perform(context, 5000, probe_smtp_network);
dc_log_info( info!(context, 0, "SMTP-jobs ended.");
context,
0,
b"SMTP-jobs ended.\x00" as *const u8 as *const libc::c_char,
);
{ {
let &(ref lock, _) = &*context.smtp_state.clone(); let &(ref lock, _) = &*context.smtp_state.clone();
@@ -1085,11 +1057,7 @@ pub unsafe fn dc_perform_smtp_jobs(context: &Context) {
} }
pub unsafe fn dc_perform_smtp_idle(context: &Context) { pub unsafe fn dc_perform_smtp_idle(context: &Context) {
dc_log_info( info!(context, 0, "SMTP-idle started...",);
context,
0,
b"SMTP-idle started...\x00" as *const u8 as *const libc::c_char,
);
{ {
let &(ref lock, ref cvar) = &*context.smtp_state.clone(); let &(ref lock, ref cvar) = &*context.smtp_state.clone();
let mut state = lock.lock().unwrap(); let mut state = lock.lock().unwrap();

View File

@@ -21,12 +21,12 @@ function. These errors must be shown to the user by a bubble or so.
usually not reported using dc_log_error() - its up to the caller to usually not reported using dc_log_error() - its up to the caller to
decide, what should be reported or done. However, these "Normal" errors decide, what should be reported or done. However, these "Normal" errors
are usually logged by dc_log_warning(). */ are usually logged by dc_log_warning(). */
unsafe fn log_vprintf( unsafe extern "C" fn log_vprintf(
context: &Context, context: &Context,
event: Event, event: Event,
data1: libc::c_int, data1: libc::c_int,
msg_format: *const libc::c_char, msg_format: *const libc::c_char,
va_0: std::ffi::VaListImpl, mut va_0: ...
) { ) {
let msg: *mut libc::c_char; let msg: *mut libc::c_char;
if !msg_format.is_null() { if !msg_format.is_null() {
@@ -35,7 +35,7 @@ unsafe fn log_vprintf(
tempbuf.as_mut_ptr(), tempbuf.as_mut_ptr(),
1024i32 as libc::c_ulong, 1024i32 as libc::c_ulong,
msg_format, msg_format,
va_0, va_0.as_va_list(),
); );
msg = dc_strdup(tempbuf.as_mut_ptr()) msg = dc_strdup(tempbuf.as_mut_ptr())
} else { } else {

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,12 @@ pub enum Error {
Sql(rusqlite::Error), Sql(rusqlite::Error),
#[fail(display = "{:?}", _0)] #[fail(display = "{:?}", _0)]
Failure(failure::Error), Failure(failure::Error),
#[fail(display = "Sqlite: Connection closed")]
SqlNoConnection,
#[fail(display = "Sqlite: Already open")]
SqlAlreadyOpen,
#[fail(display = "Sqlite: Failed to open")]
SqlFailedToOpen,
} }
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;

View File

@@ -49,7 +49,7 @@ extern "C" {
_: *mut libc::c_char, _: *mut libc::c_char,
_: libc::c_ulong, _: libc::c_ulong,
_: *const libc::c_char, _: *const libc::c_char,
_: std::ffi::VaListImpl, _: std::ffi::VaList,
) -> libc::c_int; ) -> libc::c_int;
// -- DC Methods // -- DC Methods